2013-10-02 12:26:06 +00:00
|
|
|
define(["q"], function(Q) {
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2013-10-03 11:47:38 +00:00
|
|
|
Q.stopUnhandledRejectionTracking();
|
2013-09-05 17:31:18 +00:00
|
|
|
Q.longStackSupport = true;
|
|
|
|
|
2013-11-03 14:27:29 +00:00
|
|
|
var salt = "Eech7co8Ohloopo9Ol6baimi",
|
2013-09-05 17:31:18 +00:00
|
|
|
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]
|
|
|
|
};
|
|
|
|
|
2013-11-02 17:26:33 +00:00
|
|
|
/*
|
|
|
|
* 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-02 17:26:33 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-03 14:27:29 +00:00
|
|
|
var script, endpoint,
|
|
|
|
js = document.getElementsByTagName("script");
|
2013-11-02 17:26:33 +00:00
|
|
|
|
2013-09-05 17:31:18 +00:00
|
|
|
for (var i = 0; i < js.length; i++) {
|
2013-12-02 11:14:26 +00:00
|
|
|
if (js[i].hasAttribute("data-isso")) {
|
|
|
|
endpoint = js[i].getAttribute("data-isso");
|
|
|
|
break;
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
2013-09-12 15:26:10 +00:00
|
|
|
}
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-10-02 12:26:06 +00:00
|
|
|
if (! endpoint) {
|
2013-11-03 14:27:29 +00:00
|
|
|
for (i = 0; i < js.length; i++) {
|
2013-12-02 11:14:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 17:26:33 +00:00
|
|
|
script = js[js.length - 1];
|
2013-12-02 11:14:26 +00:00
|
|
|
endpoint = script.src.substring(0, script.src.length - "/js/embed.min.js".length);
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 11:14:26 +00:00
|
|
|
// strip trailing slash
|
2013-11-02 17:39:40 +00:00
|
|
|
if (endpoint[endpoint.length - 1] === "/") {
|
|
|
|
endpoint = endpoint.substring(0, endpoint.length - 1);
|
|
|
|
}
|
|
|
|
|
2013-09-05 17:31:18 +00:00
|
|
|
var curl = function(method, url, data) {
|
|
|
|
|
2013-09-12 15:26:10 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
2013-09-05 17:31:18 +00:00
|
|
|
var response = Q.defer();
|
|
|
|
|
|
|
|
function onload() {
|
2013-10-03 11:47:38 +00:00
|
|
|
|
|
|
|
var rule = url.replace(endpoint, "").split("?", 1)[0];
|
2013-11-05 11:20:17 +00:00
|
|
|
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});
|
|
|
|
}
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-09-12 15:26:10 +00:00
|
|
|
xhr.open(method, url, true);
|
2013-11-17 10:16:46 +00:00
|
|
|
xhr.withCredentials = true;
|
|
|
|
|
|
|
|
if (method === "GET") {
|
|
|
|
xhr.setRequestHeader("X-Origin", window.location.origin);
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:26:10 +00:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
2013-09-05 17:31:18 +00:00
|
|
|
onload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (exception) {
|
|
|
|
response.reject(exception.message);
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:26:10 +00:00
|
|
|
xhr.send(data);
|
2013-09-05 17:31:18 +00:00
|
|
|
return response.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
var qs = function(params) {
|
2013-10-01 12:42:00 +00:00
|
|
|
var rv = "";
|
2013-09-05 17:31:18 +00:00
|
|
|
for (var key in params) {
|
2013-10-03 16:56:36 +00:00
|
|
|
if (params.hasOwnProperty(key) && params[key]) {
|
2013-09-05 17:31:18 +00:00
|
|
|
rv += key + "=" + encodeURIComponent(params[key]) + "&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
return rv.substring(0, rv.length - 1); // chop off trailing "&"
|
2013-10-03 11:47:38 +00:00
|
|
|
};
|
2013-09-05 17:31:18 +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-01 12:42:00 +00:00
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
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); });
|
2013-10-01 12:42:00 +00:00
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
|
|
|
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-09-05 17:31:18 +00:00
|
|
|
}
|
2013-10-03 11:47:38 +00:00
|
|
|
|
|
|
|
return JSON.parse(rv.body) === null;
|
2013-10-01 12:42:00 +00:00
|
|
|
});
|
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
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-09-05 17:31:18 +00:00
|
|
|
|
2013-10-03 16:56:36 +00:00
|
|
|
return curl("GET", endpoint + "/?" + qs({uri: location, plain: plain}), null).then(function (rv) {
|
2013-10-01 12:42:00 +00:00
|
|
|
if (rv.status === 200) {
|
|
|
|
return JSON.parse(rv.body);
|
2013-09-05 17:31:18 +00:00
|
|
|
} else {
|
2013-10-03 11:47:38 +00:00
|
|
|
return [];
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
2013-10-01 12:42:00 +00:00
|
|
|
});
|
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-09-12 15:19:04 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
2013-09-12 15:19:04 +00:00
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
var like = function(id) {
|
2013-10-03 11:47:38 +00:00
|
|
|
return curl("POST", endpoint + "/id/" + id + "/like", null).then(function(rv) {
|
2013-10-01 12:42:00 +00:00
|
|
|
return JSON.parse(rv.body);
|
2013-10-03 11:47:38 +00:00
|
|
|
});
|
|
|
|
};
|
2013-10-01 12:42:00 +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);
|
|
|
|
});
|
|
|
|
};
|
2013-10-01 12:42:00 +00:00
|
|
|
|
2013-11-02 17:41:00 +00:00
|
|
|
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
|
|
|
});
|
2013-11-02 17:41:00 +00:00
|
|
|
};
|
2013-10-01 12:42:00 +00:00
|
|
|
|
2013-09-05 17:31:18 +00:00
|
|
|
return {
|
2013-10-03 11:47:38 +00:00
|
|
|
endpoint: endpoint,
|
|
|
|
salt: salt,
|
|
|
|
|
2013-11-02 17:41:00 +00:00
|
|
|
remote_addr: remote_addr,
|
|
|
|
|
2013-09-05 17:31:18 +00:00
|
|
|
create: create,
|
2013-10-03 16:56:36 +00:00
|
|
|
modify: modify,
|
2013-09-05 17:31:18 +00:00
|
|
|
remove: remove,
|
2013-10-03 16:56:36 +00:00
|
|
|
view: view,
|
2013-10-01 12:42:00 +00:00
|
|
|
fetch: fetch,
|
|
|
|
count: count,
|
|
|
|
like: like,
|
|
|
|
dislike: dislike
|
2013-10-03 11:47:38 +00:00
|
|
|
};
|
2013-10-31 11:33:19 +00:00
|
|
|
});
|