From 771243a75f11c806d0358b3bfb72c275972ee67e Mon Sep 17 00:00:00 2001 From: jeena Date: Wed, 17 Jul 2013 03:01:43 +0200 Subject: [PATCH] fixed following/unfollowing --- WebKit/scripts/controller/Profile.js | 39 ++++++++++++++++----------- WebKit/scripts/controller/Timeline.js | 6 ++++- WebKit/scripts/helper/APICalls.js | 17 ++++++++++-- WebKit/scripts/helper/Core.js | 1 + 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/WebKit/scripts/controller/Profile.js b/WebKit/scripts/controller/Profile.js index febc78c..bd42d8d 100644 --- a/WebKit/scripts/controller/Profile.js +++ b/WebKit/scripts/controller/Profile.js @@ -247,14 +247,17 @@ function(HostApp, Core, APICalls, URI) { var url = HostApp.serverUrl("posts_feed") + "?mentions=" + encodeURIComponent(this.entity) + "&types=" + encodeURIComponent("https://tent.io/types/subscription/v0#https://tent.io/types/status/v0"); var _this = this; - APICalls.head(url, {callback: function(resp) { + APICalls.get(url, {callback: function(resp) { - var count = APICalls.getCount(resp); + var json = JSON.parse(resp.responseText); + var count = json.posts.length; if (count > 0) { _this.setFollowingButton(true); + _this.following_id = json.posts[0].id; } else { _this.setFollowingButton(false); + delete _this.following_id; } }}); @@ -331,13 +334,14 @@ function(HostApp, Core, APICalls, URI) { Profile.prototype.getMeta = function(profile) { + // FIXME! + return; + var _this = this; var url = HostApp.serverUrl("posts_feed") + "?entities=" + encodeURIComponent(this.entity) + "&types=" + encodeURIComponent("https://tent.io/types/relationship/v0#follower"); APICalls.head(url, { callback: function(resp) { - debug(APICalls.getCount(resp)) - _this.populate(_this.profile_template.followed, APICalls.getCount(resp)+" "); } }); @@ -518,16 +522,15 @@ function(HostApp, Core, APICalls, URI) { this.setFollowingButton(false); - /* - var url = APICalls.mkApiRootPath("/followings/") + this.following_id; - APICalls.http_call(url, "DELETE", function(resp) { + var url = HostApp.serverUrl("post").replace(/\{entity\}/, encodeURIComponent(HostApp.stringForKey("entity"))).replace(/\{post\}/, this.following_id); + APICalls.delete(url, { callback: function(resp) { if (resp.status >= 200 && resp.status < 300) { _this.setFollowingButton(false); - _this.following_id = null; + delete _this.following_id; } else { _this.setFollowingButton(true); } - });*/ + }}); } else { @@ -535,19 +538,23 @@ function(HostApp, Core, APICalls, URI) { var url = HostApp.serverUrl("new_post"); - var data = JSON.stringify({ - type: "https://tent.io/types/subscription/v0#https://tent.io/types/status/v0", + var data = { + content: { + type: "https://tent.io/types/status/v0" + }, mentions: [{ entity: this.entity - }] - }); + }], + type: "https://tent.io/types/subscription/v0#https://tent.io/types/status/v0" + }; - APICalls.post(url, data, { - content_type: "https://tent.io/types/subscription/v0", + APICalls.post(url, JSON.stringify(data), { + content_type: data.type, callback: function(resp) { - debug(resp.status) if (resp.status >= 200 && resp.status < 300) { _this.setFollowingButton(true); + var json = JSON.parse(resp.responseText); + _this.following_id = json.post.id; } else { _this.setFollowingButton(false); } diff --git a/WebKit/scripts/controller/Timeline.js b/WebKit/scripts/controller/Timeline.js index c01f418..c4716d7 100644 --- a/WebKit/scripts/controller/Timeline.js +++ b/WebKit/scripts/controller/Timeline.js @@ -49,7 +49,11 @@ function(Core, APICalls, HostApp, URI) { Timeline.prototype.newStatus = function(_statuses, append) { for (var entity in _statuses.profiles) { - bungloo.cache.profiles[entity] = _statuses.profiles[entity]; + if (_statuses.profiles[entity] != null) { + bungloo.cache.profiles[entity] = _statuses.profiles[entity]; + } else { + bungloo.cache.profiles[entity] = {}; + } } statuses = _statuses.posts; diff --git a/WebKit/scripts/helper/APICalls.js b/WebKit/scripts/helper/APICalls.js index 60a971b..1bdc52d 100644 --- a/WebKit/scripts/helper/APICalls.js +++ b/WebKit/scripts/helper/APICalls.js @@ -38,10 +38,9 @@ function(jQuery, HostApp, Hmac, Cache) { } else { if(options.content_type == "application/json") { content_type = "application/json"; - } else { + } else if(options.content_type) { content_type = "application/vnd.tent.post.v0+json; type=\"" + options.content_type + "\""; } - } var settings = { @@ -120,6 +119,20 @@ function(jQuery, HostApp, Hmac, Cache) { APICalls.http_call(settings); } + APICalls.delete = function(url, options) { + var settings = { + url: url, + http_method: "DELETE" + }; + + for (var key in options) { + settings[key] = options[key]; + } + + APICalls.http_call(settings); + } + + APICalls.postMultipart = function(url, callback, data, boundary, accepts) { accepts = accepts || "application/vnd.tent.v0+json"; diff --git a/WebKit/scripts/helper/Core.js b/WebKit/scripts/helper/Core.js index b4bc318..1f17530 100644 --- a/WebKit/scripts/helper/Core.js +++ b/WebKit/scripts/helper/Core.js @@ -236,6 +236,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) { } if(bungloo.cache.profiles[status.entity].name) template.username.innerText = bungloo.cache.profiles[status.entity].name; + else template.username.innerText = status.entity; template.username.href = status.entity; template.username.title = status.entity; template.username.onclick = function() {