]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
UsersalmonAction updated to stronger typing standards
[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         } else if (!empty($context->attention) &&
91                    (array_key_exists($this->user->uri, $context->attention) ||
92                     array_key_exists($common_profile_url($this->user->nickname),
93                              $context->attention)))
94         {
95             // To the attention of this user.
96         } else {
97             // TRANS: Client exception.
98             throw new ClientException(_m('Not to anyone in reply to anything.'));
99         }
100
101         $existing = Notice::getKV('uri', $this->activity->objects[0]->id);
102         if ($existing instanceof Notice) {
103             common_log(LOG_ERR, "Not saving notice with duplicate URI '".$existing->getUri()."' (seems it already exists).");
104             return;
105         }
106
107         $this->saveNotice();
108     }
109
110     /**
111      * We've gotten a follow/subscribe notification from a remote user.
112      * Save a subscription relationship for them.
113      */
114     function handleFollow()
115     {
116         $oprofile = $this->ensureProfile();
117         if ($oprofile instanceof Ostatus_profile) {
118             common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
119             Subscription::start($oprofile->localProfile(),
120                                 $this->user->getProfile());
121         } else {
122             common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
123         }
124     }
125
126     /**
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.
129      *
130      * @fixme probably catch exceptions on fail?
131      */
132     function handleUnfollow()
133     {
134         $oprofile = $this->ensureProfile();
135         if ($oprofile instanceof Ostatus_profile) {
136             common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $oprofile->getUri(), $this->user->getNickname()));
137             Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
138         } else {
139             common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
140         }
141     }
142
143     /**
144      * Remote user likes one of our posts.
145      * Confirm the post is ours, and save a local favorite event.
146      */
147
148     function handleFavorite()
149     {
150         $notice = $this->getNotice($this->activity->objects[0]);
151         $profile = $this->ensureProfile()->localProfile();
152
153         $old = Fave::pkeyGet(array('user_id' => $profile->id,
154                                    'notice_id' => $notice->id));
155
156         if ($old instanceof Fave) {
157             // TRANS: Client exception.
158             throw new ClientException(_m('This is already a favorite.'));
159         }
160
161         if (!Fave::addNew($profile, $notice)) {
162            // TRANS: Client exception.
163            throw new ClientException(_m('Could not save new favorite.'));
164         }
165     }
166
167     /**
168      * Remote user doesn't like one of our posts after all!
169      * Confirm the post is ours, and save a local favorite event.
170      */
171     function handleUnfavorite()
172     {
173         $notice = $this->getNotice($this->activity->objects[0]);
174         $profile = $this->ensureProfile()->localProfile();
175
176         $fave = Fave::pkeyGet(array('user_id' => $profile->id,
177                                    'notice_id' => $notice->id));
178         if (!$fave instanceof Fave) {
179             // TRANS: Client exception.
180             throw new ClientException(_m('Notice was not favorited!'));
181         }
182
183         $fave->delete();
184     }
185
186     function handleTag()
187     {
188         if ($this->activity->target->type == ActivityObject::_LIST) {
189             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
190                 // TRANS: Client exception.
191                 throw new ClientException(_m('Not a person object.'));
192             }
193             // this is a peopletag
194             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
195
196             if (!$tagged instanceof User) {
197                 // TRANS: Client exception.
198                 throw new ClientException(_m('Unidentified profile being listed.'));
199             }
200
201             if ($tagged->id !== $this->user->id) {
202                 // TRANS: Client exception.
203                 throw new ClientException(_m('This user is not the one being listed.'));
204             }
205
206             // save the list
207             $tagger = $this->ensureProfile();
208             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
209
210             $ptag = $list->localPeopletag();
211             $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
212             if (!$result) {
213                 // TRANS: Client exception.
214                 throw new ClientException(_m('The listing could not be saved.'));
215             }
216         }
217     }
218
219     function handleUntag()
220     {
221         if ($this->activity->target->type == ActivityObject::_LIST) {
222             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
223                 // TRANS: Client exception.
224                 throw new ClientException(_m('Not a person object.'));
225                 return false;
226             }
227             // this is a peopletag
228             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
229
230             if (!$tagged instanceof User) {
231                 // TRANS: Client exception.
232                 throw new ClientException(_m('Unidentified profile being unlisted.'));
233             }
234
235             if ($tagged->id !== $this->user->id) {
236                 // TRANS: Client exception.
237                 throw new ClientException(_m('This user is not the one being unlisted.'));
238             }
239
240             // save the list
241             $tagger = $this->ensureProfile();
242             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
243
244             $ptag = $list->localPeopletag();
245             $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
246
247             if (!$result) {
248                 // TRANS: Client exception.
249                 throw new ClientException(_m('The listing could not be deleted.'));
250             }
251         }
252     }
253
254     /**
255      * @param ActivityObject $object
256      * @return Notice
257      * @throws ClientException on invalid input
258      */
259     function getNotice(ActivityObject $object)
260     {
261         switch ($object->type) {
262         case ActivityObject::ARTICLE:
263         case ActivityObject::BLOGENTRY:
264         case ActivityObject::NOTE:
265         case ActivityObject::STATUS:
266         case ActivityObject::COMMENT:
267             break;
268         default:
269             // TRANS: Client exception.
270             throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
271         }
272
273         $notice = Notice::getKV('uri', $object->id);
274
275         if (!$notice instanceof Notice) {
276             // TRANS: Client exception. %s is an object ID.
277             throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
278         }
279
280         if ($notice->profile_id != $this->user->id) {
281             // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
282             throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));
283         }
284
285         return $notice;
286     }
287 }