]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Comet/CometPlugin.php
cff0d4c9d63fc2e52f2511a09d2a2d8985cbc286
[quix0rs-gnu-social.git] / plugins / Comet / CometPlugin.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Plugin to do "real time" updates using Comet/Bayeux
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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin to do realtime updates using Comet
36  *
37  * @category Plugin
38  * @package  Laconica
39  * @author   Evan Prodromou <evan@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  */
43
44 class CometPlugin extends Plugin
45 {
46     var $server = null;
47
48     function __construct($server=null)
49     {
50         $this->server = $server;
51
52         parent::__construct();
53     }
54
55     function onEndShowScripts($action)
56     {
57         $timeline = null;
58
59         $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
60
61         switch ($action->trimmed('action')) {
62          case 'public':
63             $timeline = '/timelines/public';
64             break;
65          case 'tag':
66             $tag = $action->trimmed('tag');
67             if (!empty($tag)) {
68                 $timeline = '/timelines/tag/'.$tag;
69             } else {
70                 return true;
71             }
72             break;
73          default:
74             return true;
75         }
76
77         $scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js');
78
79         foreach ($scripts as $script) {
80             $action->element('script', array('type' => 'text/javascript',
81                                              'src' => common_path('plugins/Comet/'.$script)),
82                          ' ');
83         }
84
85         $action->elementStart('script', array('type' => 'text/javascript'));
86         $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\");});");
87         $action->elementEnd('script');
88
89         return true;
90     }
91
92     function onEndNoticeSave($notice)
93     {
94         $this->log(LOG_INFO, "Called for save notice.");
95
96         $timelines = array();
97
98         // XXX: Add other timelines; this is just for the public one
99
100         if ($notice->is_local ||
101             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
102             $timelines[] = '/timelines/public';
103         }
104
105         $tags = $this->getNoticeTags($notice);
106
107         if (!empty($tags)) {
108             foreach ($tags as $tag) {
109                 $timelines[] = '/timelines/tag/' . $tag;
110             }
111         }
112
113         if (count($timelines) > 0) {
114             // Require this, since we need it
115             require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
116
117             $json = $this->noticeAsJson($notice);
118
119             // Bayeux? Comet? Huh? These terms confuse me
120             $bay = new Bayeux($this->server);
121
122             foreach ($timelines as $timeline) {
123                 $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
124                 $bay->publish($timeline, $json);
125             }
126
127             $bay = NULL;
128         }
129
130         return true;
131     }
132
133     function noticeAsJson($notice)
134     {
135         // FIXME: this code should be abstracted to a neutral third
136         // party, like Notice::asJson(). I'm not sure of the ethics
137         // of refactoring from within a plugin, so I'm just abusing
138         // the TwitterApiAction method. Don't do this unless you're me!
139
140         require_once(INSTALLDIR.'/lib/twitterapi.php');
141
142         $act = new TwitterApiAction('/dev/null');
143
144         $arr = $act->twitter_status_array($notice, true);
145         $arr['url'] = $notice->bestUrl();
146
147         $profile = $notice->getProfile();
148         $arr['user']['profile_url'] = $profile->profileurl;
149
150         return $arr;
151     }
152
153     function getNoticeTags($notice)
154     {
155         $tags = null;
156
157         $nt = new Notice_tag();
158         $nt->notice_id = $notice->id;
159
160         if ($nt->find()) {
161             $tags = array();
162             while ($nt->fetch()) {
163                 $tags[] = $nt->tag;
164             }
165         }
166
167         $nt->free();
168         $nt = null;
169
170         return $tags;
171     }
172
173     // Push this up to Plugin
174
175     function log($level, $msg)
176     {
177         common_log($level, get_class($this) . ': '.$msg);
178     }
179 }