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 $this->repeaturl = common_local_url('repeat');
63 // FIXME: need to find a better way to pass this pattern in
64 $this->deleteurl = common_local_url('deletenotice',
65 array('notice' => '0000000000'));
69 function onEndShowScripts($action)
71 $timeline = $this->_getTimeline($action);
73 // If there's not a timeline on this page,
76 if (empty($timeline)) {
80 $base = $action->selfUrl();
81 if (mb_strstr($base, '?')) {
82 $url = $base . '&realtime=1';
84 $url = $base . '?realtime=1';
87 $scripts = $this->_getScripts();
89 foreach ($scripts as $script) {
90 $action->script(common_path($script));
93 $user = common_current_user();
95 if (!empty($user->id)) {
101 if ($action->boolean('realtime')) {
102 $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
105 $pluginPath = common_path('plugins/Realtime/');
106 $realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
109 $script = ' $(document).ready(function() { '.
111 $this->_updateInitialize($timeline, $user_id).
113 $action->inlineScript($script);
118 function onEndShowStatusNetStyles($action)
120 $action->cssLink(common_path('plugins/Realtime/realtimeupdate.css'),
121 null, 'screen, projection, tv');
125 function onHandleQueuedNotice($notice)
129 // Add to the author's timeline
131 $user = User::staticGet('id', $notice->profile_id);
134 $paths[] = array('showstream', $user->nickname);
137 // Add to the public timeline
139 if ($notice->is_local == Notice::LOCAL_PUBLIC ||
140 ($notice->is_local == Notice::REMOTE_OMB && !common_config('public', 'localonly'))) {
141 $paths[] = array('public');
144 // Add to the tags timeline
146 $tags = $this->getNoticeTags($notice);
149 foreach ($tags as $tag) {
150 $paths[] = array('tag', $tag);
154 // Add to inbox timelines
157 $ni = $notice->whoGets();
159 foreach (array_keys($ni) as $user_id) {
160 $user = User::staticGet('id', $user_id);
161 $paths[] = array('all', $user->nickname);
164 // Add to the replies timeline
166 $reply = new Reply();
167 $reply->notice_id = $notice->id;
169 if ($reply->find()) {
170 while ($reply->fetch()) {
171 $user = User::staticGet('id', $reply->profile_id);
173 $paths[] = array('replies', $user->nickname);
178 // Add to the group timeline
181 $gi = new Group_inbox();
182 $gi->notice_id = $notice->id;
185 while ($gi->fetch()) {
186 $ug = User_group::staticGet('id', $gi->group_id);
187 $paths[] = array('showgroup', $ug->nickname);
191 if (count($paths) > 0) {
193 $json = $this->noticeAsJson($notice);
197 foreach ($paths as $path) {
198 $timeline = $this->_pathToChannel($path);
199 $this->_publish($timeline, $json);
202 $this->_disconnect();
208 function onStartShowBody($action)
210 $realtime = $action->boolean('realtime');
215 $action->elementStart('body',
216 (common_current_user()) ? array('id' => $action->trimmed('action'),
217 'class' => 'user_in realtime-popup')
218 : array('id' => $action->trimmed('action'),
219 'class'=> 'realtime-popup'));
221 // XXX hack to deal with JS that tries to get the
222 // root url from page output
224 $action->elementStart('address');
225 $action->element('a', array('class' => 'url',
226 'href' => common_local_url('public')),
228 $action->elementEnd('address');
230 if (common_logged_in()) {
231 $action->showNoticeForm();
234 $action->showContentBlock();
235 $action->showScripts();
236 $action->elementEnd('body');
237 return false; // No default processing
240 function noticeAsJson($notice)
242 // FIXME: this code should be abstracted to a neutral third
243 // party, like Notice::asJson(). I'm not sure of the ethics
244 // of refactoring from within a plugin, so I'm just abusing
245 // the ApiAction method. Don't do this unless you're me!
247 require_once(INSTALLDIR.'/lib/api.php');
249 $act = new ApiAction('/dev/null');
251 $arr = $act->twitterStatusArray($notice, true);
252 $arr['url'] = $notice->bestUrl();
253 $arr['html'] = htmlspecialchars($notice->rendered);
254 $arr['source'] = htmlspecialchars($arr['source']);
256 if (!empty($notice->reply_to)) {
257 $reply_to = Notice::staticGet('id', $notice->reply_to);
258 if (!empty($reply_to)) {
259 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
264 $profile = $notice->getProfile();
265 $arr['user']['profile_url'] = $profile->profileurl;
267 // Add needed repeat data
269 if (!empty($notice->repeat_of)) {
270 $original = Notice::staticGet('id', $notice->repeat_of);
271 if (!empty($original)) {
272 $arr['retweeted_status']['url'] = $original->bestUrl();
273 $arr['retweeted_status']['html'] = htmlspecialchars($original->rendered);
274 $arr['retweeted_status']['source'] = htmlspecialchars($original->source);
275 $originalProfile = $original->getProfile();
276 $arr['retweeted_status']['user']['profile_url'] = $originalProfile->profileurl;
277 if (!empty($original->reply_to)) {
278 $originalReply = Notice::staticGet('id', $original->reply_to);
279 $arr['retweeted_status']['in_reply_to_status_url'] = $originalReply->bestUrl();
288 function getNoticeTags($notice)
292 $nt = new Notice_tag();
293 $nt->notice_id = $notice->id;
297 while ($nt->fetch()) {
308 function _getScripts()
310 return array('plugins/Realtime/realtimeupdate.js');
313 function _updateInitialize($timeline, $user_id)
315 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->repeaturl\", \"$this->deleteurl\"); ";
322 function _publish($timeline, $json)
326 function _disconnect()
330 function _pathToChannel($path)
335 function _getTimeline($action)
340 $action_name = $action->trimmed('action');
342 switch ($action_name) {
344 $path = array('public');
347 $tag = $action->trimmed('tag');
349 $path = array('tag', $tag);
356 $nickname = common_canonical_nickname($action->trimmed('nickname'));
357 if (!empty($nickname)) {
358 $path = array($action_name, $nickname);
366 $timeline = $this->_pathToChannel($path);