3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * @package OStatusPlugin
24 * @author James Walker <james@status.net>
26 class UsersalmonAction extends SalmonAction
28 protected function prepare(array $args=array())
30 parent::prepare($args);
32 $this->user = User::getByID($this->trimmed('id'));
34 $this->target = $this->user->getProfile();
36 // Notice must either be a) in reply to a notice by this user
37 // or b) in reply to a notice to the attention of this user
38 // or c) to the attention of this user
39 // or d) reference the user as an activity:object
43 if (!empty($this->activity->context->replyToID)) {
45 $notice = Notice::getByUri($this->activity->context->replyToID);
46 } catch (NoResultException $e) {
51 if ($notice instanceof Notice &&
52 ($this->target->sameAs($notice->getProfile())
53 || in_array($this->target->getID(), $notice->getAttentionProfileIDs())
55 // In reply to a notice either from or mentioning this user.
56 common_debug('User is the owner or was in the attention list of thr:in-reply-to activity.');
57 } elseif (!empty($this->activity->context->attention) &&
58 array_key_exists($this->target->getUri(), $this->activity->context->attention)) {
59 // To the attention of this user.
60 common_debug('User was in attention list of salmon slap.');
61 } elseif (!empty($this->activity->objects) && $this->activity->objects[0]->id === $this->target->getUri()) {
62 // The user is the object of this slap (unfollow for example)
63 common_debug('User URI was the id of the salmon slap object.');
65 common_debug('User was NOT found in salmon slap context.');
66 // TRANS: Client exception.
67 throw new ClientException(_m('The owner of this salmon endpoint was not in the context of the carried slap.'));
74 * We've gotten a post event on the Salmon backchannel, probably a reply.
76 * @todo validate if we need to handle this post, then call into
77 * ostatus_profile's general incoming-post handling.
81 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
83 // @fixme: process all activity objects?
84 switch ($this->activity->objects[0]->type) {
85 case ActivityObject::ARTICLE:
86 case ActivityObject::BLOGENTRY:
87 case ActivityObject::NOTE:
88 case ActivityObject::STATUS:
89 case ActivityObject::COMMENT:
92 // TRANS: Client exception thrown when an undefied activity is performed.
93 throw new ClientException(_m('Cannot handle that kind of post.'));
98 } catch (AlreadyFulfilledException $e) {
104 * We've gotten a follow/subscribe notification from a remote user.
105 * Save a subscription relationship for them.
107 function handleFollow()
109 common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
110 Subscription::start($this->actor, $this->target);
114 * We've gotten an unfollow/unsubscribe notification from a remote user.
115 * Check if we have a subscription relationship for them and kill it.
117 * @fixme probably catch exceptions on fail?
119 function handleUnfollow()
121 common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
123 Subscription::cancel($this->actor, $this->target);
124 } catch (NoProfileException $e) {
125 common_debug('Could not find profile for Subscription: '.$e->getMessage());
131 if ($this->activity->target->type == ActivityObject::_LIST) {
132 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
133 // TRANS: Client exception.
134 throw new ClientException(_m('Not a person object.'));
136 // this is a peopletag
137 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
139 if (!$tagged instanceof User) {
140 // TRANS: Client exception.
141 throw new ClientException(_m('Unidentified profile being listed.'));
144 if ($tagged->id !== $this->target->id) {
145 // TRANS: Client exception.
146 throw new ClientException(_m('This user is not the one being listed.'));
150 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
152 $ptag = $list->localPeopletag();
153 $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
155 // TRANS: Client exception.
156 throw new ClientException(_m('The listing could not be saved.'));
161 function handleUntag()
163 if ($this->activity->target->type == ActivityObject::_LIST) {
164 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
165 // TRANS: Client exception.
166 throw new ClientException(_m('Not a person object.'));
168 // this is a peopletag
169 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
171 if (!$tagged instanceof User) {
172 // TRANS: Client exception.
173 throw new ClientException(_m('Unidentified profile being unlisted.'));
176 if ($tagged->id !== $this->target->id) {
177 // TRANS: Client exception.
178 throw new ClientException(_m('This user is not the one being unlisted.'));
182 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
184 $ptag = $list->localPeopletag();
185 $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
188 // TRANS: Client exception.
189 throw new ClientException(_m('The listing could not be deleted.'));
195 * @param ActivityObject $object
197 * @throws ClientException on invalid input
199 function getNotice(ActivityObject $object)
201 switch ($object->type) {
202 case ActivityObject::ARTICLE:
203 case ActivityObject::BLOGENTRY:
204 case ActivityObject::NOTE:
205 case ActivityObject::STATUS:
206 case ActivityObject::COMMENT:
209 // TRANS: Client exception.
210 throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
213 $notice = Notice::getKV('uri', $object->id);
215 if (!$notice instanceof Notice) {
216 // TRANS: Client exception. %s is an object ID.
217 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
220 if ($notice->profile_id != $this->target->id) {
221 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
222 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'), $object->id, $this->target->id));