]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[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 /**
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->activity->objects[0]->id}' from '{$this->activity->actor->id}'");
59
60         // @fixme: process all activity objects?
61         switch ($this->activity->objects[0]->type) {
62         case ActivityObject::ARTICLE:
63         case ActivityObject::BLOGENTRY:
64         case ActivityObject::NOTE:
65         case ActivityObject::STATUS:
66         case ActivityObject::COMMENT:
67             break;
68         default:
69             throw new ClientException("Can't handle that kind of post.");
70         }
71
72         // Notice must either be a) in reply to a notice by this user
73         // or b) 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                 throw new ClientException("In reply to unknown notice");
81             }
82             if ($notice->profile_id != $this->user->id) {
83                 throw new ClientException("In reply to a notice not by this user");
84             }
85         } else if (!empty($context->attention)) {
86             if (!in_array($this->user->uri, $context->attention) &&
87                 !in_array(common_profile_url($this->user->nickname), $context->attention)) {
88                 common_log(LOG_ERR, "{$this->user->uri} not in attention list (".implode(',', $context->attention).")");
89                 throw new ClientException("To the attention of user(s) not including this one!");
90             }
91         } else {
92             throw new ClientException("Not to anyone in reply to anything!");
93         }
94
95         $existing = Notice::staticGet('uri', $this->activity->objects[0]->id);
96
97         if (!empty($existing)) {
98             common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
99             return;
100         }
101
102         $this->saveNotice();
103     }
104
105     /**
106      * We've gotten a follow/subscribe notification from a remote user.
107      * Save a subscription relationship for them.
108      */
109
110     function handleFollow()
111     {
112         $oprofile = $this->ensureProfile();
113         if ($oprofile) {
114             common_log(LOG_INFO, "Setting up subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
115             Subscription::start($oprofile->localProfile(),
116                                 $this->user->getProfile());
117         } else {
118             common_log(LOG_INFO, "Can't set up subscription from remote; missing profile.");
119         }
120     }
121
122     /**
123      * We've gotten an unfollow/unsubscribe notification from a remote user.
124      * Check if we have a subscription relationship for them and kill it.
125      *
126      * @fixme probably catch exceptions on fail?
127      */
128     function handleUnfollow()
129     {
130         $oprofile = $this->ensureProfile();
131         if ($oprofile) {
132             common_log(LOG_INFO, "Canceling subscription from remote {$oprofile->uri} to local {$this->user->nickname}");
133             Subscription::cancel($oprofile->localProfile(), $this->user->getProfile());
134         } else {
135             common_log(LOG_ERR, "Can't cancel subscription from remote, didn't find the profile");
136         }
137     }
138
139     /**
140      * Remote user likes one of our posts.
141      * Confirm the post is ours, and save a local favorite event.
142      */
143
144     function handleFavorite()
145     {
146         $notice = $this->getNotice($this->activity->objects[0]);
147         $profile = $this->ensureProfile()->localProfile();
148
149         $old = Fave::pkeyGet(array('user_id' => $profile->id,
150                                    'notice_id' => $notice->id));
151
152         if (!empty($old)) {
153             throw new ClientException("We already know that's a fave!");
154         }
155
156         if (!Fave::addNew($profile, $notice)) {
157             throw new ClientException("Could not save new favorite.");
158         }
159     }
160
161     /**
162      * Remote user doesn't like one of our posts after all!
163      * Confirm the post is ours, and save a local favorite event.
164      */
165     function handleUnfavorite()
166     {
167         $notice = $this->getNotice($this->activity->objects[0]);
168         $profile = $this->ensureProfile()->localProfile();
169
170         $fave = Fave::pkeyGet(array('user_id' => $profile->id,
171                                    'notice_id' => $notice->id));
172         if (empty($fave)) {
173             throw new ClientException("Notice wasn't favorited!");
174         }
175
176         $fave->delete();
177     }
178
179     /**
180      * @param ActivityObject $object
181      * @return Notice
182      * @throws ClientException on invalid input
183      */
184     function getNotice($object)
185     {
186         if (!$object) {
187             throw new ClientException("Can't favorite/unfavorite without an object.");
188         }
189
190         switch ($object->type) {
191         case ActivityObject::ARTICLE:
192         case ActivityObject::BLOGENTRY:
193         case ActivityObject::NOTE:
194         case ActivityObject::STATUS:
195         case ActivityObject::COMMENT:
196             break;
197         default:
198             throw new ClientException("Can't handle that kind of object for liking/faving.");
199         }
200
201         $notice = Notice::staticGet('uri', $object->id);
202
203         if (empty($notice)) {
204             throw new ClientException("Notice with ID $object->id unknown.");
205         }
206
207         if ($notice->profile_id != $this->user->id) {
208             throw new ClientException("Notice with ID $object->id not posted by $this->user->id.");
209         }
210
211         return $notice;
212     }
213
214 }