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/>.
20 if (!defined('STATUSNET')) {
25 * Prepare PuSH and Salmon distributions for an outgoing message.
27 * @package OStatusPlugin
28 * @author Brion Vibber <brion@status.net>
30 class OStatusQueueHandler extends QueueHandler
32 // If we have more than this many subscribing sites on a single feed,
33 // break up the PuSH distribution into smaller batches which will be
34 // rolled into the queue progressively. This reduces disruption to
35 // other, shorter activities being enqueued while we work.
36 const MAX_UNBATCHED = 50;
38 // Each batch (a 'hubprep' entry) will have this many items.
39 // Selected to provide a balance between queue packet size
40 // and number of batches that will end up getting processed.
41 // For 20,000 target sites, 1000 should work acceptably.
42 const BATCH_SIZE = 1000;
49 function handle($notice)
51 assert($notice instanceof Notice);
53 $this->notice = $notice;
54 $this->user = User::getKV('id', $notice->profile_id);
57 $profile = $this->notice->getProfile();
58 } catch (Exception $e) {
59 common_log(LOG_ERR, "Can't get profile for notice; skipping: " . $e->getMessage());
63 if ($notice->isLocal()) {
64 // Notices generated on remote sites will have already
65 // been pushed to user's subscribers by their origin sites.
69 foreach ($notice->getGroups() as $group) {
70 $oprofile = Ostatus_profile::getKV('group_id', $group->id);
73 if ($notice->isLocal()) {
74 $this->pingReply($oprofile);
78 $this->pushGroup($group->id);
82 if ($notice->isLocal()) {
83 // Notices generated on other sites will have already
84 // pinged their reply-targets.
86 foreach ($notice->getReplies() as $profile_id) {
87 $oprofile = Ostatus_profile::getKV('profile_id', $profile_id);
89 $this->pingReply($oprofile);
93 if (!empty($this->notice->reply_to)) {
94 $replyTo = Notice::getKV('id', $this->notice->reply_to);
95 if (!empty($replyTo)) {
96 foreach($replyTo->getReplies() as $profile_id) {
97 $oprofile = Ostatus_profile::getKV('profile_id', $profile_id);
99 $this->pingReply($oprofile);
105 foreach ($notice->getProfileTags() as $ptag) {
106 $oprofile = Ostatus_profile::getKV('peopletag_id', $ptag->id);
108 $this->pushPeopletag($ptag);
119 // For local posts, ping the PuSH hub to update their feed.
120 // http://identi.ca/api/statuses/user_timeline/1.atom
121 $feed = common_local_url('ApiTimelineUser',
122 array('id' => $this->user->id,
123 'format' => 'atom'));
124 $this->pushFeed($feed, array($this, 'userFeedForNotice'));
128 function pushGroup($group_id)
130 // For a local group, ping the PuSH hub to update its feed.
131 // Updates may come from either a local or a remote user.
132 $feed = common_local_url('ApiTimelineGroup',
133 array('id' => $group_id,
134 'format' => 'atom'));
135 $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
138 function pushPeopletag($ptag)
140 // For a local people tag, ping the PuSH hub to update its feed.
141 // Updates may come from either a local or a remote user.
142 $feed = common_local_url('ApiTimelineList',
143 array('id' => $ptag->id,
144 'user' => $ptag->tagger,
145 'format' => 'atom'));
146 $this->pushFeed($feed, array($this, 'peopletagFeedForNotice'), $ptag);
149 function pingReply($oprofile)
152 // For local posts, send a Salmon ping to the mentioned
153 // remote user or group.
154 // @fixme as an optimization we can skip this if the
155 // remote profile is subscribed to the author.
156 $oprofile->notifyDeferred($this->notice, $this->user);
161 * @param string $feed URI to the feed
162 * @param callable $callback function to generate Atom feed update if needed
163 * any additional params are passed to the callback.
165 function pushFeed($feed, $callback)
167 $hub = common_config('ostatus', 'hub');
169 $this->pushFeedExternal($feed, $hub);
175 $args = array_slice(func_get_args(), 2);
176 $atom = call_user_func_array($callback, $args);
177 $this->pushFeedInternal($atom, $sub);
179 common_log(LOG_INFO, "No PuSH subscribers for $feed");
185 * Ping external hub about this update.
186 * The hub will pull the feed and check for new items later.
187 * Not guaranteed safe in an environment with database replication.
189 * @param string $feed feed topic URI
190 * @param string $hub PuSH hub URI
191 * @fixme can consolidate pings for user & group posts
193 function pushFeedExternal($feed, $hub)
195 $client = new HTTPClient();
197 $data = array('hub.mode' => 'publish',
199 $response = $client->post($hub, array(), $data);
200 if ($response->getStatus() == 204) {
201 common_log(LOG_INFO, "PuSH ping to hub $hub for $feed ok");
204 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed with HTTP " .
205 $response->getStatus() . ': ' .
206 $response->getBody());
208 } catch (Exception $e) {
209 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed: " . $e->getMessage());
215 * Queue up direct feed update pushes to subscribers on our internal hub.
216 * If there are a large number of subscriber sites, intermediate bulk
217 * distribution triggers may be queued.
219 * @param string $atom update feed, containing only new/changed items
220 * @param HubSub $sub open query of subscribers
222 function pushFeedInternal($atom, $sub)
224 common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
227 while ($sub->fetch()) {
229 if ($n < self::MAX_UNBATCHED) {
230 $sub->distribute($atom);
232 $batch[] = $sub->callback;
233 if (count($batch) >= self::BATCH_SIZE) {
234 $sub->bulkDistribute($atom, $batch);
239 if (count($batch) >= 0) {
240 $sub->bulkDistribute($atom, $batch);
245 * Build a single-item version of the sending user's Atom feed.
248 function userFeedForNotice()
250 $atom = new AtomUserNoticeFeed($this->user);
251 $atom->addEntryFromNotice($this->notice);
252 $feed = $atom->getString();
257 function groupFeedForNotice($group_id)
259 $group = User_group::getKV('id', $group_id);
261 $atom = new AtomGroupNoticeFeed($group);
262 $atom->addEntryFromNotice($this->notice);
263 $feed = $atom->getString();
268 function peopletagFeedForNotice($ptag)
270 $atom = new AtomListNoticeFeed($ptag);
271 $atom->addEntryFromNotice($this->notice);
272 $feed = $atom->getString();