2016-03-28 20:33:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-08 16:57:55 +00:00
|
|
|
const nprogress = require('nprogress');
|
2016-04-08 08:01:32 +00:00
|
|
|
const cookies = require('js-cookie');
|
2016-03-28 20:33:20 +00:00
|
|
|
const request = require('superagent');
|
|
|
|
const config = require('./config.js');
|
2016-04-07 17:03:49 +00:00
|
|
|
const events = require('./events.js');
|
2016-03-28 20:33:20 +00:00
|
|
|
|
2016-06-14 08:31:48 +00:00
|
|
|
class Api extends events.EventTarget {
|
2016-03-30 18:45:37 +00:00
|
|
|
constructor() {
|
2016-06-14 08:31:48 +00:00
|
|
|
super();
|
2016-03-30 20:05:57 +00:00
|
|
|
this.user = null;
|
2016-03-30 18:45:37 +00:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-05-08 14:59:25 +00:00
|
|
|
this.allRanks = [
|
|
|
|
'anonymous',
|
|
|
|
'restricted',
|
|
|
|
'regular',
|
|
|
|
'power',
|
|
|
|
'moderator',
|
|
|
|
'administrator',
|
|
|
|
'nobody',
|
|
|
|
];
|
2016-06-19 17:16:40 +00:00
|
|
|
this.rankNames = new Map([
|
|
|
|
['anonymous', 'Anonymous'],
|
|
|
|
['restricted', 'Restricted user'],
|
|
|
|
['regular', 'Regular user'],
|
|
|
|
['power', 'Power user'],
|
|
|
|
['moderator', 'Moderator'],
|
|
|
|
['administrator', 'Administrator'],
|
|
|
|
['nobody', 'Nobody'],
|
|
|
|
]);
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 09:16:25 +00:00
|
|
|
get(url, options) {
|
2016-04-11 16:45:58 +00:00
|
|
|
if (url in this.cache) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.cache[url]);
|
|
|
|
});
|
|
|
|
}
|
2016-05-22 09:16:25 +00:00
|
|
|
return this._process(url, request.get, {}, {}, options)
|
|
|
|
.then(response => {
|
|
|
|
this.cache[url] = response;
|
|
|
|
return Promise.resolve(response);
|
|
|
|
});
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 09:16:25 +00:00
|
|
|
post(url, data, files, options) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-05-22 09:16:25 +00:00
|
|
|
return this._process(url, request.post, data, files, options);
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 09:16:25 +00:00
|
|
|
put(url, data, files, options) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-05-22 09:16:25 +00:00
|
|
|
return this._process(url, request.put, data, files, options);
|
2016-04-07 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 20:44:04 +00:00
|
|
|
delete(url, data, options) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-08-06 20:44:04 +00:00
|
|
|
return this._process(url, request.delete, data, {}, options);
|
2016-04-09 07:52:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 09:16:25 +00:00
|
|
|
_process(url, requestFactory, data, files, options) {
|
|
|
|
options = options || {};
|
2016-09-03 23:25:19 +00:00
|
|
|
const [fullUrl, query] = this._getFullUrl(url);
|
2016-09-29 19:36:59 +00:00
|
|
|
|
|
|
|
let abortFunction = null;
|
|
|
|
|
|
|
|
let promise = new Promise((resolve, reject) => {
|
2016-04-10 13:55:56 +00:00
|
|
|
let req = requestFactory(fullUrl);
|
2016-09-29 19:36:59 +00:00
|
|
|
|
|
|
|
req.set('Accept', 'application/json');
|
2016-09-03 23:25:19 +00:00
|
|
|
if (query) {
|
|
|
|
req.query(query);
|
|
|
|
}
|
2016-04-10 13:55:56 +00:00
|
|
|
if (data) {
|
|
|
|
req.attach('metadata', new Blob([JSON.stringify(data)]));
|
|
|
|
}
|
|
|
|
if (files) {
|
|
|
|
for (let key of Object.keys(files)) {
|
2016-07-31 21:54:29 +00:00
|
|
|
req.attach(key, files[key] || new Blob());
|
2016-04-10 13:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 06:49:07 +00:00
|
|
|
try {
|
|
|
|
if (this.userName && this.userPassword) {
|
|
|
|
req.auth(
|
|
|
|
this.userName,
|
2016-09-24 06:49:47 +00:00
|
|
|
encodeURIComponent(this.userPassword)
|
|
|
|
.replace(/%([0-9A-F]{2})/g, (match, p1) => {
|
|
|
|
return String.fromCharCode('0x' + p1);
|
|
|
|
}));
|
2016-09-24 06:49:07 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
reject({
|
|
|
|
title: 'Authentication error',
|
|
|
|
description: 'Malformed credentials'});
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
2016-09-29 19:36:59 +00:00
|
|
|
|
|
|
|
if (!options.noProgress) {
|
|
|
|
nprogress.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
abortFunction = () => {
|
|
|
|
req.abort(); // does *NOT* call the callback passed in .end()
|
|
|
|
nprogress.done();
|
|
|
|
reject({
|
|
|
|
title: 'Cancelled',
|
|
|
|
description:
|
|
|
|
'The request was aborted due to user cancel.'});
|
|
|
|
};
|
|
|
|
|
|
|
|
req.end((error, response) => {
|
|
|
|
nprogress.done();
|
|
|
|
if (error) {
|
|
|
|
reject(response && response.body ? response.body : {
|
|
|
|
title: 'Networking error',
|
|
|
|
description: error.message});
|
|
|
|
} else {
|
|
|
|
resolve(response.body);
|
|
|
|
}
|
|
|
|
});
|
2016-03-28 20:33:20 +00:00
|
|
|
});
|
2016-09-29 19:36:59 +00:00
|
|
|
|
|
|
|
promise.abort = () => abortFunction();
|
|
|
|
|
|
|
|
return promise;
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:05:57 +00:00
|
|
|
hasPrivilege(lookup) {
|
|
|
|
let minViableRank = null;
|
|
|
|
for (let privilege of Object.keys(config.privileges)) {
|
|
|
|
if (!privilege.startsWith(lookup)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const rankName = config.privileges[privilege];
|
2016-05-08 14:59:25 +00:00
|
|
|
const rankIndex = this.allRanks.indexOf(rankName);
|
2016-03-30 20:05:57 +00:00
|
|
|
if (minViableRank === null || rankIndex < minViableRank) {
|
|
|
|
minViableRank = rankIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (minViableRank === null) {
|
2016-06-11 07:30:22 +00:00
|
|
|
throw `Bad privilege name: ${lookup}`;
|
2016-03-30 20:05:57 +00:00
|
|
|
}
|
|
|
|
let myRank = this.user !== null ?
|
2016-05-08 14:59:25 +00:00
|
|
|
this.allRanks.indexOf(this.user.rank) :
|
2016-03-30 20:05:57 +00:00
|
|
|
0;
|
|
|
|
return myRank >= minViableRank;
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 08:01:32 +00:00
|
|
|
loginFromCookies() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const auth = cookies.getJSON('auth');
|
|
|
|
if (auth && auth.user && auth.password) {
|
|
|
|
this.login(auth.user, auth.password, true)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
reject(errorMessage);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
login(userName, userPassword, doRemember) {
|
2016-06-19 19:37:44 +00:00
|
|
|
this.cache = {};
|
2016-03-30 19:01:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.userName = userName;
|
|
|
|
this.userPassword = userPassword;
|
2016-04-03 13:12:15 +00:00
|
|
|
this.get('/user/' + userName + '?bump-login=true')
|
2016-03-30 20:05:57 +00:00
|
|
|
.then(response => {
|
2016-04-08 08:01:32 +00:00
|
|
|
const options = {};
|
|
|
|
if (doRemember) {
|
|
|
|
options.expires = 365;
|
|
|
|
}
|
|
|
|
cookies.set(
|
|
|
|
'auth',
|
|
|
|
{'user': userName, 'password': userPassword},
|
|
|
|
options);
|
2016-05-30 20:20:42 +00:00
|
|
|
this.user = response;
|
2016-03-30 20:05:57 +00:00
|
|
|
resolve();
|
2016-06-14 08:31:48 +00:00
|
|
|
this.dispatchEvent(new CustomEvent('login'));
|
2016-06-19 19:37:44 +00:00
|
|
|
}, response => {
|
2016-09-24 06:49:07 +00:00
|
|
|
reject(response.description || response || 'Unknown error');
|
2016-03-30 19:01:18 +00:00
|
|
|
this.logout();
|
|
|
|
});
|
|
|
|
});
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
2016-04-01 11:09:07 +00:00
|
|
|
this.user = null;
|
2016-03-30 18:45:37 +00:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-06-14 08:31:48 +00:00
|
|
|
this.dispatchEvent(new CustomEvent('logout'));
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 17:53:53 +00:00
|
|
|
forget() {
|
|
|
|
cookies.remove('auth');
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
isLoggedIn(user) {
|
|
|
|
if (user) {
|
|
|
|
return this.userName !== null &&
|
|
|
|
this.userName.toLowerCase() === user.name.toLowerCase();
|
|
|
|
} else {
|
|
|
|
return this.userName !== null;
|
|
|
|
}
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 19:35:12 +00:00
|
|
|
_getFullUrl(url) {
|
2016-09-03 23:25:19 +00:00
|
|
|
const fullUrl =
|
|
|
|
(config.apiUrl + '/' + url).replace(/([^:])\/+/g, '$1/');
|
|
|
|
const matches = fullUrl.match(/^([^?]*)\??(.*)$/);
|
|
|
|
const baseUrl = matches[1];
|
|
|
|
const request = matches[2];
|
|
|
|
return [baseUrl, request];
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:20:34 +00:00
|
|
|
module.exports = new Api();
|