]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/hubdistribqueuehandler.php
189ccbedf9fc26e25efd075ee048ef90e077fa1f
[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         $this->pushUser($notice);
38         foreach ($notice->getGroups() as $group) {
39             $this->pushGroup($notice, $group->group_id);
40         }
41     }
42     
43     function pushUser($notice)
44     {
45         // See if there's any PuSH subscriptions, including OStatus clients.
46         // @fixme handle group subscriptions as well
47         // http://identi.ca/api/statuses/user_timeline/1.atom
48         $feed = common_local_url('ApiTimelineUser',
49                                  array('id' => $notice->profile_id,
50                                        'format' => 'atom'));
51         $sub = new HubSub();
52         $sub->topic = $feed;
53         if ($sub->find()) {
54             $atom = $this->userFeedForNotice($notice);
55             $this->pushFeeds($atom, $sub);
56         } else {
57             common_log(LOG_INFO, "No PuSH subscribers for $feed");
58         }
59     }
60
61     function pushGroup($notice, $group_id)
62     {
63         $feed = common_local_url('ApiTimelineGroup',
64                                  array('id' => $group_id,
65                                        'format' => 'atom'));
66         $sub = new HubSub();
67         $sub->topic = $feed;
68         if ($sub->find()) {
69             common_log(LOG_INFO, "Building PuSH feed for $feed");
70             $atom = $this->groupFeedForNotice($group_id, $notice);
71             $this->pushFeeds($atom, $sub);
72         } else {
73             common_log(LOG_INFO, "No PuSH subscribers for $feed");
74         }
75     }
76
77     
78     function pushFeeds($atom, $sub)
79     {
80         common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
81         $qm = QueueManager::get();
82         while ($sub->fetch()) {
83             common_log(LOG_INFO, "Prepping PuSH distribution to $sub->callback for $sub->topic");
84             $data = array('sub' => clone($sub),
85                           'atom' => $atom);
86             $qm->enqueue($data, 'hubout');
87         }
88     }
89
90     /**
91      * Build a single-item version of the sending user's Atom feed.
92      * @param Notice $notice
93      * @return string
94      */
95     function userFeedForNotice($notice)
96     {
97         // @fixme this feels VERY hacky...
98         // should probably be a cleaner way to do it
99
100         ob_start();
101         $api = new ApiTimelineUserAction();
102         $api->prepare(array('id' => $notice->profile_id,
103                             'format' => 'atom',
104                             'max_id' => $notice->id,
105                             'since_id' => $notice->id - 1));
106         $api->showTimeline();
107         $feed = ob_get_clean();
108         
109         // ...and override the content-type back to something normal... eww!
110         // hope there's no other headers that got set while we weren't looking.
111         header('Content-Type: text/html; charset=utf-8');
112
113         common_log(LOG_DEBUG, $feed);
114         return $feed;
115     }
116
117     function groupFeedForNotice($group_id, $notice)
118     {
119         // @fixme this feels VERY hacky...
120         // should probably be a cleaner way to do it
121
122         ob_start();
123         $api = new ApiTimelineGroupAction();
124         $args = array('id' => $group_id,
125                       'format' => 'atom',
126                       'max_id' => $notice->id,
127                       'since_id' => $notice->id - 1);
128         $api->prepare($args);
129         $api->handle($args);
130         $feed = ob_get_clean();
131         
132         // ...and override the content-type back to something normal... eww!
133         // hope there's no other headers that got set while we weren't looking.
134         header('Content-Type: text/html; charset=utf-8');
135
136         common_log(LOG_DEBUG, $feed);
137         return $feed;
138     }
139
140 }
141