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