3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A class for moving an account to a new server
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Moves an account from this server to another
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/
50 private $_user = null;
51 private $_profile = null;
52 private $_remote = null;
53 private $_sink = null;
55 function __construct($user, $remote, $password)
58 $this->_profile = $user->getProfile();
60 $oprofile = Ostatus_profile::ensureProfileURI($remote);
62 if (empty($oprofile)) {
63 throw new Exception("Can't locate account {$remote}");
66 $this->_remote = $oprofile->localProfile();
68 list($svcDocUrl, $username) = self::getServiceDocument($remote);
70 $this->_sink = new ActivitySink($svcDocUrl, $username, $password);
73 static function getServiceDocument($remote)
75 $discovery = new Discovery();
77 $xrd = $discovery->lookup($remote);
80 throw new Exception("Can't find XRD for $remote");
86 foreach ($xrd->links as $link) {
87 if ($link['rel'] == 'http://apinamespace.org/atom' &&
88 $link['type'] == 'application/atomsvc+xml') {
89 $svcDocUrl = $link['href'];
90 if (!empty($link['property'])) {
91 foreach ($link['property'] as $property) {
92 if ($property['type'] == 'http://apinamespace.org/atom/username') {
93 $username = $property['value'];
102 if (empty($svcDocUrl)) {
103 throw new Exception("No AtomPub API service for $remote.");
106 return array($svcDocUrl, $username);
111 $stream = new UserActivityStream($this->_user);
113 $acts = array_reverse($stream->activities);
115 // Reverse activities to run in correct chron order
117 foreach ($acts as $act) {
118 $this->_moveActivity($act);
122 private function _moveActivity($act)
124 switch ($act->verb) {
125 case ActivityVerb::FAVORITE:
126 // push it, then delete local
127 $this->_sink->postActivity($act);
128 $notice = Notice::staticGet('uri', $act->objects[0]->id);
129 if (!empty($notice)) {
130 $fave = Fave::pkeyGet(array('user_id' => $this->_user->id,
131 'notice_id' => $notice->id));
135 case ActivityVerb::POST:
136 // XXX: send a reshare, not a post
137 common_log(LOG_INFO, "Pushing notice {$act->objects[0]->id} to {$this->_remote->getURI()}");
138 $this->_sink->postActivity($act);
139 $notice = Notice::staticGet('uri', $act->objects[0]->id);
140 if (!empty($notice)) {
144 case ActivityVerb::JOIN:
145 $this->_sink->postActivity($act);
146 $group = User_group::staticGet('uri', $act->objects[0]->id);
147 if (!empty($group)) {
148 Group_member::leave($group->id, $this->_user->id);
151 case ActivityVerb::FOLLOW:
152 if ($act->actor->id == $this->_user->uri) {
153 $this->_sink->postActivity($act);
154 $other = Profile::fromURI($act->objects[0]->id);
155 if (!empty($other)) {
156 Subscription::cancel($this->_profile, $other);
159 $otherUser = User::staticGet('uri', $act->actor->id);
160 if (!empty($otherUser)) {
161 $otherProfile = $otherUser->getProfile();
162 Subscription::start($otherProfile, $this->_remote);
163 Subscription::cancel($otherProfile, $this->_user->getProfile());
165 // It's a remote subscription. Do something here!