3 * StatusNet, the distributed open-source microblogging tool
5 * Superclass for plugins that do "real time" updates of timelines using Ajax
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * Superclass for plugin to do realtime updates
37 * Based on experience with the Comet and Meteor plugins,
38 * this superclass extracts out some of the common functionality
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/
47 class RealtimePlugin extends Plugin
49 protected $replyurl = null;
50 protected $favorurl = null;
51 protected $deleteurl = null;
54 * When it's time to initialize the plugin, calculate and
55 * pass the URLs we need.
58 function onInitializePlugin()
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'));
68 function onEndShowScripts($action)
70 $timeline = $this->_getTimeline($action);
72 // If there's not a timeline on this page,
75 if (empty($timeline)) {
79 $base = $action->selfUrl();
80 if (mb_strstr($url, '?')) {
81 $url = $base . '&realtime=1';
83 $url = $base . '?realtime=1';
86 $title = $action->title();
88 $scripts = $this->_getScripts();
90 foreach ($scripts as $script) {
91 $action->script($script);
94 $user = common_current_user();
96 if (!empty($user->id)) {
102 // FIXME: Need to check if the current URL is a poped realtime window
104 $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
107 // FIXME: This icon URL is no good if fancy URLs are off.
108 $iconurl = $base.'plugins/Realtime/icon_external.gif';
109 $realtimeUI = ' RealtimeUpdate.addPopup("'.$url.'", "'.$title.'", "'. $iconurl .'");';
112 $action->elementStart('script', array('type' => 'text/javascript'));
114 $script = ' $(document).ready(function() { '.
115 $this->_updateInitialize($timeline, $user_id).
118 $action->raw($script);
120 $action->elementEnd('script');
125 function onEndNoticeSave($notice)
129 // Add to the author's timeline
131 $user = User::staticGet('id', $notice->profile_id);
134 $paths[] = array('showstream', $user->nickname);
137 // Add to the public timeline
139 if ($notice->is_local ||
140 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
141 $paths[] = array('public');
144 // Add to the tags timeline
146 $tags = $this->getNoticeTags($notice);
149 foreach ($tags as $tag) {
150 $paths[] = array('tag', $tag);
154 // Add to inbox timelines
157 $inbox = new Notice_inbox();
158 $inbox->notice_id = $notice->id;
160 if ($inbox->find()) {
161 while ($inbox->fetch()) {
162 $user = User::staticGet('id', $inbox->user_id);
163 $paths[] = array('all', $user->nickname);
167 // Add to the replies timeline
169 $reply = new Reply();
170 $reply->notice_id = $notice->id;
172 if ($reply->find()) {
173 while ($reply->fetch()) {
174 $user = User::staticGet('id', $reply->profile_id);
176 $paths[] = array('replies', $user->nickname);
181 // Add to the group timeline
184 $gi = new Group_inbox();
185 $gi->notice_id = $notice->id;
188 while ($gi->fetch()) {
189 $ug = User_group::staticGet('id', $gi->group_id);
190 $paths[] = array('showgroup', $ug->nickname);
194 if (count($paths) > 0) {
196 $json = $this->noticeAsJson($notice);
200 foreach ($paths as $path) {
201 $timeline = $this->_pathToChannel($path);
202 $this->_publish($timeline, $json);
205 $this->_disconnect();
211 function onStartShowBody($action)
213 $realtime = $action->boolean('realtime');
218 $action->elementStart('body',
219 (common_current_user()) ? array('id' => $action->trimmed('action'),
220 'class' => 'user_in')
221 : array('id' => $action->trimmed('action')));
223 $action->elementStart('div', array('id' => 'header'));
224 // XXX hack to deal with JS that tries to get the
225 // root url from page output
226 $action->elementStart('address');
227 $action->element('a', array('class' => 'url',
228 'href' => common_local_url('public')),
230 $action->elementEnd('address');
232 if (common_logged_in()) {
233 $action->showNoticeForm();
235 $action->elementEnd('div');
237 $action->showContentBlock();
238 $action->elementEnd('body');
239 return false; // No default processing
242 function noticeAsJson($notice)
244 // FIXME: this code should be abstracted to a neutral third
245 // party, like Notice::asJson(). I'm not sure of the ethics
246 // of refactoring from within a plugin, so I'm just abusing
247 // the TwitterApiAction method. Don't do this unless you're me!
249 require_once(INSTALLDIR.'/lib/twitterapi.php');
251 $act = new TwitterApiAction('/dev/null');
253 $arr = $act->twitter_status_array($notice, true);
254 $arr['url'] = $notice->bestUrl();
255 $arr['html'] = htmlspecialchars($notice->rendered);
256 $arr['source'] = htmlspecialchars($arr['source']);
258 if (!empty($notice->reply_to)) {
259 $reply_to = Notice::staticGet('id', $notice->reply_to);
260 if (!empty($reply_to)) {
261 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
266 $profile = $notice->getProfile();
267 $arr['user']['profile_url'] = $profile->profileurl;
272 function getNoticeTags($notice)
276 $nt = new Notice_tag();
277 $nt->notice_id = $notice->id;
281 while ($nt->fetch()) {
292 // Push this up to Plugin
294 function log($level, $msg)
296 common_log($level, get_class($this) . ': '.$msg);
299 function _getScripts()
301 return array('plugins/Realtime/realtimeupdate.js',
302 'plugins/Realtime/json2.js');
305 function _updateInitialize($timeline, $user_id)
307 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
314 function _publish($timeline, $json)
318 function _disconnect()
322 function _pathToChannel($path)
327 function _getTimeline($action)
332 $action_name = $action->trimmed('action');
334 switch ($action_name) {
336 $path = array('public');
339 $tag = $action->trimmed('tag');
341 $path = array('tag', $tag);
348 $nickname = common_canonical_nickname($action->trimmed('nickname'));
349 if (!empty($nickname)) {
350 $path = array($action_name, $nickname);
358 $timeline = $this->_pathToChannel($path);