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 $this->clientError(_m('No ID.'));
40 $this->user = User::staticGet('id', $id);
42 if (empty($this->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 throw new ClientException("Can't handle that kind of post.");
73 // Notice must either be a) in reply to a notice by this user
74 // or b) to the attention of this user
75 // or c) in reply to a notice to the attention of this user
77 $context = $this->activity->context;
79 if (!empty($context->replyToID)) {
80 $notice = Notice::staticGet('uri', $context->replyToID);
82 // TRANS: Client exception.
83 throw new ClientException(_m('In reply to unknown notice.'));
85 if ($notice->profile_id != $this->user->id &&
86 !in_array($this->user->id, $notice->getReplies())) {
87 // TRANS: Client exception.
88 throw new ClientException(_m('In reply to a notice not by this user and not mentioning this user.'));
90 } else if (!empty($context->attention)) {
91 if (!in_array($this->user->uri, $context->attention) &&
92 !in_array(common_profile_url($this->user->nickname), $context->attention)) {
93 common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
94 // TRANS: Client exception.
95 throw new ClientException('To the attention of user(s), not including this one.');
98 // TRANS: Client exception.
99 throw new ClientException('Not to anyone in reply to anything.');
102 $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
104 if (!empty($existing)) {
105 common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
113 * We've gotten a follow/subscribe notification from a remote user.
114 * Save a subscription relationship for them.
116 function handleFollow()
118 $oprofile = $this->ensureProfile();
120 common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
121 Subscription::start($oprofile->localProfile(),
122 $this->user->getProfile());
124 common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
129 * We've gotten an unfollow/unsubscribe notification from a remote user.
130 * Check if we have a subscription relationship for them and kill it.
132 * @fixme probably catch exceptions on fail?
134 function handleUnfollow()
136 $oprofile = $this->ensureProfile();
138 common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
139 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
141 common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
146 * Remote user likes one of our posts.
147 * Confirm the post is ours, and save a local favorite event.
150 function handleFavorite()
152 $notice = $this->getNotice($this->activity->objects[0]);
153 $profile = $this->ensureProfile()->localProfile();
155 $old = Fave::pkeyGet(array('user_id' => $profile->id,
156 'notice_id' => $notice->id));
159 // TRANS: Client exception.
160 throw new ClientException(_('This is already a favorite.'));
163 if (!Fave::addNew($profile, $notice)) {
164 // TRANS: Client exception.
165 throw new ClientException(_m('Could not save new favorite.'));
170 * Remote user doesn't like one of our posts after all!
171 * Confirm the post is ours, and save a local favorite event.
173 function handleUnfavorite()
175 $notice = $this->getNotice($this->activity->objects[0]);
176 $profile = $this->ensureProfile()->localProfile();
178 $fave = Fave::pkeyGet(array('user_id' => $profile->id,
179 'notice_id' => $notice->id));
181 // TRANS: Client exception.
182 throw new ClientException(_('Notice wasn\'t favorited!'));
189 * @param ActivityObject $object
191 * @throws ClientException on invalid input
193 function getNotice($object)
196 // TRANS: Client exception.
197 throw new ClientException(_m('Can\'t favorite/unfavorite without an object.'));
200 switch ($object->type) {
201 case ActivityObject::ARTICLE:
202 case ActivityObject::BLOGENTRY:
203 case ActivityObject::NOTE:
204 case ActivityObject::STATUS:
205 case ActivityObject::COMMENT:
208 // TRANS: Client exception.
209 throw new ClientException(_m('Can\'t handle that kind of object for liking/faving.'));
212 $notice = Notice::staticGet('uri', $object->id);
214 if (empty($notice)) {
215 // TRANS: Client exception. %s is an object ID.
216 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
219 if ($notice->profile_id != $this->user->id) {
220 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
221 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));