]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Localisation updates from http://translatewiki.net.
[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         $action->showContentBlock();
226         $action->showScripts();
227         $action->elementEnd('body');
228         return false; // No default processing
229     }
230
231     function noticeAsJson($notice)
232     {
233         // FIXME: this code should be abstracted to a neutral third
234         // party, like Notice::asJson(). I'm not sure of the ethics
235         // of refactoring from within a plugin, so I'm just abusing
236         // the ApiAction method. Don't do this unless you're me!
237
238         $act = new ApiAction('/dev/null');
239
240         $arr = $act->twitterStatusArray($notice, true);
241         $arr['url'] = $notice->bestUrl();
242         $arr['html'] = htmlspecialchars($notice->rendered);
243         $arr['source'] = htmlspecialchars($arr['source']);
244         $arr['conversation_url'] = $this->getConversationUrl($notice);
245
246         $profile = $notice->getProfile();
247         $arr['user']['profile_url'] = $profile->profileurl;
248
249         // Add needed repeat data
250
251         if (!empty($notice->repeat_of)) {
252             $original = Notice::staticGet('id', $notice->repeat_of);
253             if (!empty($original)) {
254                 $arr['retweeted_status']['url'] = $original->bestUrl();
255                 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
256                 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
257                 $originalProfile = $original->getProfile();
258                 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
259                 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
260             }
261             $original = null;
262         }
263
264         return $arr;
265     }
266
267     function getNoticeTags($notice)
268     {
269         $tags = null;
270
271         $nt = new Notice_tag();
272         $nt->notice_id = $notice->id;
273
274         if ($nt->find()) {
275             $tags = array();
276             while ($nt->fetch()) {
277                 $tags[] = $nt->tag;
278             }
279         }
280
281         $nt->free();
282         $nt = null;
283
284         return $tags;
285     }
286
287     function getConversationUrl($notice)
288     {
289         $convurl = null;
290
291         if ($notice->hasConversation()) {
292             $conv = Conversation::staticGet(
293                 'id',
294                 $notice->conversation
295             );
296             $convurl = $conv->uri;
297
298             if(empty($convurl)) {
299                 $msg = sprintf(
300                     "Couldn't find Conversation ID %d to make 'in context'"
301                     . "link for Notice ID %d",
302                     $notice->conversation,
303                     $notice->id
304                 );
305
306                 common_log(LOG_WARNING, $msg);
307             } else {
308                 $convurl .= '#notice-' . $notice->id;
309             }
310         }
311
312         return $convurl;
313     }
314
315     function _getScripts()
316     {
317         if (common_config('site', 'minify')) {
318             $js = 'realtimeupdate.min.js';
319         } else {
320             $js = 'realtimeupdate.js';
321         }
322         return array(Plugin::staticPath('Realtime', $js));
323     }
324
325     /**
326      * Export any i18n messages that need to be loaded at runtime...
327      *
328      * @param Action $action
329      * @param array $messages
330      *
331      * @return boolean hook return value
332      */
333     function onEndScriptMessages($action, &$messages)
334     {
335         // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
336         $messages['realtime_play'] = _m('BUTTON', 'Play');
337         // TRANS: Tooltip for realtime view "play" button.
338         $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
339         // TRANS: Text label for realtime view "pause" button
340         $messages['realtime_pause'] = _m('BUTTON', 'Pause');
341         // TRANS: Tooltip for realtime view "pause" button
342         $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
343         // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
344         $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
345         // TRANS: Tooltip for realtime view "popup" button.
346         $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
347
348         return true;
349     }
350
351     function _updateInitialize($timeline, $user_id)
352     {
353         return "RealtimeUpdate.init($user_id, \"$this->showurl\"); ";
354     }
355
356     function _connect()
357     {
358     }
359
360     function _publish($timeline, $json)
361     {
362     }
363
364     function _disconnect()
365     {
366     }
367
368     function _pathToChannel($path)
369     {
370         return '';
371     }
372
373     function _getTimeline($action)
374     {
375         $path = null;
376         $timeline = null;
377
378         $action_name = $action->trimmed('action');
379
380         switch ($action_name) {
381          case 'public':
382             $path = array('public');
383             break;
384          case 'tag':
385             $tag = $action->trimmed('tag');
386             if (!empty($tag)) {
387                 $path = array('tag', $tag);
388             }
389             break;
390          case 'showstream':
391          case 'all':
392          case 'replies':
393          case 'showgroup':
394             $nickname = common_canonical_nickname($action->trimmed('nickname'));
395             if (!empty($nickname)) {
396                 $path = array($action_name, $nickname);
397             }
398             break;
399          default:
400             break;
401         }
402
403         if (!empty($path)) {
404             $timeline = $this->_pathToChannel($path);
405         }
406
407         return $timeline;
408     }
409 }