You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/hydrooj/src/model/blacklist.ts

30 lines
790 B
TypeScript

import db from '../service/db';
import * as bus from '../service/bus';
5 years ago
4 years ago
const coll = db.collection('blacklist');
4 years ago
export async function add(ip: string) {
5 years ago
const expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
const res = await coll.findOneAndUpdate(
{ _id: ip },
{ $set: { expireAt } },
{ upsert: true, returnOriginal: false },
);
return res.value;
}
5 years ago
export async function get(ip: string) {
return await coll.findOne({ _id: ip });
}
5 years ago
export async function del(ip: string) {
return await coll.deleteOne({ _id: ip });
}
async function ensureIndexes() {
return await coll.createIndex('expireAt', { expireAfterSeconds: 0 });
5 years ago
}
bus.once('app/started', ensureIndexes);
global.Hydro.model.blacklist = { add, get, del };