]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/hubdistribqueuehandler.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / plugins / OStatus / lib / hubdistribqueuehandler.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 /**
21  * Send a PuSH subscription verification from our internal hub.
22  * Queue up final distribution for 
23  * @package Hub
24  * @author Brion Vibber <brion@status.net>
25  */
26 class HubDistribQueueHandler extends QueueHandler
27 {
28     function transport()
29     {
30         return 'hubdistrib';
31     }
32
33     function handle($notice)
34     {
35         assert($notice instanceof Notice);
36
37         // See if there's any PuSH subscriptions, including OStatus clients.
38         // @fixme handle group subscriptions as well
39         // http://identi.ca/api/statuses/user_timeline/1.atom
40         $feed = common_local_url('ApiTimelineUser',
41                                  array('id' => $notice->profile_id,
42                                        'format' => 'atom'));
43         $sub = new HubSub();
44         $sub->topic = $feed;
45         if ($sub->find()) {
46             common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $feed");
47             $qm = QueueManager::get();
48             $atom = $this->userFeedForNotice($notice);
49             while ($sub->fetch()) {
50                 common_log(LOG_INFO, "Prepping PuSH distribution to $sub->callback for $feed");
51                 $data = array('sub' => clone($sub),
52                               'atom' => $atom);
53                 $qm->enqueue($data, 'hubout');
54             }
55         } else {
56             common_log(LOG_INFO, "No PuSH subscribers for $feed");
57         }
58     }
59
60     /**
61      * Build a single-item version of the sending user's Atom feed.
62      * @param Notice $notice
63      * @return string
64      */
65     function userFeedForNotice($notice)
66     {
67         // @fixme this feels VERY hacky...
68         // should probably be a cleaner way to do it
69
70         ob_start();
71         $api = new ApiTimelineUserAction();
72         $api->prepare(array('id' => $notice->profile_id,
73                             'format' => 'atom',
74                             'max_id' => $notice->id,
75                             'since_id' => $notice->id - 1));
76         $api->showTimeline();
77         $feed = ob_get_clean();
78         
79         // ...and override the content-type back to something normal... eww!
80         // hope there's no other headers that got set while we weren't looking.
81         header('Content-Type: text/html; charset=utf-8');
82
83         common_log(LOG_DEBUG, $feed);
84         return $feed;
85     }
86 }
87