]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
f617c39060193c1b4cfac67afe6ec675cbcfb6aa
[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 $replyurl = null;
49     protected $favorurl = null;
50     protected $deleteurl = null;
51
52     /**
53      * When it's time to initialize the plugin, calculate and
54      * pass the URLs we need.
55      */
56
57     function onInitializePlugin()
58     {
59         $this->replyurl = common_local_url('newnotice');
60         $this->favorurl = common_local_url('favor');
61         $this->repeaturl = common_local_url('repeat');
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(Plugin::staticPath('Realtime', 'realtimeupdate.css'),
120                          null,
121                          '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         $ni = $notice->whoGets();
158
159         foreach (array_keys($ni) as $user_id) {
160             $user = User::staticGet('id', $user_id);
161             $paths[] = array('all', $user->nickname);
162         }
163
164         // Add to the replies timeline
165
166         $reply = new Reply();
167         $reply->notice_id = $notice->id;
168
169         if ($reply->find()) {
170             while ($reply->fetch()) {
171                 $user = User::staticGet('id', $reply->profile_id);
172                 if (!empty($user)) {
173                     $paths[] = array('replies', $user->nickname);
174                 }
175             }
176         }
177
178         // Add to the group timeline
179         // XXX: join
180
181         $gi = new Group_inbox();
182         $gi->notice_id = $notice->id;
183
184         if ($gi->find()) {
185             while ($gi->fetch()) {
186                 $ug = User_group::staticGet('id', $gi->group_id);
187                 $paths[] = array('showgroup', $ug->nickname);
188             }
189         }
190
191         if (count($paths) > 0) {
192
193             $json = $this->noticeAsJson($notice);
194
195             $this->_connect();
196
197             foreach ($paths as $path) {
198                 $timeline = $this->_pathToChannel($path);
199                 $this->_publish($timeline, $json);
200             }
201
202             $this->_disconnect();
203         }
204
205         return true;
206     }
207
208     function onStartShowBody($action)
209     {
210         $realtime = $action->boolean('realtime');
211         if (!$realtime) {
212             return true;
213         }
214
215         $action->elementStart('body',
216                               (common_current_user()) ? array('id' => $action->trimmed('action'),
217                                                               'class' => 'user_in realtime-popup')
218                               : array('id' => $action->trimmed('action'),
219                                       'class'=> 'realtime-popup'));
220
221         // XXX hack to deal with JS that tries to get the
222         // root url from page output
223
224         $action->elementStart('address');
225         $action->element('a', array('class' => 'url',
226                                   'href' => common_local_url('public')),
227                          '');
228         $action->elementEnd('address');
229
230         if (common_logged_in()) {
231             $action->showNoticeForm();
232         }
233
234         $action->showContentBlock();
235         $action->showScripts();
236         $action->elementEnd('body');
237         return false; // No default processing
238     }
239
240     function noticeAsJson($notice)
241     {
242         // FIXME: this code should be abstracted to a neutral third
243         // party, like Notice::asJson(). I'm not sure of the ethics
244         // of refactoring from within a plugin, so I'm just abusing
245         // the ApiAction method. Don't do this unless you're me!
246
247         $act = new ApiAction('/dev/null');
248
249         $arr = $act->twitterStatusArray($notice, true);
250         $arr['url'] = $notice->bestUrl();
251         $arr['html'] = htmlspecialchars($notice->rendered);
252         $arr['source'] = htmlspecialchars($arr['source']);
253         $arr['conversation_url'] = $this->getConversationUrl($notice);
254
255         $profile = $notice->getProfile();
256         $arr['user']['profile_url'] = $profile->profileurl;
257
258         // Add needed repeat data
259
260         if (!empty($notice->repeat_of)) {
261             $original = Notice::staticGet('id', $notice->repeat_of);
262             if (!empty($original)) {
263                 $arr['retweeted_status']['url'] = $original->bestUrl();
264                 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
265                 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
266                 $originalProfile = $original->getProfile();
267                 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
268                 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
269             }
270             $original = null;
271         }
272
273         return $arr;
274     }
275
276     function getNoticeTags($notice)
277     {
278         $tags = null;
279
280         $nt = new Notice_tag();
281         $nt->notice_id = $notice->id;
282
283         if ($nt->find()) {
284             $tags = array();
285             while ($nt->fetch()) {
286                 $tags[] = $nt->tag;
287             }
288         }
289
290         $nt->free();
291         $nt = null;
292
293         return $tags;
294     }
295
296     function getConversationUrl($notice)
297     {
298         $convurl = null;
299
300         if ($notice->hasConversation()) {
301             $conv = Conversation::staticGet(
302                 'id',
303                 $notice->conversation
304             );
305             $convurl = $conv->uri;
306
307             if(empty($convurl)) {
308                 $msg = sprintf(
309                     "Couldn't find Conversation ID %d to make 'in context'"
310                     . "link for Notice ID %d",
311                     $notice->conversation,
312                     $notice->id
313                 );
314
315                 common_log(LOG_WARNING, $msg);
316             } else {
317                 $convurl .= '#notice-' . $notice->id;
318             }
319         }
320
321         return $convurl;
322     }
323
324     function _getScripts()
325     {
326         if (common_config('site', 'minify')) {
327             $js = 'realtimeupdate.min.js';
328         } else {
329             $js = 'realtimeupdate.js';
330         }
331         return array(Plugin::staticPath('Realtime', $js));
332     }
333
334     /**
335      * Export any i18n messages that need to be loaded at runtime...
336      *
337      * @param Action $action
338      * @param array $messages
339      *
340      * @return boolean hook return value
341      */
342     function onEndScriptMessages($action, &$messages)
343     {
344         // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
345         $messages['realtime_play'] = _m('BUTTON', 'Play');
346         // TRANS: Tooltip for realtime view "play" button.
347         $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
348         // TRANS: Text label for realtime view "pause" button
349         $messages['realtime_pause'] = _m('BUTTON', 'Pause');
350         // TRANS: Tooltip for realtime view "pause" button
351         $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
352         // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
353         $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
354         // TRANS: Tooltip for realtime view "popup" button.
355         $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
356
357         return true;
358     }
359
360     function _updateInitialize($timeline, $user_id)
361     {
362         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->repeaturl\", \"$this->deleteurl\"); ";
363     }
364
365     function _connect()
366     {
367     }
368
369     function _publish($timeline, $json)
370     {
371     }
372
373     function _disconnect()
374     {
375     }
376
377     function _pathToChannel($path)
378     {
379         return '';
380     }
381
382     function _getTimeline($action)
383     {
384         $path = null;
385         $timeline = null;
386
387         $action_name = $action->trimmed('action');
388
389         switch ($action_name) {
390          case 'public':
391             $path = array('public');
392             break;
393          case 'tag':
394             $tag = $action->trimmed('tag');
395             if (!empty($tag)) {
396                 $path = array('tag', $tag);
397             }
398             break;
399          case 'showstream':
400          case 'all':
401          case 'replies':
402          case 'showgroup':
403             $nickname = common_canonical_nickname($action->trimmed('nickname'));
404             if (!empty($nickname)) {
405                 $path = array($action_name, $nickname);
406             }
407             break;
408          default:
409             break;
410         }
411
412         if (!empty($path)) {
413             $timeline = $this->_pathToChannel($path);
414         }
415
416         return $timeline;
417     }
418 }