szurubooru/client/js/api.js

185 lines
5.6 KiB
JavaScript
Raw Normal View History

'use strict';
const nprogress = require('nprogress');
const cookies = require('js-cookie');
const request = require('superagent');
const config = require('./config.js');
2016-04-07 19:03:49 +02:00
const events = require('./events.js');
class Api extends events.EventTarget {
2016-03-30 20:45:37 +02:00
constructor() {
super();
this.user = null;
2016-03-30 20:45:37 +02:00
this.userName = null;
this.userPassword = null;
this.cache = {};
2016-05-08 16:59:25 +02:00
this.allRanks = [
'anonymous',
'restricted',
'regular',
'power',
'moderator',
'administrator',
'nobody',
];
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
}
get(url, options) {
if (url in this.cache) {
return new Promise((resolve, reject) => {
resolve(this.cache[url]);
});
}
return this._process(url, request.get, {}, {}, options)
.then(response => {
this.cache[url] = response;
return Promise.resolve(response);
});
}
post(url, data, files, options) {
this.cache = {};
return this._process(url, request.post, data, files, options);
}
put(url, data, files, options) {
this.cache = {};
return this._process(url, request.put, data, files, options);
}
delete(url, data, options) {
this.cache = {};
return this._process(url, request.delete, data, {}, options);
2016-04-09 09:52:00 +02:00
}
_process(url, requestFactory, data, files, options) {
options = options || {};
const fullUrl = this._getFullUrl(url);
return new Promise((resolve, reject) => {
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)) {
req.attach(key, files[key] || new Blob());
2016-04-10 15:55:56 +02:00
}
}
if (this.userName && this.userPassword) {
req.auth(this.userName, this.userPassword);
}
req.set('Accept', 'application/json')
.end((error, response) => {
nprogress.done();
if (error) {
2016-04-02 18:57:17 +02:00
reject(response && response.body ? response.body : {
'title': 'Networking error',
'description': error.message});
} else {
resolve(response.body);
}
});
});
}
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);
if (minViableRank === null || rankIndex < minViableRank) {
minViableRank = rankIndex;
}
}
if (minViableRank === null) {
throw `Bad privilege name: ${lookup}`;
}
let myRank = this.user !== null ?
2016-05-08 16:59:25 +02:00
this.allRanks.indexOf(this.user.rank) :
0;
return myRank >= minViableRank;
2016-03-30 20:45:37 +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) {
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')
.then(response => {
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;
resolve();
this.dispatchEvent(new CustomEvent('login'));
}, response => {
2016-03-30 21:01:18 +02:00
reject(response.description);
this.logout();
});
});
2016-03-30 20:45:37 +02:00
}
logout() {
this.user = null;
2016-03-30 20:45:37 +02:00
this.userName = null;
this.userPassword = null;
this.dispatchEvent(new CustomEvent('logout'));
2016-03-30 20:45:37 +02:00
}
forget() {
cookies.remove('auth');
}
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
}
_getFullUrl(url) {
return (config.apiUrl + '/' + encodeURI(url))
.replace(/([^:])\/+/g, '$1/');
}
}
module.exports = new Api();