isso/isso/js/app/api.js

194 lines
5.5 KiB
JavaScript
Raw Normal View History

define(["q"], function(Q) {
"use strict";
2013-10-03 11:47:38 +00:00
Q.stopUnhandledRejectionTracking();
Q.longStackSupport = true;
2013-11-03 14:27:29 +00:00
var salt = "Eech7co8Ohloopo9Ol6baimi",
location = window.location.pathname;
2013-10-03 11:47:38 +00:00
var rules = {
"/": [200, 404],
"/new": [201, 202],
"/id/\\d+": [200, 403, 404],
"/id/\\d+/(like/dislike)": [200],
"/count": [200]
};
/*
* Detect Isso API endpoint. There are typically two use cases:
*
* 1. use minified, single-file JavaScript. The browser interprets
* scripts sequentially, thus we can safely use the last script
* tag. Then, we chop off some characters -- /js/embed.min.s --
* and we're done.
*
* If the script is not served by Isso directly, a custom data
* attribute can be used to override the default detection
* mechanism:
*
* .. code-block:: html
*
2013-11-05 13:25:12 +00:00
* <script data-isso="http://example.tld/path/" src="/.../embed.min.js"></script>
*/
2013-11-03 14:27:29 +00:00
var script, endpoint,
js = document.getElementsByTagName("script");
for (var i = 0; i < js.length; i++) {
if (js[i].hasAttribute("data-isso")) {
endpoint = js[i].getAttribute("data-isso");
break;
}
}
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) {
var xhr = new XMLHttpRequest();
var response = Q.defer();
function onload() {
2013-10-03 11:47:38 +00:00
var rule = url.replace(endpoint, "").split("?", 1)[0];
var cookie = xhr.getResponseHeader("X-Set-Cookie");
if (cookie && cookie.match(/^isso-/)) {
document.cookie = cookie;
}
2013-10-03 11:47:38 +00:00
if (rule in rules && rules[rule].indexOf(xhr.status) === -1) {
response.reject(xhr.responseText);
} else {
response.resolve({status: xhr.status, body: xhr.responseText});
}
}
try {
xhr.open(method, url, true);
xhr.withCredentials = true;
if (method === "GET") {
xhr.setRequestHeader("X-Origin", window.location.origin);
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
onload();
}
};
} catch (exception) {
response.reject(exception.message);
}
xhr.send(data);
return response.promise;
};
var qs = function(params) {
var rv = "";
for (var key in params) {
2013-10-03 16:56:36 +00:00
if (params.hasOwnProperty(key) && params[key]) {
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(data) {
2013-10-03 11:47:38 +00:00
return curl("POST", endpoint + "/new?" + qs({uri: location}), JSON.stringify(data)).then(
function (rv) { return JSON.parse(rv.body); });
};
2013-10-03 16:56:36 +00:00
var modify = function(id, data) {
return curl("PUT", endpoint + "/id/" + id, JSON.stringify(data)).then(
function (rv) { return JSON.parse(rv.body); });
};
var remove = function(id) {
2013-10-03 11:47:38 +00:00
return curl("DELETE", endpoint + "/id/" + id, null).then(function(rv) {
if (rv.status === 403) {
throw "Not authorized to remove this comment!";
}
2013-10-03 11:47:38 +00:00
return JSON.parse(rv.body) === null;
});
};
2013-10-03 16:56:36 +00:00
var view = function(id, plain) {
return curl("GET", endpoint + "/id/" + id + "?" + qs({plain: plain}), null).then(function (rv) {
return JSON.parse(rv.body);
});
};
var fetch = function(plain) {
2013-10-03 16:56:36 +00:00
return curl("GET", endpoint + "/?" + qs({uri: location, plain: plain}), null).then(function (rv) {
if (rv.status === 200) {
return JSON.parse(rv.body);
} else {
2013-10-03 11:47:38 +00:00
return [];
}
});
};
var count = function(uri) {
2013-10-03 11:47:38 +00:00
return curl("GET", endpoint + "/count?" + qs({uri: uri}), null).then(function(rv) {
return JSON.parse(rv.body);
});
};
var like = function(id) {
2013-10-03 11:47:38 +00:00
return curl("POST", endpoint + "/id/" + id + "/like", null).then(function(rv) {
return JSON.parse(rv.body);
2013-10-03 11:47:38 +00:00
});
};
var dislike = function(id) {
2013-10-03 11:47:38 +00:00
return curl("POST", endpoint + "/id/" + id + "/dislike", null).then(function(rv) {
return JSON.parse(rv.body);
});
};
var remote_addr = function() {
return curl("GET", endpoint + "/check-ip", null).then(function(rv) {
return rv.body;
2013-11-03 14:27:29 +00:00
});
};
return {
2013-10-03 11:47:38 +00:00
endpoint: endpoint,
salt: salt,
remote_addr: remote_addr,
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
2013-10-03 11:47:38 +00:00
};
});