isso/isso/js/app/api.js

196 lines
5.9 KiB
JavaScript
Raw Normal View History

define(["app/lib/promise"], function(Q) {
"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 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) {
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) {
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(tid, data) {
var deferred = Q.defer();
curl("POST", endpoint + "/new?" + qs({uri: tid || location}), JSON.stringify(data),
function (rv) { deferred.resolve(JSON.parse(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) {
var deferred = Q.defer();
curl("GET", endpoint + "/?" + qs({uri: tid || location}), null, function(rv) {
if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else if (rv.status === 404) {
deferred.resolve([]);
} else {
deferred.reject(rv.body);
}
});
return deferred.promise;
};
var count = function(tid) {
var deferred = Q.defer();
curl("GET", endpoint + "/count?" + qs({uri: tid || location}), null, function(rv) {
if (rv.status === 200) {
deferred.resolve(JSON.parse(rv.body));
} else if (rv.status === 404) {
deferred.resolve(0);
} 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 remote_addr = function() {
var deferred = Q.defer();
curl("GET", endpoint + "/check-ip", null, function(rv) {
if (rv.status === 200) {
deferred.resolve(rv.body);
} else {
deferred.reject(rv.body);
}
2013-11-03 14:27:29 +00:00
});
return deferred.promise;
};
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
};
});