]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
make RealtimePlugin work correctly
authorEvan Prodromou <evan@controlyourself.ca>
Tue, 14 Jul 2009 19:26:39 +0000 (15:26 -0400)
committerEvan Prodromou <evan@controlyourself.ca>
Tue, 14 Jul 2009 19:26:39 +0000 (15:26 -0400)
plugins/Realtime/RealtimePlugin.php
plugins/Realtime/realtimeupdate.js

index 45251c66f04c5384127329352aa9f6fe67e52723..cb3b30845ed612986682410e2c9c3dd9dc3bf48e 100644 (file)
@@ -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()
+    {
+    }
 }
index 170949e9bacaba300783a7590d4474c67d7ab04f..9ad8087405e66a7e6089965ff09e86f5d90ecba7 100644 (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()
-{
-     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(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/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;
      }
-}();
+};