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