123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- import async from "async";
- import mongoose from "mongoose";
- import CoreClass from "../core";
- let PunishmentsModule;
- let CacheModule;
- let DBModule;
- let UtilsModule;
- class _PunishmentsModule extends CoreClass {
- // eslint-disable-next-line require-jsdoc
- constructor() {
- super("punishments");
- PunishmentsModule = this;
- }
- /**
- * Initialises the punishments module
- *
- * @returns {Promise} - returns promise (reject, resolve)
- */
- async initialize() {
- this.setStage(1);
- CacheModule = this.moduleManager.modules.cache;
- DBModule = this.moduleManager.modules.db;
- UtilsModule = this.moduleManager.modules.utils;
- this.punishmentModel = this.PunishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" });
- this.punishmentSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "punishment" });
- return new Promise((resolve, reject) =>
- async.waterfall(
- [
- next => {
- this.setStage(2);
- CacheModule.runJob("HGETALL", { table: "punishments" })
- .then(punishments => {
- next(null, punishments);
- })
- .catch(next);
- },
- (punishments, next) => {
- this.setStage(3);
- if (!punishments) return next();
- const punishmentIds = Object.keys(punishments);
- return async.each(
- punishmentIds,
- (punishmentId, cb) => {
- PunishmentsModule.punishmentModel.findOne({ _id: punishmentId }, (err, punishment) => {
- if (err) next(err);
- else if (!punishment)
- CacheModule.runJob("HDEL", {
- table: "punishments",
- key: punishmentId
- })
- .then(() => {
- cb();
- })
- .catch(next);
- else cb();
- });
- },
- next
- );
- },
- next => {
- this.setStage(4);
- PunishmentsModule.punishmentModel.find({}, next);
- },
- (punishments, next) => {
- this.setStage(5);
- async.each(
- punishments,
- (punishment, next) => {
- if (punishment.active === false || punishment.expiresAt < Date.now()) return next();
- return CacheModule.runJob("HSET", {
- table: "punishments",
- key: punishment._id,
- value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
- })
- .then(() => next())
- .catch(next);
- },
- next
- );
- }
- ],
- async err => {
- if (err) {
- const formattedErr = await UtilsModule.runJob("GET_ERROR", { error: err });
- reject(new Error(formattedErr));
- } else resolve();
- }
- )
- );
- }
- /**
- * Gets all punishments in the cache that are active, and removes those that have expired
- *
- * @returns {Promise} - returns promise (reject, resolve)
- */
- GET_PUNISHMENTS() {
- return new Promise((resolve, reject) => {
- const punishmentsToRemove = [];
- async.waterfall(
- [
- next => {
- CacheModule.runJob("HGETALL", { table: "punishments" }, this)
- .then(punishmentsObj => next(null, punishmentsObj))
- .catch(next);
- },
- (punishmentsObj, next) => {
- const punishments = Object.keys(punishmentsObj).map(punishmentKey => {
- const punishment = punishmentsObj[punishmentKey];
- punishment.punishmentId = punishmentKey;
- return punishment;
- });
- const filteredPunishments = punishments.filter(punishment => {
- if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);
- return punishment.expiresAt > Date.now();
- });
- next(null, filteredPunishments);
- },
- (punishments, next) => {
- async.each(
- punishmentsToRemove,
- (punishment, next2) => {
- CacheModule.runJob(
- "HDEL",
- {
- table: "punishments",
- key: punishment.punishmentId
- },
- this
- ).finally(() => next2());
- },
- () => {
- next(null, punishments);
- }
- );
- }
- ],
- (err, punishments) => {
- if (err && err !== true) return reject(new Error(err));
- return resolve(punishments);
- }
- );
- });
- }
- /**
- * Gets a punishment by id
- *
- * @param {object} payload - object containing the payload
- * @param {string} payload.id - the id of the punishment we are trying to get
- * @returns {Promise} - returns promise (reject, resolve)
- */
- GET_PUNISHMENT(payload) {
- return new Promise((resolve, reject) =>
- async.waterfall(
- [
- next => {
- if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
- return CacheModule.runJob(
- "HGET",
- {
- table: "punishments",
- key: payload.id
- },
- this
- )
- .then(punishment => next(null, punishment))
- .catch(next);
- },
- (punishment, next) => {
- if (punishment) return next(true, punishment);
- return PunishmentsModule.punishmentModel.findOne({ _id: payload.id }, next);
- },
- (punishment, next) => {
- if (punishment) {
- CacheModule.runJob(
- "HSET",
- {
- table: "punishments",
- key: payload.id,
- value: punishment
- },
- this
- )
- .then(punishment => {
- next(null, punishment);
- })
- .catch(next);
- } else next("Punishment not found.");
- }
- ],
- (err, punishment) => {
- if (err && err !== true) return reject(new Error(err));
- return resolve(punishment);
- }
- )
- );
- }
- /**
- * Gets all punishments from a userId
- *
- * @param {object} payload - object containing the payload
- * @param {string} payload.userId - the userId of the punishment(s) we are trying to get
- * @returns {Promise} - returns promise (reject, resolve)
- */
- GET_PUNISHMENTS_FROM_USER_ID(payload) {
- return new Promise((resolve, reject) => {
- async.waterfall(
- [
- next => {
- PunishmentsModule.runJob("GET_PUNISHMENTS", {}, this)
- .then(punishments => {
- next(null, punishments);
- })
- .catch(next);
- },
- (punishments, next) => {
- const filteredPunishments = punishments.filter(
- punishment => punishment.type === "banUserId" && punishment.value === payload.userId
- );
- next(null, filteredPunishments);
- }
- ],
- (err, punishments) => {
- if (err && err !== true) return reject(new Error(err));
- return resolve(punishments);
- }
- );
- });
- }
- /**
- * Adds a new punishment to the database
- *
- * @param {object} payload - object containing the payload
- * @param {string} payload.reason - the reason for the punishment e.g. spam
- * @param {string} payload.type - the type of punishment (enum: ["banUserId", "banUserIp"])
- * @param {string} payload.value - the user id/ip address for the ban (depends on punishment type)
- * @param {Date} payload.expiresAt - the date at which the punishment expires at
- * @param {string} payload.punishedBy - the userId of the who initiated the punishment
- * @returns {Promise} - returns promise (reject, resolve)
- */
- ADD_PUNISHMENT(payload) {
- return new Promise((resolve, reject) =>
- async.waterfall(
- [
- next => {
- const punishment = new PunishmentsModule.PunishmentModel({
- type: payload.type,
- value: payload.value,
- reason: payload.reason,
- active: true,
- expiresAt: payload.expiresAt,
- punishedAt: Date.now(),
- punishedBy: payload.punishedBy
- });
- punishment.save((err, punishment) => {
- if (err) return next(err);
- return next(null, punishment);
- });
- },
- (punishment, next) => {
- CacheModule.runJob(
- "HSET",
- {
- table: "punishments",
- key: punishment._id,
- value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
- },
- this
- )
- .then(() => next(null, punishment))
- .catch(next);
- }
- ],
- (err, punishment) => {
- if (err) return reject(new Error(err));
- return resolve(punishment);
- }
- )
- );
- }
- }
- export default new _PunishmentsModule();
|