diff --git a/WebKit/scripts/controller/Conversation.js b/WebKit/scripts/controller/Conversation.js index 393ebd1..3fa4db2 100644 --- a/WebKit/scripts/controller/Conversation.js +++ b/WebKit/scripts/controller/Conversation.js @@ -2,11 +2,10 @@ define([ "helper/HostApp", "helper/Core", "helper/Paths", - "lib/URI", - "helper/Cache" + "lib/URI" ], -function(HostApp, Core, Paths, URI, Cache) { +function(HostApp, Core, Paths, URI) { function Conversation() { @@ -69,7 +68,7 @@ function(HostApp, Core, Paths, URI, Cache) { Paths.getURL(URI(server + "/posts/" + id).toString(), "GET", callback, null, false); } - var profile = JSON.parse(Cache.profiles.getItem(entity)); + var profile = this.cache.profiles.getItem(entity); if (entity == HostApp.stringForKey("entity")) { @@ -86,7 +85,7 @@ function(HostApp, Core, Paths, URI, Cache) { if (profile_url) { - var profile = JSON.parse(Cache.profiles.getItem(entity)); + var profile = this.cache.profiles.getItem(entity); if (profile) { getRemoteStatus(profile); @@ -95,8 +94,9 @@ function(HostApp, Core, Paths, URI, Cache) { Paths.getURL(profile_url, "GET", function(resp) { - Cache.profiles.setItem(entity, resp.responseText); - getRemoteStatus(JSON.parse(resp.responseText)); + var profile = JSON.parse(resp.responseText) + this.cache.profiles.setItem(entity, profile); + getRemoteStatus(profile); }, null, false); // do not send auth-headers } diff --git a/WebKit/scripts/controller/Mentions.js b/WebKit/scripts/controller/Mentions.js index ad7066f..3b0a18f 100644 --- a/WebKit/scripts/controller/Mentions.js +++ b/WebKit/scripts/controller/Mentions.js @@ -1,10 +1,9 @@ define([ "helper/HostApp", - "controller/Timeline", - "helper/Cache" + "controller/Timeline" ], -function(HostApp, Timeline, Cache) { +function(HostApp, Timeline) { function Mentions() { @@ -34,7 +33,7 @@ function(HostApp, Timeline, Cache) { var status = statuses[i]; var name; - var profile = JSON.parse(Cache.profiles.getItem(status.entity)); + var profile = this.cache.profiles.getItem(status.entity); if(profile) { name = profile["https://tent.io/types/info/basic/v0.1.0"].name; } diff --git a/WebKit/scripts/controller/Profile.js b/WebKit/scripts/controller/Profile.js index cbb0df6..ac2e303 100644 --- a/WebKit/scripts/controller/Profile.js +++ b/WebKit/scripts/controller/Profile.js @@ -2,11 +2,10 @@ define([ "helper/HostApp", "helper/Core", "helper/Paths", - "lib/URI", - "helper/Cache" + "lib/URI" ], -function(HostApp, Core, Paths, URI, Cache) { +function(HostApp, Core, Paths, URI) { function Profile() { @@ -16,8 +15,6 @@ function(HostApp, Core, Paths, URI, Cache) { this.action = "profile"; this.initProfileTemplate(); - - //setTimeout(Cache.getFollowings, 1000 * 60 * 5); } Profile.prototype = Object.create(Core.prototype); @@ -26,12 +23,12 @@ function(HostApp, Core, Paths, URI, Cache) { this.clear(); this.entity = entity; + this.following = null; + this.following_id = null; this.profile_template.entity.innerHTML = this.entity; this.profile_template.entity.href = this.entity; - this.setFollowingButton(!!Cache.followings.getItem(this.entity)); - - this.getProfile(); + this.getFollowing(); } Profile.prototype.initProfileTemplate = function() { @@ -156,7 +153,7 @@ function(HostApp, Core, Paths, URI, Cache) { this.profile_template.following_button.style.display = "none"; } - var profile = JSON.parse(Cache.profiles.getItem(this.entity)); + var profile = this.cache.profiles.getItem(this.entity); if (profile && profile != "null") { this.showProfile(profile); @@ -177,6 +174,23 @@ function(HostApp, Core, Paths, URI, Cache) { } } + Profile.prototype.getFollowing = function() { + var url = Paths.mkApiRootPath("/followings") + "/" + encodeURIComponent(this.entity); + var _this = this; + Paths.getURL(url, "GET", function(resp) { + if (resp.status >= 200 && resp.status < 400) { + var following = JSON.parse(resp.responseText); + _this.following_id = following.id + _this.setFollowingButton(true); + _this.showProfile(following.profile); + } else { + _this.setFollowingButton(false); + _this.following_id = null; + _this.getProfile(); + } + }) + } + Profile.prototype.showProfile = function(profile) { var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; @@ -311,7 +325,9 @@ function(HostApp, Core, Paths, URI, Cache) { } Profile.prototype.setFollowingButton = function(following) { - + + this.following = following; + if (following) { this.profile_template.following_button.className = "following"; this.profile_template.following_button.innerText = "Unfollow"; @@ -325,24 +341,31 @@ function(HostApp, Core, Paths, URI, Cache) { var _this = this; - var following = Cache.followings.getItem(this.entity); - if (following) { - var url = URI(Paths.mkApiRootPath("/followings/" + following.id)); - Paths.getURL(url.toString(), "DELETE", function(resp) { + if (this.following_id) { + + this.setFollowingButton(false); + var url = Paths.mkApiRootPath("/followings/") + this.following_id; + Paths.getURL(url, "DELETE", function(resp) { if (resp.status >= 200 && resp.status < 300) { - Cache.followings.removeItem(_this.entity); _this.setFollowingButton(false); + _this.following_id = null; + } else { + _this.setFollowingButton(true); } }); } else { + this.setFollowingButton(true); var url = URI(Paths.mkApiRootPath("/followings")); var data = JSON.stringify({"entity": this.entity }); Paths.getURL(url.toString(), "POST", function(resp) { - debug(resp.responseText) - Cache.followings.setItem(_this.entity, resp.responseText); - _this.setFollowingButton(true); + if (resp.status >= 200 && resp.status < 300) { + _this.following_id = JSON.parse(resp.responseText).id + _this.setFollowingButton(true); + } else { + _this.setFollowingButton(false); + } }, data); } } diff --git a/WebKit/scripts/controller/Timeline.js b/WebKit/scripts/controller/Timeline.js index 85cad22..6f00c27 100644 --- a/WebKit/scripts/controller/Timeline.js +++ b/WebKit/scripts/controller/Timeline.js @@ -106,6 +106,7 @@ function(Core, Paths, HostApp, URI) { those.reload_blocked = false; try { + var json = JSON.parse(resp.responseText) those.newStatus(json); diff --git a/WebKit/scripts/helper/Cache.js b/WebKit/scripts/helper/Cache.js index f83c872..2ec5c96 100644 --- a/WebKit/scripts/helper/Cache.js +++ b/WebKit/scripts/helper/Cache.js @@ -1,22 +1,29 @@ define([ - "helper/Paths", "lib/URI", - "helper/CacheStorage" + "helper/CacheStorage", + "require" ], -function(Paths, URI, CacheStorage) { +function(URI, CacheStorage, require) { - var Cache = {}; + function Cache() { + this.timeout = 2 * 60 * 1000; + this.followings_before_id = null; + this.intervall = null; - var timeout = 2 * 60 * 1000; - var followings_before_id = null; + //this.clear() - Cache.followings = new CacheStorage("followings"); - Cache.profiles = new CacheStorage("profiles"); - Cache.profile_urls = new CacheStorage("profile_urls"); + this.followings = new CacheStorage("followings"); + this.profiles = new CacheStorage("profiles"); + this.profile_urls = new CacheStorage("profile_urls"); + } + Cache.prototype.clear = function() { + localStorage.clear(); + } - Cache.getFollowings = function() { + Cache.prototype.getFollowings = function() { + var _this = this; function callback(resp) { var fs = JSON.parse(resp.responseText) @@ -26,25 +33,30 @@ function(Paths, URI, CacheStorage) { for (var i = 0; i < fs.length; i++) { var following = fs[i]; - followings_before_id = following.id; + this.followings_before_id = following.id; - Cache.followings.setItem(following.entity, following) - Cache.profiles.setItem(following.entity, following); + _this.followings.setItem(following.entity, following) + _this.profiles.setItem(following.entity, following); } } - var u = Paths.mkApiRootPath("/followings"); - - var url = URI(u); - if (followings_before_id) { - url.addSearch("before_id", followings_before_id); + var url = URI(require("helper/Paths").mkApiRootPath("/followings")); + if (this.followings_before_id) { + url.addSearch("before_id", this.followings_before_id); } - Paths.getURL(url, "GET", callback); + require("helper/Paths").getURL(url, "GET", callback); + } + + Cache.prototype.periodicallyGetFollowings = function() { + this.getFollowings(); + this.intervall = setInterval(this.getFollowings, this.timeout); + } + + Cache.prototype.stopGettingFollowings = function() { + clearTimeout(this.intervall); } - // setTimeout(function(){ Cache.getAllFollowings() }, timeout); - return Cache; }); \ No newline at end of file diff --git a/WebKit/scripts/helper/CacheStorage.js b/WebKit/scripts/helper/CacheStorage.js index 76f2dae..6af7534 100644 --- a/WebKit/scripts/helper/CacheStorage.js +++ b/WebKit/scripts/helper/CacheStorage.js @@ -16,13 +16,13 @@ function() { }; CacheStorage.prototype.getItem = function(key) { - return localStorage.getItem(this.mkPath(key)); + return JSON.parse(localStorage.getItem(this.mkPath(key))); } CacheStorage.prototype.setItem = function(key, value) { var item = this.getItem(key); - localStorage.setItem(this.mkPath(key), value); + localStorage.setItem(this.mkPath(key), JSON.stringify(value)); if (!item) { var length_path = this.mkInternalPath("_length"); diff --git a/WebKit/scripts/helper/Core.js b/WebKit/scripts/helper/Core.js index 16dc6d2..3047ea8 100644 --- a/WebKit/scripts/helper/Core.js +++ b/WebKit/scripts/helper/Core.js @@ -10,7 +10,7 @@ define([ function(jQuery, Paths, URI, HostApp, Cache) { function Core() { - + this.cache = new Cache(); } Core.prototype.getTemplate = function() { @@ -188,7 +188,7 @@ function(jQuery, Paths, URI, HostApp, Cache) { return false; } - function profile(p) { + var profile_callback = function(p) { var basic = p["https://tent.io/types/info/basic/v0.1.0"]; @@ -205,10 +205,10 @@ function(jQuery, Paths, URI, HostApp, Cache) { } - var p = JSON.parse(Cache.profiles.getItem(status.entity)); + var p = this.cache.profiles.getItem(status.entity); if (p && p != "null") { - profile(p); + profile_callback(p); } else { @@ -220,8 +220,8 @@ function(jQuery, Paths, URI, HostApp, Cache) { var p = JSON.parse(resp.responseText); if (p && p != "null") { - Cache.profiles.setItem(status.entity, resp.responseText); - profile(p); + _this.cache.profiles.setItem(status.entity, p); + profile_callback(p); } }, null, false); // do not send auth-headers @@ -240,26 +240,29 @@ function(jQuery, Paths, URI, HostApp, Cache) { return false; } - var profile = JSON.parse(Cache.profiles.getItem(status.__repost.entity)) - if (profile) { + var repost_profile = this.cache.profiles.getItem(status.__repost.entity) + if (repost_profile) { - var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; - template.reposted_by.innerText = basic.name; + var basic = repost_profile["https://tent.io/types/info/basic/v0.1.0"]; + if (basic) { + template.reposted_by.innerText = basic.name; + } } else { Paths.findProfileURL(status.__repost.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { + if (resp.status >= 200 && resp.status < 400) { + var _p = JSON.parse(resp.responseText); + _this.cache.profiles.setItem(status.__repost.entity, _p); - Cache.profiles.setItem(status.__repost.entity, resp.responseText); + var basic = _p["https://tent.io/types/info/basic/v0.1.0"]; + if (basic && basic.name) { + template.reposted_by.innerText = basic.name; + } - var p = JSON.parse(resp.responseText); - var profile = p["https://tent.io/types/info/basic/v0.1.0"]; - if (profile && profile.name) { - template.reposted_by.innerText = profile.name; } - }, null, false); // do not send auth-headers } }); @@ -294,8 +297,6 @@ function(jQuery, Paths, URI, HostApp, Cache) { if (status.type == "https://tent.io/types/post/photo/v0.1.0") { - debug(status.attachments) - for (var i = 0; i < status.attachments.length; i++) { // closure needed for the callback (function() { @@ -315,11 +316,8 @@ function(jQuery, Paths, URI, HostApp, Cache) { var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name); Paths.getURL(url, "GET", callback, null, null, attachment.type); } else { - Paths.findProfileURL(status.entity, function(profile_url) { - var url = profile_url + "/posts/" + status.id + "/attachments/" + attachment.name; - debug(url) - Paths.getURL(url, "GET", callback, null, null, attachment.type); - }); + var url = Paths.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name); + Paths.getURL(url, "GET", callback, null, null, attachment.type); } })(); } @@ -381,7 +379,7 @@ function(jQuery, Paths, URI, HostApp, Cache) { } } - var profile = JSON.parse(Cache.profiles.getItem(repost.content.entity)); + var profile = this.cache.profiles.getItem(repost.content.entity); if (profile && profile != "null") { var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0]; Paths.getURL(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false); @@ -614,7 +612,7 @@ function(jQuery, Paths, URI, HostApp, Cache) { } } - var p = JSON.parse(Cache.profiles.getItem(mention.entity)); + var p = _this.cache.profiles.getItem(mention.entity); if (p) { profile(p); @@ -624,11 +622,11 @@ function(jQuery, Paths, URI, HostApp, Cache) { Paths.findProfileURL(mention.entity, function(profile_url) { if (profile_url) { Paths.getURL(profile_url, "GET", function(resp) { - - Cache.profiles.setItem(mention.entity, resp.responseText); - - var p = JSON.parse(resp.responseText); - profile(p) + if (resp.status >= 200 && resp.status < 400) { + var p = JSON.parse(resp.responseText); + _this.cache.profiles.setItem(mention.entity, p); + profile(p) + } }, null, false); // do not send auth-headers } }); diff --git a/WebKit/scripts/helper/Paths.js b/WebKit/scripts/helper/Paths.js index 8b337f8..5d9610c 100644 --- a/WebKit/scripts/helper/Paths.js +++ b/WebKit/scripts/helper/Paths.js @@ -8,6 +8,8 @@ define([ function(jQuery, HostApp, Hmac, Cache) { var Paths = {}; + Paths.cache = new Cache(); + Paths.getUrlVars = function(url) { var vars = [], hash; if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#")); @@ -104,7 +106,7 @@ function(jQuery, HostApp, Hmac, Cache) { Paths.findProfileURL = function(entity, callback, errorCallback) { - var profile_url = Cache.profile_urls.getItem(entity); + var profile_url = Paths.cache.profile_urls.getItem(entity); if (profile_url && profile_url != "null") { @@ -129,7 +131,7 @@ function(jQuery, HostApp, Hmac, Cache) { } if (profile_url) { - Cache.profile_urls.setItem(entity, profile_url); + Paths.cache.profile_urls.setItem(entity, profile_url); callback(profile_url); } else { if(errorCallback) errorCallback(entity + " has no profile URL");