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($base, '?')) {
81 $url = $base . '&realtime=1';
83 $url = $base . '?realtime=1';
86 $scripts = $this->_getScripts();
88 foreach ($scripts as $script) {
89 $action->script($script);
92 $user = common_current_user();
94 if (!empty($user->id)) {
100 if ($action->boolean('realtime')) {
101 $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
104 $iconurl = common_path('plugins/Realtime/icon_external.gif');
105 $realtimeUI = ' RealtimeUpdate.addPopup("'.$url.'", "'.$timeline.'", "'. $iconurl .'");';
108 $action->elementStart('script', array('type' => 'text/javascript'));
110 $script = ' $(document).ready(function() { '.
112 $this->_updateInitialize($timeline, $user_id).
114 $action->raw($script);
116 $action->elementEnd('script');
121 function onEndNoticeSave($notice)
125 // Add to the author's timeline
127 $user = User::staticGet('id', $notice->profile_id);
130 $paths[] = array('showstream', $user->nickname);
133 // Add to the public timeline
135 if ($notice->is_local ||
136 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
137 $paths[] = array('public');
140 // Add to the tags timeline
142 $tags = $this->getNoticeTags($notice);
145 foreach ($tags as $tag) {
146 $paths[] = array('tag', $tag);
150 // Add to inbox timelines
153 $inbox = new Notice_inbox();
154 $inbox->notice_id = $notice->id;
156 if ($inbox->find()) {
157 while ($inbox->fetch()) {
158 $user = User::staticGet('id', $inbox->user_id);
159 $paths[] = array('all', $user->nickname);
163 // Add to the replies timeline
165 $reply = new Reply();
166 $reply->notice_id = $notice->id;
168 if ($reply->find()) {
169 while ($reply->fetch()) {
170 $user = User::staticGet('id', $reply->profile_id);
172 $paths[] = array('replies', $user->nickname);
177 // Add to the group timeline
180 $gi = new Group_inbox();
181 $gi->notice_id = $notice->id;
184 while ($gi->fetch()) {
185 $ug = User_group::staticGet('id', $gi->group_id);
186 $paths[] = array('showgroup', $ug->nickname);
190 if (count($paths) > 0) {
192 $json = $this->noticeAsJson($notice);
196 foreach ($paths as $path) {
197 $timeline = $this->_pathToChannel($path);
198 $this->_publish($timeline, $json);
201 $this->_disconnect();
207 function onStartShowBody($action)
209 $realtime = $action->boolean('realtime');
214 $action->elementStart('body',
215 (common_current_user()) ? array('id' => $action->trimmed('action'),
216 'class' => 'user_in')
217 : array('id' => $action->trimmed('action')));
219 // XXX hack to deal with JS that tries to get the
220 // root url from page output
222 $action->elementStart('address');
223 $action->element('a', array('class' => 'url',
224 'href' => common_local_url('public')),
226 $action->elementEnd('address');
228 if (common_logged_in()) {
229 $action->showNoticeForm();
232 $action->showContentBlock();
233 $action->showScripts();
234 $action->elementEnd('body');
235 return false; // No default processing
238 function noticeAsJson($notice)
240 // FIXME: this code should be abstracted to a neutral third
241 // party, like Notice::asJson(). I'm not sure of the ethics
242 // of refactoring from within a plugin, so I'm just abusing
243 // the ApiAction method. Don't do this unless you're me!
245 require_once(INSTALLDIR.'/lib/api.php');
247 $act = new ApiAction('/dev/null');
249 $arr = $act->twitterStatusArray($notice, true);
250 $arr['url'] = $notice->bestUrl();
251 $arr['html'] = htmlspecialchars($notice->rendered);
252 $arr['source'] = htmlspecialchars($arr['source']);
254 if (!empty($notice->reply_to)) {
255 $reply_to = Notice::staticGet('id', $notice->reply_to);
256 if (!empty($reply_to)) {
257 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
262 $profile = $notice->getProfile();
263 $arr['user']['profile_url'] = $profile->profileurl;
268 function getNoticeTags($notice)
272 $nt = new Notice_tag();
273 $nt->notice_id = $notice->id;
277 while ($nt->fetch()) {
288 // Push this up to Plugin
290 function log($level, $msg)
292 common_log($level, get_class($this) . ': '.$msg);
295 function _getScripts()
297 return array('plugins/Realtime/realtimeupdate.js',
298 'plugins/Realtime/json2.js');
301 function _updateInitialize($timeline, $user_id)
303 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
310 function _publish($timeline, $json)
314 function _disconnect()
318 function _pathToChannel($path)
323 function _getTimeline($action)
328 $action_name = $action->trimmed('action');
330 switch ($action_name) {
332 $path = array('public');
335 $tag = $action->trimmed('tag');
337 $path = array('tag', $tag);
344 $nickname = common_canonical_nickname($action->trimmed('nickname'));
345 if (!empty($nickname)) {
346 $path = array($action_name, $nickname);
354 $timeline = $this->_pathToChannel($path);