SalmonAction and extensions simplified

This commit is contained in:
Mikael Nordfeldth 2014-06-28 20:33:09 +02:00
parent 3f21d22a31
commit d0da552722
5 changed files with 46 additions and 78 deletions

View File

@ -48,8 +48,8 @@ class GroupsalmonAction extends SalmonAction
$this->target = $this->group; $this->target = $this->group;
$oprofile = Ostatus_profile::getKV('group_id', $id); $remote_group = Ostatus_profile::getKV('group_id', $id);
if ($oprofile instanceof Ostatus_profile) { if ($remote_group instanceof Ostatus_profile) {
// TRANS: Client error. // TRANS: Client error.
$this->clientError(_m('Cannot accept remote posts for a remote group.')); $this->clientError(_m('Cannot accept remote posts for a remote group.'));
} }
@ -121,36 +121,30 @@ class GroupsalmonAction extends SalmonAction
*/ */
function handleJoin() function handleJoin()
{ {
$oprofile = $this->ensureProfile(); if ($this->oprofile->isGroup()) {
if (!$oprofile instanceof Ostatus_profile) {
// TRANS: Client error.
$this->clientError(_m('Cannot read profile to set up group membership.'));
}
if ($oprofile->isGroup()) {
// TRANS: Client error. // TRANS: Client error.
$this->clientError(_m('Groups cannot join groups.')); $this->clientError(_m('Groups cannot join groups.'));
} }
common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}"); common_log(LOG_INFO, sprintf('Remote profile %s joining local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
$profile = $oprofile->localProfile();
if ($profile->isMember($this->group)) { if ($this->actor->isMember($this->group)) {
// Already a member; we'll take it silently to aid in resolving // Already a member; we'll take it silently to aid in resolving
// inconsistencies on the other side. // inconsistencies on the other side.
return true; return true;
} }
if (Group_block::isBlocked($this->group, $profile)) { if (Group_block::isBlocked($this->group, $this->actor)) {
// TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin. // TRANS: Client error displayed when trying to join a group the user is blocked from by a group admin.
$this->clientError(_m('You have been blocked from that group by the admin.'), 403); $this->clientError(_m('You have been blocked from that group by the admin.'), 403);
} }
try { try {
$profile->joinGroup($this->group); $this->actor->joinGroup($this->group);
} catch (Exception $e) { } catch (Exception $e) {
// TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
$this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'), $this->serverError(sprintf(_m('Could not join remote user %1$s to group %2$s.'),
$oprofile->uri, $this->group->nickname)); $this->oprofile->getUri(), $this->group->getNickname()));
} }
} }
@ -159,26 +153,20 @@ class GroupsalmonAction extends SalmonAction
*/ */
function handleLeave() function handleLeave()
{ {
$oprofile = $this->ensureProfile(); // ensureProfile throws exception on failure
if (!$oprofile instanceof Ostatus_profile) { if ($this->oprofile->isGroup()) {
// TRANS: Client error displayed when group membership cannot be cancelled
// TRANS: because the remote profile could not be read.
$this->clientError(_m('Cannot read profile to cancel group membership.'));
}
if ($oprofile->isGroup()) {
// TRANS: Client error displayed when trying to have a group join another group. // TRANS: Client error displayed when trying to have a group join another group.
$this->clientError(_m('Groups cannot join groups.')); throw new AlreadyFulfilledException(_m('Groups cannot be members of groups'));
} }
common_log(LOG_INFO, "Remote profile {$oprofile->uri} leaving local group {$this->group->nickname}"); common_log(LOG_INFO, sprintf('Remote profile %s leaving local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
$profile = $oprofile->localProfile();
try { try {
$profile->leaveGroup($this->group); $this->actor->leaveGroup($this->group);
} catch (Exception $e) { } catch (Exception $e) {
// TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname. // TRANS: Server error. %1$s is a profile URI, %2$s is a group nickname.
$this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'), $this->serverError(sprintf(_m('Could not remove remote user %1$s from group %2$s.'),
$oprofile->uri, $this->group->nickname)); $this->oprofile->getUri(), $this->group->getNickname()));
} }
} }
} }

View File

