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.'));
50 * We've gotten a post event on the Salmon backchannel, probably a reply.
52 * @todo validate if we need to handle this post, then call into
53 * ostatus_profile's general incoming-post handling.
57 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
59 // @fixme: process all activity objects?
60 switch ($this->activity->objects[0]->type) {
61 case ActivityObject::ARTICLE:
62 case ActivityObject::BLOGENTRY:
63 case ActivityObject::NOTE:
64 case ActivityObject::STATUS:
65 case ActivityObject::COMMENT:
68 throw new ClientException("Can't handle that kind of post.");
71 // Notice must either be a) in reply to a notice by this user
72 // or b) to the attention of this user
73 // or c) in reply to a notice to the attention of this user
75 $context = $this->activity->context;
77 if (!empty($context->replyToID)) {
78 $notice = Notice::staticGet('uri', $context->replyToID);
80 // TRANS: Client exception.
81 throw new ClientException(_m('In reply to unknown notice.'));
83 if ($notice->profile_id != $this->user->id &&
84 !in_array($this->user->id, $notice->getReplies())) {
85 // TRANS: Client exception.
86 throw new ClientException(_m('In reply to a notice not by this user and not mentioning this user.'));
88 } else if (!empty($context->attention)) {
89 if (!in_array($this->user->uri, $context->attention) &&
90 !in_array(common_profile_url($this->user->nickname), $context->attention)) {
91 common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
92 // TRANS: Client exception.
93 throw new ClientException('To the attention of user(s), not including this one.');
96 // TRANS: Client exception.
97 throw new ClientException('Not to anyone in reply to anything.');
100 $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
102 if (!empty($existing)) {
103 common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; 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();
118 common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
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();
136 common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
137 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
139 common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
144 * Remote user likes one of our posts.
145 * Confirm the post is ours, and save a local favorite event.
148 function handleFavorite()
150 $notice = $this->getNotice($this->activity->objects[0]);
151 $profile = $this->ensureProfile()->localProfile();
153 $old = Fave::pkeyGet(array('user_id' => $profile->id,
154 'notice_id' => $notice->id));
157 // TRANS: Client exception.
158 throw new ClientException(_('This is already a favorite.'));
161 if (!Fave::addNew($profile, $notice)) {
162 // TRANS: Client exception.
163 throw new ClientException(_m('Could not save new favorite.'));
168 * Remote user doesn't like one of our posts after all!
169 * Confirm the post is ours, and save a local favorite event.
171 function handleUnfavorite()
173 $notice = $this->getNotice($this->activity->objects[0]);
174 $profile = $this->ensureProfile()->localProfile();
176 $fave = Fave::pkeyGet(array('user_id' => $profile->id,
177 'notice_id' => $notice->id));
179 // TRANS: Client exception.
180 throw new ClientException(_('Notice wasn\'t favorited!'));
187 * @param ActivityObject $object
189 * @throws ClientException on invalid input
191 function getNotice($object)
194 // TRANS: Client exception.
195 throw new ClientException(_m('Can\'t favorite/unfavorite without an object.'));
198 switch ($object->type) {
199 case ActivityObject::ARTICLE:
200 case ActivityObject::BLOGENTRY:
201 case ActivityObject::NOTE:
202 case ActivityObject::STATUS:
203 case ActivityObject::COMMENT:
206 // TRANS: Client exception.
207 throw new ClientException(_m('Can\'t handle that kind of object for liking/faving.'));
210 $notice = Notice::staticGet('uri', $object->id);
212 if (empty($notice)) {
213 // TRANS: Client exception. %s is an object ID.
214 throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
217 if ($notice->profile_id != $this->user->id) {
218 // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
219 throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));