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 $action->elementStart('script', array('type' => 'text/javascript'));
104 $script = ' $(document).ready(function() { '.
105 $this->_updateInitialize($timeline, $user_id).
106 ' RealtimeUpdate.addPopup("'.$url.'", "'.$title.'"); '.
109 $action->raw($script);
111 $action->elementEnd('script');
116 function onEndNoticeSave($notice)
120 // Add to the author's timeline
122 $user = User::staticGet('id', $notice->profile_id);
125 $paths[] = array('showstream', $user->nickname);
128 // Add to the public timeline
130 if ($notice->is_local ||
131 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
132 $paths[] = array('public');
135 // Add to the tags timeline
137 $tags = $this->getNoticeTags($notice);
140 foreach ($tags as $tag) {
141 $paths[] = array('tag', $tag);
145 // Add to inbox timelines
148 $inbox = new Notice_inbox();
149 $inbox->notice_id = $notice->id;
151 if ($inbox->find()) {
152 while ($inbox->fetch()) {
153 $user = User::staticGet('id', $inbox->user_id);
154 $paths[] = array('all', $user->nickname);
158 // Add to the replies timeline
160 $reply = new Reply();
161 $reply->notice_id = $notice->id;
163 if ($reply->find()) {
164 while ($reply->fetch()) {
165 $user = User::staticGet('id', $reply->profile_id);
167 $paths[] = array('replies', $user->nickname);
172 // Add to the group timeline
175 $gi = new Group_inbox();
176 $gi->notice_id = $notice->id;
179 while ($gi->fetch()) {
180 $ug = User_group::staticGet('id', $gi->group_id);
181 $paths[] = array('showgroup', $ug->nickname);
185 if (count($paths) > 0) {
187 $json = $this->noticeAsJson($notice);
191 foreach ($paths as $path) {
192 $timeline = $this->_pathToChannel($path);
193 $this->_publish($timeline, $json);
196 $this->_disconnect();
202 function onStartShowBody($action)
204 $realtime = $action->boolean('realtime');
209 $action->elementStart('body',
210 (common_current_user()) ? array('id' => $action->trimmed('action'),
211 'class' => 'user_in')
212 : array('id' => $action->trimmed('action')));
214 // XXX hack to deal with JS that tries to get the
215 // root url from page output
217 $action->elementStart('address');
218 $action->element('a', array('class' => 'url',
219 'href' => common_local_url('public')),
221 $action->elementEnd('address');
223 if (common_logged_in()) {
224 $action->showNoticeForm();
226 $action->showContent();
227 $action->elementEnd('body');
228 return false; // No default processing
231 function noticeAsJson($notice)
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!
238 require_once(INSTALLDIR.'/lib/twitterapi.php');
240 $act = new TwitterApiAction('/dev/null');
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']);
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();
255 $profile = $notice->getProfile();
256 $arr['user']['profile_url'] = $profile->profileurl;
261 function getNoticeTags($notice)
265 $nt = new Notice_tag();
266 $nt->notice_id = $notice->id;
270 while ($nt->fetch()) {
281 // Push this up to Plugin
283 function log($level, $msg)
285 common_log($level, get_class($this) . ': '.$msg);
288 function _getScripts()
290 return array('plugins/Realtime/realtimeupdate.js',
291 'plugins/Realtime/json2.js');
294 function _updateInitialize($timeline, $user_id)
296 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
303 function _publish($timeline, $json)
307 function _disconnect()
311 function _pathToChannel($path)
316 function _getTimeline($action)
321 $action_name = $action->trimmed('action');
323 switch ($action_name) {
325 $path = array('public');
328 $tag = $action->trimmed('tag');
330 $path = array('tag', $tag);
337 $nickname = common_canonical_nickname($action->trimmed('nickname'));
338 if (!empty($nickname)) {
339 $path = array($action_name, $nickname);
347 $timeline = $this->_pathToChannel($path);