]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/RealtimePlugin.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.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
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             $iconurl = common_path('plugins/Realtime/icon_external.gif');
105             $realtimeUI = ' RealtimeUpdate.addPopup("'.$url.'", "'.$timeline.'", "'. $iconurl .'");';
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 onEndNoticeSave($notice)
122     {
123         $paths = array();
124
125         // Add to the author's timeline
126
127         $user = User::staticGet('id', $notice->profile_id);
128
129         if (!empty($user)) {
130             $paths[] = array('showstream', $user->nickname);
131         }
132
133         // Add to the public timeline
134
135         if ($notice->is_local ||
136             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
137             $paths[] = array('public');
138         }
139
140         // Add to the tags timeline
141
142         $tags = $this->getNoticeTags($notice);
143
144         if (!empty($tags)) {
145             foreach ($tags as $tag) {
146                 $paths[] = array('tag', $tag);
147             }
148         }
149
150         // Add to inbox timelines
151         // XXX: do a join
152
153         $inbox = new Notice_inbox();
154         $inbox->notice_id = $notice->id;
155
156         if ($inbox->find()) {
157             while ($inbox->fetch()) {
158                 $user = User::staticGet('id', $inbox->user_id);
159                 $paths[] = array('all', $user->nickname);
160             }
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')
217                               : array('id' => $action->trimmed('action')));
218
219         $action->elementStart('div', array('id' => 'header'));
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         $action->elementEnd('div');
234
235         $action->showContentBlock();
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 TwitterApiAction method. Don't do this unless you're me!
246
247         require_once(INSTALLDIR.'/lib/twitterapi.php');
248
249         $act = new TwitterApiAction('/dev/null');
250
251         $arr = $act->twitter_status_array($notice, true);
252         $arr['url'] = $notice->bestUrl();
253         $arr['html'] = htmlspecialchars($notice->rendered);
254         $arr['source'] = htmlspecialchars($arr['source']);
255
256         if (!empty($notice->reply_to)) {
257             $reply_to = Notice::staticGet('id', $notice->reply_to);
258             if (!empty($reply_to)) {
259                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
260             }
261             $reply_to = null;
262         }
263
264         $profile = $notice->getProfile();
265         $arr['user']['profile_url'] = $profile->profileurl;
266
267         return $arr;
268     }
269
270     function getNoticeTags($notice)
271     {
272         $tags = null;
273
274         $nt = new Notice_tag();
275         $nt->notice_id = $notice->id;
276
277         if ($nt->find()) {
278             $tags = array();
279             while ($nt->fetch()) {
280                 $tags[] = $nt->tag;
281             }
282         }
283
284         $nt->free();
285         $nt = null;
286
287         return $tags;
288     }
289
290     // Push this up to Plugin
291
292     function log($level, $msg)
293     {
294         common_log($level, get_class($this) . ': '.$msg);
295     }
296
297     function _getScripts()
298     {
299         return array('plugins/Realtime/realtimeupdate.js',
300                      'plugins/Realtime/json2.js');
301     }
302
303     function _updateInitialize($timeline, $user_id)
304     {
305         return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
306     }
307
308     function _connect()
309     {
310     }
311
312     function _publish($timeline, $json)
313     {
314     }
315
316     function _disconnect()
317     {
318     }
319
320     function _pathToChannel($path)
321     {
322         return '';
323     }
324
325     function _getTimeline($action)
326     {
327         $path = null;
328         $timeline = null;
329
330         $action_name = $action->trimmed('action');
331
332         switch ($action_name) {
333          case 'public':
334             $path = array('public');
335             break;
336          case 'tag':
337             $tag = $action->trimmed('tag');
338             if (!empty($tag)) {
339                 $path = array('tag', $tag);
340             }
341             break;
342          case 'showstream':
343          case 'all':
344          case 'replies':
345          case 'showgroup':
346             $nickname = common_canonical_nickname($action->trimmed('nickname'));
347             if (!empty($nickname)) {
348                 $path = array($action_name, $nickname);
349             }
350             break;
351          default:
352             break;
353         }
354
355         if (!empty($path)) {
356             $timeline = $this->_pathToChannel($path);
357         }
358
359         return $timeline;
360     }
361 }