]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
hack around address hack in util.js
[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         $scripts = $this->_getScripts();
80
81         foreach ($scripts as $script) {
82             $action->script($script);
83         }
84
85         $user = common_current_user();
86
87         if (!empty($user->id)) {
88             $user_id = $user->id;
89         } else {
90             $user_id = 0;
91         }
92
93         $action->elementStart('script', array('type' => 'text/javascript'));
94
95         $script = ' $(document).ready(function() { '.
96           $this->_updateInitialize($timeline, $user_id).
97           '}); ';
98
99         $action->raw($script);
100
101         $action->elementEnd('script');
102
103         return true;
104     }
105
106     function onEndNoticeSave($notice)
107     {
108         $paths = array();
109
110         // Add to the author's timeline
111
112         $user = User::staticGet('id', $notice->profile_id);
113
114         if (!empty($user)) {
115             $paths[] = array('showstream', $user->nickname);
116         }
117
118         // Add to the public timeline
119
120         if ($notice->is_local ||
121             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
122             $paths[] = array('public');
123         }
124
125         // Add to the tags timeline
126
127         $tags = $this->getNoticeTags($notice);
128
129         if (!empty($tags)) {
130             foreach ($tags as $tag) {
131                 $paths[] = array('tag', $tag);
132             }
133         }
134
135         // Add to inbox timelines
136         // XXX: do a join
137
138         $inbox = new Notice_inbox();
139         $inbox->notice_id = $notice->id;
140
141         if ($inbox->find()) {
142             while ($inbox->fetch()) {
143                 $user = User::staticGet('id', $inbox->user_id);
144                 $paths[] = array('all', $user->nickname);
145             }
146         }
147
148         // Add to the replies timeline
149
150         $reply = new Reply();
151         $reply->notice_id = $notice->id;
152
153         if ($reply->find()) {
154             while ($reply->fetch()) {
155                 $user = User::staticGet('id', $reply->profile_id);
156                 if (!empty($user)) {
157                     $paths[] = array('replies', $user->nickname);
158                 }
159             }
160         }
161
162         // Add to the group timeline
163         // XXX: join
164
165         $gi = new Group_inbox();
166         $gi->notice_id = $notice->id;
167
168         if ($gi->find()) {
169             while ($gi->fetch()) {
170                 $ug = User_group::staticGet('id', $gi->group_id);
171                 $paths[] = array('showgroup', $ug->nickname);
172             }
173         }
174
175         if (count($paths) > 0) {
176
177             $json = $this->noticeAsJson($notice);
178
179             $this->_connect();
180
181             foreach ($paths as $path) {
182                 $timeline = $this->_pathToChannel($path);
183                 $this->_publish($timeline, $json);
184             }
185
186             $this->_disconnect();
187         }
188
189         return true;
190     }
191
192     function onStartShowPageNotice($action)
193     {
194         $timeline = $this->_getTimeline($action);
195         if (!empty($timeline)) {
196             $base = $action->selfUrl();
197             if (mb_strstr($url, '?')) {
198                 $url = $base . '&realtime=1';
199             } else {
200                 $url = $base . '?realtime=1';
201             }
202             $title = $action->title();
203             $code = "window.open('$url', '$title', 'toolbar=no,resizable=yes,scrollbars=yes,status=yes,height=640,width=575');";
204             $action->element('a', array('href' => $base,
205                                         'onclick' => $code,
206                                         'id' => 'realtime_timeline',
207                                         'title' => _('Pop up')),
208                              'Pop up');
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         $action->showContent();
239         $action->elementEnd('body');
240         return false; // No default processing
241     }
242
243     function noticeAsJson($notice)
244     {
245         // FIXME: this code should be abstracted to a neutral third
246         // party, like Notice::asJson(). I'm not sure of the ethics
247         // of refactoring from within a plugin, so I'm just abusing
248         // the TwitterApiAction method. Don't do this unless you're me!
249
250         require_once(INSTALLDIR.'/lib/twitterapi.php');
251
252         $act = new TwitterApiAction('/dev/null');
253
254         $arr = $act->twitter_status_array($notice, true);
255         $arr['url'] = $notice->bestUrl();
256         $arr['html'] = htmlspecialchars($notice->rendered);
257         $arr['source'] = htmlspecialchars($arr['source']);
258
259         if (!empty($notice->reply_to)) {
260             $reply_to = Notice::staticGet('id', $notice->reply_to);
261             if (!empty($reply_to)) {
262                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
263             }
264             $reply_to = null;
265         }
266
267         $profile = $notice->getProfile();
268         $arr['user']['profile_url'] = $profile->profileurl;
269
270         return $arr;
271     }
272
273     function getNoticeTags($notice)
274     {
275         $tags = null;
276
277         $nt = new Notice_tag();
278         $nt->notice_id = $notice->id;
279
280         if ($nt->find()) {
281             $tags = array();
282             while ($nt->fetch()) {
283                 $tags[] = $nt->tag;
284             }
285         }
286
287         $nt->free();
288         $nt = null;
289
290         return $tags;
291     }
292
293     // Push this up to Plugin
294
295     function log($level, $msg)
296     {
297         common_log($level, get_class($this) . ': '.$msg);
298     }
299
300     function _getScripts()
301     {
302         return array('plugins/Realtime/realtimeupdate.js',
303                      'plugins/Realtime/json2.js');
304     }
305
306     function _updateInitialize($timeline, $user_id)
307     {
308         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
309     }
310
311     function _connect()
312     {
313     }
314
315     function _publish($timeline, $json)
316     {
317     }
318
319     function _disconnect()
320     {
321     }
322
323     function _pathToChannel($path)
324     {
325         return '';
326     }
327
328     function _getTimeline($action)
329     {
330         $path = null;
331         $timeline = null;
332
333         $action_name = $action->trimmed('action');
334
335         switch ($action_name) {
336          case 'public':
337             $path = array('public');
338             break;
339          case 'tag':
340             $tag = $action->trimmed('tag');
341             if (!empty($tag)) {
342                 $path = array('tag', $tag);
343             }
344             break;
345          case 'showstream':
346          case 'all':
347          case 'replies':
348          case 'showgroup':
349             $nickname = common_canonical_nickname($action->trimmed('nickname'));
350             if (!empty($nickname)) {
351                 $path = array($action_name, $nickname);
352             }
353             break;
354          default:
355             break;
356         }
357
358         if (!empty($path)) {
359             $timeline = $this->_pathToChannel($path);
360         }
361
362         return $timeline;
363     }
364 }