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