3 * Laconica, the distributed open-source microblogging tool
5 * Plugin to do "real time" updates using Comet/Bayeux
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@controlyourself.ca>
25 * @copyright 2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
35 * Plugin to do realtime updates using Comet
39 * @author Evan Prodromou <evan@controlyourself.ca>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://laconi.ca/
44 class CometPlugin extends Plugin
48 function __construct($server=null)
50 $this->server = $server;
52 parent::__construct();
55 function onEndShowScripts($action)
59 $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
61 switch ($action->trimmed('action')) {
63 $timeline = '/timelines/public';
66 $tag = $action->trimmed('tag');
68 $timeline = '/timelines/tag/'.$tag;
77 $scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js');
79 foreach ($scripts as $script) {
80 $action->element('script', array('type' => 'text/javascript',
81 'src' => common_path('plugins/Comet/'.$script)),
85 $user = common_current_user();
87 if (!empty($user->id)) {
93 $replyurl = common_local_url('newnotice');
94 $favorurl = common_local_url('favor');
95 // FIXME: need to find a better way to pass this pattern in
96 $deleteurl = common_local_url('deletenotice',
97 array('notice' => '0000000000'));
99 $action->elementStart('script', array('type' => 'text/javascript'));
100 $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
101 $action->elementEnd('script');
106 function onEndNoticeSave($notice)
108 $this->log(LOG_INFO, "Called for save notice.");
110 $timelines = array();
112 // XXX: Add other timelines; this is just for the public one
114 if ($notice->is_local ||
115 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
116 $timelines[] = '/timelines/public';
119 $tags = $this->getNoticeTags($notice);
122 foreach ($tags as $tag) {
123 $timelines[] = '/timelines/tag/' . $tag;
127 if (count($timelines) > 0) {
128 // Require this, since we need it
129 require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
131 $json = $this->noticeAsJson($notice);
133 // Bayeux? Comet? Huh? These terms confuse me
134 $bay = new Bayeux($this->server);
136 foreach ($timelines as $timeline) {
137 $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
138 $bay->publish($timeline, $json);
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);
162 $profile = $notice->getProfile();
163 $arr['user']['profile_url'] = $profile->profileurl;
168 function getNoticeTags($notice)
172 $nt = new Notice_tag();
173 $nt->notice_id = $notice->id;
177 while ($nt->fetch()) {
188 // Push this up to Plugin
190 function log($level, $msg)
192 common_log($level, get_class($this) . ': '.$msg);