]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitymover.php
74c5c68ad688d8d824394f2d414e66ae2e0dc72c
[quix0rs-gnu-social.git] / lib / activitymover.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Title of module
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Class comment
39  *
40  * @category  General
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class ActivityMover extends QueueHandler
48 {
49     function transport()
50     {
51         return 'actmove';
52     }
53
54     function handle($data)
55     {
56         list ($act, $sink, $userURI, $remoteURI) = $data;
57
58         $user   = User::getKV('uri', $userURI);
59         try {
60             $remote = Profile::fromUri($remoteURI);
61         } catch (UnknownUriException $e) {
62             // Don't retry. It's hard to tell whether it's because of
63             // lookup failures or because the URI is permanently gone.
64             // If we knew it was temporary, we'd return false here.
65             return true;
66         }
67
68         try {
69             $this->moveActivity($act, $sink, $user, $remote);
70         } catch (ClientException $cex) {
71             $this->log(LOG_WARNING,
72                        $cex->getMessage());
73             // "don't retry me"
74             return true;
75         } catch (ServerException $sex) {
76             $this->log(LOG_WARNING,
77                        $sex->getMessage());
78             // "retry me" (because we think the server might handle it next time)
79             return false;
80         } catch (Exception $ex) {
81             $this->log(LOG_WARNING,
82                        $ex->getMessage());
83             // "don't retry me"
84             return true;
85         }
86     }
87
88     function moveActivity($act, $sink, $user, $remote)
89     {
90         if (empty($user)) {
91             // TRANS: Exception thrown if a non-existing user is provided. %s is a user ID.
92             throw new Exception(sprintf(_('No such user "%s".'),$act->actor->id));
93         }
94
95         switch ($act->verb) {
96 /*        case ActivityVerb::FAVORITE:
97             $this->log(LOG_INFO,
98                        "Moving favorite of {$act->objects[0]->id} by ".
99                        "{$act->actor->id} to {$remote->nickname}.");
100             // push it, then delete local
101             $sink->postActivity($act);
102             $notice = Notice::getKV('uri', $act->objects[0]->id);
103             if (!empty($notice)) {
104                 $fave = Fave::pkeyGet(array('user_id' => $user->id,
105                                             'notice_id' => $notice->id));
106                 $fave->delete();
107             }
108             break;*/
109         case ActivityVerb::POST:
110             $this->log(LOG_INFO,
111                        "Moving notice {$act->objects[0]->id} by ".
112                        "{$act->actor->id} to {$remote->nickname}.");
113             // XXX: send a reshare, not a post
114             $sink->postActivity($act);
115             $notice = Notice::getKV('uri', $act->objects[0]->id);
116             if (!empty($notice)) {
117                 $notice->deleteAs($user->getProfile(), false);
118             }
119             break;
120         case ActivityVerb::JOIN:
121             $this->log(LOG_INFO,
122                        "Moving group join of {$act->objects[0]->id} by ".
123                        "{$act->actor->id} to {$remote->nickname}.");
124             $sink->postActivity($act);
125             $group = User_group::getKV('uri', $act->objects[0]->id);
126             if (!empty($group)) {
127                 $user->leaveGroup($group);
128             }
129             break;
130         case ActivityVerb::FOLLOW:
131             if ($act->actor->id === $user->getUri()) {
132                 $this->log(LOG_INFO,
133                            "Moving subscription to {$act->objects[0]->id} by ".
134                            "{$act->actor->id} to {$remote->nickname}.");
135                 $sink->postActivity($act);
136                 try {
137                     $other = Profile::fromUri($act->objects[0]->id);
138                     Subscription::cancel($user->getProfile(), $other);
139                 } catch (UnknownUriException $e) {
140                     // Can't cancel subscription if we don't know who to alert
141                 }
142             } else {
143                 $otherUser = User::getKV('uri', $act->actor->id);
144                 if (!empty($otherUser)) {
145                     $this->log(LOG_INFO,
146                                "Changing sub to {$act->objects[0]->id}".
147                                "by {$act->actor->id} to {$remote->nickname}.");
148                     $otherProfile = $otherUser->getProfile();
149                     Subscription::ensureStart($otherProfile, $remote);
150                     Subscription::cancel($otherProfile, $user->getProfile());
151                 } else {
152                     $this->log(LOG_NOTICE,
153                                "Not changing sub to {$act->objects[0]->id}".
154                                "by remote {$act->actor->id} ".
155                                "to {$remote->nickname}.");
156                 }
157             }
158             break;
159         }
160     }
161
162     /**
163      * Log some data
164      *
165      * Add a header for our class so we know who did it.
166      *
167      * @param int    $level   Log level, like LOG_ERR or LOG_INFO
168      * @param string $message Message to log
169      *
170      * @return void
171      */
172     protected function log($level, $message)
173     {
174         common_log($level, "ActivityMover: " . $message);
175     }
176 }