Initial work on displaying @mentions in twitter client.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Tue, 7 Dec 2010 18:13:49 +0000 (13:13 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Wed, 8 Dec 2010 02:56:28 +0000 (21:56 -0500)
Basic functionality, needs UI love.

horde/js/twitterclient.js
horde/lib/Block/twitter_timeline.php
horde/services/twitter.php
horde/templates/block/twitter-layout.html.php

index 3ddbfc5..b0a2d49 100644 (file)
@@ -20,6 +20,7 @@ var Horde_Twitter = Class.create({
     * opts.counter The domid of the node to display chars remaining.
     * opts.spinner The domid of the spinner element.
     * opts.content The main content area, where the tweets are placed.
+    * opts.mentions  The domid of where the mentions stream should be placed.
     * opts.endpoint  The url endpoint for horde/servcies/twitter.php
     * opts.inreplyto
     * opts.refreshrate How often to refresh the stream
@@ -51,6 +52,7 @@ var Horde_Twitter = Class.create({
         }.bind(this));
 
         this.instanceid = opts.instanceid;
+
         /* Get the first page */
         this.getNewEntries();
    },
@@ -132,21 +134,27 @@ var Horde_Twitter = Class.create({
      * Get newer entries, or the first page of entries if this is the first
      * request.
      */
-    getNewEntries: function() {
-        var params = {
+    getNewEntries: function(type) {
+        var callback, params = {
             actionID: 'getPage',
             i: this.instanceid
         };
-
-        if (this.newestId) {
+        if (type == 'mentions') {
+          params.mentions = 1;
+          callback = this._getNewMentionsCallback.bind(this);
+        } else {
+          callback = this._getNewEntriesCallback.bind(this);
+        }
+        if (this.newestId && type !== 'mentions') {
             params.since_id = this.newestId;
         } else {
             params.page = 1;
         }
+
         new Ajax.Request(this.opts.endpoint, {
             method: 'post',
             parameters: params,
-            onSuccess: this._getNewEntriesCallback.bind(this),
+            onSuccess: callback,
             onFailure: function() {
                 $(this.opts.spinner).toggle();
             }
@@ -173,6 +181,7 @@ var Horde_Twitter = Class.create({
      */
     _getNewEntriesCallback: function(response) {
         var content = response.responseJSON.c;
+
         if (response.responseJSON.n != this.newestId) {
             var h = $(this.opts.content).scrollHeight
             $(this.opts.content).insert({ 'top': content });
@@ -193,6 +202,32 @@ var Horde_Twitter = Class.create({
     },
 
     /**
+     * Callback for retrieving new mentions.
+     *
+     * @TODO: Implement paging, separate oldestId etc...
+     */
+    _getNewMentionsCallback: function(response) {
+        var content = response.responseJSON.c;
+        //if (response.responseJSON.n != this.newestId) {
+            var h = $(this.opts.mentions).scrollHeight
+            $(this.opts.mentions).insert({ 'top': content });
+            // Don't scroll if it's the first request.
+            if (this.newestId) {
+                $(this.opts.mentions).scrollTop = h;
+            } else {
+                $(this.opts.mentions).scrollTop = 0;
+            }
+            //this.newestId = response.responseJSON.n;
+
+            // First time we've been called, record the oldest one as well.'
+            //if (!this.oldestId) {
+            //    this.oldestId = response.responseJSON.o;
+            //}
+        //}
+        //new PeriodicalExecuter(function(pe) { this.getNewMentions(); pe.stop(); }.bind(this), this.opts.refreshrate );
+    },
+
+    /**
      * Build the reply structure
      */
     buildReply: function(id, userid, usertext) {
@@ -230,6 +265,19 @@ var Horde_Twitter = Class.create({
         $(this.opts.content).insert({top:tweet});
     },
 
+    showMentions: function()
+    {
+        $(this.opts.content).hide();
+        this.getNewEntries('mentions');
+        $(this.opts.mentions).show();
+    },
+
+    showStream: function()
+    {
+        $(this.opts.mentions).hide();
+        $(this.opts.content).show();
+    },
+
     /**
      * Clear the input field.
      */
index 175ca69..1035a50 100644 (file)
@@ -147,7 +147,8 @@ class Horde_Block_Horde_twitter_timeline extends Horde_Block
                getmore: '{$instance}_getmore',
                input: '{$instance}_newStatus',
                spinner: '{$instance}_loading',
-               content: '{$instance}_twitter_body',
+               content: '{$instance}_stream',
+               mentions: '{$instance}_mentions',
                endpoint: '{$endpoint}',
                inreplyto: '{$inReplyToNode}',
                refreshrate: {$refresh},
index 2fadd64..f58825b 100644 (file)
@@ -59,7 +59,11 @@ case 'getPage':
             $params['since_id'] = $since;
         }
         $instance = Horde_Util::getPost('i');
-        $stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline($params), Horde_Serialize::JSON);
+        if (Horde_Util::getPost('mentions', null)) {
+            $stream = Horde_Serialize::unserialize($twitter->statuses->mentions($params), Horde_Serialize::JSON);
+        } else {
+            $stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline($params), Horde_Serialize::JSON);
+        }
     } catch (Horde_Service_Twitter_Exception $e) {
         echo sprintf(_("Unable to contact Twitter. Please try again later. Error returned: %s"), $e->getMessage());
         exit;
index 5891226..343d6b0 100644 (file)
    <?php echo $this->loadingImg ?>
    <div id="currentStatus" class="" style="margin:10px;"><strong>'<?php echo _("Latest") ?></strong><?php echo $this->latestStatus ?> - <span class="fbstreaminfo"><?php echo $this->latestDate ?></span></div>
  </div>
- <div style="height:<?php echo $this->bodyHeight ?>px; overflow-y:auto;" id="<?php echo $this->instance ?>_twitter_body"></div>
+ <div>
+   <a href="#" onclick="Horde['twitter<?php echo $this->instance ?>'].showStream();"><?php echo _("Stream")?></a>
+   <a href="#" onclick="Horde['twitter<?php echo $this->instance ?>'].showMentions();"><?php echo _("Mentions") ?></a>
+ </div>
+ <div style="height:<?php echo $this->bodyHeight ?>px; overflow-y:auto;" id="<?php echo $this->instance ?>_twitter_body">
+   <div id="<?php echo $this->instance ?>_stream"></div>
+   <div id="<?php echo $this->instance ?>_mentions"></div>
+ </div>
  <div class="hordeSmGetmore"><input type="button" class="button" id="<?php echo $this->instance ?>_getmore" value="<?php echo _("Get More") ?>"></div>
 </div>
\ No newline at end of file