]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PubSubHubBub/PubSubHubBubPlugin.php
Merge branch '0.9.x' into inblob
[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('ApiTimelinePublic',array('format' => 'rss'));
69         $feeds[]=common_local_url('ApiTimelinePublic',array('format' => 'atom'));
70
71         //author's own feeds
72         $user = User::staticGet('id',$notice->profile_id);
73         $feeds[]=common_local_url('ApiTimelineUser',array('id' => $user->nickname, 'format'=>'rss'));
74         $feeds[]=common_local_url('ApiTimelineUser',array('id' => $user->nickname, 'format'=>'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('ApiTimelineTag',array('tag'=>$tag->tag, 'format'=>'rss'));
82                 $feeds[]=common_local_url('ApiTimelineTag',array('tag'=>$tag->tag, 'format'=>'atom'));
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('ApiTimelineGroup',array('id' => $group->nickname,'format'=>'rss'));
93                 $feeds[]=common_local_url('ApiTimelineGroup',array('id' => $group->nickname,'format'=>'atom'));
94             }
95         }
96
97         //feed of each user that subscribes to the notice's author
98
99         $ni = $notice->whoGets();
100
101         foreach (array_keys($ni) as $user_id) {
102             $user = User::staticGet('id', $user_id);
103             if (empty($user)) {
104                 continue;
105             }
106             $feeds[]=common_local_url('ApiTimelineUser',array('id' => $user->nickname, 'format'=>'rss'));
107             $feeds[]=common_local_url('ApiTimelineUser',array('id' => $user->nickname, 'format'=>'atom'));
108         }
109
110         //feed of user replied to
111         if($notice->reply_to){
112                 $user = User::staticGet('id',$notice->reply_to);
113                 $feeds[]=common_local_url('ApiTimelineMentions',array('id' => $user->nickname,'format'=>'rss'));
114                 $feeds[]=common_local_url('ApiTimelineMentions',array('id' => $user->nickname,'format'=>'atom'));
115         }
116
117         foreach(array_unique($feeds) as $feed){
118             if(! $publisher->publish_update($feed)){
119                 common_log_line(LOG_WARNING,$feed.' was not published to hub at '.$this->hub.':'.$publisher->last_response());
120             }
121         }
122     }
123
124     function onPluginVersion(&$versions)
125     {
126         $versions[] = array('name' => 'PubSubHubBub',
127                             'version' => STATUSNET_VERSION,
128                             'author' => 'Craig Andrews',
129                             'homepage' => 'http://status.net/wiki/Plugin:PubSubHubBub',
130                             'rawdescription' =>
131                             _m('The PubSubHubBub plugin pushes RSS/Atom updates to a <a href="http://pubsubhubbub.googlecode.com/">PubSubHubBub</a> hub.'));
132
133         return true;
134     }
135 }