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) {
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
});
});
}

View File

@ -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) {