]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
Merge remote branch 'gitorious/0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / OStatus / actions / usersalmon.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * @package OStatusPlugin
26  * @author James Walker <james@status.net>
27  */
28 class UsersalmonAction extends SalmonAction
29 {
30     function prepare($args)
31     {
32         parent::prepare($args);
33
34         $id = $this->trimmed('id');
35
36         if (!$id) {
37             $this->clientError(_m('No ID.'));
38         }
39
40         $this->user = User::staticGet('id', $id);
41
42         if (empty($this->user)) {
43             $this->clientError(_m('No such user.'));
44         }
45
46         $this->target = $this->user;
47
48         return true;
49     }
50
51     /**
52      * We've gotten a post event on the Salmon backchannel, probably a reply.
53      *
54      * @todo validate if we need to handle this post, then call into
55      * ostatus_profile's general incoming-post handling.
56      */
57     function handlePost()
58     {
59         common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
60
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:
68             break;
69         default:
70             throw new ClientException("Can't handle that kind of post.");
71         }
72
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
76
77         $context = $this->activity->context;
78
79         if (!empty($context->replyToID)) {
80             $notice = Notice::staticGet('uri', $context->replyToID);
81             if (empty($notice)) {
82                 // TRANS: Client exception.
83                 throw new ClientException(_m('In reply to unknown notice.'));
84             }
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.'));
89             }
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.');
96             }
97         } else {
98             // TRANS: Client exception.
99             throw new ClientException('Not to anyone in reply to anything.');
100         }
101
102         $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
103
104         if (!empty($existing)) {
105             common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
106             return;
107         }
108
109         $this->saveNotice();
110     }
111
112     /**
113      * We've gotten a follow/subscribe notification from a remote user.
114      * Save a subscription relationship for them.
115      */
116     function handleFollow()
117     {
118         $oprofile = $this->ensureProfile();
119         if ($oprofile) {
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());
123         } else {
124             common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
125         }
126     }
127
128     /**
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.
131      *
132      * @fixme probably catch exceptions on fail?
133      */
134     function handleUnfollow()
135     {
136         $oprofile = $this->ensureProfile();
137         if ($oprofile) {
138             common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
139             Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
140         } else {
141             common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
142         }
143     }
144
145     /**
146      * Remote user likes one of our posts.
147      * Confirm the post is ours, and save a local favorite event.
148      */
149
150     function handleFavorite()
151     {
152         $notice = $this->getNotice($this->activity->objects[0]);
153         $profile = $this->ensureProfile()->localProfile();
154
155         $old = Fave::pkeyGet(array('user_id' => $profile->id,
156                                    'notice_id' => $notice->id));
157
158         if (!empty($old)) {
159             // TRANS: Client exception.
160             throw new ClientException(_('This is already a favorite.'));
161         }
162
163         if (!Fave::addNew($profile, $notice)) {
164            // TRANS: Client exception.
165            throw new ClientException(_m('Could not save new favorite.'));
166         }
167     }
168
169     /**
170      * Remote user doesn't like one of our posts after all!
171      * Confirm the post is ours, and save a local favorite event.
172      */
173     function handleUnfavorite()
174     {
175         $notice = $this->getNotice($this->activity->objects[0]);
176         $profile = $this->ensureProfile()->localProfile();
177
178         $fave = Fave::pkeyGet(array('user_id' => $profile->id,
179                                    'notice_id' => $notice->id));
180         if (empty($fave)) {
181             // TRANS: Client exception.
182             throw new ClientException(_('Notice wasn\'t favorited!'));
183         }
184
185         $fave->delete();
186     }
187
188     /**
189      * @param ActivityObject $object
190      * @return Notice
191      * @throws ClientException on invalid input
192      */
193     function getNotice($object)
194     {
195         if (!$object) {
196             // TRANS: Client exception.
197             throw new ClientException(_m('Can\'t favorite/unfavorite without an object.'));
198         }
199
200         switch ($object->type) {
201         case ActivityObject::ARTICLE:
202         case ActivityObject::BLOGENTRY:
203         case ActivityObject::NOTE:
204         case ActivityObject::STATUS:
205         case ActivityObject::COMMENT:
206             break;
207         default:
208             // TRANS: Client exception.
209             throw new ClientException(_m('Can\'t handle that kind of object for liking/faving.'));
210         }
211
212         $notice = Notice::staticGet('uri', $object->id);
213
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));
217         }
218
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));
222         }
223
224         return $notice;
225     }
226 }