OStatus: initial hookup of remote group membership (notice delivery not yet working quite right)

- added a temp config var to disable salmon magic signatures until they're working consistently
This commit is contained in:
Brion Vibber 2010-02-22 09:43:27 -08:00
parent 7e8c3ea418
commit 06f155c02d
7 changed files with 336 additions and 55 deletions

View File

@ -211,7 +211,7 @@ class OStatusPlugin extends Plugin
// FIXME: this needs to go out in a queue handler // FIXME: this needs to go out in a queue handler
$xml = '<?xml version="1.0" encoding="UTF-8" ?>'; $xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>';
$xml .= $notice->asAtomEntry(true, true); $xml .= $notice->asAtomEntry(true, true);
$salmon = new Salmon(); $salmon = new Salmon();
@ -402,6 +402,97 @@ class OStatusPlugin extends Plugin
return true; return true;
} }
/**
* When one of our local users tries to join a remote group,
* notify the remote server. If the notification is rejected,
* deny the join.
*
* @param User_group $group
* @param User $user
*
* @return mixed hook return value
*/
function onStartJoinGroup($group, $user)
{
$oprofile = Ostatus_profile::staticGet('group_id', $group->id);
if ($oprofile) {
$member = Profile::staticGet($user->id);
$act = new Activity();
$act->id = TagURI::mint('join:%d:%d:%s',
$member->id,
$group->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::JOIN;
$act->object = $oprofile->asActivityObject();
$act->time = time();
$act->title = _m("Join");
$act->content = sprintf(_m("%s has joined group %s."),
$member->getBestName(),
$oprofile->getBestName());
if ($oprofile->notifyActivity($act)) {
return true;
} else {
throw new ServerException(_m("Failed joining remote group."));
}
}
}
/**
* When one of our local users leaves a remote group, notify the remote
* server.
*
* @fixme Might be good to schedule a resend of the leave notification
* if it failed due to a transitory error. We've canceled the local
* membership already anyway, but if the remote server comes back up
* it'll be left with a stray membership record.
*
* @param User_group $group
* @param User $user
*
* @return mixed hook return value
*/
function onEndLeaveGroup($group, $user)
{
$oprofile = Ostatus_profile::staticGet('group_id', $group->id);
if ($oprofile) {
// Drop the PuSH subscription if there are no other subscribers.
$members = $group->getMembers(0, 1);
if ($members->N == 0) {
common_log(LOG_INFO, "Unsubscribing from now-unused group feed $oprofile->feeduri");
$oprofile->unsubscribe();
}
$member = Profile::staticGet($user->id);
$act = new Activity();
$act->id = TagURI::mint('leave:%d:%d:%s',
$member->id,
$group->id,
common_date_iso8601(time()));
$act->actor = ActivityObject::fromProfile($member);
$act->verb = ActivityVerb::LEAVE;
$act->object = $oprofile->asActivityObject();
$act->time = time();
$act->title = _m("Leave");
$act->content = sprintf(_m("%s has left group %s."),
$member->getBestName(),
$oprofile->getBestName());
$oprofile->notifyActivity($act);
}
}
/** /**
* Notify remote users when their notices get favorited. * Notify remote users when their notices get favorited.
* *

View File

@ -88,21 +88,96 @@ class GroupsalmonAction extends SalmonAction
* Save a subscription relationship for them. * Save a subscription relationship for them.
*/ */
/**
* Postel's law: consider a "follow" notification as a "join".
*/
function handleFollow() function handleFollow()
{ {
$this->handleJoin(); // ??? $this->handleJoin();
} }
/**
* Postel's law: consider an "unfollow" notification as a "leave".
*/
function handleUnfollow() function handleUnfollow()
{ {
$this->handleLeave();
} }
/** /**
* A remote user joined our group. * A remote user joined our group.
* @fixme move permission checks and event call into common code,
* currently we're doing the main logic in joingroup action
* and so have to repeat it here.
*/ */
function handleJoin() function handleJoin()
{ {
$oprofile = $this->ensureProfile();
if (!$oprofile) {
$this->clientError(_m("Can't read profile to set up group membership."));
}
if ($oprofile->isGroup()) {
$this->clientError(_m("Groups can't join groups."));
}
common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
$profile = $oprofile->localProfile();
if ($profile->isMember($this->group)) {
// Already a member; we'll take it silently to aid in resolving
// inconsistencies on the other side.
return true;
}
if (Group_block::isBlocked($this->group, $profile)) {
$this->clientError(_('You have been blocked from that group by the admin.'), 403);
return false;
}
try {
// @fixme that event currently passes a user from main UI
// Event should probably move into Group_member::join
// and take a Profile object.
//
//if (Event::handle('StartJoinGroup', array($this->group, $profile))) {
Group_member::join($this->group->id, $profile->id);
//Event::handle('EndJoinGroup', array($this->group, $profile));
//}
} catch (Exception $e) {
$this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
$oprofile->uri, $this->group->nickname));
}
}
/**
* A remote user left our group.
*/
function handleLeave()
{
$oprofile = $this->ensureProfile();
if (!$oprofile) {
$this->clientError(_m("Can't read profile to cancel group membership."));
}
if ($oprofile->isGroup()) {
$this->clientError(_m("Groups can't join groups."));
}
common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}");
$profile = $oprofile->localProfile();
try {
// @fixme event needs to be refactored as above
//if (Event::handle('StartLeaveGroup', array($this->group, $profile))) {
Group_member::leave($this->group->id, $profile->id);
//Event::handle('EndLeaveGroup', array($this->group, $profile));
//}
} catch (Exception $e) {
$this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
$oprofile->uri, $this->group->nickname));
return;
}
} }
} }

