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