]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/actions/usersalmon.php
cd24dff3aca87f691e33d97ecc65f40784c593d9
[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         try {
75             $this->saveNotice();
76         } catch (AlreadyFulfilledException $e) {
77             return;
78         }
79     }
80
81     /**
82      * We've gotten a follow/subscribe notification from a remote user.
83      * Save a subscription relationship for them.
84      */
85     function handleFollow()
86     {
87         common_log(LOG_INFO, sprintf('Setting up subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
88         Subscription::start($this->actor, $this->target);
89     }
90
91     /**
92      * We've gotten an unfollow/unsubscribe notification from a remote user.
93      * Check if we have a subscription relationship for them and kill it.
94      *
95      * @fixme probably catch exceptions on fail?
96      */
97     function handleUnfollow()
98     {
99         common_log(LOG_INFO, sprintf('Canceling subscription from remote %s to local %s', $this->oprofile->getUri(), $this->target->getNickname()));
100         try {
101             Subscription::cancel($this->actor, $this->target);
102         } catch (NoProfileException $e) {
103             common_debug('Could not find profile for Subscription: '.$e->getMessage());
104         }
105     }
106
107     function handleTag()
108     {
109         if ($this->activity->target->type == ActivityObject::_LIST) {
110             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
111                 // TRANS: Client exception.
112                 throw new ClientException(_m('Not a person object.'));
113             }
114             // this is a peopletag
115             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
116
117             if (!$tagged instanceof User) {
118                 // TRANS: Client exception.
119                 throw new ClientException(_m('Unidentified profile being listed.'));
120             }
121
122             if ($tagged->id !== $this->target->id) {
123                 // TRANS: Client exception.
124                 throw new ClientException(_m('This user is not the one being listed.'));
125             }
126
127             // save the list
128             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
129
130             $ptag = $list->localPeopletag();
131             $result = Profile_tag::setTag($ptag->tagger, $tagged->id, $ptag->tag);
132             if (!$result) {
133                 // TRANS: Client exception.
134                 throw new ClientException(_m('The listing could not be saved.'));
135             }
136         }
137     }
138
139     function handleUntag()
140     {
141         if ($this->activity->target->type == ActivityObject::_LIST) {
142             if ($this->activity->objects[0]->type != ActivityObject::PERSON) {
143                 // TRANS: Client exception.
144                 throw new ClientException(_m('Not a person object.'));
145             }
146             // this is a peopletag
147             $tagged = User::getKV('uri', $this->activity->objects[0]->id);
148
149             if (!$tagged instanceof User) {
150                 // TRANS: Client exception.
151                 throw new ClientException(_m('Unidentified profile being unlisted.'));
152             }
153
154             if ($tagged->id !== $this->target->id) {
155                 // TRANS: Client exception.
156                 throw new ClientException(_m('This user is not the one being unlisted.'));
157             }
158
159             // save the list
160             $list   = Ostatus_profile::ensureActivityObjectProfile($this->activity->target);
161
162             $ptag = $list->localPeopletag();
163             $result = Profile_tag::unTag($ptag->tagger, $tagged->id, $ptag->tag);
164
165             if (!$result) {
166                 // TRANS: Client exception.
167                 throw new ClientException(_m('The listing could not be deleted.'));
168             }
169         }
170     }
171
172     /**
173      * @param ActivityObject $object
174      * @return Notice
175      * @throws ClientException on invalid input
176      */
177     function getNotice(ActivityObject $object)
178     {
179         switch ($object->type) {
180         case ActivityObject::ARTICLE:
181         case ActivityObject::BLOGENTRY:
182         case ActivityObject::NOTE:
183         case ActivityObject::STATUS:
184         case ActivityObject::COMMENT:
185             break;
186         default:
187             // TRANS: Client exception.
188             throw new ClientException(_m('Cannot handle that kind of object for liking/faving.'));
189         }
190
191         $notice = Notice::getKV('uri', $object->id);
192
193         if (!$notice instanceof Notice) {
194             // TRANS: Client exception. %s is an object ID.
195             throw new ClientException(sprintf(_m('Notice with ID %s unknown.'),$object->id));
196         }
197
198         if ($notice->profile_id != $this->target->id) {
199             // TRANS: Client exception. %1$s is a notice ID, %2$s is a user ID.
200             throw new ClientException(sprintf(_m('Notice with ID %1$s not posted by %2$s.'), $object->id, $this->target->id));
201         }
202
203         return $notice;
204     }
205 }