2016-03-28 22:33:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-08 18:57:55 +02:00
|
|
|
const nprogress = require('nprogress');
|
2016-04-08 10:01:32 +02:00
|
|
|
const cookies = require('js-cookie');
|
2016-03-28 22:33:20 +02:00
|
|
|
const request = require('superagent');
|
|
|
|
const config = require('./config.js');
|
2016-04-07 19:03:49 +02:00
|
|
|
const events = require('./events.js');
|
2016-03-28 22:33:20 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
class Api extends events.EventTarget {
|
2016-03-30 20:45:37 +02:00
|
|
|
constructor() {
|
2016-06-14 10:31:48 +02:00
|
|
|
super();
|
2016-03-30 22:05:57 +02:00
|
|
|
this.user = null;
|
2016-03-30 20:45:37 +02:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-04-11 18:45:58 +02:00
|
|
|
this.cache = {};
|
2016-05-08 16:59:25 +02:00
|
|
|
this.allRanks = [
|
|
|
|
'anonymous',
|
|
|
|
'restricted',
|
|
|
|
'regular',
|
|
|
|
'power',
|
|
|
|
'moderator',
|
|
|
|
'administrator',
|
|
|
|
'nobody',
|
|
|
|
];
|
2016-06-19 19:16:40 +02: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 20:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:16:25 +02:00
|
|
|
get(url, options) {
|
2016-04-11 18:45:58 +02:00
|
|
|
if (url in this.cache) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.cache[url]);
|
|
|
|
});
|
|
|
|
}
|
2016-05-22 11:16:25 +02:00
|
|
|
return this._process(url, request.get, {}, {}, options)
|
|
|
|
.then(response => {
|
|
|
|
this.cache[url] = response;
|
|
|
|
return Promise.resolve(response);
|
|
|
|
});
|
2016-03-28 22:33:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:16:25 +02:00
|
|
|
post(url, data, files, options) {
|
2016-04-11 18:45:58 +02:00
|
|
|
this.cache = {};
|
2016-05-22 11:16:25 +02:00
|
|
|
return this._process(url, request.post, data, files, options);
|
2016-03-28 22:33:20 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:16:25 +02:00
|
|
|
put(url, data, files, options) {
|
2016-04-11 18:45:58 +02:00
|
|
|
this.cache = {};
|
2016-05-22 11:16:25 +02:00
|
|
|
return this._process(url, request.put, data, files, options);
|
2016-04-07 22:54:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 22:44:04 +02:00
|
|
|
delete(url, data, options) {
|
2016-04-11 18:45:58 +02:00
|
|
|
this.cache = {};
|
2016-08-06 22:44:04 +02:00
|
|
|
return this._process(url, request.delete, data, {}, options);
|
2016-04-09 09:52:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-22 11:16:25 +02:00
|
|
|
_process(url, requestFactory, data, files, options) {
|
|
|
|
options = options || {};
|
2016-05-20 21:35:12 +02:00
|
|
|
const fullUrl = this._getFullUrl(url);
|
2016-03-28 22:33:20 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-05-22 11:16:25 +02:00
|
|
|
if (!options.noProgress) {
|
|
|
|
nprogress.start();
|
|
|
|
}
|
2016-04-10 15:55:56 +02:00
|
|
|
let req = requestFactory(fullUrl);
|
|
|
|
if (data) {
|
|
|
|
req.attach('metadata', new Blob([JSON.stringify(data)]));
|
|
|
|
}
|
|
|
|
if (files) {
|
|
|
|
for (let key of Object.keys(files)) {
|
2016-07-31 23:54:29 +02:00
|
|
|
req.attach(key, files[key] || new Blob());
|
2016-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 22:33:20 +02:00
|
|
|
if (this.userName && this.userPassword) {
|
|
|
|
req.auth(this.userName, this.userPassword);
|
|
|
|
}
|
|
|
|
req.set('Accept', 'application/json')
|
|
|
|
.end((error, response) => {
|
2016-05-08 18:57:55 +02:00
|
|
|
nprogress.done();
|
2016-03-28 22:33:20 +02:00
|
|
|
if (error) {
|
2016-04-02 18:57:17 +02:00
|
|
|
reject(response && response.body ? response.body : {
|
|
|
|
'title': 'Networking error',
|
|
|
|
'description': error.message});
|
2016-03-28 22:33:20 +02:00
|
|
|
} else {
|
|
|
|
resolve(response.body);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-30 22:05:57 +02: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 16:59:25 +02:00
|
|
|
const rankIndex = this.allRanks.indexOf(rankName);
|
2016-03-30 22:05:57 +02:00
|
|
|
if (minViableRank === null || rankIndex < minViableRank) {
|
|
|
|
minViableRank = rankIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (minViableRank === null) {
|
2016-06-11 09:30:22 +02:00
|
|
|
throw `Bad privilege name: ${lookup}`;
|
2016-03-30 22:05:57 +02:00
|
|
|
}
|
|
|
|
let myRank = this.user !== null ?
|
2016-05-08 16:59:25 +02:00
|
|
|
this.allRanks.indexOf(this.user.rank) :
|
2016-03-30 22:05:57 +02:00
|
|
|
0;
|
|
|
|
return myRank >= minViableRank;
|
2016-03-30 20:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 10:01:32 +02: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 21:37:44 +02:00
|
|
|
this.cache = {};
|
2016-03-30 21:01:18 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.userName = userName;
|
|
|
|
this.userPassword = userPassword;
|
2016-04-03 15:12:15 +02:00
|
|
|
this.get('/user/' + userName + '?bump-login=true')
|
2016-03-30 22:05:57 +02:00
|
|
|
.then(response => {
|
2016-04-08 10:01:32 +02:00
|
|
|
const options = {};
|
|
|
|
if (doRemember) {
|
|
|
|
options.expires = 365;
|
|
|
|
}
|
|
|
|
cookies.set(
|
|
|
|
'auth',
|
|
|
|
{'user': userName, 'password': userPassword},
|
|
|
|
options);
|
2016-05-30 22:20:42 +02:00
|
|
|
this.user = response;
|
2016-03-30 22:05:57 +02:00
|
|
|
resolve();
|
2016-06-14 10:31:48 +02:00
|
|
|
this.dispatchEvent(new CustomEvent('login'));
|
2016-06-19 21:37:44 +02:00
|
|
|
}, response => {
|
2016-03-30 21:01:18 +02:00
|
|
|
reject(response.description);
|
|
|
|
this.logout();
|
|
|
|
});
|
|
|
|
});
|
2016-03-30 20:45:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
2016-04-01 13:09:07 +02:00
|
|
|
this.user = null;
|
2016-03-30 20:45:37 +02:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-06-14 10:31:48 +02:00
|
|
|
this.dispatchEvent(new CustomEvent('logout'));
|
2016-03-30 20:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-09 19:53:53 +02:00
|
|
|
forget() {
|
|
|
|
cookies.remove('auth');
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:07:33 +02:00
|
|
|
isLoggedIn(user) {
|
|
|
|
if (user) {
|
|
|
|
return this.userName !== null &&
|
|
|
|
this.userName.toLowerCase() === user.name.toLowerCase();
|
|
|
|
} else {
|
|
|
|
return this.userName !== null;
|
|
|
|
}
|
2016-03-30 20:45:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_getFullUrl(url) {
|
2016-06-22 23:42:57 +02:00
|
|
|
return (config.apiUrl + '/' + encodeURI(url))
|
|
|
|
.replace(/([^:])\/+/g, '$1/');
|
2016-03-28 22:33:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
module.exports = new Api();
|