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 switch ($action->trimmed('action')) {
61 $timeline = '/timelines/public';
67 $action->element('script', array('type' => 'text/javascript',
68 'src' => common_path('plugins/Comet/jquery.comet.js')),
70 $action->elementStart('script', array('type' => 'text/javascript'));
71 $action->raw("var _timelineServer = \"$this->server\"; ".
72 "var _timeline = \"$timeline\";");
73 $action->elementEnd('script');
74 $action->element('script', array('type' => 'text/javascript',
75 'src' => common_path('plugins/Comet/updatetimeline.js')),
80 function onEndNoticeSave($notice)
82 $this->log(LOG_INFO, "Called for save notice.");
86 // XXX: Add other timelines; this is just for the public one
88 if ($notice->is_local ||
89 ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
90 $timelines[] = '/timelines/public';
93 if (count($timelines) > 0) {
94 // Require this, since we need it
95 require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
97 $json = $this->noticeAsJson($notice);
99 $this->log(LOG_DEBUG, "JSON = '$json'");
101 // Bayeux? Comet? Huh? These terms confuse me
102 $bay = new Bayeux($this->server);
104 foreach ($timelines as $timeline) {
105 $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
106 $bay->publish($timeline, $json);
107 $this->log(LOG_DEBUG, "Done posting notice $notice->id to '$timeline'.");
113 $this->log(LOG_DEBUG, "All done.");
117 function noticeAsJson($notice)
119 // FIXME: this code should be abstracted to a neutral third
120 // party, like Notice::asJson(). I'm not sure of the ethics
121 // of refactoring from within a plugin, so I'm just abusing
122 // the TwitterApiAction method. Don't do this unless you're me!
124 require_once(INSTALLDIR.'/lib/twitterapi.php');
126 $act = new TwitterApiAction('/dev/null');
128 $arr = $act->twitter_status_array($notice, true);
132 // Push this up to Plugin
134 function log($level, $msg)
136 common_log($level, get_class($this) . ': '.$msg);