@ -42,14 +42,16 @@ class PeopletagsalmonAction extends SalmonAction
$this->peopletag = Profile_list::getKV('id', $id); $this->peopletag = Profile_list::getKV('id', $id);
if (empty($this->peopletag)) { if (!$this->peopletag instanceof Profile_list) {
// TRANS: Client error displayed when referring to a non-existing list. // TRANS: Client error displayed when referring to a non-existing list.
$this->clientError(_m('No such list.')); $this->clientError(_m('No such list.'));
} }
$oprofile = Ostatus_profile::getKV('peopletag_id', $id); $this->target = $this->peopletag;
if (!empty($oprofile)) { $remote_list = Ostatus_profile::getKV('peopletag_id', $id);
if ($remote_list instanceof Ostatus_profile) {
// TRANS: Client error displayed when trying to send a message to a remote list. // TRANS: Client error displayed when trying to send a message to a remote list.
$this->clientError(_m('Cannot accept remote posts for a remote list.')); $this->clientError(_m('Cannot accept remote posts for a remote list.'));
} }
@ -83,24 +85,16 @@ class PeopletagsalmonAction extends SalmonAction
* @fixme move permission checks and event call into common code, * @fixme move permission checks and event call into common code,
* currently we're doing the main logic in joingroup action * currently we're doing the main logic in joingroup action
* and so have to repeat it here. * and so have to repeat it here.
*
* @throws NoProfileException from localProfile if missing locally stored Profile object
*/ */
function handleSubscribe() function handleSubscribe()
{ {
$oprofile = $this->ensureProfile(); if ($this->oprofile->isGroup()) {
if (!$oprofile) {
// TRANS: Client error displayed when referring to a non-existing remote list.
$this->clientError(_m('Cannot read profile to set up list subscription.'));
}
if ($oprofile->isGroup()) {
// TRANS: Client error displayed when trying to subscribe a group to a list. // TRANS: Client error displayed when trying to subscribe a group to a list.
$this->clientError(_m('Groups cannot subscribe to lists.')); $this->clientError(_m('Groups cannot subscribe to lists.'));
} }
common_log(LOG_INFO, "Remote profile {$oprofile->uri} subscribing to local peopletag ".$this->peopletag->getBestName()); common_log(LOG_INFO, sprintf('Remote profile %s subscribing to local peopletag %s', $this->oprofile->getUri(), $this->peopletag->getBestName()));
$profile = $oprofile->localProfile(); if ($this->peopletag->hasSubscriber($this->actor)) {
if ($this->peopletag->hasSubscriber($profile)) {
// Already a member; we'll take it silently to aid in resolving // Already a member; we'll take it silently to aid in resolving
// inconsistencies on the other side. // inconsistencies on the other side.
return true; return true;
@ -110,12 +104,12 @@ class PeopletagsalmonAction extends SalmonAction
// his own updates? // his own updates?
try { try {
Profile_tag_subscription::add($this->peopletag, $profile); Profile_tag_subscription::add($this->peopletag, $this->actor);
} catch (Exception $e) { } catch (Exception $e) {
// TRANS: Server error displayed when subscribing a remote user to a list fails. // TRANS: Server error displayed when subscribing a remote user to a list fails.
// TRANS: %1$s is a profile URI, %2$s is a list name. // TRANS: %1$s is a profile URI, %2$s is a list name.
$this->serverError(sprintf(_m('Could not subscribe remote user %1$s to list %2$s.'), $this->serverError(sprintf(_m('Could not subscribe remote user %1$s to list %2$s.'),
$oprofile->uri, $this->peopletag->getBestName())); $this->oprofile->getUri(), $this->peopletag->getBestName()));
} }
} }
@ -127,25 +121,19 @@ class PeopletagsalmonAction extends SalmonAction
*/ */
function handleUnsubscribe() function handleUnsubscribe()
{ {
$oprofile = $this->ensureProfile(); if ($this->oprofile->isGroup()) {
if (!$oprofile instanceof Ostatus_profile) {
// TRANS: Client error displayed when trying to unsubscribe from non-existing list.
$this->clientError(_m('Cannot read profile to cancel list subscription.'));
}
if ($oprofile->isGroup()) {
// TRANS: Client error displayed when trying to unsubscribe a group from a list. // TRANS: Client error displayed when trying to unsubscribe a group from a list.
$this->clientError(_m('Groups cannot subscribe to lists.')); $this->clientError(_m('Groups cannot subscribe to lists.'));
} }
common_log(LOG_INFO, "Remote profile {$oprofile->uri} unsubscribing from local peopletag ".$this->peopletag->getBestName()); common_log(LOG_INFO, sprintf('Remote profile %s unsubscribing from local peopletag %s', $this->oprofile->getUri(), $this->peopletag->getBestName()));
try { try {
$profile = $oprofile->localProfile(); Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $this->actor->id);
Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $profile->id);
} catch (Exception $e) { } catch (Exception $e) {
// TRANS: Client error displayed when trying to unsubscribe a remote user from a list fails. // TRANS: Client error displayed when trying to unsubscribe a remote user from a list fails.
// TRANS: %1$s is a profile URL, %2$s is a list name. // TRANS: %1$s is a profile URL, %2$s is a list name.
$this->serverError(sprintf(_m('Could not unsubscribe remote user %1$s from list %2$s.'), $this->serverError(sprintf(_m('Could not unsubscribe remote user %1$s from list %2$s.'),
$oprofile->getUri(), $this->peopletag->getBestName())); $this->oprofile->getUri(), $this->peopletag->getBestName()));
} }
} }
} }

View File

