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 $scripts = $this->_getScripts();
81 foreach ($scripts as $script) {
82 $action->script($script);
85 $user = common_current_user();
87 if (!empty($user->id)) {
93 $action->elementStart('script', array('type' => 'text/javascript'));
95 $script = ' $(document).ready(function() { '.
96 $this->_updateInitialize($timeline, $user_id).
99 $action->raw($script);
101 $action->elementEnd('script');
106 function onEndNoticeSave($notice)
110 // Add to the author's timeline
112 $user = User::staticGet('id', $notice->profile_id);
115 $paths[] = array('showstream', $user->nickname);
118 // Add to the public timeline
120 if ($notice->is_local ||
121 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
122 $paths[] = array('public');
125 // Add to the tags timeline
127 $tags = $this->getNoticeTags($notice);
130 foreach ($tags as $tag) {
131 $paths[] = array('tag', $tag);
135 // Add to inbox timelines
138 $inbox = new Notice_inbox();
139 $inbox->notice_id = $notice->id;
141 if ($inbox->find()) {
142 while ($inbox->fetch()) {
143 $user = User::staticGet('id', $inbox->user_id);
144 $paths[] = array('all', $user->nickname);
148 // Add to the replies timeline
150 $reply = new Reply();
151 $reply->notice_id = $notice->id;
153 if ($reply->find()) {
154 while ($reply->fetch()) {
155 $user = User::staticGet('id', $reply->profile_id);
157 $paths[] = array('replies', $user->nickname);
162 // Add to the group timeline
165 $gi = new Group_inbox();
166 $gi->notice_id = $notice->id;
169 while ($gi->fetch()) {
170 $ug = User_group::staticGet('id', $gi->group_id);
171 $paths[] = array('showgroup', $ug->nickname);
175 if (count($paths) > 0) {
177 $json = $this->noticeAsJson($notice);
181 foreach ($paths as $path) {
182 $timeline = $this->_pathToChannel($path);
183 $this->_publish($timeline, $json);
186 $this->_disconnect();
192 function onStartShowPageNotice($action)
194 $timeline = $this->_getTimeline($action);
195 if (!empty($timeline)) {
196 $base = $action->selfUrl();
197 if (mb_strstr($url, '?')) {
198 $url = $base . '&realtime=1';
200 $url = $base . '?realtime=1';
202 $title = $action->title();
203 $code = "window.open('$url', '$title', 'toolbar=no,resizable=yes,scrollbars=yes,status=yes,height=640,width=575');";
204 $action->element('a', array('href' => $base,
206 'id' => 'realtime_timeline',
207 'title' => _('Pop up')),
214 function onStartShowBody($action)
216 $realtime = $action->boolean('realtime');
221 $action->elementStart('body',
222 (common_current_user()) ? array('id' => $action->trimmed('action'),
223 'class' => 'user_in')
224 : array('id' => $action->trimmed('action')));
226 // XXX hack to deal with JS that tries to get the
227 // root url from page output
229 $action->elementStart('address');
230 $action->element('a', array('class' => 'url',
231 'href' => common_local_url('public')),
233 $action->elementEnd('address');
235 if (common_logged_in()) {
236 $action->showNoticeForm();
238 $action->showContent();
239 $action->elementEnd('body');
240 return false; // No default processing
243 function noticeAsJson($notice)
245 // FIXME: this code should be abstracted to a neutral third
246 // party, like Notice::asJson(). I'm not sure of the ethics
247 // of refactoring from within a plugin, so I'm just abusing
248 // the TwitterApiAction method. Don't do this unless you're me!
250 require_once(INSTALLDIR.'/lib/twitterapi.php');
252 $act = new TwitterApiAction('/dev/null');
254 $arr = $act->twitter_status_array($notice, true);
255 $arr['url'] = $notice->bestUrl();
256 $arr['html'] = htmlspecialchars($notice->rendered);
257 $arr['source'] = htmlspecialchars($arr['source']);
259 if (!empty($notice->reply_to)) {
260 $reply_to = Notice::staticGet('id', $notice->reply_to);
261 if (!empty($reply_to)) {
262 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
267 $profile = $notice->getProfile();
268 $arr['user']['profile_url'] = $profile->profileurl;
273 function getNoticeTags($notice)
277 $nt = new Notice_tag();
278 $nt->notice_id = $notice->id;
282 while ($nt->fetch()) {
293 // Push this up to Plugin
295 function log($level, $msg)
297 common_log($level, get_class($this) . ': '.$msg);
300 function _getScripts()
302 return array('plugins/Realtime/realtimeupdate.js',
303 'plugins/Realtime/json2.js');
306 function _updateInitialize($timeline, $user_id)
308 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
315 function _publish($timeline, $json)
319 function _disconnect()
323 function _pathToChannel($path)
328 function _getTimeline($action)
333 $action_name = $action->trimmed('action');
335 switch ($action_name) {
337 $path = array('public');
340 $tag = $action->trimmed('tag');
342 $path = array('tag', $tag);
349 $nickname = common_canonical_nickname($action->trimmed('nickname'));
350 if (!empty($nickname)) {
351 $path = array($action_name, $nickname);
359 $timeline = $this->_pathToChannel($path);