]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
misplaced dollar sign, also URLs != attention URIs
[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('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * @package OStatusPlugin
24  * @author James Walker <james@status.net>
25  */
26 class UsersalmonAction extends SalmonAction
27 {
28     protected function prepare(array $args=array())
29     {
30         parent::prepare($args);
31
32         $id = $this->trimmed('id');
33
34         if (!$id) {
35             // TRANS: Client error displayed trying to perform an action without providing an ID.
36             $this->clientError(_m('No ID.'));
37         }
38
39         $this->user = User::getKV('id', $id);
40
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.'));
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             // TRANS: Client exception thrown when an undefied activity is performed.
71             throw new ClientException(_m('Cannot handle that kind of post.'));
72         }
73
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
77
78         $context = $this->activity->context;
79         $notice = false;
80
81         if (!empty($context->replyToID)) {
82             $notice = Notice::getKV('uri', $context->replyToID);
83         }
84
85         if ($notice instanceof Notice &&
86             ($notice->profile_id == $this->user->id ||
87              array_key_exists($this->user->id, $notice->getReplies())))
88         {
89             // In reply to a notice either from or mentioning this user.
90         } elseif (!empty($context->attention) &&
91                    array_key_exists($this->user->getUri(), $context->attention)) {
92         {
93             // To the attention of this user.
94         } else {
95             // TRANS: Client exception.
96             throw new ClientException(_m('Not to anyone in reply to anything.'));
97         }
98
99         $existing = Notice::getKV('uri', $this->activity->objects[0]->id);
100         if ($existing instanceof Notice) {
101             common_log(LOG_ERR, "Not saving notice with duplicate URI '".$existing->getUri()."' (seems it already exists).");
102             return;
103         }
104
105         $this->saveNotice();
106     }
107
108     /**
109      * We've gotten a follow/subscribe notification from a remote user.
110      * Save a subscription relationship for them.
111      */
112     function handleFollow()
113     {
114         $oprofile = $this->ensureProfile();
115         if ($oprofile instanceof Ostatus_profile) {
116             common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
117             Subscription::start($oprofile->localProfile(),
118                                 $this->user->getProfile());
119         } else {
120             common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
121         }
122     }
123
124     /**
125      * We've gotten an unfollow/unsubscribe notification from a remote user.
126      * Check if we have a subscription relationship for them and kill it.
127      *
128      * @fixme probably catch exceptions on fail?
129      */
130     function handleUnfollow()
131     {
132         $oprofile = $this->ensureProfile();
133         if ($oprofile instanceof Ostatus_profile) {
134             common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
135             try {
136                 Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
137             } catch (AlreadyFulfilledException $e) {
138                 common_debug('Subscription did not exist, so there was nothing to cancel');
139             }
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 ($old instanceof Fave) {
159             // TRANS: Client exception.
160             throw new ClientException(_m('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 (!$fave instanceof Fave) {
181             // TRANS: Client exception.
182             throw new ClientException(_m('Notice was not favorited!'));
183         }
184
185         $fave->delete();
186     }
187
188     function handleTag()
189     {
190         if ($this->activity->target->type == ActivityObject::_LIST) {
191             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
192                 // TRANS: Client exception.
193                 throw new ClientException(_m('Not a person object.'));
194             }
195             // this is a peopletag
196             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
197
198             if (!$tagged instanceof User) {
199                 // TRANS: Client exception.
200                 throw new ClientException(_m('Unidentified profile being listed.'));
201             }
202
203             if ($tagged->id !== $this->user->id) {
204                 // TRANS: Client exception.
205                 throw new ClientException(_m('This user is not the one being listed.'));
206             }
207
208             // save the list
209             $tagger = $this->ensureProfile();
210             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
211
212             $ptag = $list->localPeopletag();
213             $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
214             if (!$result) {
215                 // TRANS: Client exception.
216                 throw new ClientException(_m('The listing could not be saved.'));
217             }
218         }
219     }
220
221     function handleUntag()
222     {
223         if ($this->activity->target->type == ActivityObject::_LIST) {
224             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
225                 // TRANS: Client exception.
226                 throw new ClientException(_m('Not a person object.'));
227                 return false;
228             }
229             // this is a peopletag
230             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
231
232             if (!$tagged instanceof User) {
233                 // TRANS: Client exception.
234                 throw new ClientException(_m('Unidentified profile being unlisted.'));
235             }
236
237             if ($tagged->id !== $this->user->id) {
238                 // TRANS: Client exception.
239                 throw new ClientException(_m('This user is not the one being unlisted.'));
240             }
241
242             // save the list
243             $tagger = $this->ensureProfile();
244             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
245
246             $ptag = $list->localPeopletag();
247             $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
248
249             if (!$result) {
250                 // TRANS: Client exception.
251                 throw new ClientException(_m('The listing could not be deleted.'));
252             }
253         }
254     }
255
256     /**
257      * @param ActivityObject $object
258      * @return Notice
259      * @throws ClientException on invalid input
260      */
261     function getNotice(ActivityObject $object)
262     {
263         switch ($object->type) {
264         case ActivityObject::ARTICLE:
265         case ActivityObject::BLOGENTRY:
266         case ActivityObject::NOTE:
267         case ActivityObject::STATUS:
268         case ActivityObject::COMMENT:
269             break;
270         default:
271             // TRANS: Client exception.
272             throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
273         }
274
275         $notice = Notice::getKV('uri', $object->id);
276
277         if (!$notice instanceof Notice) {
278             // TRANS: Client exception. %s is an object ID.
279             throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
280         }
281
282         if ($notice->profile_id != $this->user->id) {
283             // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
284             throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));
285         }
286
287         return $notice;
288     }
289 }