]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PubSubHubBub/PubSubHubBubPlugin.php
Merge branch '0.9.x' into schema
[quix0rs-gnu-social.git] / plugins / PubSubHubBub / PubSubHubBubPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to push RSS/Atom updates to a PubSubHubBub hub
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Craig Andrews <candrews@integralblue.com>
25  * @copyright 2009 Craig Andrews http://candrews.integralblue.com
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 define('DEFAULT_HUB','http://pubsubhubbub.appspot.com');
35
36 require_once(INSTALLDIR.'/plugins/PubSubHubBub/publisher.php');
37
38 class PubSubHubBubPlugin extends Plugin
39 {
40     private $hub;
41
42     function __construct()
43     {
44         parent::__construct();
45     }
46
47     function onInitializePlugin(){
48         $this->hub = common_config('PubSubHubBub', 'hub');
49         if(empty($this->hub)){
50             $this->hub = DEFAULT_HUB;
51         }
52     }
53
54     function onStartApiAtom($action){
55         $action->element('link',array('rel'=>'hub','href'=>$this->hub),null);
56     }
57
58     function onStartApiRss($action){
59         $action->element('atom:link',array('rel'=>'hub','href'=>$this->hub),null);
60     }
61
62     function onHandleQueuedNotice($notice){
63         $publisher = new Publisher($this->hub);
64
65         $feeds = array();
66
67         //public timeline feeds
68         $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'public_timeline.rss'));
69         $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'public_timeline.atom'));
70
71         //author's own feeds
72         $user = User::staticGet('id',$notice->profile_id);
73         $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss'));
74         $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom'));
75
76         //tag feeds
77         $tag = new Notice_tag();
78         $tag->notice_id = $notice->id;
79         if ($tag->find()) {
80             while ($tag->fetch()) {
81                 $feeds[]=common_local_url('api',array('apiaction' => 'tags','method' => 'timeline', 'argument'=>$tag->tag.'.atom'));
82                 $feeds[]=common_local_url('api',array('apiaction' => 'tags','method' => 'timeline', 'argument'=>$tag->tag.'.rss'));
83             }
84         }
85
86         //group feeds
87         $group_inbox = new Group_inbox();
88         $group_inbox->notice_id = $notice->id;
89         if ($group_inbox->find()) {
90             while ($group_inbox->fetch()) {
91                 $group = User_group::staticGet('id',$group_inbox->group_id);
92                 $feeds[]=common_local_url('api',array('apiaction' => 'groups','method' => 'timeline','argument' => $group->nickname.'.rss'));
93                 $feeds[]=common_local_url('api',array('apiaction' => 'groups','method' => 'timeline','argument' => $group->nickname.'.atom'));
94             }
95         }
96
97         //feed of each user that subscribes to the notice's author
98         $notice_inbox = new Notice_inbox();
99         $notice_inbox->notice_id = $notice->id;
100         if ($notice_inbox->find()) {
101             while ($notice_inbox->fetch()) {
102                 $user = User::staticGet('id',$notice_inbox->user_id);
103                 $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss'));
104                 $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom'));
105             }
106         }
107
108         /* TODO: when the reply page gets RSS and ATOM feeds, implement this
109         //feed of user replied to
110         if($notice->reply_to){
111                 $user = User::staticGet('id',$notice->reply_to);
112                 $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.rss'));
113                 $feeds[]=common_local_url('api',array('apiaction' => 'statuses','method' => 'user_timeline','argument' => $user->nickname.'.atom'));
114         }*/
115
116         foreach(array_unique($feeds) as $feed){
117             if(! $publisher->publish_update($feed)){
118                 common_log_line(LOG_WARNING,$feed.' was not published to hub at '.$this->hub.':'.$publisher->last_response());
119             }
120         }
121     }
122 }