isso/isso/js/app/api.js

213 lines
6.6 KiB
JavaScript
Raw Normal View History

define(["app/lib/promise", "app/globals"], function(Q, globals) {
"use strict";
2013-11-03 14:27:29 +00:00
var salt = "Eech7co8Ohloopo9Ol6baimi",
location = window.location.pathname;
2013-11-03 14:27:29 +00:00
var script, endpoint,
js = document.getElementsByTagName("script");
// prefer `data-isso="//host/api/endpoint"` if provided
for (var i = 0; i < js.length; i++) {
if (js[i].hasAttribute("data-isso")) {
endpoint = js[i].getAttribute("data-isso");
break;
}
}
// if no async-script is embedded, use the last script tag of `js`
if (! endpoint) {
2013-11-03 14:27:29 +00:00
for (i = 0; i < js.length; i++) {
if (js[i].getAttribute("async") || js[i].getAttribute("defer")) {
2013-11-03 14:27:29 +00:00
throw "Isso's automatic configuration detection failed, please " +
"refer to https://github.com/posativ/isso#client-configuration " +
2013-11-05 22:35:38 +00:00
"and add a custom `data-isso` attribute.";
2013-11-03 14:27:29 +00:00
}
}
script = js[js.length - 1];
endpoint = script.src.substring(0, script.src.length - "/js/embed.min.js".length);
}
// strip trailing slash
if (endpoint[endpoint.length - 1] === "/") {
endpoint = endpoint.substring(0, endpoint.length - 1);
}
var curl = function(method, url, data, resolve, reject) {
var xhr = new XMLHttpRequest();
function onload() {
2013-10-03 11:47:38 +00:00
var date = xhr.getResponseHeader("Date");
if (date !== null) {
globals.offset.update(new Date(date));
}
var cookie = xhr.getResponseHeader("X-Set-Cookie");
if (cookie && cookie.match(/^isso-/)) {
document.cookie = cookie;
}
2013-10-03 11:47:38 +00:00
if (xhr.status >= 500) {
if (reject) {
reject(xhr.body);
}
} else {
resolve({status: xhr.status, body: xhr.responseText});
}
}
try {
xhr.open(method, url, true);
xhr.withCredentials = true;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
onload();
}
};
} catch (exception) {
(reject || console.log)(exception.message);
}
xhr.send(data);
};
var qs = function(params) {
var rv = "";
for (var key in params) {
if (params.hasOwnProperty(key) &&
params[key] !== null && typeof(params[key]) !== "undefined") {
rv += key + "=" + encodeURIComponent(params[key]) + "&";
}
}
return rv.substring(0, rv.length - 1); // chop off trailing "&"
2013-10-03 11:47:38 +00:00
};
var create = function(tid, data) {
var deferred = Q.defer();
curl("POST", endpoint + "/new?" + qs({uri: tid || location}), JSON.stringify(data),
function (rv) {
if (rv.status === 201 || rv.status === 202) {
deferred.resolve(JSON.parse(rv.body));
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};
2013-10-03 16:56:36 +00:00
var modify = function(id, data) {
var deferred = Q.defer();
curl("PUT", endpoint + "/id/" + id, JSON.stringify(data), function (rv) {
if (rv.status === 403) {
deferred.reject("Not authorized to modify this comment!");
} else if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};
var remove = function(id) {
var deferred = Q.defer();
curl("DELETE", endpoint + "/id/" + id, null, function(rv) {
2013-10-03 11:47:38 +00:00
if (rv.status === 403) {
deferred.reject("Not authorized to remove this comment!");
} else if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body) === null);
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};
2013-10-03 16:56:36 +00:00
var view = function(id, plain) {
var deferred = Q.defer();
curl("GET", endpoint + "/id/" + id + "?" + qs({plain: plain}), null,
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
return deferred.promise;
2013-10-03 16:56:36 +00:00
};
var fetch = function(tid, limit, nested_limit, parent, lastcreated) {
if (typeof(limit) === 'undefined') { limit = "inf"; }
if (typeof(nested_limit) === 'undefined') { nested_limit = "inf"; }
if (typeof(parent) === 'undefined') { parent = null; }
var query_dict = {uri: tid || location, after: lastcreated, parent: parent};
if(limit !== "inf") {
query_dict['limit'] = limit;
}
if(nested_limit !== "inf"){
query_dict['nested_limit'] = nested_limit;
}
var deferred = Q.defer();
curl("GET", endpoint + "/?" +
qs(query_dict), null, function(rv) {
if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else if (rv.status === 404) {
deferred.resolve({total_replies: 0});
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};
var count = function(urls) {
var deferred = Q.defer();
curl("POST", endpoint + "/count", JSON.stringify(urls), function(rv) {
if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else {
deferred.reject(rv.body);
}
2013-10-03 11:47:38 +00:00
});
return deferred.promise;
2013-10-03 11:47:38 +00:00
};
var like = function(id) {
var deferred = Q.defer();
curl("POST", endpoint + "/id/" + id + "/like", null,
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
return deferred.promise;
2013-10-03 11:47:38 +00:00
};
var dislike = function(id) {
var deferred = Q.defer();
curl("POST", endpoint + "/id/" + id + "/dislike", null,
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
return deferred.promise;
2013-10-03 11:47:38 +00:00
};
var feed = function(tid) {
return endpoint + "/feed?" + qs({uri: tid || location});
}
return {
2013-10-03 11:47:38 +00:00
endpoint: endpoint,
salt: salt,
create: create,
2013-10-03 16:56:36 +00:00
modify: modify,
remove: remove,
2013-10-03 16:56:36 +00:00
view: view,
fetch: fetch,
count: count,
like: like,
dislike: dislike,
feed: feed
2013-10-03 11:47:38 +00:00
};
});