3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
21 * Prepare PuSH and Salmon distributions for an outgoing message.
23 * @package OStatusPlugin
24 * @author Brion Vibber <brion@status.net>
26 class OStatusQueueHandler extends QueueHandler
33 function handle($notice)
35 assert($notice instanceof Notice);
37 $this->notice = $notice;
38 $this->user = User::staticGet($notice->profile_id);
42 foreach ($notice->getGroups() as $group) {
43 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
45 $this->pingReply($oprofile);
47 $this->pushGroup($group->id);
51 foreach ($notice->getReplies() as $profile_id) {
52 $oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
54 $this->pingReply($oprofile);
64 // For local posts, ping the PuSH hub to update their feed.
65 // http://identi.ca/api/statuses/user_timeline/1.atom
66 $feed = common_local_url('ApiTimelineUser',
67 array('id' => $this->user->id,
69 $this->pushFeed($feed, array($this, 'userFeedForNotice'));
73 function pushGroup($group_id)
75 // For a local group, ping the PuSH hub to update its feed.
76 // Updates may come from either a local or a remote user.
77 $feed = common_local_url('ApiTimelineGroup',
78 array('id' => $group_id,
80 $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
83 function pingReply($oprofile)
86 // For local posts, send a Salmon ping to the mentioned
87 // remote user or group.
88 // @fixme as an optimization we can skip this if the
89 // remote profile is subscribed to the author.
90 $oprofile->notifyDeferred($this->notice);
95 * @param string $feed URI to the feed
96 * @param callable $callback function to generate Atom feed update if needed
97 * any additional params are passed to the callback.
99 function pushFeed($feed, $callback)
101 $hub = common_config('ostatus', 'hub');
103 $this->pushFeedExternal($feed, $hub);
109 $args = array_slice(func_get_args(), 2);
110 $atom = call_user_func_array($callback, $args);
111 $this->pushFeedInternal($atom, $sub);
113 common_log(LOG_INFO, "No PuSH subscribers for $feed");
119 * Ping external hub about this update.
120 * The hub will pull the feed and check for new items later.
121 * Not guaranteed safe in an environment with database replication.
123 * @param string $feed feed topic URI
124 * @param string $hub PuSH hub URI
125 * @fixme can consolidate pings for user & group posts
127 function pushFeedExternal($feed, $hub)
129 $client = new HTTPClient();
131 $data = array('hub.mode' => 'publish',
133 $response = $client->post($hub, array(), $data);
134 if ($response->getStatus() == 204) {
135 common_log(LOG_INFO, "PuSH ping to hub $hub for $feed ok");
138 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed with HTTP " .
139 $response->getStatus() . ': ' .
140 $response->getBody());
142 } catch (Exception $e) {
143 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed: " . $e->getMessage());
149 * Queue up direct feed update pushes to subscribers on our internal hub.
150 * @param string $atom update feed, containing only new/changed items
151 * @param HubSub $sub open query of subscribers
153 function pushFeedInternal($atom, $sub)
155 common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
156 while ($sub->fetch()) {
157 $sub->distribute($atom);
162 * Build a single-item version of the sending user's Atom feed.
165 function userFeedForNotice()
167 // @fixme this feels VERY hacky...
168 // should probably be a cleaner way to do it
171 $api = new ApiTimelineUserAction();
172 $api->prepare(array('id' => $this->notice->profile_id,
174 'max_id' => $this->notice->id,
175 'since_id' => $this->notice->id - 1));
176 $api->showTimeline();
177 $feed = ob_get_clean();
179 // ...and override the content-type back to something normal... eww!
180 // hope there's no other headers that got set while we weren't looking.
181 header('Content-Type: text/html; charset=utf-8');
183 common_log(LOG_DEBUG, $feed);
187 function groupFeedForNotice($group_id)
189 // @fixme this feels VERY hacky...
190 // should probably be a cleaner way to do it
193 $api = new ApiTimelineGroupAction();
194 $args = array('id' => $group_id,
196 'max_id' => $this->notice->id,
197 'since_id' => $this->notice->id - 1);
198 $api->prepare($args);
200 $feed = ob_get_clean();
202 // ...and override the content-type back to something normal... eww!
203 // hope there's no other headers that got set while we weren't looking.
204 header('Content-Type: text/html; charset=utf-8');
206 common_log(LOG_DEBUG, $feed);