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;
53 function onInitializePlugin()
55 $this->replyurl = common_local_url('newnotice');
56 $this->favorurl = common_local_url('favor');
57 // FIXME: need to find a better way to pass this pattern in
58 $this->deleteurl = common_local_url('deletenotice',
59 array('notice' => '0000000000'));
62 function onEndShowScripts($action)
66 $a = $action->trimmed('action');
69 case 'public': case 'all': case 'replies': case 'showstream':
73 $tag = $action->trimmed('tag');
75 $path = array('tag', $tag);
84 $timeline = $this->_pathToChannel($path);
86 $scripts = $this->_getScripts();
88 foreach ($scripts as $script) {
89 $action->script($script);
92 $user = common_current_user();
94 if (!empty($user->id)) {
100 $action->elementStart('script', array('type' => 'text/javascript'));
101 $action->raw("$(document).ready(function() { ");
102 $action->raw($this->_updateInitialize($timeline, $user_id));
103 $action->raw(" });");
104 $action->elementEnd('script');
109 function onEndNoticeSave($notice)
113 // TODO: Replies timeline
115 if ($notice->is_local ||
116 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
117 foreach (array('public', 'all', 'replies', 'showstream') as $a) {
118 $paths[] = array($a);
122 $tags = $this->getNoticeTags($notice);
125 foreach ($tags as $tag) {
126 $paths[] = array('tag', $tag);
130 if (count($paths) > 0) {
132 $json = $this->noticeAsJson($notice);
136 foreach ($paths as $path) {
137 $timeline = $this->_pathToChannel($path);
138 $this->_publish($timeline, $json);
141 $this->_disconnect();
147 function noticeAsJson($notice)
149 // FIXME: this code should be abstracted to a neutral third
150 // party, like Notice::asJson(). I'm not sure of the ethics
151 // of refactoring from within a plugin, so I'm just abusing
152 // the TwitterApiAction method. Don't do this unless you're me!
154 require_once(INSTALLDIR.'/lib/twitterapi.php');
156 $act = new TwitterApiAction('/dev/null');
158 $arr = $act->twitter_status_array($notice, true);
159 $arr['url'] = $notice->bestUrl();
160 $arr['html'] = htmlspecialchars($notice->rendered);
161 $arr['source'] = htmlspecialchars($arr['source']);
163 if (!empty($notice->reply_to)) {
164 $reply_to = Notice::staticGet('id', $notice->reply_to);
165 if (!empty($reply_to)) {
166 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
171 $profile = $notice->getProfile();
172 $arr['user']['profile_url'] = $profile->profileurl;
177 function getNoticeTags($notice)
181 $nt = new Notice_tag();
182 $nt->notice_id = $notice->id;
186 while ($nt->fetch()) {
197 // Push this up to Plugin
199 function log($level, $msg)
201 common_log($level, get_class($this) . ': '.$msg);
204 function _getScripts()
206 return array('plugins/Realtime/realtimeupdate.js',
207 'plugins/Realtime/json2.js');
210 function _updateInitialize($timeline, $user_id)
212 return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
219 function _publish($timeline, $json)
223 function _disconnect()
227 function _pathToChannel($path)