/**
* Laconica, the distributed open-source microblogging tool
*
- * Plugin to do "real time" updates using Comet/Bayeux
+ * Superclass for plugins that do "real time" updates of timelines using Ajax
*
* PHP version 5
*
}
/**
- * Plugin to do realtime updates using Comet
+ * Superclass for plugin to do realtime updates
+ *
+ * Based on experience with the Comet and Meteor plugins,
+ * this superclass extracts out some of the common functionality
*
* @category Plugin
* @package Laconica
* @link http://laconi.ca/
*/
-class CometPlugin extends Plugin
+class RealtimePlugin extends Plugin
{
- var $server = null;
+ protected $replyurl = null;
+ protected $favorurl = null;
+ protected $deleteurl = null;
- function __construct($server=null, $username=null, $password=null)
+ function onInitializePlugin()
{
- $this->server = $server;
- $this->username = $username;
- $this->password = $password;
-
- parent::__construct();
+ $this->replyurl = common_local_url('newnotice');
+ $this->favorurl = common_local_url('favor');
+ // FIXME: need to find a better way to pass this pattern in
+ $this->deleteurl = common_local_url('deletenotice',
+ array('notice' => '0000000000'));
}
function onEndShowScripts($action)
{
$timeline = null;
- $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
-
switch ($action->trimmed('action')) {
case 'public':
$timeline = '/timelines/public';
return true;
}
- $scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js');
+ $scripts = $this->_getScripts();
foreach ($scripts as $script) {
$action->element('script', array('type' => 'text/javascript',
- 'src' => common_path('plugins/Comet/'.$script)),
+ 'src' => $script),
' ');
}
$user_id = 0;
}
- $replyurl = common_local_url('newnotice');
- $favorurl = common_local_url('favor');
- // FIXME: need to find a better way to pass this pattern in
- $deleteurl = common_local_url('deletenotice',
- array('notice' => '0000000000'));
-
$action->elementStart('script', array('type' => 'text/javascript'));
- $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
+ $action->raw("$(document).ready(function() { ");
+ $action->raw($this->_updateInitialize($timeline, $user_id));
+ $action->raw(" });");
$action->elementEnd('script');
return true;
function onEndNoticeSave($notice)
{
- $this->log(LOG_INFO, "Called for save notice.");
-
$timelines = array();
// XXX: Add other timelines; this is just for the public one
}
if (count($timelines) > 0) {
- // Require this, since we need it
- require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
$json = $this->noticeAsJson($notice);
- // Bayeux? Comet? Huh? These terms confuse me
- $bay = new Bayeux($this->server, $this->user, $this->password);
+ $this->_connect();
foreach ($timelines as $timeline) {
- $this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'.");
- $bay->publish($timeline, $json);
+ $this->_publish($timeline, $json);
}
- $bay = NULL;
+ $this->_disconnect();
}
return true;
{
common_log($level, get_class($this) . ': '.$msg);
}
+
+ function _getScripts()
+ {
+ return array(common_local_path('plugins/Realtime/realtimeupdater.js'));
+ }
+
+ function _updateInitialize()
+ {
+ return '; ';
+ }
+
+ function _connect()
+ {
+ }
+
+ function _publish()
+ {
+ }
+
+ function _disconnect()
+ {
+ }
}
-// update the local timeline from a Comet server
+// add a notice encoded as JSON into the current timeline
//
-var updater = function()
-{
- var _server;
- var _timeline;
- var _userid;
- var _replyurl;
- var _favorurl;
- var _deleteurl;
- var _cometd;
-
- return {
- init: function(server, timeline, userid, replyurl, favorurl, deleteurl)
- {
- _cometd = $.cometd; // Uses the default Comet object
- _cometd.setLogLevel('debug');
- _cometd.init(server);
- _server = server;
- _timeline = timeline;
- _userid = userid;
- _favorurl = favorurl;
- _replyurl = replyurl;
- _deleteurl = deleteurl;
- _cometd.subscribe(timeline, receive);
- $(window).unload(leave);
- }
- }
-
- function leave()
- {
- _cometd.disconnect();
- }
+RealtimeUpdate = {
- function receive(message)
+ receive: function(message)
{
- id = message.data.id;
+ id = data.id;
// Don't add it if it already exists
return;
}
- var noticeItem = makeNoticeItem(message.data);
+ var noticeItem = RealtimeUpdate.makeNoticeItem(data);
$("#notices_primary .notices").prepend(noticeItem, true);
$("#notices_primary .notice:first").css({display:"none"});
$("#notices_primary .notice:first").fadeIn(1000);
NoticeReply();
}
- function makeNoticeItem(data)
+ makeNoticeItem: function(data)
{
user = data['user'];
html = data['html'].replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
if (_userid != 0) {
var input = $("form#form_notice fieldset input#token");
var session_key = input.val();
- ni = ni+makeFavoriteForm(data['id'], session_key);
- ni = ni+makeReplyLink(data['id'], data['user']['screen_name']);
+ ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
+ ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
if (_userid == data['user']['id']) {
- ni = ni+makeDeleteLink(data['id']);
+ ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
}
}
return ni;
}
- function makeFavoriteForm(id, session_key)
+ makeFavoriteForm: function(id, session_key)
{
var ff;
return ff;
}
- function makeReplyLink(id, nickname)
+ makeReplyLink: function(id, nickname)
{
var rl;
rl = "<dl class=\"notice_reply\">"+
return rl;
}
- function makeDeleteLink(id)
+ makeDeleteLink: function(id)
{
var dl, delurl;
delurl = _deleteurl.replace("0000000000", id);
return dl;
}
-}();
+};