Integrate ACP with GlobalBanDB class

This commit is contained in:
Calvin Montgomery 2017-06-05 22:53:35 -07:00
parent b80a87ba01
commit ed811db6ec
2 changed files with 36 additions and 50 deletions

View file

@ -28,64 +28,40 @@ function handleAnnounceClear(user) {
} }
function handleGlobalBan(user, data) { function handleGlobalBan(user, data) {
db.globalBanIP(data.ip, data.note, function (err, res) { const globalBanDB = db.getGlobalBanDB();
if (err) { globalBanDB.addGlobalIPBan(data.ip, data.note).then(() => {
user.socket.emit("errMessage", {
msg: err
});
return;
}
Logger.eventlog.log("[acp] " + eventUsername(user) + " global banned " + data.ip); Logger.eventlog.log("[acp] " + eventUsername(user) + " global banned " + data.ip);
return globalBanDB.listGlobalBans().then(bans => {
db.listGlobalBans(function (err, bans) { // Why is it called reason in the DB and note in the socket frame?
if (err) { // Who knows...
user.socket.emit("errMessage", { const mappedBans = bans.map(ban => {
msg: err return { ip: ban.ip, note: ban.reason };
}); });
return; user.socket.emit("acp-gbanlist", mappedBans);
} });
}).catch(error => {
var flat = []; user.socket.emit("errMessage", {
for (var ip in bans) { msg: error.message
flat.push({
ip: ip,
note: bans[ip].reason
});
}
user.socket.emit("acp-gbanlist", flat);
}); });
}); });
} }
function handleGlobalBanDelete(user, data) { function handleGlobalBanDelete(user, data) {
db.globalUnbanIP(data.ip, function (err, res) { const globalBanDB = db.getGlobalBanDB();
if (err) { globalBanDB.removeGlobalIPBan(data.ip).then(() => {
user.socket.emit("errMessage", {
msg: err
});
return;
}
Logger.eventlog.log("[acp] " + eventUsername(user) + " un-global banned " + Logger.eventlog.log("[acp] " + eventUsername(user) + " un-global banned " +
data.ip); data.ip);
return globalBanDB.listGlobalBans().then(bans => {
db.listGlobalBans(function (err, bans) { // Why is it called reason in the DB and note in the socket frame?
if (err) { // Who knows...
user.socket.emit("errMessage", { const mappedBans = bans.map(ban => {
msg: err return { ip: ban.ip, note: ban.reason };
}); });
return; user.socket.emit("acp-gbanlist", mappedBans);
} });
}).catch(error => {
var flat = []; user.socket.emit("errMessage", {
for (var ip in bans) { msg: error.message
flat.push({
ip: ip,
note: bans[ip].reason
});
}
user.socket.emit("acp-gbanlist", flat);
}); });
}); });
} }

View file

@ -7,11 +7,13 @@ var util = require("./utilities");
import * as Metrics from 'cytube-common/lib/metrics/metrics'; import * as Metrics from 'cytube-common/lib/metrics/metrics';
import { LoggerFactory } from '@calzoneman/jsli'; import { LoggerFactory } from '@calzoneman/jsli';
import knex from 'knex'; import knex from 'knex';
import { GlobalBanDB } from './db/globalban';
const LOGGER = LoggerFactory.getLogger('database'); const LOGGER = LoggerFactory.getLogger('database');
var global_ipbans = {}; var global_ipbans = {};
let db = null; let db = null;
let globalBanDB = null;
class Database { class Database {
constructor(knexConfig = null) { constructor(knexConfig = null) {
@ -71,6 +73,14 @@ module.exports.getDB = function getDB() {
return db; return db;
}; };
module.exports.getGlobalBanDB = function getGlobalBanDB() {
if (globalBanDB === null) {
globalBanDB = new GlobalBanDB(db);
}
return globalBanDB;
};
function legacySetup() { function legacySetup() {
tables.init(module.exports.query, function (err) { tables.init(module.exports.query, function (err) {
if (err) { if (err) {