]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/ostatusqueuehandler.php
Merge branch 'master' into nightly
[quix0rs-gnu-social.git] / plugins / OStatus / lib / ostatusqueuehandler.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Prepare PuSH and Salmon distributions for an outgoing message.
26  *
27  * @package OStatusPlugin
28  * @author Brion Vibber <brion@status.net>
29  */
30 class OStatusQueueHandler extends QueueHandler
31 {
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;
37
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;
43
44     function transport()
45     {
46         return 'ostatus';
47     }
48
49     function handle($notice)
50     {
51         assert($notice instanceof Notice);
52
53         $this->notice = $notice;
54         $this->user = User::getKV('id', $notice->profile_id);
55
56         try {
57             $profile = $this->notice->getProfile();
58         } catch (Exception $e) {
59             common_log(LOG_ERR, "Can't get profile for notice; skipping: " . $e->getMessage());
60             return true;
61         }
62
63         foreach ($notice->getAttentionProfiles() as $target) {
64             common_debug("OSTATUS [{$this->notice->getID()}]: Attention target profile {$target->getNickname()} ({$target->getID()})");
65             if ($target->isGroup()) {
66                 common_debug("OSTATUS [{$this->notice->getID()}]: {$target->getID()} is a group");
67                 $oprofile = Ostatus_profile::getKV('group_id', $target->getGroup()->getID());
68                 if (!$oprofile instanceof Ostatus_profile) {
69                     // we don't save profiles like this yet, but in the future
70                     $oprofile = Ostatus_profile::getKV('profile_id', $target->getID());
71                 }
72                 if ($oprofile instanceof Ostatus_profile) {
73                     // remote group
74                     if ($notice->isLocal()) {
75                         common_debug("OSTATUS [{$this->notice->getID()}]: notice is local and remote group with profile ID {$target->getID()} gets a ping");
76                         $this->pingReply($oprofile);
77                     }
78                 } else {
79                     common_debug("OSTATUS [{$this->notice->getID()}]: local group with profile id {$target->getID()} gets pushed out");
80                     // local group
81                     $this->pushGroup($target->getGroup());
82                 }
83             } elseif ($notice->isLocal()) {
84                 // Notices generated on other sites will have already
85                 // pinged their reply-targets, so only do these things
86                 // if the target is not a group and the notice is locally generated
87
88                 $oprofile = Ostatus_profile::getKV('profile_id', $target->getID());
89                 if ($oprofile instanceof Ostatus_profile) {
90                     common_debug("OSTATUS [{$this->notice->getID()}]: Notice is local and {$target->getID()} is remote profile, getting pingReply");
91                     $this->pingReply($oprofile);
92                 }
93             }
94         }
95
96         if ($notice->isLocal()) {
97             // Notices generated on remote sites will have already
98             // been pushed to user's subscribers by their origin sites.
99             $this->pushUser();
100
101             try {
102                 $parent = $this->notice->getParent();
103                 foreach($parent->getAttentionProfiles() as $related) {
104                     if ($related->isGroup()) {
105                         // don't ping groups in parent notices since we might not be a member of them,
106                         // though it could be useful if we study this and use it correctly
107                         continue;
108                     }
109                     common_debug("OSTATUS [{$this->notice->getID()}]: parent notice {$parent->getID()} has related profile id=={$related->getID()}");
110                     // FIXME: don't ping twice in case someone is in both notice attention spans!
111                     $oprofile = Ostatus_profile::getKV('profile_id', $related->getID());
112                     if ($oprofile instanceof Ostatus_profile) {
113                         $this->pingReply($oprofile);
114                     }
115                 }
116             } catch (NoParentNoticeException $e) {
117                 // nothing to do then
118             }
119
120             foreach ($notice->getProfileTags() as $ptag) {
121                 $oprofile = Ostatus_profile::getKV('peopletag_id', $ptag->id);
122                 if (!$oprofile) {
123                     $this->pushPeopletag($ptag);
124                 }
125             }
126         }
127
128         return true;
129     }
130
131     function pushUser()
132     {
133         if ($this->user) {
134             common_debug("OSTATUS [{$this->notice->getID()}]: pushing feed for local user {$this->user->getID()}");
135             // For local posts, ping the PuSH hub to update their feed.
136             // http://identi.ca/api/statuses/user_timeline/1.atom
137             $feed = common_local_url('ApiTimelineUser',
138                                      array('id' => $this->user->id,
139                                            'format' => 'atom'));
140             $this->pushFeed($feed, array($this, 'userFeedForNotice'));
141         }
142     }
143
144     function pushGroup(User_group $group)
145     {
146         common_debug("OSTATUS [{$this->notice->getID()}]: pushing group '{$group->getNickname()}' profile_id={$group->profile_id}");
147         // For a local group, ping the PuSH hub to update its feed.
148         // Updates may come from either a local or a remote user.
149         $feed = common_local_url('ApiTimelineGroup',
150                                  array('id' => $group->getID(),
151                                        'format' => 'atom'));
152         $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group->getID());
153     }
154
155     function pushPeopletag($ptag)
156     {
157         common_debug("OSTATUS [{$this->notice->getID()}]: pushing peopletag '{$ptag->id}'");
158         // For a local people tag, ping the PuSH hub to update its feed.
159         // Updates may come from either a local or a remote user.
160         $feed = common_local_url('ApiTimelineList',
161                                  array('id' => $ptag->id,
162                                        'user' => $ptag->tagger,
163                                        'format' => 'atom'));
164         $this->pushFeed($feed, array($this, 'peopletagFeedForNotice'), $ptag);
165     }
166
167     function pingReply(Ostatus_profile $oprofile)
168     {
169         if ($this->user) {
170             common_debug("OSTATUS [{$this->notice->getID()}]: pinging reply to {$oprofile->localProfile()->getNickname()} for local user '{$this->user->getID()}'");
171             // For local posts, send a Salmon ping to the mentioned
172             // remote user or group.
173             // @fixme as an optimization we can skip this if the
174             // remote profile is subscribed to the author.
175             $oprofile->notifyDeferred($this->notice, $this->user);
176         }
177     }
178
179     /**
180      * @param string $feed URI to the feed
181      * @param callable $callback function to generate Atom feed update if needed
182      *        any additional params are passed to the callback.
183      */
184     function pushFeed($feed, $callback)
185     {
186         // NOTE: external hub pings will not be fixed by
187         // our legacy_http thing!
188         $hub = common_config('ostatus', 'hub');
189         if ($hub) {
190             $this->pushFeedExternal($feed, $hub);
191         }
192
193         // If we used to be http but now are https, see if we find an http entry for this feed URL
194         // and then upgrade it. This self-healing feature needs to be enabled manually in config.
195         // This code is based on a patch by @hannes2peer@quitter.se
196         if (common_config('fix', 'legacy_http') && parse_url($feed, PHP_URL_SCHEME) === 'https') {
197             common_log(LOG_DEBUG, "OSTATUS [{$this->notice->getID()}]: Searching for http scheme instead for HubSub feed topic: "._ve($feed));
198             $http_feed = str_replace('https://', 'http://', $feed);
199             $sub = new HubSub();
200             $sub->topic = $http_feed;
201             // If we find it we upgrade the rows in the hubsub table.
202             if ($sub->find()) {
203                 common_log(LOG_INFO, "OSTATUS [{$this->notice->getID()}]: Found topic with http scheme for "._ve($feed).", will update the rows to use https instead!");
204                 // we found an http:// URL but we use https:// now
205                 // so let's update the rows to reflect on this!
206                 while ($sub->fetch()) {
207                     common_debug("OSTATUS [{$this->notice->getID()}]: Changing topic URL to https for feed callback "._ve($sub->callback));
208                     $orig = clone($sub);
209                     $sub->topic = $feed;
210                     // hashkey column will be set automagically in HubSub->onUpdateKeys through updateWithKeys
211                     $sub->updateWithKeys($orig);
212                     unset($orig);
213                 }
214             }
215         }
216
217         $sub = new HubSub();
218         $sub->topic = $feed;
219         if ($sub->find()) {
220             $args = array_slice(func_get_args(), 2);
221             $atom = call_user_func_array($callback, $args);
222             $this->pushFeedInternal($atom, $sub);
223         } else {
224             common_log(LOG_INFO, "OSTATUS [{$this->notice->getID()}]: No PuSH subscribers for $feed");
225         }
226     }
227
228     /**
229      * Ping external hub about this update.
230      * The hub will pull the feed and check for new items later.
231      * Not guaranteed safe in an environment with database replication.
232      *
233      * @param string $feed feed topic URI
234      * @param string $hub PuSH hub URI
235      * @fixme can consolidate pings for user & group posts
236      */
237     function pushFeedExternal($feed, $hub)
238     {
239         $client = new HTTPClient();
240         try {
241             $data = array('hub.mode' => 'publish',
242                           'hub.url' => $feed);
243             $response = $client->post($hub, array(), $data);
244             if ($response->getStatus() == 204) {
245                 common_log(LOG_INFO, "PuSH ping to hub $hub for $feed ok");
246                 return true;
247             } else {
248                 common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed with HTTP " .
249                                     $response->getStatus() . ': ' .
250                                     $response->getBody());
251             }
252         } catch (Exception $e) {
253             common_log(LOG_ERR, "PuSH ping to hub $hub for $feed failed: " . $e->getMessage());
254             return false;
255         }
256     }
257
258     /**
259      * Queue up direct feed update pushes to subscribers on our internal hub.
260      * If there are a large number of subscriber sites, intermediate bulk
261      * distribution triggers may be queued.
262      *
263      * @param string $atom update feed, containing only new/changed items
264      * @param HubSub $sub open query of subscribers
265      */
266     function pushFeedInternal($atom, $sub)
267     {
268         common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
269         $n = 0;
270         $batch = array();
271         while ($sub->fetch()) {
272             $n++;
273             if ($n < self::MAX_UNBATCHED) {
274                 $sub->distribute($atom);
275             } else {
276                 $batch[] = $sub->callback;
277                 if (count($batch) >= self::BATCH_SIZE) {
278                     $sub->bulkDistribute($atom, $batch);
279                     $batch = array();
280                 }
281             }
282         }
283         if (count($batch) > 0) {
284             $sub->bulkDistribute($atom, $batch);
285         }
286     }
287
288     /**
289      * Build a single-item version of the sending user's Atom feed.
290      * @return string
291      */
292     function userFeedForNotice()
293     {
294         $atom = new AtomUserNoticeFeed($this->user);
295         $atom->addEntryFromNotice($this->notice);
296         $feed = $atom->getString();
297
298         return $feed;
299     }
300
301     function groupFeedForNotice($group_id)
302     {
303         $group = User_group::getKV('id', $group_id);
304
305         $atom = new AtomGroupNoticeFeed($group);
306         $atom->addEntryFromNotice($this->notice);
307         $feed = $atom->getString();
308
309         return $feed;
310     }
311
312     function peopletagFeedForNotice($ptag)
313     {
314         $atom = new AtomListNoticeFeed($ptag);
315         $atom->addEntryFromNotice($this->notice);
316         $feed = $atom->getString();
317
318         return $feed;
319     }
320 }