api calls
This commit is contained in:
parent
422dbd34ff
commit
f73ce7d196
10 changed files with 371 additions and 86 deletions
|
@ -1,11 +1,11 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Core",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(HostApp, Core, Paths, URI) {
|
||||
function(HostApp, Core, APICalls, URI) {
|
||||
|
||||
|
||||
function Conversation(standalone) {
|
||||
|
@ -86,15 +86,15 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
function getRemoteStatus(profile) {
|
||||
var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0];
|
||||
Paths.getURL(URI(server + "/posts/" + id).toString(), "GET", callback, null, false);
|
||||
APICalls.http_call(URI(server + "/posts/" + id).toString(), "GET", callback, null, false);
|
||||
}
|
||||
|
||||
var profile = this.cache.profiles.getItem(entity);
|
||||
|
||||
if (entity == HostApp.stringForKey("entity")) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts/" + id));
|
||||
Paths.getURL(url.toString(), "GET", callback, null);
|
||||
var url = URI(APICalls.mkApiRootPath("/posts/" + id));
|
||||
APICalls.http_call(url.toString(), "GET", callback, null);
|
||||
|
||||
} else if(profile) {
|
||||
|
||||
|
@ -102,7 +102,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(entity, function(profile_url) {
|
||||
APICalls.findProfileURL(entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
|
||||
|
@ -113,7 +113,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
} else {
|
||||
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
|
||||
var profile = JSON.parse(resp.responseText)
|
||||
this.cache.profiles.setItem(entity, profile);
|
||||
|
@ -128,7 +128,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
Conversation.prototype.appendMentioned = function(id, entity, node) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
||||
url.addSearch("mentioned_post", id);
|
||||
url.addSearch("post_types", "https%3A%2F%2Ftent.io%2Ftypes%2Fpost%2Fstatus%2Fv0.1.0");
|
||||
|
||||
|
@ -147,7 +147,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
}
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "GET", callback);
|
||||
APICalls.http_call(url.toString(), "GET", callback);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,11 @@ define([
|
|||
"helper/HostApp",
|
||||
"controller/Timeline",
|
||||
"lib/URI",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"helper/Core"
|
||||
],
|
||||
|
||||
function(HostApp, Timeline, URI, Paths, Core) {
|
||||
function(HostApp, Timeline, URI, APICalls, Core) {
|
||||
|
||||
|
||||
function Mentions() {
|
||||
|
@ -90,7 +90,7 @@ function(HostApp, Timeline, URI, Paths, Core) {
|
|||
if (!status.__repost) {
|
||||
if (status && status.type == "https://tent.io/types/post/status/v0.1.0") {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
|
||||
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": {
|
||||
|
@ -104,7 +104,7 @@ function(HostApp, Timeline, URI, Paths, Core) {
|
|||
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "PUT", callback, JSON.stringify(body));
|
||||
APICalls.http_call(url.toString(), "PUT", callback, JSON.stringify(body));
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -115,11 +115,11 @@ function(HostApp, Timeline, URI, Paths, Core) {
|
|||
|
||||
Mentions.prototype.getLatestMentionRead = function() {
|
||||
|
||||
var cursor_url = URI(Paths.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
|
||||
var cursor_url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
|
||||
|
||||
Paths.getURL(cursor_url.toString(), "GET", function(resp) {
|
||||
APICalls.http_call(cursor_url.toString(), "GET", function(resp) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts/count"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts/count"));
|
||||
var post_types = [
|
||||
"https://tent.io/types/post/status/v0.1.0",
|
||||
];
|
||||
|
@ -139,7 +139,7 @@ function(HostApp, Timeline, URI, Paths, Core) {
|
|||
HostApp.unreadMentions(this.unread_mentions);
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "GET", callback); // FIXME: error callback
|
||||
APICalls.http_call(url.toString(), "GET", callback); // FIXME: error callback
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"helper/Hmac"
|
||||
],
|
||||
|
||||
function(HostApp, Paths, Hmac) {
|
||||
function(HostApp, APICalls, Hmac) {
|
||||
|
||||
function Oauth() {
|
||||
this.app_info = {
|
||||
|
@ -70,7 +70,7 @@ function(HostApp, Paths, Hmac) {
|
|||
|
||||
Oauth.prototype.requestProfileURL = function (entity) {
|
||||
var those = this;
|
||||
Paths.findProfileURL(entity,
|
||||
APICalls.findProfileURL(entity,
|
||||
function(profile_url) {
|
||||
if (profile_url && (profile_url.startsWith("http://") || profile_url.startsWith("https://"))) {
|
||||
those.register(profile_url);
|
||||
|
@ -87,7 +87,7 @@ function(HostApp, Paths, Hmac) {
|
|||
|
||||
Oauth.prototype.register = function (url) {
|
||||
var those = this;
|
||||
Paths.getURL(url, "GET", function(resp) {
|
||||
APICalls.get(url, { callback: function(resp) {
|
||||
|
||||
those.profile = JSON.parse(resp.responseText);
|
||||
those.entity = those.profile.content.entity;
|
||||
|
@ -98,16 +98,16 @@ function(HostApp, Paths, Hmac) {
|
|||
var app_id = JSON.parse(resp.responseText).id;
|
||||
var header_string = resp.getAllResponseHeaders();
|
||||
var regexp = /https:\/\/tent.io\/rels\/credentials/i
|
||||
var url = Paths.parseHeaderForLink(header_string, regexp);
|
||||
Paths.getURL(url, "GET", function(resp) {
|
||||
var url = APICalls.parseHeaderForLink(header_string, regexp);
|
||||
APICalls.http_call(url, "GET", function(resp) {
|
||||
var data = JSON.parse(resp.responseText);
|
||||
those.authRequest(data, app_id);
|
||||
}, null, false)
|
||||
}
|
||||
|
||||
Paths.getURL(HostApp.serverUrl("new_post"), "POST", callback, JSON.stringify(those.app_info), false);
|
||||
APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), {callback: callback});
|
||||
|
||||
}, null, false);
|
||||
}});
|
||||
}
|
||||
|
||||
Oauth.prototype.authRequest = function(credentials, app_id) {
|
||||
|
@ -126,7 +126,7 @@ function(HostApp, Paths, Hmac) {
|
|||
// /oauthtoken?code=51d0115b04d1ed94001dde751c5b360f&state=aQfH1VEohYsQr86qqyv
|
||||
// https://app.example.com/oauth?code=K4m2J2bGI9rcICBqmUCYuQ&state=d173d2bb868a
|
||||
|
||||
var urlVars = Paths.getUrlVars(responseBody);
|
||||
var urlVars = APICalls.getUrlVars(responseBody);
|
||||
if(this.state && this.state != "" && urlVars["state"] == this.state) {
|
||||
|
||||
var url = HostApp.serverUrl("oauth_token");
|
||||
|
@ -150,7 +150,7 @@ function(HostApp, Paths, Hmac) {
|
|||
requestBody
|
||||
);
|
||||
|
||||
Paths.getURL(url, http_method, callback, requestBody, auth_header);
|
||||
APICalls.http_call(url, http_method, callback, requestBody, auth_header);
|
||||
|
||||
} else {
|
||||
console.error("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}")
|
||||
|
@ -174,7 +174,7 @@ function(HostApp, Paths, Hmac) {
|
|||
|
||||
Oauth.prototype.logout = function() {
|
||||
|
||||
var url = Paths.mkApiRootPath("/apps/" + HostApp.stringForKey("app_id"));
|
||||
var url = APICalls.mkApiRootPath("/apps/" + HostApp.stringForKey("app_id"));
|
||||
var http_method = "DELETE";
|
||||
var auth_header = Hmac.makeAuthHeader(
|
||||
url,
|
||||
|
@ -183,7 +183,7 @@ function(HostApp, Paths, Hmac) {
|
|||
HostApp.stringForKey("app_mac_key_id")
|
||||
);
|
||||
|
||||
Paths.getURL(url, http_method, function(resp) {
|
||||
APICalls.http_call(url, http_method, function(resp) {
|
||||
HostApp.setStringForKey(null, "app_mac_key");
|
||||
HostApp.setStringForKey(null, "app_mac_key_id");
|
||||
HostApp.setStringForKey(null, "app_id");
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Core",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(HostApp, Core, Paths, URI) {
|
||||
function(HostApp, Core, APICalls, URI) {
|
||||
|
||||
|
||||
function Profile() {
|
||||
|
@ -239,11 +239,11 @@ function(HostApp, Core, Paths, URI) {
|
|||
this.profile = profile;
|
||||
|
||||
} else {
|
||||
Paths.findProfileURL(this.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(this.entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
|
||||
profile = JSON.parse(resp.responseText);
|
||||
_this.showProfile(profile);
|
||||
|
@ -258,9 +258,9 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
Profile.prototype.getFollowing = function() {
|
||||
if(this.entity != HostApp.stringForKey("entity")) {
|
||||
var url = Paths.mkApiRootPath("/followings") + "/" + encodeURIComponent(this.entity);
|
||||
var url = APICalls.mkApiRootPath("/followings") + "/" + encodeURIComponent(this.entity);
|
||||
var _this = this;
|
||||
Paths.getURL(url, "GET", function(resp) {
|
||||
APICalls.http_call(url, "GET", function(resp) {
|
||||
if (resp.status >= 200 && resp.status < 400) {
|
||||
var following = JSON.parse(resp.responseText);
|
||||
_this.following_id = following.id
|
||||
|
@ -325,18 +325,18 @@ function(HostApp, Core, Paths, URI) {
|
|||
Profile.prototype.getMeta = function(root_url) {
|
||||
|
||||
var _this = this;
|
||||
Paths.getURL(URI(root_url + "/followings/count").toString(), "GET", function(resp) {
|
||||
APICalls.http_call(URI(root_url + "/followings/count").toString(), "GET", function(resp) {
|
||||
|
||||
_this.populate(_this.profile_template.following, resp.responseText);
|
||||
}, null, false);
|
||||
|
||||
Paths.getURL(URI(root_url + "/followers/count").toString(), "GET", function(resp) {
|
||||
APICalls.http_call(URI(root_url + "/followers/count").toString(), "GET", function(resp) {
|
||||
|
||||
_this.populate(_this.profile_template.followed, resp.responseText);
|
||||
}, null, false);
|
||||
|
||||
if (this.entity != HostApp.stringForKey("entity")) {
|
||||
Paths.getURL(URI(root_url + "/followers/" + encodeURIComponent(HostApp.stringForKey("entity"))).toString(), "GET", function(resp) {
|
||||
APICalls.http_call(URI(root_url + "/followers/" + encodeURIComponent(HostApp.stringForKey("entity"))).toString(), "GET", function(resp) {
|
||||
if (resp.status == 200) {
|
||||
_this.relationships.following_you = true;
|
||||
}
|
||||
|
@ -344,7 +344,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
}, null, false);
|
||||
|
||||
Paths.getURL(URI(Paths.mkApiRootPath("/followings/" + encodeURIComponent(this.entity))), "GET", function(resp) {
|
||||
APICalls.http_call(URI(APICalls.mkApiRootPath("/followings/" + encodeURIComponent(this.entity))), "GET", function(resp) {
|
||||
if (resp.status == 200) {
|
||||
_this.relationships.followed_by_you = true;
|
||||
}
|
||||
|
@ -363,7 +363,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
];
|
||||
url.addSearch("post_types", post_types.join(","));
|
||||
|
||||
Paths.getURL(url.toString(), "GET", function(resp) {
|
||||
APICalls.http_call(url.toString(), "GET", function(resp) {
|
||||
|
||||
_this.populate(_this.profile_template.posts, resp.responseText);
|
||||
}, null, false);
|
||||
|
@ -405,7 +405,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
url.addSearch(key, add_search[key]);
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "GET", function(resp) {
|
||||
APICalls.http_call(url.toString(), "GET", function(resp) {
|
||||
|
||||
var statuses = JSON.parse(resp.responseText);
|
||||
|
||||
|
@ -495,8 +495,8 @@ function(HostApp, Core, Paths, URI) {
|
|||
if (this.following_id) {
|
||||
|
||||
this.setFollowingButton(false);
|
||||
var url = Paths.mkApiRootPath("/followings/") + this.following_id;
|
||||
Paths.getURL(url, "DELETE", function(resp) {
|
||||
var url = APICalls.mkApiRootPath("/followings/") + this.following_id;
|
||||
APICalls.http_call(url, "DELETE", function(resp) {
|
||||
if (resp.status >= 200 && resp.status < 300) {
|
||||
_this.setFollowingButton(false);
|
||||
_this.following_id = null;
|
||||
|
@ -508,10 +508,10 @@ function(HostApp, Core, Paths, URI) {
|
|||
} else {
|
||||
|
||||
this.setFollowingButton(true);
|
||||
var url = URI(Paths.mkApiRootPath("/followings"));
|
||||
var url = URI(APICalls.mkApiRootPath("/followings"));
|
||||
var data = JSON.stringify({"entity": this.entity });
|
||||
|
||||
Paths.getURL(url.toString(), "POST", function(resp) {
|
||||
APICalls.http_call(url.toString(), "POST", function(resp) {
|
||||
if (resp.status >= 200 && resp.status < 300) {
|
||||
_this.following_id = JSON.parse(resp.responseText).id
|
||||
_this.setFollowingButton(true);
|
||||
|
@ -542,7 +542,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
var url = URI(this.server + "/followings");
|
||||
url.addSearch("limit", 200);
|
||||
Paths.getURL(url.toString(), "GET", callback, null, false);
|
||||
APICalls.http_call(url.toString(), "GET", callback, null, false);
|
||||
}
|
||||
|
||||
Profile.prototype.showFollowers = function() {
|
||||
|
@ -561,7 +561,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
var url = URI(this.server + "/followers");
|
||||
url.addSearch("limit", 200);
|
||||
Paths.getURL(url.toString(), "GET", callback, null, false);
|
||||
APICalls.http_call(url.toString(), "GET", callback, null, false);
|
||||
}
|
||||
|
||||
Profile.prototype.getDOMSmallProfile = function(profile) {
|
||||
|
@ -643,10 +643,10 @@ function(HostApp, Core, Paths, URI) {
|
|||
} else {
|
||||
|
||||
var _this = this;
|
||||
Paths.findProfileURL(profile.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(profile.entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
if (p && p != "null") {
|
||||
_this.cache.profiles.setItem(profile.entity, p);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Core",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(HostApp, Core, Paths, URI) {
|
||||
function(HostApp, Core, APICalls, URI) {
|
||||
|
||||
|
||||
function Search() {
|
||||
|
@ -79,7 +79,7 @@ function(HostApp, Core, Paths, URI) {
|
|||
|
||||
var _this = this;
|
||||
|
||||
Paths.getURL(url.toString(), "GET", function(resp) {
|
||||
APICalls.http_call(url.toString(), "GET", function(resp) {
|
||||
|
||||
var results = JSON.parse(resp.responseText).results;
|
||||
if (results && results.length > 0) {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"helper/Cache"
|
||||
],
|
||||
|
||||
function(HostApp, Paths, Cache) {
|
||||
function(HostApp, APICalls, Cache) {
|
||||
|
||||
|
||||
function Sidebar() {
|
||||
|
@ -121,10 +121,10 @@ function(HostApp, Paths, Cache) {
|
|||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(entity, function(profile_url) {
|
||||
APICalls.findProfileURL(entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
if (p && p != "null") {
|
||||
_this.cache.profiles.setItem(entity, p);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
define([
|
||||
"helper/Core",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"helper/HostApp",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(Core, Paths, HostApp, URI) {
|
||||
function(Core, APICalls, HostApp, URI) {
|
||||
|
||||
function Timeline() {
|
||||
|
||||
|
@ -100,7 +100,7 @@ function(Core, Paths, HostApp, URI) {
|
|||
add_to_search = add_to_search || {};
|
||||
|
||||
var those = this;
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
||||
|
||||
var post_types = [
|
||||
"https://tent.io/types/post/repost/v0.1.0",
|
||||
|
@ -143,7 +143,7 @@ function(Core, Paths, HostApp, URI) {
|
|||
|
||||
if (!this.reload_blocked) {
|
||||
this.reload_blocked = true;
|
||||
Paths.getURL(url.toString(), http_method, callback, data); // FIXME: error callback
|
||||
APICalls.http_call(url.toString(), http_method, callback, data); // FIXME: error callback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
285
WebKit/scripts/helper/APICalls.js
Normal file
285
WebKit/scripts/helper/APICalls.js
Normal file
|
@ -0,0 +1,285 @@
|
|||
define([
|
||||
"jquery",
|
||||
"helper/HostApp",
|
||||
"helper/Hmac",
|
||||
"helper/Cache"
|
||||
],
|
||||
|
||||
function(jQuery, HostApp, Hmac, Cache) {
|
||||
var APICalls = {};
|
||||
|
||||
APICalls.cache = new Cache();
|
||||
|
||||
APICalls.getUrlVars = function(url) {
|
||||
var vars = [], hash;
|
||||
if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#"));
|
||||
var hashes = url.slice(url.indexOf('?') + 1).split('&');
|
||||
for(var i = 0; i < hashes.length; i++)
|
||||
{
|
||||
hash = hashes[i].split('=');
|
||||
vars.push(hash[0]);
|
||||
vars[hash[0]] = hash[1];
|
||||
}
|
||||
return vars;
|
||||
}
|
||||
/*
|
||||
APICalls.http_call = function(url, http_method, callback, data, auth_header, accepts) {
|
||||
|
||||
if(accepts !== false) accepts = accepts || "application/vnd.tent.post.v0+json";
|
||||
|
||||
var options = {
|
||||
|
||||
beforeSend: function(xhr) {
|
||||
|
||||
if(accepts !== false) xhr.setRequestHeader("Accept", accepts);
|
||||
|
||||
if (data) xhr.setRequestHeader("Content-Length", data.length);
|
||||
|
||||
if (auth_header) { // if is_set? auth_header
|
||||
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
|
||||
} else {
|
||||
|
||||
var user_access_token = HostApp.stringForKey("user_access_token");
|
||||
|
||||
if (auth_header !== false && typeof user_access_token != "undefined") {
|
||||
|
||||
auth_header = Hmac.makeAuthHeader(
|
||||
url,
|
||||
http_method,
|
||||
HostApp.secret(),
|
||||
user_access_token
|
||||
);
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
}
|
||||
}
|
||||
},
|
||||
url: url,
|
||||
contentType: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/app/v0#"',
|
||||
type: http_method,
|
||||
complete: callback,
|
||||
data: data,
|
||||
processData: false,
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error("getURL (" + xhr.status + ")" + xhr.statusText + " " + http_method + " (" + url + "): '" + xhr.responseText + "'");
|
||||
}
|
||||
}
|
||||
debug(url)
|
||||
jQuery.ajax(options);
|
||||
}
|
||||
*/
|
||||
APICalls.http_call = function(options) {
|
||||
|
||||
if(!options.content_type) {
|
||||
console.error("No content type for " + options.url);
|
||||
return;
|
||||
}
|
||||
|
||||
var settings = {
|
||||
beforeSend: function(xhr) {
|
||||
if (options.data) xhr.setRequestHeader("Content-Length", data.length);
|
||||
if (options.accept) xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
|
||||
var user_access_token = HostApp.stringForKey("user_access_token");
|
||||
if (!no_auth && user_access_token) {
|
||||
var auth_header = Hmac.makeHawkAuthHeader(
|
||||
options.url,
|
||||
options.http_method,
|
||||
HostApp.secret(),
|
||||
user_access_token
|
||||
);
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
} else {
|
||||
console.error("No user_access_token yet - " + options.url);
|
||||
}
|
||||
}
|
||||
url: options.url,
|
||||
contentType: options.content_type,
|
||||
type: url.http_method,
|
||||
complete: options.callback,
|
||||
data: options.data,
|
||||
processData: false,
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error("HTTP CALL (" + xhr.status + ")" + xhr.statusText + " " + options.http_method + " (" + options.url + "): '" + xhr.responseText + "'");
|
||||
}
|
||||
};
|
||||
|
||||
jQuery.ajax(settings);
|
||||
}
|
||||
|
||||
APICalls.get = function(url, options) {
|
||||
var settings = {
|
||||
url: url,
|
||||
http_method: "GET",
|
||||
accept: null,
|
||||
data: null,
|
||||
no_auth: false
|
||||
content_type: null
|
||||
};
|
||||
|
||||
jQuery.extend(settings, options);
|
||||
|
||||
APICalls.http_call(settings);
|
||||
}
|
||||
|
||||
APICalls.post = function(url, data, options) {
|
||||
var settings = {
|
||||
url: url,
|
||||
http_method: "POST",
|
||||
data: data
|
||||
};
|
||||
|
||||
jQuery.extend(settings, options);
|
||||
|
||||
APICalls.http_call(settings);
|
||||
}
|
||||
|
||||
APICalls.postMultipart = function(url, callback, data, boundary, accepts) {
|
||||
|
||||
accepts = accepts || "application/vnd.tent.v0+json";
|
||||
|
||||
jQuery.ajax({
|
||||
|
||||
beforeSend: function(xhr) {
|
||||
xhr.setRequestHeader("Accept", accepts);
|
||||
|
||||
if (data) xhr.setRequestHeader("Content-Length", data.length);
|
||||
|
||||
var user_access_token = HostApp.stringForKey("user_access_token");
|
||||
|
||||
if (user_access_token) {
|
||||
|
||||
auth_header = Hmac.makeAuthHeader(
|
||||
url,
|
||||
"POST",
|
||||
HostApp.secret(),
|
||||
user_access_token
|
||||
);
|
||||
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
}
|
||||
},
|
||||
url: url,
|
||||
contentType: "multipart/form-data;boundary=" + boundary,
|
||||
type: "POST",
|
||||
complete: callback,
|
||||
data: data,
|
||||
processData: false,
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
console.error("postMultipart (" + xhr.status + ")" + xhr.statusText + " (" + url + "): '" + xhr.responseText + "'");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
APICalls.findProfileURL = function(entity, callback, errorCallback) {
|
||||
var profile_url = APICalls.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 = APICalls.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_url;
|
||||
}
|
||||
}
|
||||
|
||||
if (profile_url) {
|
||||
APICalls.cache.profile_urls.setItem(entity, profile_url);
|
||||
callback(profile_url);
|
||||
} else {
|
||||
APICalls.http_call(entity, "GET", function(resp) {
|
||||
|
||||
if (resp.status >= 200 && resp.status < 300) {
|
||||
var doc = document.implementation.createHTMLDocument("");
|
||||
doc.documentElement.innerHTML = resp.responseText;
|
||||
var links = $(doc).find("link[rel='https://tent.io/rels/meta-post']");
|
||||
|
||||
if (links.length > 0) {
|
||||
var href = links.get(0).href;
|
||||
APICalls.cache.profile_urls.setItem(entity, href);
|
||||
if (!href.startsWith("http")) {
|
||||
href = entity + href;
|
||||
}
|
||||
callback(href);
|
||||
|
||||
} else {
|
||||
if(errorCallback) errorCallback(entity + " has no profile URL");
|
||||
}
|
||||
} else {
|
||||
if(errorCallback) errorCallback(entity + " has no profile URL");
|
||||
}
|
||||
|
||||
}, null, false, false);
|
||||
|
||||
//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)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
APICalls.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) == "/")) {
|
||||
api_root = api_root.substring(0, api_root.length -1);
|
||||
}
|
||||
return api_root + path;
|
||||
}
|
||||
|
||||
APICalls.parseHeaderForProfiles = function(header_string) {
|
||||
var regexp = /https:\/\/tent.io\/rels\/meta-post/i;
|
||||
return APICalls.parseHeaderForLink(header_string, regexp);
|
||||
}
|
||||
|
||||
APICalls.parseHeaderForLink = function(header_string, match) {
|
||||
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 things = [];
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
var item = items[i];
|
||||
if (item.match(match)) {
|
||||
var n = item.match(/<([^>]*)>/);
|
||||
if (n) {
|
||||
things.push(n[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return things;
|
||||
}
|
||||
|
||||
return APICalls;
|
||||
});
|
|
@ -40,12 +40,12 @@ function(URI, CacheStorage, require) {
|
|||
}
|
||||
}
|
||||
|
||||
var url = URI(require("helper/Paths").mkApiRootPath("/followings"));
|
||||
var url = URI(require("helper/APICalls").mkApiRootPath("/followings"));
|
||||
if (this.followings_before_id) {
|
||||
url.addSearch("before_id", this.followings_before_id);
|
||||
}
|
||||
|
||||
require("helper/Paths").getURL(url, "GET", callback);
|
||||
require("helper/APICalls").getURL(url, "GET", callback);
|
||||
}
|
||||
|
||||
Cache.prototype.periodicallyGetFollowings = function() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
define([
|
||||
"jquery",
|
||||
"helper/Paths",
|
||||
"helper/APICalls",
|
||||
"lib/URI",
|
||||
"helper/HostApp",
|
||||
"helper/Cache",
|
||||
|
@ -8,7 +8,7 @@ define([
|
|||
"lib/SingleDoubleClick"
|
||||
],
|
||||
|
||||
function(jQuery, Paths, URI, HostApp, Cache) {
|
||||
function(jQuery, APICalls, URI, HostApp, Cache) {
|
||||
|
||||
function Core() {
|
||||
this.cache = new Cache();
|
||||
|
@ -247,10 +247,10 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(status.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(status.entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
if (p && p != "null") {
|
||||
_this.cache.profiles.setItem(status.entity, p);
|
||||
|
@ -308,11 +308,11 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
}
|
||||
|
||||
if (status.entity == HostApp.stringForKey("entity")) {
|
||||
var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
|
||||
Paths.getURL(url, "GET", callback, null, null, attachment.type);
|
||||
var url = APICalls.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
|
||||
APICalls.http_call(url, "GET", callback, null, null, attachment.type);
|
||||
} else {
|
||||
var url = Paths.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
|
||||
Paths.getURL(url, "GET", callback, null, null, attachment.type);
|
||||
var url = APICalls.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
|
||||
APICalls.http_call(url, "GET", callback, null, null, attachment.type);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
@ -431,9 +431,9 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
});
|
||||
|
||||
var _this = this;
|
||||
Paths.findProfileURL(repost.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(repost.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
if (resp.status >= 200 && resp.status < 400) {
|
||||
var _p = JSON.parse(resp.responseText);
|
||||
_this.cache.profiles.setItem(repost.entity, _p);
|
||||
|
@ -460,14 +460,14 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
}
|
||||
}
|
||||
|
||||
Paths.findProfileURL(repost.content.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(repost.content.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
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);
|
||||
APICalls.http_call(URI(server + "/posts/" + repost.content.id).toString(), "GET", callback, null, false);
|
||||
|
||||
}, null, false); // do not send auth-headers
|
||||
}
|
||||
|
@ -483,7 +483,7 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
|
||||
} else {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
||||
|
||||
var http_method = "POST";
|
||||
|
||||
|
@ -517,13 +517,13 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
}
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), http_method, callback, JSON.stringify(data));
|
||||
APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Core.prototype.repost = function(id, entity, callback) {
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
||||
|
||||
var data = {
|
||||
"type": "https://tent.io/types/post/repost/v0.1.0",
|
||||
|
@ -549,12 +549,12 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
_this.highlight(id);
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "POST", new_callback, JSON.stringify(data));
|
||||
APICalls.http_call(url.toString(), "POST", new_callback, JSON.stringify(data));
|
||||
}
|
||||
|
||||
Core.prototype.sendNewMessageWithImage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
var url = URI(APICalls.mkApiRootPath("/posts"));
|
||||
|
||||
var data = {
|
||||
"type": "https://tent.io/types/post/photo/v0.1.0",
|
||||
|
@ -623,14 +623,14 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
callback(resp);
|
||||
}
|
||||
|
||||
Paths.postMultipart(url.toString(), newCallback, post, boundary);
|
||||
APICalls.postMultipart(url.toString(), newCallback, post, boundary);
|
||||
}
|
||||
|
||||
Core.prototype.remove = function(id, callback, type) {
|
||||
type = type || "post";
|
||||
if (confirm("Really delete this " + type + "?")) {
|
||||
var url = URI(Paths.mkApiRootPath("/posts/" + id));
|
||||
Paths.getURL(url.toString(), "DELETE", callback);
|
||||
var url = URI(APICalls.mkApiRootPath("/posts/" + id));
|
||||
APICalls.http_call(url.toString(), "DELETE", callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -738,9 +738,9 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(mention.entity, function(profile_url) {
|
||||
APICalls.findProfileURL(mention.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
APICalls.http_call(profile_url, "GET", function(resp) {
|
||||
if (resp.status >= 200 && resp.status < 400) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
_this.cache.profiles.setItem(mention.entity, p);
|
||||
|
@ -838,7 +838,7 @@ function(jQuery, Paths, URI, HostApp, Cache) {
|
|||
|
||||
} else if(word.startsWith("http://youtube.com/") || word.startsWith("http://www.youtube.com/") || word.startsWith("https://youtube.com/") || word.startsWith("https://www.youtube.com/")) {
|
||||
|
||||
var v = Paths.getUrlVars(word)["v"];
|
||||
var v = APICalls.getUrlVars(word)["v"];
|
||||
this.addYouTube(v, images);
|
||||
|
||||
} else if (word.startsWith("http://youtu.be/") || word.startsWith("https://youtu.be/")) {
|
||||
|
|
Reference in a new issue