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