added Cache
This commit is contained in:
parent
dacd2ee019
commit
c3d1ce7d24
8 changed files with 280 additions and 105 deletions
|
@ -1,10 +1,11 @@
|
|||
define([
|
||||
"jquery",
|
||||
"helper/HostApp",
|
||||
"helper/Hmac"
|
||||
"helper/Hmac",
|
||||
"helper/Cache"
|
||||
],
|
||||
|
||||
function(jQuery, HostApp, Hmac) {
|
||||
function(jQuery, HostApp, Hmac, Cache) {
|
||||
var Paths = {};
|
||||
|
||||
Paths.getUrlVars = function(url) {
|
||||
|
@ -102,39 +103,51 @@ function(jQuery, HostApp, Hmac) {
|
|||
}
|
||||
|
||||
Paths.findProfileURL = function(entity, callback, errorCallback) {
|
||||
|
||||
jQuery.ajax({
|
||||
url: entity,
|
||||
type: "HEAD",
|
||||
complete: function(resp) {
|
||||
if(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/; // FIXME: parse it!
|
||||
var ret = headers.match(regex);
|
||||
var profile_url = null;
|
||||
if(ret && ret.length > 1) {
|
||||
var profile_url = ret[1];
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
|
||||
var profile_url = Cache.profile_urls.getItem(entity);
|
||||
|
||||
if (profile_url && profile_url != "null") {
|
||||
|
||||
callback(profile_url);
|
||||
|
||||
} else {
|
||||
|
||||
jQuery.ajax({
|
||||
url: entity,
|
||||
type: "HEAD",
|
||||
complete: function(resp) {
|
||||
if(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
|
||||
var profile_urls = Paths.parseHeaderForProfiles(headers);
|
||||
var profile_url = null;
|
||||
if(profile_urls.length > 0) {
|
||||
var profile_url = profile_urls[0];
|
||||
if (!profile_url.startsWith("http")) {
|
||||
profile_url = entity + "/profile";
|
||||
}
|
||||
}
|
||||
|
||||
if (profile_url) {
|
||||
Cache.profile_urls.setItem(entity, profile_url);
|
||||
callback(profile_url);
|
||||
} else {
|
||||
if(errorCallback) errorCallback(entity + " has no profile URL");
|
||||
}
|
||||
}
|
||||
|
||||
if (profile_url) {
|
||||
callback(profile_url);
|
||||
} else {
|
||||
if(errorCallback) errorCallback(entity + " has no profile URL");
|
||||
}
|
||||
},
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error("findProfileURL " + xhr.statusText + " (" + entity + "): " + xhr.responseText);
|
||||
if (errorCallback) errorCallback(xhr.statusText + " - " + xhr.responseText)
|
||||
}
|
||||
},
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error("findProfileURL " + xhr.statusText + " (" + entity + "): " + xhr.responseText);
|
||||
if (errorCallback) errorCallback(xhr.statusText + " - " + xhr.responseText)
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Paths.mkApiRootPath = function(path) {
|
||||
|
||||
var api_root = HostApp.stringForKey("api_root");
|
||||
|
||||
if((api_root.substring(api_root.length - 1, api_root.length) != "/") && (path.substring(0, 1) != "/")) {
|
||||
api_root += "/";
|
||||
} else if((api_root.substring(api_root.length - 1, api_root.length) == "/") && (path.substring(0, 1) == "/")) {
|
||||
|
@ -143,5 +156,33 @@ function(jQuery, HostApp, Hmac) {
|
|||
return api_root + path;
|
||||
}
|
||||
|
||||
Paths.parseHeaderForProfiles = function(header_string) {
|
||||
var headers = header_string.split(/\n/);
|
||||
var links = [];
|
||||
for (var i = 0; i < headers.length; i++) {
|
||||
var header = headers[i];
|
||||
if (header.match(/^Link:(.*)/i)) {
|
||||
links.push(header.replace(/\r/, "").substr(5).trim());
|
||||
}
|
||||
}
|
||||
|
||||
var items = [];
|
||||
for (var i = 0; i < links.length; i++) {
|
||||
items = items.concat(links[i].split(","));
|
||||
}
|
||||
var profiles = [];
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
var item = items[i];
|
||||
if (item.match(/https:\/\/tent.io\/rels\/profile/i)) {
|
||||
var n = item.match(/<([^>]*)>/);
|
||||
if (n) {
|
||||
profiles.push(n[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return profiles;
|
||||
}
|
||||
|
||||
return Paths;
|
||||
});
|
Reference in a new issue