@ -110,13 +110,8 @@ class UsersalmonAction extends SalmonAction
*/ */
function handleFollow() function handleFollow()
{ {
$oprofile = $this->ensureProfile(); common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
if ($oprofile instanceof Ostatus_profile) { Subscription::start($this->actor, $this->target);
common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $oprofile->getUri(), $this->target->getNickname()));
Subscription::start($oprofile->localProfile(), $this->target);
} else {
common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
}
} }
/** /**
@ -127,16 +122,11 @@ class UsersalmonAction extends SalmonAction
*/ */
function handleUnfollow() function handleUnfollow()
{ {
$oprofile = $this->ensureProfile(); common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
if ($oprofile instanceof Ostatus_profile) { try {
common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $oprofile->getUri(), $this->target->getNickname())); Subscription::cancel($this->actor, $this->target);
try { } catch (NoProfileException $e) {
Subscription::cancel($oprofile->localProfile(), $this->target); common_debug('Could not find profile for Subscription: '.$e->getMessage());
} catch (NoProfileException $e) {
common_debug('Could not find profile for Subscription: '.$e->getMessage());
}
} else {
common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
} }
} }
@ -148,9 +138,8 @@ class UsersalmonAction extends SalmonAction
function handleFavorite() function handleFavorite()
{ {
$notice = $this->getNotice($this->activity->objects[0]); $notice = $this->getNotice($this->activity->objects[0]);
$profile = $this->ensureProfile()->localProfile();
$old = Fave::pkeyGet(array('user_id' => $profile->id, $old = Fave::pkeyGet(array('user_id' => $this->actor->id,
'notice_id' => $notice->id)); 'notice_id' => $notice->id));
if ($old instanceof Fave) { if ($old instanceof Fave) {
@ -158,7 +147,7 @@ class UsersalmonAction extends SalmonAction
throw new AlreadyFulfilledException(_m('This is already a favorite.')); throw new AlreadyFulfilledException(_m('This is already a favorite.'));
} }
if (!Fave::addNew($profile, $notice)) { if (!Fave::addNew($this->actor, $notice)) {
// TRANS: Client exception. // TRANS: Client exception.
throw new ClientException(_m('Could not save new favorite.')); throw new ClientException(_m('Could not save new favorite.'));
} }
@ -171,9 +160,8 @@ class UsersalmonAction extends SalmonAction
function handleUnfavorite() function handleUnfavorite()
{ {
$notice = $this->getNotice($this->activity->objects[0]); $notice = $this->getNotice($this->activity->objects[0]);
$profile = $this->ensureProfile()->localProfile();
$fave = Fave::pkeyGet(array('user_id' => $profile->id, $fave = Fave::pkeyGet(array('user_id' => $this->actor->id,
'notice_id' => $notice->id)); 'notice_id' => $notice->id));
if (!$fave instanceof Fave) { if (!$fave instanceof Fave) {
// TRANS: Client exception. // TRANS: Client exception.
@ -204,7 +192,6 @@ class UsersalmonAction extends SalmonAction
} }
// save the list // save the list
$tagger = $this->ensureProfile();
$list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target); $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
$ptag = $list->localPeopletag(); $ptag = $list->localPeopletag();
@ -222,7 +209,6 @@ class UsersalmonAction extends SalmonAction
if ($this->activity->objects[0]->type != ActivityObject::PERSON) { if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
// TRANS: Client exception. // TRANS: Client exception.
throw new ClientException(_m('Not a person object.')); throw new ClientException(_m('Not a person object.'));
return false;
} }
// this is a peopletag // this is a peopletag
$tagged = User::getKV('uri', $this->activity->objects[0]->id); $tagged = User::getKV('uri', $this->activity->objects[0]->id);
@ -238,7 +224,6 @@ class UsersalmonAction extends SalmonAction
} }
// save the list // save the list
$tagger = $this->ensureProfile();
$list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target); $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
$ptag = $list->localPeopletag(); $ptag = $list->localPeopletag();

View File

@ -1387,7 +1387,7 @@ class Ostatus_profile extends Managed_DataObject
public static function ensureActivityObjectProfile($object, $hints=array()) public static function ensureActivityObjectProfile($object, $hints=array())
{ {
$profile = self::getActivityObjectProfile($object); $profile = self::getActivityObjectProfile($object);
if ($profile) { if ($profile instanceof Ostatus_profile) {
$profile->updateFromActivityObject($object, $hints); $profile->updateFromActivityObject($object, $hints);
} else { } else {
$profile = self::createActivityObjectProfile($object, $hints); $profile = self::createActivityObjectProfile($object, $hints);

View File

@ -28,6 +28,9 @@ class SalmonAction extends Action
{ {
protected $needPost = true; protected $needPost = true;
protected $oprofile = null; // Ostatus_profile of the actor
protected $actor = null; // Profile object of the actor
var $xml = null; var $xml = null;
var $activity = null; var $activity = null;
var $target = null; var $target = null;
@ -63,6 +66,9 @@ class SalmonAction extends Action
$this->clientError(_m('Salmon signature verification failed.')); $this->clientError(_m('Salmon signature verification failed.'));
} }
$this->oprofile = $this->ensureProfile();
$this->actor = $this->oprofile->localProfile();
return true; return true;
} }
@ -216,6 +222,7 @@ class SalmonAction extends Action
throw new Exception(_m('Received a salmon slap from unidentified actor.')); throw new Exception(_m('Received a salmon slap from unidentified actor.'));
} }
// ensureActivityObjectProfile throws exception on failure
return Ostatus_profile::ensureActivityObjectProfile($actor); return Ostatus_profile::ensureActivityObjectProfile($actor);
} }