From 3ca7a28ec72d6a4551f2ceb686fc2fb74598a07f Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Tue, 7 Dec 2010 13:13:49 -0500 Subject: [PATCH] Initial work on displaying @mentions in twitter client. Basic functionality, needs UI love. --- horde/js/twitterclient.js | 58 ++++++++++++++++++++++++--- horde/lib/Block/twitter_timeline.php | 3 +- horde/services/twitter.php | 6 ++- horde/templates/block/twitter-layout.html.php | 9 ++++- 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/horde/js/twitterclient.js b/horde/js/twitterclient.js index 3ddbfc57f..b0a2d49c6 100644 --- a/horde/js/twitterclient.js +++ b/horde/js/twitterclient.js @@ -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. */ diff --git a/horde/lib/Block/twitter_timeline.php b/horde/lib/Block/twitter_timeline.php index 175ca697c..1035a5069 100644 --- a/horde/lib/Block/twitter_timeline.php +++ b/horde/lib/Block/twitter_timeline.php @@ -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}, diff --git a/horde/services/twitter.php b/horde/services/twitter.php index 2fadd64ec..f58825b2f 100644 --- a/horde/services/twitter.php +++ b/horde/services/twitter.php @@ -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; diff --git a/horde/templates/block/twitter-layout.html.php b/horde/templates/block/twitter-layout.html.php index 589122663..343d6b089 100644 --- a/horde/templates/block/twitter-layout.html.php +++ b/horde/templates/block/twitter-layout.html.php @@ -18,6 +18,13 @@ loadingImg ?>
'latestStatus ?> - latestDate ?>
-
+
+ + +
+
+
+
+
">
\ No newline at end of file -- 2.11.0