From ed811db6ec6313b52f9003ffde9125e0d398d8cc Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Mon, 5 Jun 2017 22:53:35 -0700 Subject: [PATCH] Integrate ACP with GlobalBanDB class --- src/acp.js | 76 +++++++++++++++++-------------------------------- src/database.js | 10 +++++++ 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/acp.js b/src/acp.js index 14dc37ac..5f7e1333 100644 --- a/src/acp.js +++ b/src/acp.js @@ -28,64 +28,40 @@ function handleAnnounceClear(user) { } function handleGlobalBan(user, data) { - db.globalBanIP(data.ip, data.note, function (err, res) { - if (err) { - user.socket.emit("errMessage", { - msg: err - }); - return; - } - + const globalBanDB = db.getGlobalBanDB(); + globalBanDB.addGlobalIPBan(data.ip, data.note).then(() => { Logger.eventlog.log("[acp] " + eventUsername(user) + " global banned " + data.ip); - - db.listGlobalBans(function (err, bans) { - if (err) { - user.socket.emit("errMessage", { - msg: err - }); - return; - } - - var flat = []; - for (var ip in bans) { - flat.push({ - ip: ip, - note: bans[ip].reason - }); - } - user.socket.emit("acp-gbanlist", flat); + return globalBanDB.listGlobalBans().then(bans => { + // Why is it called reason in the DB and note in the socket frame? + // Who knows... + const mappedBans = bans.map(ban => { + return { ip: ban.ip, note: ban.reason }; + }); + user.socket.emit("acp-gbanlist", mappedBans); + }); + }).catch(error => { + user.socket.emit("errMessage", { + msg: error.message }); }); } function handleGlobalBanDelete(user, data) { - db.globalUnbanIP(data.ip, function (err, res) { - if (err) { - user.socket.emit("errMessage", { - msg: err - }); - return; - } - + const globalBanDB = db.getGlobalBanDB(); + globalBanDB.removeGlobalIPBan(data.ip).then(() => { Logger.eventlog.log("[acp] " + eventUsername(user) + " un-global banned " + data.ip); - - db.listGlobalBans(function (err, bans) { - if (err) { - user.socket.emit("errMessage", { - msg: err - }); - return; - } - - var flat = []; - for (var ip in bans) { - flat.push({ - ip: ip, - note: bans[ip].reason - }); - } - user.socket.emit("acp-gbanlist", flat); + return globalBanDB.listGlobalBans().then(bans => { + // Why is it called reason in the DB and note in the socket frame? + // Who knows... + const mappedBans = bans.map(ban => { + return { ip: ban.ip, note: ban.reason }; + }); + user.socket.emit("acp-gbanlist", mappedBans); + }); + }).catch(error => { + user.socket.emit("errMessage", { + msg: error.message }); }); } diff --git a/src/database.js b/src/database.js index 402afe68..72cad045 100644 --- a/src/database.js +++ b/src/database.js @@ -7,11 +7,13 @@ var util = require("./utilities"); import * as Metrics from 'cytube-common/lib/metrics/metrics'; import { LoggerFactory } from '@calzoneman/jsli'; import knex from 'knex'; +import { GlobalBanDB } from './db/globalban'; const LOGGER = LoggerFactory.getLogger('database'); var global_ipbans = {}; let db = null; +let globalBanDB = null; class Database { constructor(knexConfig = null) { @@ -71,6 +73,14 @@ module.exports.getDB = function getDB() { return db; }; +module.exports.getGlobalBanDB = function getGlobalBanDB() { + if (globalBanDB === null) { + globalBanDB = new GlobalBanDB(db); + } + + return globalBanDB; +}; + function legacySetup() { tables.init(module.exports.query, function (err) { if (err) {