View File

@ -248,7 +248,7 @@ class OStatusSubAction extends Action
$group = $this->oprofile->localGroup(); $group = $this->oprofile->localGroup();
if ($user->isMember($group)) { if ($user->isMember($group)) {
$this->showForm(_m('Already a member!')); $this->showForm(_m('Already a member!'));
} elseif (Group_member::join($this->profile->group_id, $user->id)) { } elseif (Group_member::join($this->oprofile->group_id, $user->id)) {
$this->showForm(_m('Joined remote group!')); $this->showForm(_m('Joined remote group!'));
} else { } else {
$this->showForm(_m('Remote group join failed!')); $this->showForm(_m('Remote group join failed!'));

View File

@ -137,12 +137,49 @@ class Ostatus_profile extends Memcached_DataObject
return null; return null;
} }
/**
* Returns an ActivityObject describing this remote user or group profile.
* Can then be used to generate Atom chunks.
*
* @return ActivityObject
*/
function asActivityObject()
{
if ($this->isGroup()) {
$object = new ActivityObject();
$object->type = 'http://activitystrea.ms/schema/1.0/group';
$object->id = $this->uri;
$self = $this->localGroup();
// @fixme put a standard getAvatar() interface on groups too
if ($self->homepage_logo) {
$object->avatar = $self->homepage_logo;
$map = array('png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif');
$extension = pathinfo(parse_url($avatarHref, PHP_URL_PATH), PATHINFO_EXTENSION);
if (isset($map[$extension])) {
// @fixme this ain't used/saved yet
$object->avatarType = $map[$extension];
}
}
$object->link = $this->uri; // @fixme accurate?
return $object;
} else {
return ActivityObject::fromProfile($this->localProfile());
}
}
/** /**
* Returns an XML string fragment with profile information as an * Returns an XML string fragment with profile information as an
* Activity Streams noun object with the given element type. * Activity Streams noun object with the given element type.
* *
* Assumes that 'activity' namespace has been previously defined. * Assumes that 'activity' namespace has been previously defined.
* *
* @fixme replace with wrappers on asActivityObject when it's got everything.
*
* @param string $element one of 'actor', 'subject', 'object', 'target' * @param string $element one of 'actor', 'subject', 'object', 'target'
* @return string * @return string
*/ */
@ -202,11 +239,19 @@ class Ostatus_profile extends Memcached_DataObject
} }
/** /**
* Damn dirty hack! * @return boolean true if this is a remote group
*/ */
function isGroup() function isGroup()
{ {
return (strpos($this->feeduri, '/groups/') !== false); if ($this->profile_id && !$this->group_id) {
return false;
} else if ($this->group_id && !$this->profile_id) {
return true;
} else if ($this->group_id && $this->profile_id) {
throw new ServerException("Invalid ostatus_profile state: both group and profile IDs set for $this->uri");
} else {
throw new ServerException("Invalid ostatus_profile state: both group and profile IDs empty for $this->uri");
}
} }
/** /**
@ -353,22 +398,24 @@ class Ostatus_profile extends Memcached_DataObject
common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml"); common_log(LOG_INFO, "Posting to Salmon endpoint $this->salmonuri: $xml");
$salmon = new Salmon(); // ? $salmon = new Salmon(); // ?
$salmon->post($this->salmonuri, $xml); return $salmon->post($this->salmonuri, $xml);
} }
return false;
} }
public function notifyActivity($activity) public function notifyActivity($activity)
{ {
if ($this->salmonuri) { if ($this->salmonuri) {
$xml = $activity->asString(true); $xml = '<?xml version="1.0" encoding="UTF-8" ?' . '>' .
$activity->asString(true);
$salmon = new Salmon(); // ? $salmon = new Salmon(); // ?
$salmon->post($this->salmonuri, $xml); return $salmon->post($this->salmonuri, $xml);
} }
return; return false;
} }
function getBestName() function getBestName()
@ -597,10 +644,23 @@ class Ostatus_profile extends Memcached_DataObject
*/ */
protected function updateAvatar($url) protected function updateAvatar($url)
{ {
if ($this->isGroup()) {
$self = $this->localGroup();
} else {
$self = $this->localProfile();
}
if (!$self) {
throw new ServerException(sprintf(
_m("Tried to update avatar for unsaved remote profile %s"),
$this->uri));
}
// @fixme this should be better encapsulated // @fixme this should be better encapsulated
// ripped from oauthstore.php (for old OMB client) // ripped from oauthstore.php (for old OMB client)
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
copy($url, $temp_filename); if (!copy($url, $temp_filename)) {
throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
}
if ($this->isGroup()) { if ($this->isGroup()) {
$id = $this->group_id; $id = $this->group_id;
@ -614,13 +674,7 @@ class Ostatus_profile extends Memcached_DataObject
null, null,
common_timestamp()); common_timestamp());
rename($temp_filename, Avatar::path($filename)); rename($temp_filename, Avatar::path($filename));
if ($this->isGroup()) { $self->setOriginal($filename);
$group = $this->localGroup();
$group->setOriginal($filename);
} else {
$profile = $this->localProfile();
$profile->setOriginal($filename);
}
} }
protected static function getActivityObjectAvatar($object) protected static function getActivityObjectAvatar($object)
@ -747,6 +801,18 @@ class Ostatus_profile extends Memcached_DataObject
self::createActivityObjectProfile($actor, $feeduri, $salmonuri); self::createActivityObjectProfile($actor, $feeduri, $salmonuri);
} }
/**
* Create local ostatus_profile and profile/user_group entries for
* the provided remote user or group.
*
* @param ActivityObject $object
* @param string $feeduri
* @param string $salmonuri
* @param array $hints
*
* @fixme fold $feeduri/$salmonuri into $hints
* @return Ostatus_profile
*/
protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array()) protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array())
{ {
$homeuri = $object->id; $homeuri = $object->id;
@ -784,6 +850,16 @@ class Ostatus_profile extends Memcached_DataObject
} }
} }
$oprofile = new Ostatus_profile();
$oprofile->uri = $homeuri;
$oprofile->feeduri = $feeduri;
$oprofile->salmonuri = $salmonuri;
$oprofile->created = common_sql_now();
$oprofile->modified = common_sql_now();
if ($object->type == ActivityObject::PERSON) {
$profile = new Profile(); $profile = new Profile();
$profile->nickname = $nickname; $profile->nickname = $nickname;
$profile->fullname = $object->title; $profile->fullname = $object->title;
@ -800,30 +876,39 @@ class Ostatus_profile extends Memcached_DataObject
// @todo tags from categories // @todo tags from categories
// @todo lat/lon/location? // @todo lat/lon/location?
$profile_id = $profile->insert(); $oprofile->profile_id = $profile->insert();
if (!$profile_id) { if (!$oprofile->profile_id) {
throw new ServerException("Can't save local profile"); throw new ServerException("Can't save local profile");
} }
} else {
$group = new User_group();
$group->nickname = $nickname;
$group->fullname = $object->title;
// @fixme no canonical profileurl; using homepage instead for now
$group->homepage = $homeuri;
$group->created = common_sql_now();
// @fixme either need to do feed discovery here // @fixme homepage
// or need to split out some of the feed stuff // @fixme bio
// so we can leave it empty until later. // @fixme tags/categories
// @fixme location?
// @todo tags from categories
// @todo lat/lon/location?
$oprofile = new Ostatus_profile(); $oprofile->group_id = $group->insert();
$oprofile->uri = $homeuri; if (!$oprofile->group_id) {
$oprofile->feeduri = $feeduri; throw new ServerException("Can't save local profile");
$oprofile->salmonuri = $salmonuri; }
$oprofile->profile_id = $profile_id; }
$oprofile->created = common_sql_now();
$oprofile->modified = common_sql_now();
$ok = $oprofile->insert(); $ok = $oprofile->insert();
if ($ok) { if ($ok) {
if ($avatar) {
$oprofile->updateAvatar($avatar); $oprofile->updateAvatar($avatar);
}
return $oprofile; return $oprofile;
} else { } else {
throw new ServerException("Can't save OStatus profile"); throw new ServerException("Can't save OStatus profile");

View File

@ -367,6 +367,9 @@ class ActivityObject
return $object; return $object;
} }
/**
* @fixme missing avatar, bio info, etc
*/
static function fromProfile($profile) static function fromProfile($profile)
{ {
$object = new ActivityObject(); $object = new ActivityObject();
@ -379,6 +382,9 @@ class ActivityObject
return $object; return $object;
} }
/**
* @fixme missing avatar, bio info, etc
*/
function asString($tag='activity:object') function asString($tag='activity:object')
{ {
$xs = new XMLStringer(true); $xs = new XMLStringer(true);

View File

@ -28,15 +28,26 @@
*/ */
class Salmon class Salmon
{ {
/**
* Sign and post the given Atom entry as a Salmon message.
*
* @fixme pass through the actor for signing?
*
* @param string $endpoint_uri
* @param string $xml
* @return boolean success
*/
public function post($endpoint_uri, $xml) public function post($endpoint_uri, $xml)
{ {
if (empty($endpoint_uri)) { if (empty($endpoint_uri)) {
return FALSE; return false;
} }
if (!common_config('ostatus', 'skip_signatures')) {
$xml = $this->createMagicEnv($xml); $xml = $this->createMagicEnv($xml);
}
$headers = array('Content-type: application/atom+xml'); $headers = array('Content-Type: application/atom+xml');
try { try {
$client = new HTTPClient(); $client = new HTTPClient();
@ -51,7 +62,7 @@ class Salmon
$response->getStatus() . ': ' . $response->getBody()); $response->getStatus() . ': ' . $response->getBody());
return false; return false;
} }
return true;
} }
public function createMagicEnv($text) public function createMagicEnv($text)

View File

@ -41,7 +41,7 @@ class SalmonAction extends Action
$this->clientError(_('This method requires a POST.')); $this->clientError(_('This method requires a POST.'));
} }
if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') { if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
$this->clientError(_('Salmon requires application/atom+xml')); $this->clientError(_('Salmon requires application/atom+xml'));
} }
@ -57,10 +57,12 @@ class SalmonAction extends Action
// Check the signature // Check the signature
$salmon = new Salmon; $salmon = new Salmon;
if (!common_config('ostatus', 'skip_signatures')) {
if (!$salmon->verifyMagicEnv($dom)) { if (!$salmon->verifyMagicEnv($dom)) {
common_log(LOG_DEBUG, "Salmon signature verification failed."); common_log(LOG_DEBUG, "Salmon signature verification failed.");
$this->clientError(_m('Salmon signature verification failed.')); $this->clientError(_m('Salmon signature verification failed.'));
} }
}
$this->act = new Activity($dom->documentElement); $this->act = new Activity($dom->documentElement);
return true; return true;
@ -101,6 +103,9 @@ class SalmonAction extends Action
case ActivityVerb::JOIN: case ActivityVerb::JOIN:
$this->handleJoin(); $this->handleJoin();
break; break;
case ActivityVerb::LEAVE:
$this->handleLeave();
break;
default: default:
throw new ClientException(_("Unimplemented.")); throw new ClientException(_("Unimplemented."));
} }
@ -154,6 +159,14 @@ class SalmonAction extends Action
throw new ClientException(_("Unimplemented!")); throw new ClientException(_("Unimplemented!"));
} }
/**
* Hmmmm
*/
function handleLeave()
{
throw new ClientException(_("Unimplemented!"));
}
/** /**
* @return Ostatus_profile * @return Ostatus_profile
*/ */