From e8392eff7ae3cb02779de24a8433f4d29772324c Mon Sep 17 00:00:00 2001 From: jeena Date: Mon, 9 Sep 2013 03:33:43 +0200 Subject: [PATCH] fixed mentions badget and cursor --- WebKit/scripts/controller/Mentions.js | 259 ++++++++++++++++---------- WebKit/scripts/controller/NewPost.js | 5 +- WebKit/scripts/controller/Timeline.js | 2 +- 3 files changed, 161 insertions(+), 105 deletions(-) diff --git a/WebKit/scripts/controller/Mentions.js b/WebKit/scripts/controller/Mentions.js index 04e49d0..898ae5a 100644 --- a/WebKit/scripts/controller/Mentions.js +++ b/WebKit/scripts/controller/Mentions.js @@ -1,146 +1,203 @@ define([ - "helper/HostApp", - "controller/Timeline", - "lib/URI", - "helper/APICalls", - "helper/Core" + "helper/HostApp", + "controller/Timeline", + "lib/URI", + "helper/APICalls", + "helper/Core" ], function(HostApp, Timeline, URI, APICalls, Core) { - function Mentions() { + function Mentions() { - this.is_not_init = false; - this.unread_mentions = 0; + this.is_not_init = false; + this.unread_mentions = 0; - Timeline.call(this); + Timeline.call(this); - this.action = "mentions"; - this.container.className = this.action; - - this.hide(); - } + this.action = "mentions"; + this.container.className = this.action; + + this.hide(); + } - Mentions.prototype = Object.create(Timeline.prototype); + Mentions.prototype = Object.create(Timeline.prototype); - Mentions.prototype.show = function() { - Core.prototype.show.call(this, this.container); - } + Mentions.prototype.show = function() { + Core.prototype.show.call(this, this.container); + } - Mentions.prototype.hide = function() { - Core.prototype.hide.call(this, this.container); - } + Mentions.prototype.hide = function() { + Core.prototype.hide.call(this, this.container); + } - Mentions.prototype.newStatus = function(statuses, append) { + Mentions.prototype.newStatus = function(statuses, append) { - Timeline.prototype.newStatus.call(this, statuses, append); + Timeline.prototype.newStatus.call(this, statuses, append); - if(this.is_not_init) { - for (var i = 0; i < statuses.posts.length; i++) { - - var status = statuses.posts[i]; - var name = bungloo.cache.profiles[status.entity] ? bungloo.cache.profiles[status.entity].name : status.entity + if(this.is_not_init) { + for (var i = 0; i < statuses.posts.length; i++) { + + var status = statuses.posts[i]; + var name = bungloo.cache.profiles[status.entity] ? bungloo.cache.profiles[status.entity].name : status.entity - if(!append) HostApp.notificateUserAboutMention(status.content.text, name, status.id, status.entity); - } - } + if(!append) HostApp.notificateUserAboutMention(status.content.text, name, status.id, status.entity); + } + } - this.is_not_init = true; - } + this.is_not_init = true; + } - Mentions.prototype.getNewData = function(add_to_search, append, query) { + Mentions.prototype.getNewData = function(add_to_search, append, query) { - add_to_search = add_to_search || {}; + add_to_search = add_to_search || {}; - if (!add_to_search["mentions"]) { - add_to_search["mentions"] = HostApp.stringForKey("entity"); - } + if (!add_to_search["mentions"]) { + add_to_search["mentions"] = HostApp.stringForKey("entity"); + } - Timeline.prototype.getNewData.call(this, add_to_search, append, query); + Timeline.prototype.getNewData.call(this, add_to_search, append, query); - //this.getLatestMentionRead(); - } + this.getLatestMentionRead(); + } - Mentions.prototype.mentionRead = function(id, entity) { - if (this.unread_mentions > 0) { - this.unread_mentions--; - HostApp.unreadMentions(this.unread_mentions); - } - } + Mentions.prototype.mentionRead = function(id, entity) { + if (this.unread_mentions > 0) { + this.unread_mentions--; + HostApp.unreadMentions(this.unread_mentions); + } + } - Mentions.prototype.setAllMentionsRead = function() { - this.unread_mentions = 0; - HostApp.unreadMentions(this.unread_mentions); - //this.updateLatestMentionRead(); - } + Mentions.prototype.setAllMentionsRead = function() { + this.unread_mentions = 0; + HostApp.unreadMentions(this.unread_mentions); + this.updateLatestMentionRead(); + } - // FIXME: those two functions need to be rewritten - /* - Mentions.prototype.updateLatestMentionRead = function() { + Mentions.prototype.updateLatestMentionRead = function() { - for (var i = 0; i < this.body.childNodes.length; i++) { + var cursor_url = HostApp.serverUrl("posts_feed") + "?types=" + "https://tent.io/types/cursor/v0"; - var status = this.body.childNodes[i].status; + // find the first real post + for (var i = 0; i < this.body.childNodes.length; i++) { - if (!status.__repost) { - if (status && status.type == "https://tent.io/types/post/status/v0.1.0") { + var status = this.body.childNodes[i].status; - var url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0"))); - var body = { - "mentions": { - "https://tent.io/types/post/status/v0.1.0": { - "post": status.id, - "entity": status.entity - } - } - } + if (!status.__repost) { + if (status && status.type.startsWith("https://tent.io/types/status/v0#")) { - var callback = function(resp) { + // First find out if there is such a cursor or if we need to create it first + APICalls.get(cursor_url, { callback: function(resp) { - } + var posts = JSON.parse(resp.responseText).posts; + var mentions_post = null; + + for (var i = 0; i < posts.length; i++) { + var post = posts[i]; - APICalls.http_call(url.toString(), "PUT", callback, JSON.stringify(body)); - } + if(post.type == "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions") { - break; - } - } - } + mentions_post = post; + + } + }; + + // Now prepare the cursor + + + var data = { + type: "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions", + content: {}, + permissions: { + public: false, + }, + refs: [ + { + "post": status.id, + "type": status.type, + "entity": status.entity + } + ] + }; + + // update version if the post exists + if(mentions_post) { + data.version = { + parents: [ + { + version: mentions_post.version.id + } + ] + } + } + + var options = { + content_type: data.type, + accept: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions"', + callback: function(resp) { + + }}; + + // either update or create the cursor + if(mentions_post) { + var url = HostApp.serverUrl("post") + .replace(/\{entity\}/, encodeURIComponent(HostApp.stringForKey("entity"))) + .replace(/\{post\}/, mentions_post.id) + APICalls.put(url, JSON.stringify(data), options); + } else { + var url = HostApp.serverUrl("posts_feed"); + APICalls.post(url, JSON.stringify(data), options); + } + + }}); + } + + break; + } + } + + } - Mentions.prototype.getLatestMentionRead = function() { + Mentions.prototype.getLatestMentionRead = function() { - var cursor_url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0"))); + var cursor_url = HostApp.serverUrl("posts_feed") + "?types=" + "https://tent.io/types/cursor/v0"; - APICalls.http_call(cursor_url.toString(), "GET", function(resp) { + APICalls.get(cursor_url, { callback: function(resp) { - var url = URI(APICalls.mkApiRootPath("/posts/count")); - var post_types = [ - "https://tent.io/types/post/status/v0.1.0", - ]; - url.addSearch("post_types", post_types.join(",")); - url.addSearch("mentioned_entity", HostApp.stringForKey("entity")); + var posts = JSON.parse(resp.responseText).posts; + + for (var i = 0; i < posts.length; i++) { + var post = posts[i]; + if(post.type == "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions") { - try { // don't crash when there is no cursor yet - var body = JSON.parse(resp.responseText); - var cursor = body["mentions"]["https://tent.io/types/post/status/v0.1.0"]; - url.addSearch("since_id", cursor["post"]); - url.addSearch("since_id_entity", cursor["entity"]); - } catch(e) { } + var since = post.version.received_at; + var post_id = post.refs[0].post; + var post_types = [ + "https://tent.io/types/status/v0#reply", + "https://tent.io/types/status/v0#" + ]; - var callback = function(resp) { - this.unread_mentions = parseInt(resp.responseText, 10); - HostApp.unreadMentions(this.unread_mentions); - } - - APICalls.http_call(url.toString(), "GET", callback); // FIXME: error callback - }); - } - */ + var uri = URI(HostApp.serverUrl("posts_feed")); + uri.addSearch("types", post_types.join(",")); + uri.addSearch("since", since); + uri.addSearch("mentions", HostApp.stringForKey("entity")); - return Mentions; + APICalls.head(uri.toString(), { callback: function(resp) { + + this.unread_mentions = APICalls.getCount(resp); + HostApp.unreadMentions(this.unread_mentions); + + }}); + } + }; + + }}); + } + + return Mentions; }); \ No newline at end of file diff --git a/WebKit/scripts/controller/NewPost.js b/WebKit/scripts/controller/NewPost.js index 7806aba..f7849dc 100644 --- a/WebKit/scripts/controller/NewPost.js +++ b/WebKit/scripts/controller/NewPost.js @@ -38,7 +38,7 @@ function(APICalls, HostApp) { var buttons = $( "

" + //"" + - "" + + " " + "" + "

"); @@ -267,9 +267,8 @@ function(APICalls, HostApp) { var content = this.textarea.val(); - var type = "https://tent.io/types/status/v0#"; var data = { - type: type, + type: "https://tent.io/types/status/v0#", content: { text: content }, diff --git a/WebKit/scripts/controller/Timeline.js b/WebKit/scripts/controller/Timeline.js index a22bc6d..f41447c 100644 --- a/WebKit/scripts/controller/Timeline.js +++ b/WebKit/scripts/controller/Timeline.js @@ -131,7 +131,7 @@ function(Core, APICalls, HostApp, URI) { uri.addSearch("types", post_types.join(",")); //uri.addSearch("sort_by", "published_at"); uri.addSearch("limit", this.posts_limit); - uri.addSearch("max_refs", 20); + //uri.addSearch("max_refs", 20); uri.addSearch("profiles", "entity"); if(this.since_time) {