Can not use pages in this type of client since there have likely been more *recent...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 2 Jul 2010 23:43:50 +0000 (19:43 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 2 Jul 2010 23:43:50 +0000 (19:43 -0400)
Need to remember the oldest and newest tweet ids ourselves and do our own paging.

horde/js/twitterclient.js
horde/lib/Block/twitter_timeline.php
horde/services/twitter.php

index 9096ad5..05dece0 100644 (file)
@@ -10,6 +10,8 @@
 var Horde_Twitter = Class.create({
    inReplyTo: '',
    page: 1,
+   oldestId: null,
+   newestId: null,
 
    /**
     * Const'r
@@ -33,8 +35,7 @@ var Horde_Twitter = Class.create({
         }.bind(this));
 
         /* Get the first page */
-        this.updateStream(1);
-
+        this.getNewEntries();
    },
 
    /**
@@ -91,20 +92,15 @@ var Horde_Twitter = Class.create({
      *
      * @param integer page  The page number to retrieve.
      */
-    updateStream: function(page) {
+    getOlderEntries: function() {
+        var params = { actionID: 'getPage' };
+        if (this.oldestId) {
+            params.max_id = this.oldestId;
+        }
         new Ajax.Request(this.opts.endpoint, {
             method: 'post',
-            parameters: { actionID: 'getPage', 'page': page },
-            onComplete: function(response) {
-                var h = $(this.opts.content).scrollHeight
-                $(this.opts.content).insert(response.responseText);
-                // Don't scroll if it's the first request.
-                if (page != 1) {
-                    $(this.opts.content).scrollTop = h;
-                } else {
-                    $(this.opts.content).scrollTop = 0;
-                }
-            }.bind(this),
+            parameters: params,
+            onComplete: this._getOlderEntriesCallback.bind(this),
             onFailure: function() {
                 $(this.opts.spinner).toggle();
             }
@@ -112,6 +108,63 @@ var Horde_Twitter = Class.create({
     },
 
     /**
+     * Get newer entries, or the first page of entries if this is the first
+     * request.
+     */
+    getNewEntries: function() {
+        var params = { actionID: 'getPage' };
+        if (this.newestId) {
+            params.since_id = this.newestId;
+        } else {
+            params.page = 1;
+        }
+        new Ajax.Request(this.opts.endpoint, {
+            method: 'post',
+            parameters: params,
+            onComplete: this._getNewEntriesCallback.bind(this),
+            onFailure: function() {
+                $(this.opts.spinner).toggle();
+            }
+        });
+    },
+
+    /**
+     * Callback for updateStream request. Updates display, remembers the oldest
+     * id we know about.
+     *
+     */
+    _getOlderEntriesCallback: function(response) {
+        var content = response.responseJSON.c;
+        this.oldestId = response.responseJSON.o;
+        var h = $(this.opts.content).scrollHeight
+        $(this.opts.content).insert(content);
+        $(this.opts.content).scrollTop = h;
+    },
+
+    /**
+     * Callback for retrieving new entries. Updates the display and remembers
+     * the newest id, and possible the older id as well.
+     * 
+     */
+    _getNewEntriesCallback: function(response) {
+        var content = response.responseJSON.c;
+        var h = $(this.opts.content).scrollHeight
+        $(this.opts.content).insert({ 'top': content });
+        // Don't scroll if it's the first request.
+        if (this.newestId) {
+            $(this.opts.content).scrollTop = h;
+        } else {
+            $(this.opts.content).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;
+        }
+    },
+
+    /**
      * Build the reply structure
      */
     buildReply: function(id, userid, usertext) {
index c7e28f8..7fbf513 100644 (file)
@@ -167,7 +167,7 @@ EOT;
         $html .= '<div style="height:' . (empty($this->_params['height']) ? 250 : $this->_params['height']) . 'px;overflow-y:auto;" id="twitter_body' . $instance . '">';
         $filter = Horde_Text_Filter::factory('Text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
         $html .= '</div>';
-        $html .= '<div class="control fbgetmore"><a href="#" onclick="Horde.twitter.updateStream(++Horde.twitter.page);return false;">' . _("Get More") . '</a></div>';
+        $html .= '<div class="control fbgetmore"><a href="#" onclick="Horde.twitter.getOlderEntries();return false;">' . _("Get More") . '</a></div>';
         $html .= '</div>';
 
         return $html;
index d2d553f..e7b9dd3 100644 (file)
@@ -52,12 +52,20 @@ case 'retweet':
 
 case 'getPage':
     try {
-        $stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline(array('page' => Horde_Util::getPost('page'))), Horde_Serialize::JSON);
+        $params = array();
+        if ($max = Horde_Util::getPost('max_id')) {
+            $params['max_id'] = $max;
+        } elseif ($since = Horde_Util::getPost('since_id')) {
+            $params['since_id'] = $since;
+        }
+        
+        $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;
     }
     $html = '';
+    $newest = $stream[0]->id;
     foreach ($stream as $tweet) {
         /* links */
         $body = Horde_Text_Filter::filter($tweet->text, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL));
@@ -94,8 +102,16 @@ case 'getPage':
         $html .= '<div class="fbstreaminfo">' . Horde::link('#', '', '', '', 'Horde.twitter.buildReply(\'' . $tweet->id . '\', \'' . $tweet->user->screen_name . '\', \'' . $tweet->user->name . '\')') .  _("Reply") . '</a>';
         $html .= '&nbsp;|&nbsp;' . Horde::link('#', '', '', '', 'Horde.twitter.retweet(\'' . $tweet->id . '\')') . _("Retweet") . '</a>';
         $html .= '</div><div class="clear">&nbsp;</div></div></div>';
+        $oldest = $tweet->id;
     }
-    echo $html;
+
+    $result = array(
+        'o' => $oldest,
+        'n' => $newest,
+        'c' => $html
+    );
+    header('Content-Type: application/json');
+    echo Horde_Serialize::serialize($result, Horde_Serialize::JSON);
     exit;
 }