sync/src/controller/banned-channels.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-09-03 15:58:42 -07:00
import { eventlog } from '../logger';
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-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`);
}
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}`);
this.globalMessageBus.emit(
'ChannelUnbanned',
{ channel: name }
);
await this.dbChannels.removeBannedChannel(name);
}
async getBannedChannel(name) {
// TODO: cache
return this.dbChannels.getBannedChannel(name);
}
}
class Cache {
constructor({ maxElem, maxAge }) {
this.maxElem = maxElem;
this.maxAge = maxAge;
this.cache = new Map();
}
put(key, value) {
this.cache.set(key, { value: value, at: Date.now() });
if (this.cache.size > this.maxElem) {
this.cache.delete(this.cache.keys().next());
}
}
get(key) {
let val = this.cache.get(key);
if (val != null) {
return val.value;
} else {
return null;
}
}
delete(key) {
this.cache.delete(key);
}
cleanup() {
let now = Date.now();
for (let [key, value] of this.cache) {
if (value.at < now - this.maxAge) {
this.cache.delete(key);
}
}
}
2022-09-01 20:17:21 -07:00
}