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 common_debug('Referenced Notice object found with URI: '.$notice->getUri());
47 } catch (NoResultException $e) {
48 common_debug('Referenced Notice object NOT found with URI: '.$this->activity->context->replyToID);
53 if ($notice instanceof Notice &&
54 ($this->target->sameAs($notice->getProfile())
55 || in_array($this->target->getID(), $notice->getAttentionProfileIDs())
57 // In reply to a notice either from or mentioning this user.
58 common_debug('User is the owner or was in the attention list of thr:in-reply-to activity.');
59 } elseif (!empty($this->activity->context->attention) &&
60 array_key_exists($this->target->getUri(), $this->activity->context->attention)) {
61 // To the attention of this user.
62 common_debug('User was in attention list of salmon slap.');
63 } elseif (!empty($this->activity->objects) && $this->activity->objects[0]->id === $this->target->getUri()) {
64 // The user is the object of this slap (unfollow for example)
65 common_debug('User URI was the id of the salmon slap object.');
67 common_debug('User was NOT found in salmon slap context.');
68 // TRANS: Client exception.
69 throw new ClientException(_m('The owner of this salmon endpoint was not in the context of the carried slap.'));
76 * We've gotten a post event on the Salmon backchannel, probably a reply.
78 * @todo validate if we need to handle this post, then call into
79 * ostatus_profile's general incoming-post handling.
83 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
85 // @fixme: process all activity objects?
86 switch ($this->activity->objects[0]->type) {
87 case ActivityObject::ARTICLE:
88 case ActivityObject::BLOGENTRY:
89 case ActivityObject::NOTE:
90 case ActivityObject::STATUS:
91 case ActivityObject::COMMENT:
94 // TRANS: Client exception thrown when an undefied activity is performed.
95 throw new ClientException(_m('Cannot handle that kind of post.'));
100 } catch (AlreadyFulfilledException $e) {
106 * We've gotten a follow/subscribe notification from a remote user.
107 * Save a subscription relationship for them.
109 function handleFollow()
111 common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
112 Subscription::start($this->actor, $this->target);
116 * We've gotten an unfollow/unsubscribe notification from a remote user.
117 * Check if we have a subscription relationship for them and kill it.
119 * @fixme probably catch exceptions on fail?
121 function handleUnfollow()
123 common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
125 Subscription::cancel($this->actor, $this->target);
126 } catch (NoProfileException $e) {
127 common_debug('Could not find profile for Subscription: '.$e->getMessage());
133 if ($this->activity->target->type == ActivityObject::_LIST) {
134 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
135 // TRANS: Client exception.
136 throw new ClientException(_m('Not a person object.'));
138 // this is a peopletag
139 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
141 if (!$tagged instanceof User) {
142 // TRANS: Client exception.
143 throw new ClientException(_m('Unidentified profile being listed.'));
146 if ($tagged->id !== $this->target->id) {
147 // TRANS: Client exception.
148 throw new ClientException(_m('This user is not the one being listed.'));
152 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
154 $ptag = $list->localPeopletag();
155 $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
157 // TRANS: Client exception.
158 throw new ClientException(_m('The listing could not be saved.'));
163 function handleUntag()
165 if ($this->activity->target->type == ActivityObject::_LIST) {
166 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
167 // TRANS: Client exception.
168 throw new ClientException(_m('Not a person object.'));
170 // this is a peopletag
171 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
173 if (!$tagged instanceof User) {
174 // TRANS: Client exception.
175 throw new ClientException(_m('Unidentified profile being unlisted.'));
178 if ($tagged->id !== $this->target->id) {
179 // TRANS: Client exception.
180 throw new ClientException(_m('This user is not the one being unlisted.'));
184 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
186 $ptag = $list->localPeopletag();
187 $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
190 // TRANS: Client exception.
191 throw new ClientException(_m('The listing could not be deleted.'));
197 * @param ActivityObject $object
199 * @throws ClientException on invalid input
201 function getNotice(ActivityObject $object)
203 switch ($object->type) {
204 case ActivityObject::ARTICLE:
205 case ActivityObject::BLOGENTRY:
206 case ActivityObject::NOTE:
207 case ActivityObject::STATUS:
208 case ActivityObject::COMMENT:
211 // TRANS: Client exception.
212 throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
215 $notice = Notice::getKV('uri', $object->id);
217 if (!$notice instanceof Notice) {
218 // TRANS: Client exception. %s is an object ID.
219 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
222 if ($notice->profile_id != $this->target->id) {
223 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
224 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'), $object->id, $this->target->id));