]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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         // 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         $action->elementStart('script', array('type' => 'text/javascript'));
109
110         $script = ' $(document).ready(function() { '.
111           $realtimeUI.
112           $this->_updateInitialize($timeline, $user_id).
113           '}); ';
114         $action->raw($script);
115
116         $action->elementEnd('script');
117
118         return true;
119     }
120
121     function onEndShowStatusNetStyles($action)
122     {
123         $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'), 
124                          null, 'screen, projection, tv');
125         return true;
126     }
127
128     function onHandleQueuedNotice($notice)
129     {
130         $paths = array();
131
132         // Add to the author's timeline
133
134         $user = User::staticGet('id', $notice->profile_id);
135
136         if (!empty($user)) {
137             $paths[] = array('showstream', $user->nickname);
138         }
139
140         // Add to the public timeline
141
142         if ($notice->is_local == Notice::LOCAL_PUBLIC ||
143             ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
144             $paths[] = array('public');
145         }
146
147         // Add to the tags timeline
148
149         $tags = $this->getNoticeTags($notice);
150
151         if (!empty($tags)) {
152             foreach ($tags as $tag) {
153                 $paths[] = array('tag', $tag);
154             }
155         }
156
157         // Add to inbox timelines
158         // XXX: do a join
159
160         $inbox = new Notice_inbox();
161         $inbox->notice_id = $notice->id;
162
163         if ($inbox->find()) {
164             while ($inbox->fetch()) {
165                 $user = User::staticGet('id', $inbox->user_id);
166                 $paths[] = array('all', $user->nickname);
167             }
168         }
169
170         // Add to the replies timeline
171
172         $reply = new Reply();
173         $reply->notice_id = $notice->id;
174
175         if ($reply->find()) {
176             while ($reply->fetch()) {
177                 $user = User::staticGet('id', $reply->profile_id);
178                 if (!empty($user)) {
179                     $paths[] = array('replies', $user->nickname);
180                 }
181             }
182         }
183
184         // Add to the group timeline
185         // XXX: join
186
187         $gi = new Group_inbox();
188         $gi->notice_id = $notice->id;
189
190         if ($gi->find()) {
191             while ($gi->fetch()) {
192                 $ug = User_group::staticGet('id', $gi->group_id);
193                 $paths[] = array('showgroup', $ug->nickname);
194             }
195         }
196
197         if (count($paths) > 0) {
198
199             $json = $this->noticeAsJson($notice);
200
201             $this->_connect();
202
203             foreach ($paths as $path) {
204                 $timeline = $this->_pathToChannel($path);
205                 $this->_publish($timeline, $json);
206             }
207
208             $this->_disconnect();
209         }
210
211         return true;
212     }
213
214     function onStartShowBody($action)
215     {
216         $realtime = $action->boolean('realtime');
217         if (!$realtime) {
218             return true;
219         }
220
221         $action->elementStart('body',
222                               (common_current_user()) ? array('id' => $action->trimmed('action'),
223                                                               'class' => 'user_in')
224                               : array('id' => $action->trimmed('action')));
225
226         // XXX hack to deal with JS that tries to get the
227         // root url from page output
228
229         $action->elementStart('address');
230         $action->element('a', array('class' => 'url',
231                                   'href' => common_local_url('public')),
232                          '');
233         $action->elementEnd('address');
234
235         if (common_logged_in()) {
236             $action->showNoticeForm();
237         }
238
239         $action->showContentBlock();
240         $action->showScripts();
241         $action->elementEnd('body');
242         return false; // No default processing
243     }
244
245     function noticeAsJson($notice)
246     {
247         // FIXME: this code should be abstracted to a neutral third
248         // party, like Notice::asJson(). I'm not sure of the ethics
249         // of refactoring from within a plugin, so I'm just abusing
250         // the ApiAction method. Don't do this unless you're me!
251
252         require_once(INSTALLDIR.'/lib/api.php');
253
254         $act = new ApiAction('/dev/null');
255
256         $arr = $act->twitterStatusArray($notice, true);
257         $arr['url'] = $notice->bestUrl();
258         $arr['html'] = htmlspecialchars($notice->rendered);
259         $arr['source'] = htmlspecialchars($arr['source']);
260
261         if (!empty($notice->reply_to)) {
262             $reply_to = Notice::staticGet('id', $notice->reply_to);
263             if (!empty($reply_to)) {
264                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
265             }
266             $reply_to = null;
267         }
268
269         $profile = $notice->getProfile();
270         $arr['user']['profile_url'] = $profile->profileurl;
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     // Push this up to Plugin
296
297     function log($level, $msg)
298     {
299         common_log($level, get_class($this) . ': '.$msg);
300     }
301
302     function _getScripts()
303     {
304         return array('plugins/Realtime/realtimeupdate.js',
305                      'plugins/Realtime/json2.js');
306     }
307
308     function _updateInitialize($timeline, $user_id)
309     {
310         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
311     }
312
313     function _connect()
314     {
315     }
316
317     function _publish($timeline, $json)
318     {
319     }
320
321     function _disconnect()
322     {
323     }
324
325     function _pathToChannel($path)
326     {
327         return '';
328     }
329
330     function _getTimeline($action)
331     {
332         $path = null;
333         $timeline = null;
334
335         $action_name = $action->trimmed('action');
336
337         switch ($action_name) {
338          case 'public':
339             $path = array('public');
340             break;
341          case 'tag':
342             $tag = $action->trimmed('tag');
343             if (!empty($tag)) {
344                 $path = array('tag', $tag);
345             }
346             break;
347          case 'showstream':
348          case 'all':
349          case 'replies':
350          case 'showgroup':
351             $nickname = common_canonical_nickname($action->trimmed('nickname'));
352             if (!empty($nickname)) {
353                 $path = array($action_name, $nickname);
354             }
355             break;
356          default:
357             break;
358         }
359
360         if (!empty($path)) {
361             $timeline = $this->_pathToChannel($path);
362         }
363
364         return $timeline;
365     }
366 }