2014-01-05 10:33:23 +00:00
|
|
|
define(["app/lib/promise"], function(Q) {
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2013-11-03 14:27:29 +00:00
|
|
|
var salt = "Eech7co8Ohloopo9Ol6baimi",
|
2013-09-05 17:31:18 +00:00
|
|
|
location = window.location.pathname;
|
|
|
|
|
2013-11-03 14:27:29 +00:00
|
|
|
var script, endpoint,
|
|
|
|
js = document.getElementsByTagName("script");
|
2013-11-02 17:26:33 +00:00
|
|
|
|
2014-01-05 10:33:23 +00:00
|
|
|
// prefer `data-isso="//host/api/endpoint"` if provided
|
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
|
|
|
|
2014-01-05 10:33:23 +00:00
|
|
|
// if no async-script is embedded, use the last script tag of `js`
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-05 10:33:23 +00:00
|
|
|
var curl = function(method, url, data, resolve, reject) {
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-09-12 15:26:10 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
2013-09-05 17:31:18 +00:00
|
|
|
|
|
|
|
function onload() {
|
2013-10-03 11:47:38 +00:00
|
|
|
|
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
|
|
|
|
2014-01-06 19:09:09 +00:00
|
|
|
if (xhr.status >= 500) {
|
|
|
|
reject(xhr.body);
|
|
|
|
} else {
|
|
|
|
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;
|
2013-12-04 22:19:51 +00:00
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
2013-11-17 10:16:46 +00:00
|
|
|
|
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) {
|
2014-01-05 10:33:23 +00:00
|
|
|
(reject || console.log)(exception.message);
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:26:10 +00:00
|
|
|
xhr.send(data);
|
2013-09-05 17:31:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-29 13:07:26 +00:00
|
|
|
var create = function(tid, data) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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-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) {
|
2014-01-05 10:33:23 +00:00
|
|
|
var deferred = Q.defer();
|
|
|
|
curl("PUT", endpoint + "/id/" + id, JSON.stringify(data), function (rv) {
|
2014-01-06 19:09:09 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-01-05 10:33:23 +00:00
|
|
|
});
|
|
|
|
return deferred.promise;
|
2013-10-01 12:42:00 +00:00
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
|
|
|
var remove = function(id) {
|
2014-01-05 10:33:23 +00:00
|
|
|
var deferred = Q.defer();
|
|
|
|
curl("DELETE", endpoint + "/id/" + id, null, function(rv) {
|
2013-10-03 11:47:38 +00:00
|
|
|
if (rv.status === 403) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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);
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
2013-10-01 12:42:00 +00:00
|
|
|
});
|
2014-01-05 10:33:23 +00:00
|
|
|
return deferred.promise;
|
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) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-11-29 13:07:26 +00:00
|
|
|
var fetch = function(tid) {
|
2014-01-05 10:33:23 +00:00
|
|
|
var deferred = Q.defer();
|
|
|
|
curl("GET", endpoint + "/?" + qs({uri: tid || location}), null, function(rv) {
|
2013-10-01 12:42:00 +00:00
|
|
|
if (rv.status === 200) {
|
2014-01-05 10:33:23 +00:00
|
|
|
deferred.resolve(JSON.parse(rv.body));
|
|
|
|
} else if (rv.status === 404) {
|
|
|
|
deferred.resolve([]);
|
2013-09-05 17:31:18 +00:00
|
|
|
} else {
|
2014-01-05 10:33:23 +00:00
|
|
|
deferred.reject(rv.body);
|
2013-09-05 17:31:18 +00:00
|
|
|
}
|
2013-10-01 12:42:00 +00:00
|
|
|
});
|
2014-01-05 10:33:23 +00:00
|
|
|
return deferred.promise;
|
2013-10-01 12:42:00 +00:00
|
|
|
};
|
2013-09-05 17:31:18 +00:00
|
|
|
|
2013-11-29 13:07:26 +00:00
|
|
|
var count = function(tid) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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
|
|
|
});
|
2014-01-05 10:33:23 +00:00
|
|
|
return deferred.promise;
|
2013-10-03 11:47:38 +00:00
|
|
|
};
|
2013-09-12 15:19:04 +00:00
|
|
|
|
2013-10-01 12:42:00 +00:00
|
|
|
var like = function(id) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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
|
|
|
};
|
2013-10-01 12:42:00 +00:00
|
|
|
|
|
|
|
var dislike = function(id) {
|
2014-01-05 10:33:23 +00:00
|
|
|
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
|
|
|
};
|
2013-10-01 12:42:00 +00:00
|
|
|
|
2013-11-02 17:41:00 +00:00
|
|
|
var remote_addr = function() {
|
2014-01-05 10:33:23 +00:00
|
|
|
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
|
|
|
});
|
2014-01-05 10:33:23 +00:00
|
|
|
return deferred.promise;
|
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
|
|
|
});
|