]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Realtime/RealtimePlugin.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / plugins / Realtime / RealtimePlugin.php
index 45251c66f04c5384127329352aa9f6fe67e52723..0f0d0f9f42309b134298220d3fc1d148e3ae2ed7 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, 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
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Plugin
- * @package   Laconica
- * @author    Evan Prodromou <evan@controlyourself.ca>
- * @copyright 2009 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
 /**
- * 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
- * @author   Evan Prodromou <evan@controlyourself.ca>
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://laconi.ca/
+ * @link     http://status.net/
  */
 
-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)
-    {
-        $this->server   = $server;
-        $this->username = $username;
-        $this->password = $password;
+    /**
+     * When it's time to initialize the plugin, calculate and
+     * pass the URLs we need.
+     */
 
-        parent::__construct();
+    function onInitializePlugin()
+    {
+        $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'));
+        return true;
     }
 
     function onEndShowScripts($action)
     {
-        $timeline = null;
+        $timeline = $this->_getTimeline($action);
 
-        $this->log(LOG_DEBUG, 'got action ' . $action->trimmed('action'));
+        // If there's not a timeline on this page,
+        // just return true
 
-        switch ($action->trimmed('action')) {
-         case 'public':
-            $timeline = '/timelines/public';
-            break;
-         case 'tag':
-            $tag = $action->trimmed('tag');
-            if (!empty($tag)) {
-                $timeline = '/timelines/tag/'.$tag;
-            } else {
-                return true;
-            }
-            break;
-         default:
+        if (empty($timeline)) {
             return true;
         }
 
-        $scripts = array('jquery.comet.js', 'json2.js', 'updatetimeline.js');
+        $base = $action->selfUrl();
+        if (mb_strstr($base, '?')) {
+            $url = $base . '&realtime=1';
+        } else {
+            $url = $base . '?realtime=1';
+        }
+
+        $scripts = $this->_getScripts();
 
         foreach ($scripts as $script) {
-            $action->element('script', array('type' => 'text/javascript',
-                                             'src' => common_path('plugins/Comet/'.$script)),
-                         ' ');
+            $action->script($script);
         }
 
         $user = common_current_user();
@@ -92,14 +97,22 @@ 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'));
+        if ($action->boolean('realtime')) {
+            $realtimeUI = ' RealtimeUpdate.initPopupWindow();';
+        }
+        else {
+            $iconurl = common_path('plugins/Realtime/icon_external.gif');
+            $realtimeUI = ' RealtimeUpdate.addPopup("'.$url.'", "'.$timeline.'", "'. $iconurl .'");';
+        }
 
         $action->elementStart('script', array('type' => 'text/javascript'));
-        $action->raw("$(document).ready(function() { updater.init(\"$this->server\", \"$timeline\", $user_id, \"$replyurl\", \"$favorurl\", \"$deleteurl\"); });");
+
+        $script = ' $(document).ready(function() { '.
+          $realtimeUI.
+          $this->_updateInitialize($timeline, $user_id).
+          '}); ';
+        $action->raw($script);
+
         $action->elementEnd('script');
 
         return true;
@@ -107,45 +120,120 @@ class CometPlugin extends Plugin
 
     function onEndNoticeSave($notice)
     {
-        $this->log(LOG_INFO, "Called for save notice.");
+        $paths = array();
+
+        // Add to the author's timeline
 
-        $timelines = array();
+        $user = User::staticGet('id', $notice->profile_id);
 
-        // XXX: Add other timelines; this is just for the public one
+        if (!empty($user)) {
+            $paths[] = array('showstream', $user->nickname);
+        }
+
+        // Add to the public timeline
 
         if ($notice->is_local ||
             ($notice->is_local == 0 && !common_config('public', 'localonly'))) {
-            $timelines[] = '/timelines/public';
+            $paths[] = array('public');
         }
 
+        // Add to the tags timeline
+
         $tags = $this->getNoticeTags($notice);
 
         if (!empty($tags)) {
             foreach ($tags as $tag) {
-                $timelines[] = '/timelines/tag/' . $tag;
+                $paths[] = array('tag', $tag);
+            }
+        }
+
+        // Add to inbox timelines
+        // XXX: do a join
+
+        $inbox = new Notice_inbox();
+        $inbox->notice_id = $notice->id;
+
+        if ($inbox->find()) {
+            while ($inbox->fetch()) {
+                $user = User::staticGet('id', $inbox->user_id);
+                $paths[] = array('all', $user->nickname);
             }
         }
 
-        if (count($timelines) > 0) {
-            // Require this, since we need it
-            require_once(INSTALLDIR.'/plugins/Comet/bayeux.class.inc.php');
+        // Add to the replies timeline
+
+        $reply = new Reply();
+        $reply->notice_id = $notice->id;
+
+        if ($reply->find()) {
+            while ($reply->fetch()) {
+                $user = User::staticGet('id', $reply->profile_id);
+                if (!empty($user)) {
+                    $paths[] = array('replies', $user->nickname);
+                }
+            }
+        }
+
+        // Add to the group timeline
+        // XXX: join
+
+        $gi = new Group_inbox();
+        $gi->notice_id = $notice->id;
+
+        if ($gi->find()) {
+            while ($gi->fetch()) {
+                $ug = User_group::staticGet('id', $gi->group_id);
+                $paths[] = array('showgroup', $ug->nickname);
+            }
+        }
+
+        if (count($paths) > 0) {
 
             $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);
+            foreach ($paths as $path) {
+                $timeline = $this->_pathToChannel($path);
+                $this->_publish($timeline, $json);
             }
 
-            $bay = NULL;
+            $this->_disconnect();
         }
 
         return true;
     }
 
+    function onStartShowBody($action)
+    {
+        $realtime = $action->boolean('realtime');
+        if (!$realtime) {
+            return true;
+        }
+
+        $action->elementStart('body',
+                              (common_current_user()) ? array('id' => $action->trimmed('action'),
+                                                              'class' => 'user_in')
+                              : array('id' => $action->trimmed('action')));
+
+        // XXX hack to deal with JS that tries to get the
+        // root url from page output
+
+        $action->elementStart('address');
+        $action->element('a', array('class' => 'url',
+                                  'href' => common_local_url('public')),
+                         '');
+        $action->elementEnd('address');
+
+        if (common_logged_in()) {
+            $action->showNoticeForm();
+        }
+
+        $action->showContentBlock();
+        $action->elementEnd('body');
+        return false; // No default processing
+    }
+
     function noticeAsJson($notice)
     {
         // FIXME: this code should be abstracted to a neutral third
@@ -202,4 +290,69 @@ class CometPlugin extends Plugin
     {
         common_log($level, get_class($this) . ': '.$msg);
     }
+
+    function _getScripts()
+    {
+        return array('plugins/Realtime/realtimeupdate.js',
+                     'plugins/Realtime/json2.js');
+    }
+
+    function _updateInitialize($timeline, $user_id)
+    {
+        return "RealtimeUpdate.init($user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\"); ";
+    }
+
+    function _connect()
+    {
+    }
+
+    function _publish($timeline, $json)
+    {
+    }
+
+    function _disconnect()
+    {
+    }
+
+    function _pathToChannel($path)
+    {
+        return '';
+    }
+
+    function _getTimeline($action)
+    {
+        $path = null;
+        $timeline = null;
+
+        $action_name = $action->trimmed('action');
+
+        switch ($action_name) {
+         case 'public':
+            $path = array('public');
+            break;
+         case 'tag':
+            $tag = $action->trimmed('tag');
+            if (!empty($tag)) {
+                $path = array('tag', $tag);
+            }
+            break;
+         case 'showstream':
+         case 'all':
+         case 'replies':
+         case 'showgroup':
+            $nickname = common_canonical_nickname($action->trimmed('nickname'));
+            if (!empty($nickname)) {
+                $path = array($action_name, $nickname);
+            }
+            break;
+         default:
+            break;
+        }
+
+        if (!empty($path)) {
+            $timeline = $this->_pathToChannel($path);
+        }
+
+        return $timeline;
+    }
 }