sync/src/controller/banned-channels.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-09-03 15:58:42 -07:00
import { eventlog } from '../logger';
2022-09-19 22:59:06 -07:00
import { SimpleCache } from '../util/simple-cache';
2022-09-01 20:17:21 -07:00
const LOGGER = require('@calzoneman/jsli')('BannedChannelsController');
export class BannedChannelsController {
constructor(dbChannels, globalMessageBus) {
this.dbChannels = dbChannels;
this.globalMessageBus = globalMessageBus;
2022-09-19 22:59:06 -07:00
this.cache = new SimpleCache({
maxElem: 1000,
maxAge: 5 * 60_000
});
2022-09-01 20:17:21 -07:00
}
2022-09-03 15:58:42 -07:00
/*
* TODO: add an audit log to the database
*/
2022-09-01 20:17:21 -07:00
async banChannel({ name, externalReason, internalReason, bannedBy }) {
LOGGER.info(`Banning channel ${name} (banned by ${bannedBy})`);
2022-09-03 15:58:42 -07:00
eventlog.log(`[acp] ${bannedBy} banned channel ${name}`);
2022-09-01 20:17:21 -07:00
let banInfo = await this.dbChannels.getBannedChannel(name);
if (banInfo !== null) {
LOGGER.warn(`Channel ${name} is already banned, updating ban reason`);
}
2022-09-19 22:59:06 -07:00
this.cache.delete(name);
2022-09-01 20:17:21 -07:00
await this.dbChannels.putBannedChannel({
name,
externalReason,
internalReason,
bannedBy
});
this.globalMessageBus.emit(
'ChannelBanned',
{ channel: name, externalReason }
);
}
2022-09-03 15:58:42 -07:00
async unbanChannel(name, unbannedBy) {
LOGGER.info(`Unbanning channel ${name}`);
eventlog.log(`[acp] ${unbannedBy} unbanned channel ${name}`);
2022-09-19 22:59:06 -07:00
this.cache.delete(name);
2022-09-03 15:58:42 -07:00
this.globalMessageBus.emit(
'ChannelUnbanned',
{ channel: name }
);
await this.dbChannels.removeBannedChannel(name);
}
async getBannedChannel(name) {
2022-09-19 22:59:06 -07:00
name = name.toLowerCase();
2022-09-03 15:58:42 -07:00
2022-09-19 22:59:06 -07:00
let info = this.cache.get(name);
if (info === null) {
info = await this.dbChannels.getBannedChannel(name);
this.cache.put(name, info);
2022-09-03 15:58:42 -07:00
}
2022-09-19 22:59:06 -07:00
return info;
2022-09-03 15:58:42 -07:00
}
2022-09-01 20:17:21 -07:00
}