Update README.md
[KisSync.git] / src / db / globalban.js
blob8d992fb5efeec4edc3c15900f64e7ad6c0e12289
1 const LOGGER = require('@calzoneman/jsli')('GlobalBanDB');
3 class GlobalBanDB {
4 constructor(db) {
5 this.db = db;
8 listGlobalBans() {
9 return this.db.runTransaction(tx => {
10 return tx.table('global_bans').select();
11 }).catch(error => {
12 LOGGER.error('Failed to list global IP bans: %s', error.stack);
13 throw error;
14 });
17 addGlobalIPBan(ip, reason) {
18 return this.db.runTransaction(tx => {
19 return tx.table('global_bans')
20 .insert({ ip, reason })
21 .catch(error => {
22 if (error.code === 'ER_DUP_ENTRY') {
23 return tx.table('global_bans')
24 .where({ ip })
25 .update({ reason });
26 } else {
27 throw error;
29 });
30 }).catch(error => {
31 LOGGER.error('Failed to add global IP ban for IP %s: %s', ip, error.stack);
32 throw error;
33 });
36 removeGlobalIPBan(ip) {
37 return this.db.runTransaction(tx => {
38 return tx.table('global_bans')
39 .where({ ip })
40 .del();
41 }).catch(error => {
42 LOGGER.error('Failed to remove global IP ban for IP %s: %s', ip, error.stack);
43 throw error;
44 });
48 export { GlobalBanDB };