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;
$oprofile = Ostatus_profile::getKV('group_id', $id);
if ($oprofile instanceof Ostatus_profile) {
$remote_group = Ostatus_profile::getKV('group_id', $id);
if ($remote_group instanceof Ostatus_profile) {
// TRANS: Client error.
$this->clientError(_m('Cannot accept remote posts for a remote group.'));
}
@ -121,36 +121,30 @@ class GroupsalmonAction extends SalmonAction
*/
function handleJoin()
{
$oprofile = $this->ensureProfile();
if (!$oprofile instanceof Ostatus_profile) {
// TRANS: Client error.
$this->clientError(_m('Cannot read profile to set up group membership.'));
}
if ($oprofile->isGroup()) {
if ($this->oprofile->isGroup()) {
// TRANS: Client error.
$this->clientError(_m('Groups cannot join groups.'));
}
common_log(LOG_INFO, "Remote profile {$oprofile->uri} joining local group {$this->group->nickname}");
$profile = $oprofile->localProfile();
common_log(LOG_INFO, sprintf('Remote profile %s joining local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
if ($profile->isMember($this->group)) {
if ($this->actor->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)) {
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.
$this->clientError(_m('You have been blocked from that group by the admin.'), 403);
}
try {
$profile->joinGroup($this->group);
$this->actor->joinGroup($this->group);
} catch (Exception $e) {
// 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.'),
$oprofile->uri, $this->group->nickname));
$this->oprofile->getUri(), $this->group->getNickname()));
}
}
@ -159,26 +153,20 @@ class GroupsalmonAction extends SalmonAction
*/
function handleLeave()
{
$oprofile = $this->ensureProfile();
if (!$oprofile instanceof Ostatus_profile) {
// 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()) {
// ensureProfile throws exception on failure
if ($this->oprofile->isGroup()) {
// 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}");
$profile = $oprofile->localProfile();
common_log(LOG_INFO, sprintf('Remote profile %s leaving local group %s', $this->oprofile->getUri(), $this->group->getNickname()));
try {
$profile->leaveGroup($this->group);
$this->actor->leaveGroup($this->group);
} catch (Exception $e) {
// 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.'),
$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);
if (empty($this->peopletag)) {
if (!$this->peopletag instanceof Profile_list) {
// TRANS: Client error displayed when referring to a non-existing 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.
$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,
* currently we're doing the main logic in joingroup action
* and so have to repeat it here.
*
* @throws NoProfileException from localProfile if missing locally stored Profile object
*/
function handleSubscribe()
{
$oprofile = $this->ensureProfile();
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()) {
if ($this->oprofile->isGroup()) {
// TRANS: Client error displayed when trying to subscribe a group to a list.
$this->clientError(_m('Groups cannot subscribe to lists.'));
}
common_log(LOG_INFO, "Remote profile {$oprofile->uri} subscribing to local peopletag ".$this->peopletag->getBestName());
$profile = $oprofile->localProfile();
if ($this->peopletag->hasSubscriber($profile)) {
common_log(LOG_INFO, sprintf('Remote profile %s subscribing to local peopletag %s', $this->oprofile->getUri(), $this->peopletag->getBestName()));
if ($this->peopletag->hasSubscriber($this->actor)) {
// Already a member; we'll take it silently to aid in resolving
// inconsistencies on the other side.
return true;
@ -110,12 +104,12 @@ class PeopletagsalmonAction extends SalmonAction
// his own updates?
try {
Profile_tag_subscription::add($this->peopletag, $profile);
Profile_tag_subscription::add($this->peopletag, $this->actor);
} catch (Exception $e) {
// 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.
$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()
{
$oprofile = $this->ensureProfile();
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()) {
if ($this->oprofile->isGroup()) {
// TRANS: Client error displayed when trying to unsubscribe a group from a list.
$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 {
$profile = $oprofile->localProfile();
Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $profile->id);
Profile_tag_subscription::remove($this->peopletag->tagger, $this->peopletag->tag, $this->actor->id);
} catch (Exception $e) {
// 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.
$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()
{
$oprofile = $this->ensureProfile();
if ($oprofile instanceof Ostatus_profile) {
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.");
}
common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
Subscription::start($this->actor, $this->target);
}
/**
@ -127,17 +122,12 @@ class UsersalmonAction extends SalmonAction
*/
function handleUnfollow()
{
$oprofile = $this->ensureProfile();
if ($oprofile instanceof Ostatus_profile) {
common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $oprofile->getUri(), $this->target->getNickname()));
common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
try {
Subscription::cancel($oprofile->localProfile(), $this->target);
Subscription::cancel($this->actor, $this->target);
} 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()
{
$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));
if ($old instanceof Fave) {
@ -158,7 +147,7 @@ class UsersalmonAction extends SalmonAction
throw new AlreadyFulfilledException(_m('This is already a favorite.'));
}
if (!Fave::addNew($profile, $notice)) {
if (!Fave::addNew($this->actor, $notice)) {
// TRANS: Client exception.
throw new ClientException(_m('Could not save new favorite.'));
}
@ -171,9 +160,8 @@ class UsersalmonAction extends SalmonAction
function handleUnfavorite()
{
$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));
if (!$fave instanceof Fave) {
// TRANS: Client exception.
@ -204,7 +192,6 @@ class UsersalmonAction extends SalmonAction
}
// save the list
$tagger = $this->ensureProfile();
$list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
$ptag = $list->localPeopletag();
@ -222,7 +209,6 @@ class UsersalmonAction extends SalmonAction
if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
// TRANS: Client exception.
throw new ClientException(_m('Not a person object.'));
return false;
}
// this is a peopletag
$tagged = User::getKV('uri', $this->activity->objects[0]->id);
@ -238,7 +224,6 @@ class UsersalmonAction extends SalmonAction
}
// save the list
$tagger = $this->ensureProfile();
$list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
$ptag = $list->localPeopletag();

View File

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

View File

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