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/
46 class RealtimePlugin extends Plugin
48 protected $showurl = null;
51 * When it's time to initialize the plugin, calculate and
52 * pass the URLs we need.
55 function onInitializePlugin()
57 // FIXME: need to find a better way to pass this pattern in
58 $this->showurl = common_local_url('shownotice',
59 array('notice' => '0000000000'));
63 function onCheckSchema()
65 $schema = Schema::get();
66 $schema->ensureTable('realtime_channel', Realtime_channel::schemaDef());
70 function onAutoload($cls)
72 $dir = dirname(__FILE__);
76 case 'Realtime_channel':
77 include_once $dir.'/'.$cls.'.php';
84 function onEndShowScripts($action)
86 $timeline = $this->_getTimeline($action);
88 // If there's not a timeline on this page,
91 if (empty($timeline)) {
95 $base = $action->selfUrl();
96 if (mb_strstr($base, '?')) {
97 $url = $base . '&realtime=1';
99 $url = $base . '?realtime=1';
102 $scripts = $this->_getScripts();
104 foreach ($scripts as $script) {
105 $action->script($script);
108 $user = common_current_user();
110 if (!empty($user->id)) {
111 $user_id = $user->id;
116 if ($action->boolean('realtime')) {
117 $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
120 $pluginPath = common_path('plugins/Realtime/');
121 $realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
124 $script = ' $(document).ready(function() { '.
126 $this->_updateInitialize($timeline, $user_id).
128 $action->inlineScript($script);
133 function onEndShowStatusNetStyles($action)
135 $action->cssLink(Plugin::staticPath('Realtime', 'realtimeupdate.css'),
137 'screen, projection, tv');
141 function onHandleQueuedNotice($notice)
145 // Add to the author's timeline
147 $user = User::staticGet('id', $notice->profile_id);
150 $paths[] = array('showstream', $user->nickname, null);
153 // Add to the public timeline
155 if ($notice->is_local == Notice::LOCAL_PUBLIC ||
156 ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
157 $paths[] = array('public', null, null);
160 // Add to the tags timeline
162 $tags = $this->getNoticeTags($notice);
165 foreach ($tags as $tag) {
166 $paths[] = array('tag', $tag, null);
170 // Add to inbox timelines
173 $ni = $notice->whoGets();
175 foreach (array_keys($ni) as $user_id) {
176 $user = User::staticGet('id', $user_id);
177 $paths[] = array('all', $user->nickname, null);
180 // Add to the replies timeline
182 $reply = new Reply();
183 $reply->notice_id = $notice->id;
185 if ($reply->find()) {
186 while ($reply->fetch()) {
187 $user = User::staticGet('id', $reply->profile_id);
189 $paths[] = array('replies', $user->nickname, null);
194 // Add to the group timeline
197 $gi = new Group_inbox();
198 $gi->notice_id = $notice->id;
201 while ($gi->fetch()) {
202 $ug = User_group::staticGet('id', $gi->group_id);
203 $paths[] = array('showgroup', $ug->nickname, null);
207 if (count($paths) > 0) {
209 $json = $this->noticeAsJson($notice);
213 foreach ($paths as $path) {
215 list($action, $arg1, $arg2) = $path;
217 $channels = Realtime_channel::getAllChannels($action, $arg1, $arg2);
219 foreach ($channels as $channel) {
220 if (is_null($channel->user_id)) {
223 $profile = Profile::staticGet('id', $channel->user_id);
225 if ($notice->inScope($profile)) {
226 $timeline = $this->_pathToChannel(array($channel->channel_key));
227 $this->_publish($timeline, $json);
232 $this->_disconnect();
238 function onStartShowBody($action)
240 $realtime = $action->boolean('realtime');
245 $action->elementStart('body',
246 (common_current_user()) ? array('id' => $action->trimmed('action'),
247 'class' => 'user_in realtime-popup')
248 : array('id' => $action->trimmed('action'),
249 'class'=> 'realtime-popup'));
251 // XXX hack to deal with JS that tries to get the
252 // root url from page output
254 $action->elementStart('address');
255 $action->element('a', array('class' => 'url',
256 'href' => common_local_url('public')),
258 $action->elementEnd('address');
260 $action->showContentBlock();
261 $action->showScripts();
262 $action->elementEnd('body');
263 return false; // No default processing
266 function noticeAsJson($notice)
268 // FIXME: this code should be abstracted to a neutral third
269 // party, like Notice::asJson(). I'm not sure of the ethics
270 // of refactoring from within a plugin, so I'm just abusing
271 // the ApiAction method. Don't do this unless you're me!
273 $act = new ApiAction('/dev/null');
275 $arr = $act->twitterStatusArray($notice, true);
276 $arr['url'] = $notice->bestUrl();
277 $arr['html'] = htmlspecialchars($notice->rendered);
278 $arr['source'] = htmlspecialchars($arr['source']);
279 $arr['conversation_url'] = $this->getConversationUrl($notice);
281 $profile = $notice->getProfile();
282 $arr['user']['profile_url'] = $profile->profileurl;
284 // Add needed repeat data
286 if (!empty($notice->repeat_of)) {
287 $original = Notice::staticGet('id', $notice->repeat_of);
288 if (!empty($original)) {
289 $arr['retweeted_status']['url'] = $original->bestUrl();
290 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
291 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
292 $originalProfile = $original->getProfile();
293 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
294 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
302 function getNoticeTags($notice)
306 $nt = new Notice_tag();
307 $nt->notice_id = $notice->id;
311 while ($nt->fetch()) {
322 function getConversationUrl($notice)
326 if ($notice->hasConversation()) {
327 $conv = Conversation::staticGet(
329 $notice->conversation
331 $convurl = $conv->uri;
333 if(empty($convurl)) {
335 "Couldn't find Conversation ID %d to make 'in context'"
336 . "link for Notice ID %d",
337 $notice->conversation,
341 common_log(LOG_WARNING, $msg);
343 $convurl .= '#notice-' . $notice->id;
350 function _getScripts()
352 if (common_config('site', 'minify')) {
353 $js = 'realtimeupdate.min.js';
355 $js = 'realtimeupdate.js';
357 return array(Plugin::staticPath('Realtime', $js));
361 * Export any i18n messages that need to be loaded at runtime...
363 * @param Action $action
364 * @param array $messages
366 * @return boolean hook return value
368 function onEndScriptMessages($action, &$messages)
370 // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
371 $messages['realtime_play'] = _m('BUTTON', 'Play');
372 // TRANS: Tooltip for realtime view "play" button.
373 $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
374 // TRANS: Text label for realtime view "pause" button
375 $messages['realtime_pause'] = _m('BUTTON', 'Pause');
376 // TRANS: Tooltip for realtime view "pause" button
377 $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
378 // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
379 $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
380 // TRANS: Tooltip for realtime view "popup" button.
381 $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
386 function _updateInitialize($timeline, $user_id)
388 return "RealtimeUpdate.init($user_id, \"$this->showurl\"); ";
395 function _publish($timeline, $json)
399 function _disconnect()
403 function _pathToChannel($path)
408 function _getTimeline($action)
414 $action_name = $action->trimmed('action');
418 // FIXME: profile + tag
420 switch ($action_name) {
425 $tag = $action->trimmed('tag');
429 $this->log(LOG_NOTICE, "Unexpected 'tag' action without tag argument");
437 $nickname = common_canonical_nickname($action->trimmed('nickname'));
438 if (!empty($nickname)) {
441 $this->log(LOG_NOTICE, "Unexpected $action_name action without nickname argument.");
449 $user = common_current_user();
451 $user_id = (!empty($user)) ? $user->id : null;
453 $channel = Realtime_channel::getChannel($user_id,
458 if (!empty($channel)) {
459 $timeline = $this->_pathToChannel(array($channel->channel_key));