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