]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
3e33fdaf1979887851437f3fd81cfbf7712676e7
[quix0rs-gnu-social.git] / plugins / Realtime / RealtimePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Superclass for plugins that do "real time" updates of timelines using Ajax
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    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
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') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * Superclass for plugin to do realtime updates
36  *
37  * Based on experience with the Comet and Meteor plugins,
38  * this superclass extracts out some of the common functionality
39  *
40  * @category Plugin
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class RealtimePlugin extends Plugin
48 {
49     protected $replyurl = null;
50     protected $favorurl = null;
51     protected $deleteurl = null;
52
53     /**
54      * When it's time to initialize the plugin, calculate and
55      * pass the URLs we need.
56      */
57
58     function onInitializePlugin()
59     {
60         $this->replyurl = common_local_url('newnotice');
61         $this->favorurl = common_local_url('favor');
62         // FIXME: need to find a better way to pass this pattern in
63         $this->deleteurl = common_local_url('deletenotice',
64                                             array('notice' => '0000000000'));
65         return true;
66     }
67
68     function onEndShowScripts($action)
69     {
70         $timeline = $this->_getTimeline($action);
71
72         // If there's not a timeline on this page,
73         // just return true
74
75         if (empty($timeline)) {
76             return true;
77         }
78
79         $base = $action->selfUrl();
80         if (mb_strstr($base, '?')) {
81             $url = $base . '&realtime=1';
82         } else {
83             $url = $base . '?realtime=1';
84         }
85
86         $scripts = $this->_getScripts();
87
88         foreach ($scripts as $script) {
89             $action->script($script);
90         }
91
92         $user = common_current_user();
93
94         if (!empty($user->id)) {
95             $user_id = $user->id;
96         } else {
97             $user_id = 0;
98         }
99
100         if ($action->boolean('realtime')) {
101             $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
102         }
103         else {
104             $pluginPath = common_path('plugins/Realtime/');
105             $realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
106         }
107
108         $script = ' $(document).ready(function() { '.
109           $realtimeUI.
110           $this->_updateInitialize($timeline, $user_id).
111           '}); ';
112         $action->inlineScript($script);
113
114         return true;
115     }
116
117     function onEndShowStatusNetStyles($action)
118     {
119         $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'),
120                          null, 'screen, projection, tv');
121         return true;
122     }
123
124     function onHandleQueuedNotice($notice)
125     {
126         $paths = array();
127
128         // Add to the author's timeline
129
130         $user = User::staticGet('id', $notice->profile_id);
131
132         if (!empty($user)) {
133             $paths[] = array('showstream', $user->nickname);
134         }
135
136         // Add to the public timeline
137
138         if ($notice->is_local == Notice::LOCAL_PUBLIC ||
139             ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
140             $paths[] = array('public');
141         }
142
143         // Add to the tags timeline
144
145         $tags = $this->getNoticeTags($notice);
146
147         if (!empty($tags)) {
148             foreach ($tags as $tag) {
149                 $paths[] = array('tag', $tag);
150             }
151         }
152
153         // Add to inbox timelines
154         // XXX: do a join
155
156         $inbox = new Notice_inbox();
157         $inbox->notice_id = $notice->id;
158
159         if ($inbox->find()) {
160             while ($inbox->fetch()) {
161                 $user = User::staticGet('id', $inbox->user_id);
162                 $paths[] = array('all', $user->nickname);
163             }
164         }
165
166         // Add to the replies timeline
167
168         $reply = new Reply();
169         $reply->notice_id = $notice->id;
170
171         if ($reply->find()) {
172             while ($reply->fetch()) {
173                 $user = User::staticGet('id', $reply->profile_id);
174                 if (!empty($user)) {
175                     $paths[] = array('replies', $user->nickname);
176                 }
177             }
178         }
179
180         // Add to the group timeline
181         // XXX: join
182
183         $gi = new Group_inbox();
184         $gi->notice_id = $notice->id;
185
186         if ($gi->find()) {
187             while ($gi->fetch()) {
188                 $ug = User_group::staticGet('id', $gi->group_id);
189                 $paths[] = array('showgroup', $ug->nickname);
190             }
191         }
192
193         if (count($paths) > 0) {
194
195             $json = $this->noticeAsJson($notice);
196
197             $this->_connect();
198
199             foreach ($paths as $path) {
200                 $timeline = $this->_pathToChannel($path);
201                 $this->_publish($timeline, $json);
202             }
203
204             $this->_disconnect();
205         }
206
207         return true;
208     }
209
210     function onStartShowBody($action)
211     {
212         $realtime = $action->boolean('realtime');
213         if (!$realtime) {
214             return true;
215         }
216
217         $action->elementStart('body',
218                               (common_current_user()) ? array('id' => $action->trimmed('action'),
219                                                               'class' => 'user_in realtime-popup')
220                               : array('id' => $action->trimmed('action'),
221                                       'class'=> 'realtime-popup'));
222
223         // XXX hack to deal with JS that tries to get the
224         // root url from page output
225
226         $action->elementStart('address');
227         $action->element('a', array('class' => 'url',
228                                   'href' => common_local_url('public')),
229                          '');
230         $action->elementEnd('address');
231
232         if (common_logged_in()) {
233             $action->showNoticeForm();
234         }
235
236         $action->showContentBlock();
237         $action->showScripts();
238         $action->elementEnd('body');
239         return false; // No default processing
240     }
241
242     function noticeAsJson($notice)
243     {
244         // FIXME: this code should be abstracted to a neutral third
245         // party, like Notice::asJson(). I'm not sure of the ethics
246         // of refactoring from within a plugin, so I'm just abusing
247         // the ApiAction method. Don't do this unless you're me!
248
249         require_once(INSTALLDIR.'/lib/api.php');
250
251         $act = new ApiAction('/dev/null');
252
253         $arr = $act->twitterStatusArray($notice, true);
254         $arr['url'] = $notice->bestUrl();
255         $arr['html'] = htmlspecialchars($notice->rendered);
256         $arr['source'] = htmlspecialchars($arr['source']);
257
258         if (!empty($notice->reply_to)) {
259             $reply_to = Notice::staticGet('id', $notice->reply_to);
260             if (!empty($reply_to)) {
261                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
262             }
263             $reply_to = null;
264         }
265
266         $profile = $notice->getProfile();
267         $arr['user']['profile_url'] = $profile->profileurl;
268
269         return $arr;
270     }
271
272     function getNoticeTags($notice)
273     {
274         $tags = null;
275
276         $nt = new Notice_tag();
277         $nt->notice_id = $notice->id;
278
279         if ($nt->find()) {
280             $tags = array();
281             while ($nt->fetch()) {
282                 $tags[] = $nt->tag;
283             }
284         }
285
286         $nt->free();
287         $nt = null;
288
289         return $tags;
290     }
291
292     function _getScripts()
293     {
294         return array('plugins/Realtime/realtimeupdate.js',
295                      'plugins/Realtime/json2.js');
296     }
297
298     function _updateInitialize($timeline, $user_id)
299     {
300         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
301     }
302
303     function _connect()
304     {
305     }
306
307     function _publish($timeline, $json)
308     {
309     }
310
311     function _disconnect()
312     {
313     }
314
315     function _pathToChannel($path)
316     {
317         return '';
318     }
319
320     function _getTimeline($action)
321     {
322         $path = null;
323         $timeline = null;
324
325         $action_name = $action->trimmed('action');
326
327         switch ($action_name) {
328          case 'public':
329             $path = array('public');
330             break;
331          case 'tag':
332             $tag = $action->trimmed('tag');
333             if (!empty($tag)) {
334                 $path = array('tag', $tag);
335             }
336             break;
337          case 'showstream':
338          case 'all':
339          case 'replies':
340          case 'showgroup':
341             $nickname = common_canonical_nickname($action->trimmed('nickname'));
342             if (!empty($nickname)) {
343                 $path = array($action_name, $nickname);
344             }
345             break;
346          default:
347             break;
348         }
349
350         if (!empty($path)) {
351             $timeline = $this->_pathToChannel($path);
352         }
353
354         return $timeline;
355     }
356 }