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 $iconurl = $base.'plugins/Realtime/icon_external.gif';
104 $action->elementStart('script', array('type' => 'text/javascript'));
106 $script = ' $(document).ready(function() { '.
107 $this->_updateInitialize($timeline, $user_id).
108 ' RealtimeUpdate.addPopup("'.$url.'", "'.$title.'", "'. $iconurl .'");'
111 $action->raw($script);
113 $action->elementEnd('script');
118 function onEndNoticeSave($notice)
122 // Add to the author's timeline
124 $user = User::staticGet('id', $notice->profile_id);
127 $paths[] = array('showstream', $user->nickname);
130 // Add to the public timeline
132 if ($notice->is_local ||
133 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
134 $paths[] = array('public');
137 // Add to the tags timeline
139 $tags = $this->getNoticeTags($notice);
142 foreach ($tags as $tag) {
143 $paths[] = array('tag', $tag);
147 // Add to inbox timelines
150 $inbox = new Notice_inbox();
151 $inbox->notice_id = $notice->id;
153 if ($inbox->find()) {
154 while ($inbox->fetch()) {
155 $user = User::staticGet('id', $inbox->user_id);
156 $paths[] = array('all', $user->nickname);
160 // Add to the replies timeline
162 $reply = new Reply();
163 $reply->notice_id = $notice->id;
165 if ($reply->find()) {
166 while ($reply->fetch()) {
167 $user = User::staticGet('id', $reply->profile_id);
169 $paths[] = array('replies', $user->nickname);
174 // Add to the group timeline
177 $gi = new Group_inbox();
178 $gi->notice_id = $notice->id;
181 while ($gi->fetch()) {
182 $ug = User_group::staticGet('id', $gi->group_id);
183 $paths[] = array('showgroup', $ug->nickname);
187 if (count($paths) > 0) {
189 $json = $this->noticeAsJson($notice);
193 foreach ($paths as $path) {
194 $timeline = $this->_pathToChannel($path);
195 $this->_publish($timeline, $json);
198 $this->_disconnect();
204 function onStartShowBody($action)
206 $realtime = $action->boolean('realtime');
211 $action->elementStart('body',
212 (common_current_user()) ? array('id' => $action->trimmed('action'),
213 'class' => 'user_in')
214 : array('id' => $action->trimmed('action')));
216 $action->elementStart('div', array('id' => 'header'));
217 // XXX hack to deal with JS that tries to get the
218 // root url from page output
219 $action->elementStart('address');
220 $action->element('a', array('class' => 'url',
221 'href' => common_local_url('public')),
223 $action->elementEnd('address');
225 if (common_logged_in()) {
226 $action->showNoticeForm();
228 $action->elementEnd('div');
230 $action->showContentBlock();
231 $action->elementEnd('body');
232 return false; // No default processing
235 function noticeAsJson($notice)
237 // FIXME: this code should be abstracted to a neutral third
238 // party, like Notice::asJson(). I'm not sure of the ethics
239 // of refactoring from within a plugin, so I'm just abusing
240 // the TwitterApiAction method. Don't do this unless you're me!
242 require_once(INSTALLDIR.'/lib/twitterapi.php');
244 $act = new TwitterApiAction('/dev/null');
246 $arr = $act->twitter_status_array($notice, true);
247 $arr['url'] = $notice->bestUrl();
248 $arr['html'] = htmlspecialchars($notice->rendered);
249 $arr['source'] = htmlspecialchars($arr['source']);
251 if (!empty($notice->reply_to)) {
252 $reply_to = Notice::staticGet('id', $notice->reply_to);
253 if (!empty($reply_to)) {
254 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
259 $profile = $notice->getProfile();
260 $arr['user']['profile_url'] = $profile->profileurl;
265 function getNoticeTags($notice)
269 $nt = new Notice_tag();
270 $nt->notice_id = $notice->id;
274 while ($nt->fetch()) {
285 // Push this up to Plugin
287 function log($level, $msg)
289 common_log($level, get_class($this) . ': '.$msg);
292 function _getScripts()
294 return array('plugins/Realtime/realtimeupdate.js',
295 'plugins/Realtime/json2.js');
298 function _updateInitialize($timeline, $user_id)
300 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
307 function _publish($timeline, $json)
311 function _disconnect()
315 function _pathToChannel($path)
320 function _getTimeline($action)
325 $action_name = $action->trimmed('action');
327 switch ($action_name) {
329 $path = array('public');
332 $tag = $action->trimmed('tag');
334 $path = array('tag', $tag);
341 $nickname = common_canonical_nickname($action->trimmed('nickname'));
342 if (!empty($nickname)) {
343 $path = array($action_name, $nickname);
351 $timeline = $this->_pathToChannel($path);