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/>.
21 * @package OStatusPlugin
22 * @author James Walker <james@status.net>
25 if (!defined('STATUSNET')) {
29 class UsersalmonAction extends SalmonAction
31 function prepare($args)
33 parent::prepare($args);
35 $id = $this->trimmed('id');
38 $this->clientError(_('No ID.'));
41 $this->user = User::staticGet('id', $id);
43 if (empty($this->user)) {
44 $this->clientError(_('No such user.'));
51 * We've gotten a post event on the Salmon backchannel, probably a reply.
53 * @todo validate if we need to handle this post, then call into
54 * ostatus_profile's general incoming-post handling.
58 common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
60 // @fixme: process all activity objects?
61 switch ($this->activity->objects[0]->type) {
62 case ActivityObject::ARTICLE:
63 case ActivityObject::BLOGENTRY:
64 case ActivityObject::NOTE:
65 case ActivityObject::STATUS:
66 case ActivityObject::COMMENT:
69 throw new ClientException("Can't handle that kind of post.");
72 // Notice must either be a) in reply to a notice by this user
73 // or b) to the attention of this user
74 // or c) in reply to a notice to the attention of this user
76 $context = $this->activity->context;
78 if (!empty($context->replyToID)) {
79 common_log(LOG_DEBUG, "Got a notice in reply to '{$context->replyToID}' ({$context->replyToUrl})");
80 $notice = Notice::staticGet('uri', $context->replyToID);
82 throw new ClientException("In reply to unknown notice");
84 if ($notice->profile_id != $this->user->id &&
85 !in_array($this->user->id, $notice->getReplies())) {
86 throw new ClientException("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 throw new ClientException("To the attention of user(s) not including this one!");
95 throw new ClientException("Not to anyone in reply to anything!");
98 $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
100 if (!empty($existing)) {
101 common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
109 * We've gotten a follow/subscribe notification from a remote user.
110 * Save a subscription relationship for them.
113 function handleFollow()
115 $oprofile = $this->ensureProfile();
117 common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
118 Subscription::start($oprofile->localProfile(),
119 $this->user->getProfile());
121 common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
126 * We've gotten an unfollow/unsubscribe notification from a remote user.
127 * Check if we have a subscription relationship for them and kill it.
129 * @fixme probably catch exceptions on fail?
131 function handleUnfollow()
133 $oprofile = $this->ensureProfile();
135 common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
136 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
138 common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
143 * Remote user likes one of our posts.
144 * Confirm the post is ours, and save a local favorite event.
147 function handleFavorite()
149 $notice = $this->getNotice($this->activity->objects[0]);
150 $profile = $this->ensureProfile()->localProfile();
152 $old = Fave::pkeyGet(array('user_id' => $profile->id,
153 'notice_id' => $notice->id));
156 throw new ClientException("We already know that's a fave!");
159 if (!Fave::addNew($profile, $notice)) {
160 throw new ClientException("Could not save new favorite.");
165 * Remote user doesn't like one of our posts after all!
166 * Confirm the post is ours, and save a local favorite event.
168 function handleUnfavorite()
170 $notice = $this->getNotice($this->activity->objects[0]);
171 $profile = $this->ensureProfile()->localProfile();
173 $fave = Fave::pkeyGet(array('user_id' => $profile->id,
174 'notice_id' => $notice->id));
176 throw new ClientException("Notice wasn't favorited!");
183 * @param ActivityObject $object
185 * @throws ClientException on invalid input
187 function getNotice($object)
190 throw new ClientException("Can't favorite/unfavorite without an object.");
193 switch ($object->type) {
194 case ActivityObject::ARTICLE:
195 case ActivityObject::BLOGENTRY:
196 case ActivityObject::NOTE:
197 case ActivityObject::STATUS:
198 case ActivityObject::COMMENT:
201 throw new ClientException("Can't handle that kind of object for liking/faving.");
204 $notice = Notice::staticGet('uri', $object->id);
206 if (empty($notice)) {
207 throw new ClientException("Notice with ID $object->id unknown.");
210 if ($notice->profile_id != $this->user->id) {
211 throw new ClientException("Notice with ID $object->id not posted by $this->user->id.");