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('STATUSNET')) {
25 * @package OStatusPlugin
26 * @author James Walker <james@status.net>
28 class UsersalmonAction extends SalmonAction
30 function prepare($args)
32 parent::prepare($args);
34 $id = $this->trimmed('id');
37 // TRANS: Client error displayed trying to perform an action without providing an ID.
38 $this->clientError(_m('No ID.'));
41 $this->user = User::getKV('id', $id);
43 if (empty($this->user)) {
44 // TRANS: Client error displayed when referring to a non-existing user.
45 $this->clientError(_m('No such user.'));
48 $this->target = $this->user;
54 * We've gotten a post event on the Salmon backchannel, probably a reply.
56 * @todo validate if we need to handle this post, then call into
57 * ostatus_profile's general incoming-post handling.
61 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
63 // @fixme: process all activity objects?
64 switch ($this->activity->objects[0]->type) {
65 case ActivityObject::ARTICLE:
66 case ActivityObject::BLOGENTRY:
67 case ActivityObject::NOTE:
68 case ActivityObject::STATUS:
69 case ActivityObject::COMMENT:
72 // TRANS: Client exception thrown when an undefied activity is performed.
73 throw new ClientException(_m('Cannot handle that kind of post.'));
76 // Notice must either be a) in reply to a notice by this user
77 // or b) to the attention of this user
78 // or c) in reply to a notice to the attention of this user
80 $context = $this->activity->context;
82 if (!empty($context->replyToID)) {
83 $notice = Notice::getKV('uri', $context->replyToID);
85 // TRANS: Client exception.
86 throw new ClientException(_m('In reply to unknown notice.'));
88 if ($notice->profile_id != $this->user->id &&
89 !in_array($this->user->id, $notice->getReplies())) {
90 // TRANS: Client exception.
91 throw new ClientException(_m('In reply to a notice not by this user and not mentioning this user.'));
93 } else if (!empty($context->attention)) {
94 if (!in_array($this->user->uri, $context->attention) &&
95 !in_array(common_profile_url($this->user->nickname), $context->attention)) {
96 common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
97 // TRANS: Client exception.
98 throw new ClientException(_m('To the attention of user(s), not including this one.'));
101 // TRANS: Client exception.
102 throw new ClientException(_m('Not to anyone in reply to anything.'));
105 $existing = Notice::getKV('uri', $this->activity->objects[0]->id);
107 if (!empty($existing)) {
108 common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
116 * We've gotten a follow/subscribe notification from a remote user.
117 * Save a subscription relationship for them.
119 function handleFollow()
121 $oprofile = $this->ensureProfile();
123 common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
124 Subscription::start($oprofile->localProfile(),
125 $this->user->getProfile());
127 common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
132 * We've gotten an unfollow/unsubscribe notification from a remote user.
133 * Check if we have a subscription relationship for them and kill it.
135 * @fixme probably catch exceptions on fail?
137 function handleUnfollow()
139 $oprofile = $this->ensureProfile();
141 common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
142 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
144 common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
149 * Remote user likes one of our posts.
150 * Confirm the post is ours, and save a local favorite event.
153 function handleFavorite()
155 $notice = $this->getNotice($this->activity->objects[0]);
156 $profile = $this->ensureProfile()->localProfile();
158 $old = Fave::pkeyGet(array('user_id' => $profile->id,
159 'notice_id' => $notice->id));
162 // TRANS: Client exception.
163 throw new ClientException(_m('This is already a favorite.'));
166 if (!Fave::addNew($profile, $notice)) {
167 // TRANS: Client exception.
168 throw new ClientException(_m('Could not save new favorite.'));
173 * Remote user doesn't like one of our posts after all!
174 * Confirm the post is ours, and save a local favorite event.
176 function handleUnfavorite()
178 $notice = $this->getNotice($this->activity->objects[0]);
179 $profile = $this->ensureProfile()->localProfile();
181 $fave = Fave::pkeyGet(array('user_id' => $profile->id,
182 'notice_id' => $notice->id));
184 // TRANS: Client exception.
185 throw new ClientException(_m('Notice was not favorited!'));
193 if ($this->activity->target->type == ActivityObject::_LIST) {
194 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
195 // TRANS: Client exception.
196 throw new ClientException(_m('Not a person object.'));
199 // this is a peopletag
200 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
202 if (empty($tagged)) {
203 // TRANS: Client exception.
204 throw new ClientException(_m('Unidentified profile being listed.'));
207 if ($tagged->id !== $this->user->id) {
208 // TRANS: Client exception.
209 throw new ClientException(_m('This user is not the one being listed.'));
213 $tagger = $this->ensureProfile();
214 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
216 $ptag = $list->localPeopletag();
217 $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
219 // TRANS: Client exception.
220 throw new ClientException(_m('The listing could not be saved.'));
225 function handleUntag()
227 if ($this->activity->target->type == ActivityObject::_LIST) {
228 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
229 // TRANS: Client exception.
230 throw new ClientException(_m('Not a person object.'));
233 // this is a peopletag
234 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
236 if (empty($tagged)) {
237 // TRANS: Client exception.
238 throw new ClientException(_m('Unidentified profile being unlisted.'));
241 if ($tagged->id !== $this->user->id) {
242 // TRANS: Client exception.
243 throw new ClientException(_m('This user is not the one being unlisted.'));
247 $tagger = $this->ensureProfile();
248 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
250 $ptag = $list->localPeopletag();
251 $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
254 // TRANS: Client exception.
255 throw new ClientException(_m('The listing could not be deleted.'));
261 * @param ActivityObject $object
263 * @throws ClientException on invalid input
265 function getNotice($object)
268 // TRANS: Client exception.
269 throw new ClientException(_m('Cannot favorite/unfavorite without an object.'));
272 switch ($object->type) {
273 case ActivityObject::ARTICLE:
274 case ActivityObject::BLOGENTRY:
275 case ActivityObject::NOTE:
276 case ActivityObject::STATUS:
277 case ActivityObject::COMMENT:
280 // TRANS: Client exception.
281 throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
284 $notice = Notice::getKV('uri', $object->id);
286 if (empty($notice)) {
287 // TRANS: Client exception. %s is an object ID.
288 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
291 if ($notice->profile_id != $this->user->id) {
292 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
293 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));