var Horde_Twitter = Class.create({
inReplyTo: '',
page: 1,
+ oldestId: null,
+ newestId: null,
/**
* Const'r
}.bind(this));
/* Get the first page */
- this.updateStream(1);
-
+ this.getNewEntries();
},
/**
*
* @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();
}
},
/**
+ * 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) {
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));
$html .= '<div class="fbstreaminfo">' . Horde::link('#', '', '', '', 'Horde.twitter.buildReply(\'' . $tweet->id . '\', \'' . $tweet->user->screen_name . '\', \'' . $tweet->user->name . '\')') . _("Reply") . '</a>';
$html .= ' | ' . Horde::link('#', '', '', '', 'Horde.twitter.retweet(\'' . $tweet->id . '\')') . _("Retweet") . '</a>';
$html .= '</div><div class="clear"> </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;
}