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 onEndShowScripts($action)
65 $timeline = $this->_getTimeline($action);
67 // If there's not a timeline on this page,
70 if (empty($timeline)) {
74 $base = $action->selfUrl();
75 if (mb_strstr($base, '?')) {
76 $url = $base . '&realtime=1';
78 $url = $base . '?realtime=1';
81 $scripts = $this->_getScripts();
83 foreach ($scripts as $script) {
84 $action->script($script);
87 $user = common_current_user();
89 if (!empty($user->id)) {
95 if ($action->boolean('realtime')) {
96 $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
99 $pluginPath = common_path('plugins/Realtime/');
100 $realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
103 $script = ' $(document).ready(function() { '.
105 $this->_updateInitialize($timeline, $user_id).
107 $action->inlineScript($script);
112 function onEndShowStatusNetStyles($action)
114 $action->cssLink(Plugin::staticPath('Realtime', 'realtimeupdate.css'),
116 'screen, projection, tv');
120 function onHandleQueuedNotice($notice)
124 // Add to the author's timeline
126 $user = User::staticGet('id', $notice->profile_id);
129 $paths[] = array('showstream', $user->nickname);
132 // Add to the public timeline
134 if ($notice->is_local == Notice::LOCAL_PUBLIC ||
135 ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
136 $paths[] = array('public');
139 // Add to the tags timeline
141 $tags = $this->getNoticeTags($notice);
144 foreach ($tags as $tag) {
145 $paths[] = array('tag', $tag);
149 // Add to inbox timelines
152 $ni = $notice->whoGets();
154 foreach (array_keys($ni) as $user_id) {
155 $user = User::staticGet('id', $user_id);
156 $paths[] = array('all', $user->nickname);
159 // Add to the replies timeline
161 $reply = new Reply();
162 $reply->notice_id = $notice->id;
164 if ($reply->find()) {
165 while ($reply->fetch()) {
166 $user = User::staticGet('id', $reply->profile_id);
168 $paths[] = array('replies', $user->nickname);
173 // Add to the group timeline
176 $gi = new Group_inbox();
177 $gi->notice_id = $notice->id;
180 while ($gi->fetch()) {
181 $ug = User_group::staticGet('id', $gi->group_id);
182 $paths[] = array('showgroup', $ug->nickname);
186 if (count($paths) > 0) {
188 $json = $this->noticeAsJson($notice);
192 foreach ($paths as $path) {
193 $timeline = $this->_pathToChannel($path);
194 $this->_publish($timeline, $json);
197 $this->_disconnect();
203 function onStartShowBody($action)
205 $realtime = $action->boolean('realtime');
210 $action->elementStart('body',
211 (common_current_user()) ? array('id' => $action->trimmed('action'),
212 'class' => 'user_in realtime-popup')
213 : array('id' => $action->trimmed('action'),
214 'class'=> 'realtime-popup'));
216 // XXX hack to deal with JS that tries to get the
217 // 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();
229 $action->showContentBlock();
230 $action->showScripts();
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 ApiAction method. Don't do this unless you're me!
242 $act = new ApiAction('/dev/null');
244 $arr = $act->twitterStatusArray($notice, true);
245 $arr['url'] = $notice->bestUrl();
246 $arr['html'] = htmlspecialchars($notice->rendered);
247 $arr['source'] = htmlspecialchars($arr['source']);
248 $arr['conversation_url'] = $this->getConversationUrl($notice);
250 $profile = $notice->getProfile();
251 $arr['user']['profile_url'] = $profile->profileurl;
253 // Add needed repeat data
255 if (!empty($notice->repeat_of)) {
256 $original = Notice::staticGet('id', $notice->repeat_of);
257 if (!empty($original)) {
258 $arr['retweeted_status']['url'] = $original->bestUrl();
259 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
260 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
261 $originalProfile = $original->getProfile();
262 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
263 $arr['retweeted_status']['conversation_url'] = $this->getConversationUrl($original);
271 function getNoticeTags($notice)
275 $nt = new Notice_tag();
276 $nt->notice_id = $notice->id;
280 while ($nt->fetch()) {
291 function getConversationUrl($notice)
295 if ($notice->hasConversation()) {
296 $conv = Conversation::staticGet(
298 $notice->conversation
300 $convurl = $conv->uri;
302 if(empty($convurl)) {
304 "Couldn't find Conversation ID %d to make 'in context'"
305 . "link for Notice ID %d",
306 $notice->conversation,
310 common_log(LOG_WARNING, $msg);
312 $convurl .= '#notice-' . $notice->id;
319 function _getScripts()
321 if (common_config('site', 'minify')) {
322 $js = 'realtimeupdate.min.js';
324 $js = 'realtimeupdate.js';
326 return array(Plugin::staticPath('Realtime', $js));
330 * Export any i18n messages that need to be loaded at runtime...
332 * @param Action $action
333 * @param array $messages
335 * @return boolean hook return value
337 function onEndScriptMessages($action, &$messages)
339 // TRANS: Text label for realtime view "play" button, usually replaced by an icon.
340 $messages['realtime_play'] = _m('BUTTON', 'Play');
341 // TRANS: Tooltip for realtime view "play" button.
342 $messages['realtime_play_tooltip'] = _m('TOOLTIP', 'Play');
343 // TRANS: Text label for realtime view "pause" button
344 $messages['realtime_pause'] = _m('BUTTON', 'Pause');
345 // TRANS: Tooltip for realtime view "pause" button
346 $messages['realtime_pause_tooltip'] = _m('TOOLTIP', 'Pause');
347 // TRANS: Text label for realtime view "popup" button, usually replaced by an icon.
348 $messages['realtime_popup'] = _m('BUTTON', 'Pop up');
349 // TRANS: Tooltip for realtime view "popup" button.
350 $messages['realtime_popup_tooltip'] = _m('TOOLTIP', 'Pop up in a window');
355 function _updateInitialize($timeline, $user_id)
357 return "RealtimeUpdate.init($user_id, \"$this->showurl\"); ";
364 function _publish($timeline, $json)
368 function _disconnect()
372 function _pathToChannel($path)
377 function _getTimeline($action)
382 $action_name = $action->trimmed('action');
384 switch ($action_name) {
386 $path = array('public');
389 $tag = $action->trimmed('tag');
391 $path = array('tag', $tag);
398 $nickname = common_canonical_nickname($action->trimmed('nickname'));
399 if (!empty($nickname)) {
400 $path = array($action_name, $nickname);
408 $timeline = $this->_pathToChannel($path);