]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[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 class RealtimePlugin extends Plugin
47 {
48     protected $showurl = null;
49
50     /**
51      * When it's time to initialize the plugin, calculate and
52      * pass the URLs we need.
53      */
54
55     function onInitializePlugin()
56     {
57         // FIXME: need to find a better way to pass this pattern in
58         $this->showurl = common_local_url('shownotice',
59                                             array('notice' => '0000000000'));
60         return true;
61     }
62
63     function onEndShowScripts($action)
64     {
65         $timeline = $this->_getTimeline($action);
66
67         // If there's not a timeline on this page,
68         // just return true
69
70         if (empty($timeline)) {
71             return true;
72         }
73
74         $base = $action->selfUrl();
75         if (mb_strstr($base, '?')) {
76             $url = $base . '&realtime=1';
77         } else {
78             $url = $base . '?realtime=1';
79         }
80
81         $scripts = $this->_getScripts();
82
83         foreach ($scripts as $script) {
84             $action->script($script);
85         }
86
87         $user = common_current_user();
88
89         if (!empty($user->id)) {
90             $user_id = $user->id;
91         } else {
92             $user_id = 0;
93         }
94
95         if ($action->boolean('realtime')) {
96             $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
97         }
98         else {
99             $pluginPath = common_path('plugins/Realtime/');
100             $realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
101         }
102
103         $script = ' $(document).ready(function() { '.
104           $realtimeUI.
105           $this->_updateInitialize($timeline, $user_id).
106           '}); ';
107         $action->inlineScript($script);
108
109         return true;
110     }
111
112     function onEndShowStatusNetStyles($action)
113     {
114         $action->cssLink(Plugin::staticPath('Realtime', 'realtimeupdate.css'),
115                          null,
116                          'screen, projection, tv');
117         return true;
118     }
119
120     function onHandleQueuedNotice($notice)
121     {
122         $paths = array();
123
124         // Add to the author's timeline
125
126         $user = User::staticGet('id', $notice->profile_id);
127
128         if (!empty($user)) {
129             $paths[] = array('showstream', $user->nickname);
130         }
131
132         // Add to the public timeline
133
134         if ($notice->is_local == Notice::LOCAL_PUBLIC ||
135             ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
136             $paths[] = array('public');
137         }
138
139         // Add to the tags timeline
140
141         $tags = $this->getNoticeTags($notice);
142
143         if (!empty($tags)) {
144             foreach ($tags as $tag) {
145                 $paths[] = array('tag', $tag);
146             }
147         }
148
149         // Add to inbox timelines
150         // XXX: do a join
151
152         $ni = $notice->whoGets();
153
154         foreach (array_keys($ni) as $user_id) {
155             $user = User::staticGet('id', $user_id);
156             $paths[] = array('all', $user->nickname);
157         }
158
159         // Add to the replies timeline
160
161         $reply = new Reply();
162         $reply->notice_id = $notice->id;
163
164         if ($reply->find()) {
165             while ($reply->fetch()) {
166                 $user = User::staticGet('id', $reply->profile_id);
167                 if (!empty($user)) {
168                     $paths[] = array('replies', $user->nickname);
169                 }
170             }
171         }
172
173         // Add to the group timeline
174         // XXX: join
175
176         $gi = new Group_inbox();
177         $gi->notice_id = $notice->id;
178
179         if ($gi->find()) {
180             while ($gi->fetch()) {
181                 $ug = User_group::staticGet('id', $gi->group_id);
182                 $paths[] = array('showgroup', $ug->nickname);
183             }
184         }
185
186         if (count($paths) > 0) {
187
188             $json = $this->noticeAsJson($notice);
189
190             $this->_connect();
191
192             foreach ($paths as $path) {
193                 $timeline = $this->_pathToChannel($path);
194                 $this->_publish($timeline, $json);
195             }
196
197             $this->_disconnect();
198         }
199
200         return true;
201     }
202
203     function onStartShowBody($action)
204     {
205         $realtime = $action->boolean('realtime');
206         if (!$realtime) {
207             return true;
208         }
209
210         $action->elementStart('body',
211                               (common_current_user()) ? array('id' => $action->trimmed('action'),
212                                                               'class' => 'user_in realtime-popup')
213                               : array('id' => $action->trimmed('action'),
214                                       'class'=> 'realtime-popup'));
215
216         // XXX hack to deal with JS that tries to get the
217         // root url from page output
218
219         $action->elementStart('address');
220         $action->element('a', array('class' => 'url',
221                                   'href' => common_local_url('public')),
222                          '');
223         $action->elementEnd('address');
224
225         if (common_logged_in()) {
226             $action->showNoticeForm();
227         }
228
229         $action->showContentBlock();
230         $action->showScripts();
231         $action->elementEnd('body');
232         return false; // No default processing
233     }
234
235     function noticeAsJson($notice)
236     {
237         // FIXME: this code should be abstracted to a neutral third
238         // party, like Notice::asJson(). I'm not sure of the ethics
239         // of refactoring from within a plugin, so I'm just abusing
240         // the ApiAction method. Don't do this unless you're me!
241
242         $act = new ApiAction('/dev/null');
243
244         $arr = $act->twitterStatusArray($notice, true);
245         $arr['url'] = $notice->bestUrl();
246         $arr['html'] = htmlspecialchars($notice->rendered);
247         $arr['source'] = htmlspecialchars($arr['source']);
248         $arr['conversation_url'] = $this->getConversationUrl($notice);
249
250         $profile = $notice->getProfile();
251         $arr['user']['profile_url'] = $profile->profileurl;
252
253         // Add needed repeat data
254
255         if (!empty($notice->repeat_of)) {
256             $original = Notice::staticGet('id', $notice->repeat_of);
257             if (!empty($original)) {
258                 $arr['retweeted_status']['url'] = $original->bestUrl();
259                 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
260                 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
261                 $originalProfile = $original->getProfile();
262                 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
263                 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
264             }
265             $original = null;
266         }
267
268         return $arr;
269     }
270
271     function getNoticeTags($notice)
272     {
273         $tags = null;
274
275         $nt = new Notice_tag();
276         $nt->notice_id = $notice->id;
277
278         if ($nt->find()) {
279             $tags = array();
280             while ($nt->fetch()) {
281                 $tags[] = $nt->tag;
282             }
283         }
284
285         $nt->free();
286         $nt = null;
287
288         return $tags;
289     }
290
291     function getConversationUrl($notice)
292     {
293         $convurl = null;
294
295         if ($notice->hasConversation()) {
296             $conv = Conversation::staticGet(
297                 'id',
298                 $notice->conversation
299             );
300             $convurl = $conv->uri;
301
302             if(empty($convurl)) {
303                 $msg = sprintf(
304                     "Couldn't find Conversation ID %d to make 'in context'"
305                     . "link for Notice ID %d",
306                     $notice->conversation,
307                     $notice->id
308                 );
309
310                 common_log(LOG_WARNING, $msg);
311             } else {
312                 $convurl .= '#notice-' . $notice->id;
313             }
314         }
315
316         return $convurl;
317     }
318
319     function _getScripts()
320     {
321         if (common_config('site', 'minify')) {
322             $js = 'realtimeupdate.min.js';
323         } else {
324             $js = 'realtimeupdate.js';
325         }
326         return array(Plugin::staticPath('Realtime', $js));
327     }
328
329     /**
330      * Export any i18n messages that need to be loaded at runtime...
331      *
332      * @param Action $action
333      * @param array $messages
334      *
335      * @return boolean hook return value
336      */
337     function onEndScriptMessages($action, &$messages)
338     {
339         // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
340         $messages['realtime_play'] = _m('BUTTON', 'Play');
341         // TRANS: Tooltip for realtime view "play" button.
342         $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
343         // TRANS: Text label for realtime view "pause" button
344         $messages['realtime_pause'] = _m('BUTTON', 'Pause');
345         // TRANS: Tooltip for realtime view "pause" button
346         $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
347         // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
348         $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
349         // TRANS: Tooltip for realtime view "popup" button.
350         $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
351
352         return true;
353     }
354
355     function _updateInitialize($timeline, $user_id)
356     {
357         return "RealtimeUpdate.init($user_id, \"$this->showurl\"); ";
358     }
359
360     function _connect()
361     {
362     }
363
364     function _publish($timeline, $json)
365     {
366     }
367
368     function _disconnect()
369     {
370     }
371
372     function _pathToChannel($path)
373     {
374         return '';
375     }
376
377     function _getTimeline($action)
378     {
379         $path = null;
380         $timeline = null;
381
382         $action_name = $action->trimmed('action');
383
384         switch ($action_name) {
385          case 'public':
386             $path = array('public');
387             break;
388          case 'tag':
389             $tag = $action->trimmed('tag');
390             if (!empty($tag)) {
391                 $path = array('tag', $tag);
392             }
393             break;
394          case 'showstream':
395          case 'all':
396          case 'replies':
397          case 'showgroup':
398             $nickname = common_canonical_nickname($action->trimmed('nickname'));
399             if (!empty($nickname)) {
400                 $path = array($action_name, $nickname);
401             }
402             break;
403          default:
404             break;
405         }
406
407         if (!empty($path)) {
408             $timeline = $this->_pathToChannel($path);
409         }
410
411         return $timeline;
412     }
413 }