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 $id = $this->trimmed('id');
35 // TRANS: Client error displayed trying to perform an action without providing an ID.
36 $this->clientError(_m('No ID.'));
39 $this->user = User::getKV('id', $id);
41 if (!$this->user instanceof User) {
42 // TRANS: Client error displayed when referring to a non-existing user.
43 $this->clientError(_m('No such user.'));
46 $this->target = $this->user;
52 * We've gotten a post event on the Salmon backchannel, probably a reply.
54 * @todo validate if we need to handle this post, then call into
55 * ostatus_profile's general incoming-post handling.
59 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
61 // @fixme: process all activity objects?
62 switch ($this->activity->objects[0]->type) {
63 case ActivityObject::ARTICLE:
64 case ActivityObject::BLOGENTRY:
65 case ActivityObject::NOTE:
66 case ActivityObject::STATUS:
67 case ActivityObject::COMMENT:
70 // TRANS: Client exception thrown when an undefied activity is performed.
71 throw new ClientException(_m('Cannot handle that kind of post.'));
74 // Notice must either be a) in reply to a notice by this user
75 // or b) in reply to a notice to the attention of this user
76 // or c) to the attention of this user
78 $context = $this->activity->context;
81 if (!empty($context->replyToID)) {
82 $notice = Notice::getKV('uri', $context->replyToID);
85 if ($notice instanceof Notice &&
86 ($notice->profile_id == $this->user->id ||
87 array_key_exists($this->user->id, $notice->getReplies())))
89 // In reply to a notice either from or mentioning this user.
90 } else if (!empty($context->attention) &&
91 (array_key_exists($this->user->uri, $context->attention) ||
92 array_key_exists($common_profile_url($this->user->nickname),
93 $context->attention)))
95 // To the attention of this user.
97 // TRANS: Client exception.
98 throw new ClientException(_m('Not to anyone in reply to anything.'));
101 $existing = Notice::getKV('uri', $this->activity->objects[0]->id);
102 if ($existing instanceof Notice) {
103 common_log(LOG_ERR, "Not saving notice with duplicate URI '".$existing->getUri()."' (seems it already exists).");
111 * We've gotten a follow/subscribe notification from a remote user.
112 * Save a subscription relationship for them.
114 function handleFollow()
116 $oprofile = $this->ensureProfile();
117 if ($oprofile instanceof Ostatus_profile) {
118 common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
119 Subscription::start($oprofile->localProfile(),
120 $this->user->getProfile());
122 common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
127 * We've gotten an unfollow/unsubscribe notification from a remote user.
128 * Check if we have a subscription relationship for them and kill it.
130 * @fixme probably catch exceptions on fail?
132 function handleUnfollow()
134 $oprofile = $this->ensureProfile();
135 if ($oprofile instanceof Ostatus_profile) {
136 common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
138 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
139 } catch (AlreadyFulfilledException $e) {
140 common_debug('Subscription did not exist, so there was nothing to cancel');
143 common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
148 * Remote user likes one of our posts.
149 * Confirm the post is ours, and save a local favorite event.
152 function handleFavorite()
154 $notice = $this->getNotice($this->activity->objects[0]);
155 $profile = $this->ensureProfile()->localProfile();
157 $old = Fave::pkeyGet(array('user_id' => $profile->id,
158 'notice_id' => $notice->id));
160 if ($old instanceof Fave) {
161 // TRANS: Client exception.
162 throw new ClientException(_m('This is already a favorite.'));
165 if (!Fave::addNew($profile, $notice)) {
166 // TRANS: Client exception.
167 throw new ClientException(_m('Could not save new favorite.'));
172 * Remote user doesn't like one of our posts after all!
173 * Confirm the post is ours, and save a local favorite event.
175 function handleUnfavorite()
177 $notice = $this->getNotice($this->activity->objects[0]);
178 $profile = $this->ensureProfile()->localProfile();
180 $fave = Fave::pkeyGet(array('user_id' => $profile->id,
181 'notice_id' => $notice->id));
182 if (!$fave instanceof Fave) {
183 // TRANS: Client exception.
184 throw new ClientException(_m('Notice was not favorited!'));
192 if ($this->activity->target->type == ActivityObject::_LIST) {
193 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
194 // TRANS: Client exception.
195 throw new ClientException(_m('Not a person object.'));
197 // this is a peopletag
198 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
200 if (!$tagged instanceof User) {
201 // TRANS: Client exception.
202 throw new ClientException(_m('Unidentified profile being listed.'));
205 if ($tagged->id !== $this->user->id) {
206 // TRANS: Client exception.
207 throw new ClientException(_m('This user is not the one being listed.'));
211 $tagger = $this->ensureProfile();
212 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
214 $ptag = $list->localPeopletag();
215 $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
217 // TRANS: Client exception.
218 throw new ClientException(_m('The listing could not be saved.'));
223 function handleUntag()
225 if ($this->activity->target->type == ActivityObject::_LIST) {
226 if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
227 // TRANS: Client exception.
228 throw new ClientException(_m('Not a person object.'));
231 // this is a peopletag
232 $tagged = User::getKV('uri', $this->activity->objects[0]->id);
234 if (!$tagged instanceof User) {
235 // TRANS: Client exception.
236 throw new ClientException(_m('Unidentified profile being unlisted.'));
239 if ($tagged->id !== $this->user->id) {
240 // TRANS: Client exception.
241 throw new ClientException(_m('This user is not the one being unlisted.'));
245 $tagger = $this->ensureProfile();
246 $list = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
248 $ptag = $list->localPeopletag();
249 $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
252 // TRANS: Client exception.
253 throw new ClientException(_m('The listing could not be deleted.'));
259 * @param ActivityObject $object
261 * @throws ClientException on invalid input
263 function getNotice(ActivityObject $object)
265 switch ($object->type) {
266 case ActivityObject::ARTICLE:
267 case ActivityObject::BLOGENTRY:
268 case ActivityObject::NOTE:
269 case ActivityObject::STATUS:
270 case ActivityObject::COMMENT:
273 // TRANS: Client exception.
274 throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
277 $notice = Notice::getKV('uri', $object->id);
279 if (!$notice instanceof Notice) {
280 // TRANS: Client exception. %s is an object ID.
281 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
284 if ($notice->profile_id != $this->user->id) {
285 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
286 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));