]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
Merge branch '0.9.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         return true;
47     }
48
49     /**
50      * We've gotten a post event on the Salmon backchannel, probably a reply.
51      *
52      * @todo validate if we need to handle this post, then call into
53      * ostatus_profile's general incoming-post handling.
54      */
55     function handlePost()
56     {
57         common_log(LOG_INFO, "Received post of '{$this->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
58
59         // @fixme: process all activity objects?
60         switch ($this->activity->objects[0]->type) {
61         case ActivityObject::ARTICLE:
62         case ActivityObject::BLOGENTRY:
63         case ActivityObject::NOTE:
64         case ActivityObject::STATUS:
65         case ActivityObject::COMMENT:
66             break;
67         default:
68             throw new ClientException("Can't handle that kind of post.");
69         }
70
71         // Notice must either be a) in reply to a notice by this user
72         // or b) to the attention of this user
73         // or c) in reply to a notice to the attention of this user
74
75         $context = $this->activity->context;
76
77         if (!empty($context->replyToID)) {
78             $notice = Notice::staticGet('uri', $context->replyToID);
79             if (empty($notice)) {
80                 // TRANS: Client exception.
81                 throw new ClientException(_m('In reply to unknown notice.'));
82             }
83             if ($notice->profile_id != $this->user->id &&
84                 !in_array($this->user->id, $notice->getReplies())) {
85                 // TRANS: Client exception.
86                 throw new ClientException(_m('In reply to a notice not by this user and not mentioning this user.'));
87             }
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                 // TRANS: Client exception.
93                 throw new ClientException('To the attention of user(s), not including this one.');
94             }
95         } else {
96             // TRANS: Client exception.
97             throw new ClientException('Not to anyone in reply to anything.');
98         }
99
100         $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
101
102         if (!empty($existing)) {
103             common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; 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) {
118             common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
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) {
136             common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
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 (!empty($old)) {
157             // TRANS: Client exception.
158             throw new ClientException(_('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 (empty($fave)) {
179             // TRANS: Client exception.
180             throw new ClientException(_('Notice wasn\'t favorited!'));
181         }
182
183         $fave->delete();
184     }
185
186     /**
187      * @param ActivityObject $object
188      * @return Notice
189      * @throws ClientException on invalid input
190      */
191     function getNotice($object)
192     {
193         if (!$object) {
194             // TRANS: Client exception.
195             throw new ClientException(_m('Can\'t favorite/unfavorite without an object.'));
196         }
197
198         switch ($object->type) {
199         case ActivityObject::ARTICLE:
200         case ActivityObject::BLOGENTRY:
201         case ActivityObject::NOTE:
202         case ActivityObject::STATUS:
203         case ActivityObject::COMMENT:
204             break;
205         default:
206             // TRANS: Client exception.
207             throw new ClientException(_m('Can\'t handle that kind of object for liking/faving.'));
208         }
209
210         $notice = Notice::staticGet('uri', $object->id);
211
212         if (empty($notice)) {
213             // TRANS: Client exception. %s is an object ID.
214             throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
215         }
216
217         if ($notice->profile_id != $this->user->id) {
218             // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
219             throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'),$object->id,$this->user->id));
220         }
221
222         return $notice;
223     }
224 }