make RealtimePlugin work correctly

This commit is contained in:
Evan Prodromou 2009-07-14 15:26:39 -04:00
parent 7a84b349a2
commit 86533d99d7
2 changed files with 58 additions and 73 deletions

View File

@ -2,7 +2,7 @@
/** /**
* Laconica, the distributed open-source microblogging tool * 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 * PHP version 5
* *
@ -32,7 +32,10 @@ if (!defined('LACONICA')) {
} }
/** /**
* 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 * @category Plugin
* @package Laconica * @package Laconica
@ -41,25 +44,25 @@ if (!defined('LACONICA')) {
* @link http://laconi.ca/ * @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->replyurl = common_local_url('newnotice');
$this->username = $username; $this->favorurl = common_local_url('favor');
$this->password = $password; // FIXME: need to find a better way to pass this pattern in
$this->deleteurl = common_local_url('deletenotice',
parent::__construct(); array('notice' => '0000000000'));
} }
function onEndShowScripts($action) function onEndShowScripts($action)
{ {
$timeline = null; $timeline = null;
$this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
switch ($action->trimmed('action')) { switch ($action->trimmed('action')) {
case 'public': case 'public':
$timeline = '/timelines/public'; $timeline = '/timelines/public';
@ -76,11 +79,11 @@ class CometPlugin extends Plugin
return true; return true;
} }
$scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js'); $scripts = $this->_getScripts();
foreach ($scripts as $script) { foreach ($scripts as $script) {
$action->element('script', array('type' => 'text/javascript', $action->element('script', array('type' => 'text/javascript',
'src' => common_path('plugins/Comet/'.$script)), 'src' => $script),
' '); ' ');
} }
@ -92,14 +95,10 @@ class CometPlugin extends Plugin
$user_id = 0; $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->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'); $action->elementEnd('script');
return true; return true;
@ -107,8 +106,6 @@ class CometPlugin extends Plugin
function onEndNoticeSave($notice) function onEndNoticeSave($notice)
{ {
$this->log(LOG_INFO, "Called for save notice.");
$timelines = array(); $timelines = array();
// XXX: Add other timelines; this is just for the public one // XXX: Add other timelines; this is just for the public one
@ -127,20 +124,16 @@ class CometPlugin extends Plugin
} }
if (count($timelines) > 0) { if (count($timelines) > 0) {
// Require this, since we need it
require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
$json = $this->noticeAsJson($notice); $json = $this->noticeAsJson($notice);
// Bayeux? Comet? Huh? These terms confuse me $this->_connect();
$bay = new Bayeux($this->server, $this->user, $this->password);
foreach ($timelines as $timeline) { foreach ($timelines as $timeline) {
$this->log(LOG_INFO, "Posting notice $notice->id to '$timeline'."); $this->_publish($timeline, $json);
$bay->publish($timeline, $json);
} }
$bay = NULL; $this->_disconnect();
} }
return true; return true;
@ -202,4 +195,26 @@ class CometPlugin extends Plugin
{ {
common_log($level, get_class($this) . ': '.$msg); 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()
{
}
} }

View File

@ -1,41 +1,11 @@
// update the local timeline from a Comet server // add a notice encoded as JSON into the current timeline
// //
var updater = function() RealtimeUpdate = {
{
var _server;
var _timeline;
var _userid;
var _replyurl;
var _favorurl;
var _deleteurl;
var _cometd;
return { receive: function(message)
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(); id = data.id;
}
function receive(message)
{
id = message.data.id;
// Don't add it if it already exists // Don't add it if it already exists
@ -43,7 +13,7 @@ var updater = function()
return; return;
} }
var noticeItem = makeNoticeItem(message.data); var noticeItem = RealtimeUpdate.makeNoticeItem(data);
$("#notices_primary .notices").prepend(noticeItem, true); $("#notices_primary .notices").prepend(noticeItem, true);
$("#notices_primary .notice:first").css({display:"none"}); $("#notices_primary .notice:first").css({display:"none"});
$("#notices_primary .notice:first").fadeIn(1000); $("#notices_primary .notice:first").fadeIn(1000);
@ -51,7 +21,7 @@ var updater = function()
NoticeReply(); NoticeReply();
} }
function makeNoticeItem(data) makeNoticeItem: function(data)
{ {
user = data['user']; user = data['user'];
html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"'); html = data['html'].replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"');
@ -96,10 +66,10 @@ var updater = function()
if (_userid != 0) { if (_userid != 0) {
var input = $("form#form_notice fieldset input#token"); var input = $("form#form_notice fieldset input#token");
var session_key = input.val(); var session_key = input.val();
ni = ni+makeFavoriteForm(data['id'], session_key); ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key);
ni = ni+makeReplyLink(data['id'], data['user']['screen_name']); ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']);
if (_userid == data['user']['id']) { if (_userid == data['user']['id']) {
ni = ni+makeDeleteLink(data['id']); ni = ni+RealtimeUpdate.makeDeleteLink(data['id']);
} }
} }
@ -108,7 +78,7 @@ var updater = function()
return ni; return ni;
} }
function makeFavoriteForm(id, session_key) makeFavoriteForm: function(id, session_key)
{ {
var ff; var ff;
@ -123,7 +93,7 @@ var updater = function()
return ff; return ff;
} }
function makeReplyLink(id, nickname) makeReplyLink: function(id, nickname)
{ {
var rl; var rl;
rl = "<dl class=\"notice_reply\">"+ rl = "<dl class=\"notice_reply\">"+
@ -136,7 +106,7 @@ var updater = function()
return rl; return rl;
} }
function makeDeleteLink(id) makeDeleteLink: function(id)
{ {
var dl, delurl; var dl, delurl;
delurl = _deleteurl.replace("0000000000", id); delurl = _deleteurl.replace("0000000000", id);
@ -150,5 +120,5 @@ var updater = function()
return dl; return dl;
} }
}(); };