implemented cache fixed #89

This commit is contained in:
Jeena Paradies 2012-12-18 23:03:19 +01:00
parent 448cb24ca3
commit fff75df0df
8 changed files with 119 additions and 84 deletions

View file

@ -2,11 +2,10 @@ define([
"helper/HostApp", "helper/HostApp",
"helper/Core", "helper/Core",
"helper/Paths", "helper/Paths",
"lib/URI", "lib/URI"
"helper/Cache"
], ],
function(HostApp, Core, Paths, URI, Cache) { function(HostApp, Core, Paths, URI) {
function Conversation() { function Conversation() {
@ -69,7 +68,7 @@ function(HostApp, Core, Paths, URI, Cache) {
Paths.getURL(URI(server + "/posts/" + id).toString(), "GET", callback, null, false); 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")) { if (entity == HostApp.stringForKey("entity")) {
@ -86,7 +85,7 @@ function(HostApp, Core, Paths, URI, Cache) {
if (profile_url) { if (profile_url) {
var profile = JSON.parse(Cache.profiles.getItem(entity)); var profile = this.cache.profiles.getItem(entity);
if (profile) { if (profile) {
getRemoteStatus(profile); getRemoteStatus(profile);
@ -95,8 +94,9 @@ function(HostApp, Core, Paths, URI, Cache) {
Paths.getURL(profile_url, "GET", function(resp) { Paths.getURL(profile_url, "GET", function(resp) {
Cache.profiles.setItem(entity, resp.responseText); var profile = JSON.parse(resp.responseText)
getRemoteStatus(JSON.parse(resp.responseText)); this.cache.profiles.setItem(entity, profile);
getRemoteStatus(profile);
}, null, false); // do not send auth-headers }, null, false); // do not send auth-headers
} }

View file

@ -1,10 +1,9 @@
define([ define([
"helper/HostApp", "helper/HostApp",
"controller/Timeline", "controller/Timeline"
"helper/Cache"
], ],
function(HostApp, Timeline, Cache) { function(HostApp, Timeline) {
function Mentions() { function Mentions() {
@ -34,7 +33,7 @@ function(HostApp, Timeline, Cache) {
var status = statuses[i]; var status = statuses[i];
var name; var name;
var profile = JSON.parse(Cache.profiles.getItem(status.entity)); var profile = this.cache.profiles.getItem(status.entity);
if(profile) { if(profile) {
name = profile["https://tent.io/types/info/basic/v0.1.0"].name; name = profile["https://tent.io/types/info/basic/v0.1.0"].name;
} }

View file

@ -2,11 +2,10 @@ define([
"helper/HostApp", "helper/HostApp",
"helper/Core", "helper/Core",
"helper/Paths", "helper/Paths",
"lib/URI", "lib/URI"
"helper/Cache"
], ],
function(HostApp, Core, Paths, URI, Cache) { function(HostApp, Core, Paths, URI) {
function Profile() { function Profile() {
@ -16,8 +15,6 @@ function(HostApp, Core, Paths, URI, Cache) {
this.action = "profile"; this.action = "profile";
this.initProfileTemplate(); this.initProfileTemplate();
//setTimeout(Cache.getFollowings, 1000 * 60 * 5);
} }
Profile.prototype = Object.create(Core.prototype); Profile.prototype = Object.create(Core.prototype);
@ -26,12 +23,12 @@ function(HostApp, Core, Paths, URI, Cache) {
this.clear(); this.clear();
this.entity = entity; this.entity = entity;
this.following = null;
this.following_id = null;
this.profile_template.entity.innerHTML = this.entity; this.profile_template.entity.innerHTML = this.entity;
this.profile_template.entity.href = this.entity; this.profile_template.entity.href = this.entity;
this.setFollowingButton(!!Cache.followings.getItem(this.entity)); this.getFollowing();
this.getProfile();
} }
Profile.prototype.initProfileTemplate = function() { Profile.prototype.initProfileTemplate = function() {
@ -156,7 +153,7 @@ function(HostApp, Core, Paths, URI, Cache) {
this.profile_template.following_button.style.display = "none"; 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") { if (profile && profile != "null") {
this.showProfile(profile); 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) { Profile.prototype.showProfile = function(profile) {
var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; 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) { Profile.prototype.setFollowingButton = function(following) {
this.following = following;
if (following) { if (following) {
this.profile_template.following_button.className = "following"; this.profile_template.following_button.className = "following";
this.profile_template.following_button.innerText = "Unfollow"; this.profile_template.following_button.innerText = "Unfollow";
@ -325,24 +341,31 @@ function(HostApp, Core, Paths, URI, Cache) {
var _this = this; var _this = this;
var following = Cache.followings.getItem(this.entity); if (this.following_id) {
if (following) {
var url = URI(Paths.mkApiRootPath("/followings/" + following.id)); this.setFollowingButton(false);
Paths.getURL(url.toString(), "DELETE", function(resp) { var url = Paths.mkApiRootPath("/followings/") + this.following_id;
Paths.getURL(url, "DELETE", function(resp) {
if (resp.status >= 200 && resp.status < 300) { if (resp.status >= 200 && resp.status < 300) {
Cache.followings.removeItem(_this.entity);
_this.setFollowingButton(false); _this.setFollowingButton(false);
_this.following_id = null;
} else {
_this.setFollowingButton(true);
} }
}); });
} else { } else {
this.setFollowingButton(true);
var url = URI(Paths.mkApiRootPath("/followings")); var url = URI(Paths.mkApiRootPath("/followings"));
var data = JSON.stringify({"entity": this.entity }); var data = JSON.stringify({"entity": this.entity });
Paths.getURL(url.toString(), "POST", function(resp) { Paths.getURL(url.toString(), "POST", function(resp) {
debug(resp.responseText) if (resp.status >= 200 && resp.status < 300) {
Cache.followings.setItem(_this.entity, resp.responseText); _this.following_id = JSON.parse(resp.responseText).id
_this.setFollowingButton(true); _this.setFollowingButton(true);
} else {
_this.setFollowingButton(false);
}
}, data); }, data);
} }
} }

View file

@ -106,6 +106,7 @@ function(Core, Paths, HostApp, URI) {
those.reload_blocked = false; those.reload_blocked = false;
try { try {
var json = JSON.parse(resp.responseText) var json = JSON.parse(resp.responseText)
those.newStatus(json); those.newStatus(json);

View file

@ -1,22 +1,29 @@
define([ define([
"helper/Paths",
"lib/URI", "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; //this.clear()
var followings_before_id = null;
Cache.followings = new CacheStorage("followings"); this.followings = new CacheStorage("followings");
Cache.profiles = new CacheStorage("profiles"); this.profiles = new CacheStorage("profiles");
Cache.profile_urls = new CacheStorage("profile_urls"); 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) { function callback(resp) {
var fs = JSON.parse(resp.responseText) var fs = JSON.parse(resp.responseText)
@ -26,25 +33,30 @@ function(Paths, URI, CacheStorage) {
for (var i = 0; i < fs.length; i++) { for (var i = 0; i < fs.length; i++) {
var following = fs[i]; var following = fs[i];
followings_before_id = following.id; this.followings_before_id = following.id;
Cache.followings.setItem(following.entity, following) _this.followings.setItem(following.entity, following)
Cache.profiles.setItem(following.entity, following); _this.profiles.setItem(following.entity, following);
} }
} }
var u = Paths.mkApiRootPath("/followings"); var url = URI(require("helper/Paths").mkApiRootPath("/followings"));
if (this.followings_before_id) {
var url = URI(u); url.addSearch("before_id", this.followings_before_id);
if (followings_before_id) {
url.addSearch("before_id", 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; return Cache;
}); });

View file

@ -16,13 +16,13 @@ function() {
}; };
CacheStorage.prototype.getItem = function(key) { 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) { CacheStorage.prototype.setItem = function(key, value) {
var item = this.getItem(key); var item = this.getItem(key);
localStorage.setItem(this.mkPath(key), value); localStorage.setItem(this.mkPath(key), JSON.stringify(value));
if (!item) { if (!item) {
var length_path = this.mkInternalPath("_length"); var length_path = this.mkInternalPath("_length");

View file

@ -10,7 +10,7 @@ define([
function(jQuery, Paths, URI, HostApp, Cache) { function(jQuery, Paths, URI, HostApp, Cache) {
function Core() { function Core() {
this.cache = new Cache();
} }
Core.prototype.getTemplate = function() { Core.prototype.getTemplate = function() {
@ -188,7 +188,7 @@ function(jQuery, Paths, URI, HostApp, Cache) {
return false; return false;
} }
function profile(p) { var profile_callback = function(p) {
var basic = p["https://tent.io/types/info/basic/v0.1.0"]; 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") { if (p && p != "null") {
profile(p); profile_callback(p);
} else { } else {
@ -220,8 +220,8 @@ function(jQuery, Paths, URI, HostApp, Cache) {
var p = JSON.parse(resp.responseText); var p = JSON.parse(resp.responseText);
if (p && p != "null") { if (p && p != "null") {
Cache.profiles.setItem(status.entity, resp.responseText); _this.cache.profiles.setItem(status.entity, p);
profile(p); profile_callback(p);
} }
}, null, false); // do not send auth-headers }, null, false); // do not send auth-headers
@ -240,26 +240,29 @@ function(jQuery, Paths, URI, HostApp, Cache) {
return false; return false;
} }
var profile = JSON.parse(Cache.profiles.getItem(status.__repost.entity)) var repost_profile = this.cache.profiles.getItem(status.__repost.entity)
if (profile) { if (repost_profile) {
var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; var basic = repost_profile["https://tent.io/types/info/basic/v0.1.0"];
template.reposted_by.innerText = basic.name; if (basic) {
template.reposted_by.innerText = basic.name;
}
} else { } else {
Paths.findProfileURL(status.__repost.entity, function(profile_url) { Paths.findProfileURL(status.__repost.entity, function(profile_url) {
if (profile_url) { if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) { 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 }, 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") { if (status.type == "https://tent.io/types/post/photo/v0.1.0") {
debug(status.attachments)
for (var i = 0; i < status.attachments.length; i++) { for (var i = 0; i < status.attachments.length; i++) {
// closure needed for the callback // closure needed for the callback
(function() { (function() {
@ -315,11 +316,8 @@ function(jQuery, Paths, URI, HostApp, Cache) {
var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name); var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
Paths.getURL(url, "GET", callback, null, null, attachment.type); Paths.getURL(url, "GET", callback, null, null, attachment.type);
} else { } else {
Paths.findProfileURL(status.entity, function(profile_url) { var url = Paths.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
var url = profile_url + "/posts/" + status.id + "/attachments/" + attachment.name; Paths.getURL(url, "GET", callback, null, null, attachment.type);
debug(url)
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") { if (profile && profile != "null") {
var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0]; 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); 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) { if (p) {
profile(p); profile(p);
@ -624,11 +622,11 @@ function(jQuery, Paths, URI, HostApp, Cache) {
Paths.findProfileURL(mention.entity, function(profile_url) { Paths.findProfileURL(mention.entity, function(profile_url) {
if (profile_url) { if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) { Paths.getURL(profile_url, "GET", function(resp) {
if (resp.status >= 200 && resp.status < 400) {
Cache.profiles.setItem(mention.entity, resp.responseText); var p = JSON.parse(resp.responseText);
_this.cache.profiles.setItem(mention.entity, p);
var p = JSON.parse(resp.responseText); profile(p)
profile(p) }
}, null, false); // do not send auth-headers }, null, false); // do not send auth-headers
} }
}); });

View file

@ -8,6 +8,8 @@ define([
function(jQuery, HostApp, Hmac, Cache) { function(jQuery, HostApp, Hmac, Cache) {
var Paths = {}; var Paths = {};
Paths.cache = new Cache();
Paths.getUrlVars = function(url) { Paths.getUrlVars = function(url) {
var vars = [], hash; var vars = [], hash;
if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#")); 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) { 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") { if (profile_url && profile_url != "null") {
@ -129,7 +131,7 @@ function(jQuery, HostApp, Hmac, Cache) {
} }
if (profile_url) { if (profile_url) {
Cache.profile_urls.setItem(entity, profile_url); Paths.cache.profile_urls.setItem(entity, profile_url);
callback(profile_url); callback(profile_url);
} else { } else {
if(errorCallback) errorCallback(entity + " has no profile URL"); if(errorCallback) errorCallback(entity + " has no profile URL");