szurubooru/client/js/api.js

153 lines
4.5 KiB
JavaScript
Raw Normal View History

'use strict';
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 {
2016-03-30 20:45:37 +02:00
constructor() {
this.user = null;
2016-03-30 20:45:37 +02:00
this.userName = null;
this.userPassword = null;
this.cache = {};
2016-03-30 20:45:37 +02:00
}
get(url) {
if (url in this.cache) {
return new Promise((resolve, reject) => {
resolve(this.cache[url]);
});
}
return this._process(url, request.get).then(response => {
this.cache[url] = response;
return Promise.resolve(response);
});
}
2016-04-10 15:55:56 +02:00
post(url, data, files) {
this.cache = {};
2016-04-10 15:55:56 +02:00
return this._process(url, request.post, data, files);
}
2016-04-10 15:55:56 +02:00
put(url, data, files) {
this.cache = {};
2016-04-10 15:55:56 +02:00
return this._process(url, request.put, data, files);
}
2016-04-10 15:55:56 +02:00
delete(url) {
this.cache = {};
2016-04-10 15:55:56 +02:00
return this._process(url, request.delete);
2016-04-09 09:52:00 +02:00
}
2016-04-10 15:55:56 +02:00
_process(url, requestFactory, data, files) {
const fullUrl = this.getFullUrl(url);
return new Promise((resolve, reject) => {
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]);
}
}
if (this.userName && this.userPassword) {
req.auth(this.userName, this.userPassword);
}
req.set('Accept', 'application/json')
.end((error, response) => {
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-04-06 20:38:45 +02:00
const rankIndex = config.ranks.indexOf(rankName);
if (minViableRank === null || rankIndex < minViableRank) {
minViableRank = rankIndex;
}
}
if (minViableRank === null) {
console.error('Bad privilege name: ' + lookup);
}
let myRank = this.user !== null ?
2016-04-06 20:38:45 +02:00
config.ranks.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) {
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);
this.user = response.user;
resolve();
2016-04-07 19:03:49 +02:00
events.notify(events.Authentication);
}).catch(response => {
2016-03-30 21:01:18 +02:00
reject(response.description);
this.logout();
2016-04-07 19:03:49 +02:00
events.notify(events.Authentication);
2016-03-30 21:01:18 +02:00
});
});
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;
2016-04-07 19:03:49 +02:00
events.notify(events.Authentication);
2016-03-30 20:45:37 +02:00
}
forget() {
cookies.remove('auth');
}
2016-03-30 20:45:37 +02:00
isLoggedIn() {
return this.userName !== null;
}
getFullUrl(url) {
2016-04-06 20:38:45 +02:00
return (config.apiUrl + '/' + url).replace(/([^:])\/+/g, '$1/');
}
}
module.exports = new Api();