]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activitymover.php
Merge branch '0.9.x' into 1.0.x
[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::staticGet('uri', $userURI);
59         $remote = Profile::fromURI($remoteURI);
60
61         try {
62             $this->moveActivity($act, $sink, $user, $remote);
63         } catch (ClientException $cex) {
64             $this->log(LOG_WARNING,
65                        $cex->getMessage());
66             // "don't retry me"
67             return true;
68         } catch (ServerException $sex) {
69             $this->log(LOG_WARNING,
70                        $sex->getMessage());
71             // "retry me" (because we think the server might handle it next time)
72             return false;
73         } catch (Exception $ex) {
74             $this->log(LOG_WARNING,
75                        $ex->getMessage());
76             // "don't retry me"
77             return true;
78         }
79     }
80
81     function moveActivity($act, $sink, $user, $remote)
82     {
83         if (empty($user)) {
84             throw new Exception(sprintf(_("No such user %s."),$act->actor->id));
85         }
86
87         switch ($act->verb) {
88         case ActivityVerb::FAVORITE:
89             $this->log(LOG_INFO,
90                        "Moving favorite of {$act->objects[0]->id} by ".
91                        "{$act->actor->id} to {$remote->nickname}.");
92             // push it, then delete local
93             $sink->postActivity($act);
94             $notice = Notice::staticGet('uri', $act->objects[0]->id);
95             if (!empty($notice)) {
96                 $fave = Fave::pkeyGet(array('user_id' => $user->id,
97                                             'notice_id' => $notice->id));
98                 $fave->delete();
99             }
100             break;
101         case ActivityVerb::POST:
102             $this->log(LOG_INFO,
103                        "Moving notice {$act->objects[0]->id} by ".
104                        "{$act->actor->id} to {$remote->nickname}.");
105             // XXX: send a reshare, not a post
106             $sink->postActivity($act);
107             $notice = Notice::staticGet('uri', $act->objects[0]->id);
108             if (!empty($notice)) {
109                 $notice->delete();
110             }
111             break;
112         case ActivityVerb::JOIN:
113             $this->log(LOG_INFO,
114                        "Moving group join of {$act->objects[0]->id} by ".
115                        "{$act->actor->id} to {$remote->nickname}.");
116             $sink->postActivity($act);
117             $group = User_group::staticGet('uri', $act->objects[0]->id);
118             if (!empty($group)) {
119                 Group_member::leave($group->id, $user->id);
120             }
121             break;
122         case ActivityVerb::FOLLOW:
123             if ($act->actor->id == $user->uri) {
124                 $this->log(LOG_INFO,
125                            "Moving subscription to {$act->objects[0]->id} by ".
126                            "{$act->actor->id} to {$remote->nickname}.");
127                 $sink->postActivity($act);
128                 $other = Profile::fromURI($act->objects[0]->id);
129                 if (!empty($other)) {
130                     Subscription::cancel($user->getProfile(), $other);
131                 }
132             } else {
133                 $otherUser = User::staticGet('uri', $act->actor->id);
134                 if (!empty($otherUser)) {
135                     $this->log(LOG_INFO,
136                                "Changing sub to {$act->objects[0]->id}".
137                                "by {$act->actor->id} to {$remote->nickname}.");
138                     $otherProfile = $otherUser->getProfile();
139                     Subscription::start($otherProfile, $remote);
140                     Subscription::cancel($otherProfile, $user->getProfile());
141                 } else {
142                     $this->log(LOG_NOTICE,
143                                "Not changing sub to {$act->objects[0]->id}".
144                                "by remote {$act->actor->id} ".
145                                "to {$remote->nickname}.");
146                 }
147             }
148             break;
149         }
150     }
151
152     /**
153      * Log some data
154      *
155      * Add a header for our class so we know who did it.
156      *
157      * @param int    $level   Log level, like LOG_ERR or LOG_INFO
158      * @param string $message Message to log
159      *
160      * @return void
161      */
162     protected function log($level, $message)
163     {
164         common_log($level, "ActivityMover: " . $message);
165     }
166 }