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