diff options
author | Evan Prodromou <evan@controlyourself.ca> | 2009-07-14 15:26:39 -0400 |
---|---|---|
committer | Evan Prodromou <evan@controlyourself.ca> | 2009-07-14 15:26:39 -0400 |
commit | 86533d99d760b7d6b3a6a047898eec5e25497138 (patch) | |
tree | 887b44d4197af7ce13932e4d075e34179c3c015b | |
parent | 7a84b349a2f6ee36f4813bf2828c5bf2399d1614 (diff) |
make RealtimePlugin work correctly
-rw-r--r-- | plugins/Realtime/RealtimePlugin.php | 75 | ||||
-rw-r--r-- | plugins/Realtime/realtimeupdate.js | 56 |
2 files changed, 58 insertions, 73 deletions
diff --git a/plugins/Realtime/RealtimePlugin.php b/plugins/Realtime/RealtimePlugin.php index 45251c66f..cb3b30845 100644 --- a/plugins/Realtime/RealtimePlugin.php +++ b/plugins/Realtime/RealtimePlugin.php @@ -2,7 +2,7 @@ /** * 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 * @@ -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 * @package Laconica @@ -41,25 +44,25 @@ if (!defined('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'; @@ -76,11 +79,11 @@ class CometPlugin extends Plugin 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), ' '); } @@ -92,14 +95,10 @@ class CometPlugin extends Plugin $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; @@ -107,8 +106,6 @@ class CometPlugin extends Plugin function onEndNoticeSave($notice) { - $this->log(LOG_INFO, "Called for save notice."); - $timelines = array(); // XXX: Add other timelines; this is just for the public one @@ -127,20 +124,16 @@ class CometPlugin extends Plugin } 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; @@ -202,4 +195,26 @@ class CometPlugin extends Plugin { 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() + { + } } diff --git a/plugins/Realtime/realtimeupdate.js b/plugins/Realtime/realtimeupdate.js index 170949e9b..9ad808740 100644 --- a/plugins/Realtime/realtimeupdate.js +++ b/plugins/Realtime/realtimeupdate.js @@ -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() -{ - 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 @@ -43,7 +13,7 @@ var updater = function() 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); @@ -51,7 +21,7 @@ var updater = function() NoticeReply(); } - function makeNoticeItem(data) + makeNoticeItem: function(data) { user = data['user']; html = data['html'].replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); @@ -96,10 +66,10 @@ var updater = function() 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']); } } @@ -108,7 +78,7 @@ var updater = function() return ni; } - function makeFavoriteForm(id, session_key) + makeFavoriteForm: function(id, session_key) { var ff; @@ -123,7 +93,7 @@ var updater = function() return ff; } - function makeReplyLink(id, nickname) + makeReplyLink: function(id, nickname) { var rl; rl = "<dl class=\"notice_reply\">"+ @@ -136,7 +106,7 @@ var updater = function() return rl; } - function makeDeleteLink(id) + makeDeleteLink: function(id) { var dl, delurl; delurl = _deleteurl.replace("0000000000", id); @@ -150,5 +120,5 @@ var updater = function() return dl; } -}(); +}; |