2014-03-04 14:48:44 +00:00
|
|
|
define(["app/lib/promise", "app/globals"], function(Q, globals) {
|
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
|
|
|
|
2014-03-28 10:44:03 +00:00
|
|
|
var date = xhr.getResponseHeader("Date");
|
|
|
|
if (date !== null) {
|
|
|
|
globals.offset.update(new Date(date));
|
|
|
|
}
|
2013-11-05 11:20:17 +00:00
|
|
|
|
2014-03-04 14:48:44 +00:00
|
|
|
var cookie = xhr.getResponseHeader("X-Set-Cookie");
|
2013-11-05 11:20:17 +00:00
|
|
|
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) {
|
2015-02-24 20:37:57 +00:00
|
|
|
if (reject) {
|
|
|
|
reject(xhr.body);
|
|
|
|
}
|
2014-01-06 19:09:09 +00:00
|
|
|
} 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) {
|
2014-05-03 08:48:57 +00:00
|
|
|
if (params.hasOwnProperty(key) &&
|
|
|
|
params[key] !== null && typeof(params[key]) !== "undefined") {
|
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),
|
2014-12-19 14:19:55 +00:00
|
|
|
function (rv) {
|
2014-12-25 20:08:10 +00:00
|
|
|
if (rv.status === 201 || rv.status === 202) {
|
2014-12-19 14:19:55 +00:00
|
|
|
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
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-04-27 06:14:18 +00:00
|
|
|
var fetch = function(tid, limit, nested_limit, parent, lastcreated) {
|
2014-05-03 08:48:57 +00:00
|
|
|
if (typeof(limit) === 'undefined') { limit = "inf"; }
|
|
|
|
if (typeof(nested_limit) === 'undefined') { nested_limit = "inf"; }
|
|
|
|
if (typeof(parent) === 'undefined') { parent = null; }
|
|
|
|
|
2014-05-02 19:47:11 +00:00
|
|
|
var query_dict = {uri: tid || location, after: lastcreated, parent: parent};
|
2014-05-03 08:48:57 +00:00
|
|
|
|
|
|
|
if(limit !== "inf") {
|
2014-05-02 19:47:11 +00:00
|
|
|
query_dict['limit'] = limit;
|
|
|
|
}
|
2014-05-03 08:48:57 +00:00
|
|
|
if(nested_limit !== "inf"){
|
2014-05-02 19:47:11 +00:00
|
|
|
query_dict['nested_limit'] = nested_limit;
|
|
|
|
}
|
2014-05-03 08:48:57 +00:00
|
|
|
|
2014-01-05 10:33:23 +00:00
|
|
|
var deferred = Q.defer();
|
2014-04-27 06:14:18 +00:00
|
|
|
curl("GET", endpoint + "/?" +
|
2014-05-02 19:47:11 +00:00
|
|
|
qs(query_dict), null, function(rv) {
|
2014-04-27 06:14:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2014-03-25 17:38:17 +00:00
|
|
|
var count = function(urls) {
|
2014-01-05 10:33:23 +00:00
|
|
|
var deferred = Q.defer();
|
2014-03-25 17:38:17 +00:00
|
|
|
curl("POST", endpoint + "/count", JSON.stringify(urls), function(rv) {
|
2014-01-05 10:33:23 +00:00
|
|
|
if (rv.status === 200) {
|
|
|
|
deferred.resolve(JSON.parse(rv.body));
|
|
|
|
} 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
|
|
|
|
2018-04-22 11:00:22 +00:00
|
|
|
var feed = function(tid) {
|
|
|
|
return endpoint + "/feed?" + qs({uri: tid || location});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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,
|
2018-04-22 11:00:22 +00:00
|
|
|
dislike: dislike,
|
|
|
|
feed: feed
|
2013-10-03 11:47:38 +00:00
|
|
|
};
|
2013-10-31 11:33:19 +00:00
|
|
|
});
|