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::staticGet($notice->profile_id);
58 foreach ($notice->getGroups() as $group) {
59 $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
61 $this->pingReply($oprofile);
63 $this->pushGroup($group->id);
67 if (!empty($this->notice->reply_to)) {
68 $replyTo = Notice::staticGet('id', $this->notice->reply_to);
69 if (!empty($replyTo)) {
70 foreach($replyTo->getReplies() as $profile_id) {
71 $oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
73 $this->pingReply($oprofile);
79 foreach ($notice->getProfileTags() as $ptag) {
80 $oprofile = Ostatus_profile::staticGet('peopletag_id', $ptag->id);
82 $this->pushPeopletag($ptag);
92 // For local posts, ping the PuSH hub to update their feed.
93 // http://identi.ca/api/statuses/user_timeline/1.atom
94 $feed = common_local_url('ApiTimelineUser',
95 array('id' => $this->user->id,
97 $this->pushFeed($feed, array($this, 'userFeedForNotice'));
101 function pushGroup($group_id)
103 // For a local group, ping the PuSH hub to update its feed.
104 // Updates may come from either a local or a remote user.
105 $feed = common_local_url('ApiTimelineGroup',
106 array('id' => $group_id,
107 'format' => 'atom'));
108 $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
111 function pushPeopletag($ptag)
113 // For a local people tag, ping the PuSH hub to update its feed.
114 // Updates may come from either a local or a remote user.
115 $feed = common_local_url('ApiTimelineList',
116 array('id' => $ptag->id,
117 'user' => $ptag->tagger,
118 'format' => 'atom'));
119 $this->pushFeed($feed, array($this, 'peopletagFeedForNotice'), $ptag);
122 function pingReply($oprofile)
125 // For local posts, send a Salmon ping to the mentioned
126 // remote user or group.
127 // @fixme as an optimization we can skip this if the
128 // remote profile is subscribed to the author.
129 $oprofile->notifyDeferred($this->notice, $this->user);
134 * @param string $feed URI to the feed
135 * @param callable $callback function to generate Atom feed update if needed
136 * any additional params are passed to the callback.
138 function pushFeed($feed, $callback)
140 $hub = common_config('ostatus', 'hub');
142 $this->pushFeedExternal($feed, $hub);
148 $args = array_slice(func_get_args(), 2);
149 $atom = call_user_func_array($callback, $args);
150 $this->pushFeedInternal($atom, $sub);
152 common_log(LOG_INFO, "No PuSH subscribers for $feed");
158 * Ping external hub about this update.
159 * The hub will pull the feed and check for new items later.
160 * Not guaranteed safe in an environment with database replication.
162 * @param string $feed feed topic URI
163 * @param string $hub PuSH hub URI
164 * @fixme can consolidate pings for user & group posts
166 function pushFeedExternal($feed, $hub)
168 $client = new HTTPClient();
170 $data = array('hub.mode' => 'publish',
172 $response = $client->post($hub, array(), $data);
173 if ($response->getStatus() == 204) {
174 common_log(LOG_INFO, "PuSH ping to hub $hub for $feed ok");
177 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed with HTTP " .
178 $response->getStatus() . ': ' .
179 $response->getBody());
181 } catch (Exception $e) {
182 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed: " . $e->getMessage());
188 * Queue up direct feed update pushes to subscribers on our internal hub.
189 * If there are a large number of subscriber sites, intermediate bulk
190 * distribution triggers may be queued.
192 * @param string $atom update feed, containing only new/changed items
193 * @param HubSub $sub open query of subscribers
195 function pushFeedInternal($atom, $sub)
197 common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
200 while ($sub->fetch()) {
202 if ($n < self::MAX_UNBATCHED) {
203 $sub->distribute($atom);
205 $batch[] = $sub->callback;
206 if (count($batch) >= self::BATCH_SIZE) {
207 $sub->bulkDistribute($atom, $batch);
212 if (count($batch) >= 0) {
213 $sub->bulkDistribute($atom, $batch);
218 * Build a single-item version of the sending user's Atom feed.
221 function userFeedForNotice()
223 $atom = new AtomUserNoticeFeed($this->user);
224 $atom->addEntryFromNotice($this->notice);
225 $feed = $atom->getString();
230 function groupFeedForNotice($group_id)
232 $group = User_group::staticGet('id', $group_id);
234 $atom = new AtomGroupNoticeFeed($group);
235 $atom->addEntryFromNotice($this->notice);
236 $feed = $atom->getString();
241 function peopletagFeedForNotice($ptag)
243 $atom = new AtomListNoticeFeed($ptag);
244 $atom->addEntryFromNotice($this->notice);
245 $feed = $atom->getString();