]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Starting on making Realtime plugin's UI messages localizable: pause/play, popup butto...
[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         $i18n = $this->_getMessages();
110         $script = ' $(document).ready(function() { '.
111           'RealtimeUpdate._messages=' . json_encode($i18n) . ';' .
112           $realtimeUI.
113           $this->_updateInitialize($timeline, $user_id).
114           '}); ';
115         $action->inlineScript($script);
116
117         return true;
118     }
119
120     function onEndShowStatusNetStyles($action)
121     {
122         $action->cssLink('plugins/Realtime/realtimeupdate.css',
123                          null, 'screen, projection, tv');
124         return true;
125     }
126
127     function onHandleQueuedNotice($notice)
128     {
129         $paths = array();
130
131         // Add to the author's timeline
132
133         $user = User::staticGet('id', $notice->profile_id);
134
135         if (!empty($user)) {
136             $paths[] = array('showstream', $user->nickname);
137         }
138
139         // Add to the public timeline
140
141         if ($notice->is_local == Notice::LOCAL_PUBLIC ||
142             ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
143             $paths[] = array('public');
144         }
145
146         // Add to the tags timeline
147
148         $tags = $this->getNoticeTags($notice);
149
150         if (!empty($tags)) {
151             foreach ($tags as $tag) {
152                 $paths[] = array('tag', $tag);
153             }
154         }
155
156         // Add to inbox timelines
157         // XXX: do a join
158
159         $ni = $notice->whoGets();
160
161         foreach (array_keys($ni) as $user_id) {
162             $user = User::staticGet('id', $user_id);
163             $paths[] = array('all', $user->nickname);
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         $act = new ApiAction('/dev/null');
250
251         $arr = $act->twitterStatusArray($notice, true);
252         $arr['url'] = $notice->bestUrl();
253         $arr['html'] = htmlspecialchars($notice->rendered);
254         $arr['source'] = htmlspecialchars($arr['source']);
255         $arr['conversation_url'] = $this->getConversationUrl($notice);
256
257         $profile = $notice->getProfile();
258         $arr['user']['profile_url'] = $profile->profileurl;
259
260         // Add needed repeat data
261
262         if (!empty($notice->repeat_of)) {
263             $original = Notice::staticGet('id', $notice->repeat_of);
264             if (!empty($original)) {
265                 $arr['retweeted_status']['url'] = $original->bestUrl();
266                 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
267                 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
268                 $originalProfile = $original->getProfile();
269                 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
270                 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
271             }
272             $original = null;
273         }
274
275         return $arr;
276     }
277
278     function getNoticeTags($notice)
279     {
280         $tags = null;
281
282         $nt = new Notice_tag();
283         $nt->notice_id = $notice->id;
284
285         if ($nt->find()) {
286             $tags = array();
287             while ($nt->fetch()) {
288                 $tags[] = $nt->tag;
289             }
290         }
291
292         $nt->free();
293         $nt = null;
294
295         return $tags;
296     }
297
298     function getConversationUrl($notice)
299     {
300         $convurl = null;
301
302         if ($notice->hasConversation()) {
303             $conv = Conversation::staticGet(
304                 'id',
305                 $notice->conversation
306             );
307             $convurl = $conv->uri;
308
309             if(empty($convurl)) {
310                 $msg = sprintf(
311                     "Couldn't find Conversation ID %d to make 'in context'"
312                     . "link for Notice ID %d",
313                     $notice->conversation,
314                     $notice->id
315                 );
316
317                 common_log(LOG_WARNING, $msg);
318             } else {
319                 $convurl .= '#notice-' . $notice->id;
320             }
321         }
322
323         return $convurl;
324     }
325
326     function _getScripts()
327     {
328         return array('plugins/Realtime/realtimeupdate.js');
329     }
330
331     /**
332      * Any i18n messages that need to be loaded at runtime.
333      * @return array of string key to output text string pairs
334      */
335     function _getMessages()
336     {
337         return array(
338             // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
339             'play' => _m('BUTTON', 'Play'),
340             // TRANS: Tooltip for realtime view "play" button.
341             'play_tooltip' => _m('TOOLTIP', 'Play'),
342             // TRANS: Text label for realtime view "pause" button
343             'pause' => _m('BUTTON', 'Pause'),
344             // TRANS: Tooltip for realtime view "pause" button
345             'pause_tooltip' => _m('TOOLTIP', 'Pause'),
346             // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
347             'popup' => _m('BUTTON', 'Pop up'),
348             // TRANS: Tooltip for realtime view "popup" button.
349             'popup_tooltip' => _m('TOOLTIP', 'Pop up in a window'),
350         );
351     }
352
353     function _updateInitialize($timeline, $user_id)
354     {
355         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->repeaturl\", \"$this->deleteurl\"); ";
356     }
357
358     function _connect()
359     {
360     }
361
362     function _publish($timeline, $json)
363     {
364     }
365
366     function _disconnect()
367     {
368     }
369
370     function _pathToChannel($path)
371     {
372         return '';
373     }
374
375     function _getTimeline($action)
376     {
377         $path = null;
378         $timeline = null;
379
380         $action_name = $action->trimmed('action');
381
382         switch ($action_name) {
383          case 'public':
384             $path = array('public');
385             break;
386          case 'tag':
387             $tag = $action->trimmed('tag');
388             if (!empty($tag)) {
389                 $path = array('tag', $tag);
390             }
391             break;
392          case 'showstream':
393          case 'all':
394          case 'replies':
395          case 'showgroup':
396             $nickname = common_canonical_nickname($action->trimmed('nickname'));
397             if (!empty($nickname)) {
398                 $path = array($action_name, $nickname);
399             }
400             break;
401          default:
402             break;
403         }
404
405         if (!empty($path)) {
406             $timeline = $this->_pathToChannel($path);
407         }
408
409         return $timeline;
410     }
411 }