punishments.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import CoreClass from "../core";
  4. let PunishmentsModule;
  5. let CacheModule;
  6. let DBModule;
  7. let UtilsModule;
  8. class _PunishmentsModule extends CoreClass {
  9. // eslint-disable-next-line require-jsdoc
  10. constructor() {
  11. super("punishments");
  12. PunishmentsModule = this;
  13. }
  14. /**
  15. * Initialises the punishments module
  16. *
  17. * @returns {Promise} - returns promise (reject, resolve)
  18. */
  19. async initialize() {
  20. this.setStage(1);
  21. CacheModule = this.moduleManager.modules.cache;
  22. DBModule = this.moduleManager.modules.db;
  23. UtilsModule = this.moduleManager.modules.utils;
  24. this.punishmentModel = this.PunishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" });
  25. this.punishmentSchemaCache = await DBModule.runJob("GET_SCHEMA", { schemaName: "punishment" });
  26. return new Promise((resolve, reject) =>
  27. async.waterfall(
  28. [
  29. next => {
  30. this.setStage(2);
  31. CacheModule.runJob("HGETALL", { table: "punishments" })
  32. .then(punishments => {
  33. next(null, punishments);
  34. })
  35. .catch(next);
  36. },
  37. (punishments, next) => {
  38. this.setStage(3);
  39. if (!punishments) return next();
  40. const punishmentIds = Object.keys(punishments);
  41. return async.each(
  42. punishmentIds,
  43. (punishmentId, cb) => {
  44. PunishmentsModule.punishmentModel.findOne({ _id: punishmentId }, (err, punishment) => {
  45. if (err) next(err);
  46. else if (!punishment)
  47. CacheModule.runJob("HDEL", {
  48. table: "punishments",
  49. key: punishmentId
  50. })
  51. .then(() => {
  52. cb();
  53. })
  54. .catch(next);
  55. else cb();
  56. });
  57. },
  58. next
  59. );
  60. },
  61. next => {
  62. this.setStage(4);
  63. PunishmentsModule.punishmentModel.find({}, next);
  64. },
  65. (punishments, next) => {
  66. this.setStage(5);
  67. async.each(
  68. punishments,
  69. (punishment, next) => {
  70. if (punishment.active === false || punishment.expiresAt < Date.now()) return next();
  71. return CacheModule.runJob("HSET", {
  72. table: "punishments",
  73. key: punishment._id,
  74. value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
  75. })
  76. .then(() => next())
  77. .catch(next);
  78. },
  79. next
  80. );
  81. }
  82. ],
  83. async err => {
  84. if (err) {
  85. const formattedErr = await UtilsModule.runJob("GET_ERROR", { error: err });
  86. reject(new Error(formattedErr));
  87. } else resolve();
  88. }
  89. )
  90. );
  91. }
  92. /**
  93. * Gets all punishments in the cache that are active, and removes those that have expired
  94. *
  95. * @returns {Promise} - returns promise (reject, resolve)
  96. */
  97. GET_PUNISHMENTS() {
  98. return new Promise((resolve, reject) => {
  99. const punishmentsToRemove = [];
  100. async.waterfall(
  101. [
  102. next => {
  103. CacheModule.runJob("HGETALL", { table: "punishments" }, this)
  104. .then(punishmentsObj => next(null, punishmentsObj))
  105. .catch(next);
  106. },
  107. (punishments, next) => {
  108. let filteredPunishments = [];
  109. Object.keys(punishments).forEach(punishmentKey => {
  110. const punishment = punishments[punishmentKey];
  111. punishment.punishmentId = punishmentKey;
  112. punishments.push(punishment);
  113. });
  114. filteredPunishments = punishments.filter(punishment => {
  115. if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);
  116. return punishment.expiresAt > Date.now();
  117. });
  118. next(null, filteredPunishments);
  119. },
  120. (punishments, next) => {
  121. async.each(
  122. punishmentsToRemove,
  123. (punishment, next2) => {
  124. CacheModule.runJob(
  125. "HDEL",
  126. {
  127. table: "punishments",
  128. key: punishment.punishmentId
  129. },
  130. this
  131. ).finally(() => next2());
  132. },
  133. () => {
  134. next(null, punishments);
  135. }
  136. );
  137. }
  138. ],
  139. (err, punishments) => {
  140. if (err && err !== true) return reject(new Error(err));
  141. return resolve(punishments);
  142. }
  143. );
  144. });
  145. }
  146. /**
  147. * Gets a punishment by id
  148. *
  149. * @param {object} payload - object containing the payload
  150. * @param {string} payload.id - the id of the punishment we are trying to get
  151. * @returns {Promise} - returns promise (reject, resolve)
  152. */
  153. GET_PUNISHMENT(payload) {
  154. return new Promise((resolve, reject) =>
  155. async.waterfall(
  156. [
  157. next => {
  158. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  159. return CacheModule.runJob(
  160. "HGET",
  161. {
  162. table: "punishments",
  163. key: payload.id
  164. },
  165. this
  166. )
  167. .then(punishment => next(null, punishment))
  168. .catch(next);
  169. },
  170. (punishment, next) => {
  171. if (punishment) return next(true, punishment);
  172. return PunishmentsModule.punishmentModel.findOne({ _id: payload.id }, next);
  173. },
  174. (punishment, next) => {
  175. if (punishment) {
  176. CacheModule.runJob(
  177. "HSET",
  178. {
  179. table: "punishments",
  180. key: payload.id,
  181. value: punishment
  182. },
  183. this
  184. )
  185. .then(punishment => {
  186. next(null, punishment);
  187. })
  188. .catch(next);
  189. } else next("Punishment not found.");
  190. }
  191. ],
  192. (err, punishment) => {
  193. if (err && err !== true) return reject(new Error(err));
  194. return resolve(punishment);
  195. }
  196. )
  197. );
  198. }
  199. /**
  200. * Gets all punishments from a userId
  201. *
  202. * @param {object} payload - object containing the payload
  203. * @param {string} payload.userId - the userId of the punishment(s) we are trying to get
  204. * @returns {Promise} - returns promise (reject, resolve)
  205. */
  206. GET_PUNISHMENTS_FROM_USER_ID(payload) {
  207. return new Promise((resolve, reject) => {
  208. async.waterfall(
  209. [
  210. next => {
  211. PunishmentsModule.runJob("GET_PUNISHMENTS", {}, this)
  212. .then(punishments => {
  213. next(null, punishments);
  214. })
  215. .catch(next);
  216. },
  217. (punishments, next) => {
  218. const filteredPunishments = punishments.filter(
  219. punishment => punishment.type === "banUserId" && punishment.value === payload.userId
  220. );
  221. next(null, filteredPunishments);
  222. }
  223. ],
  224. (err, punishments) => {
  225. if (err && err !== true) return reject(new Error(err));
  226. return resolve(punishments);
  227. }
  228. );
  229. });
  230. }
  231. /**
  232. * Adds a new punishment to the database
  233. *
  234. * @param {object} payload - object containing the payload
  235. * @param {string} payload.reason - the reason for the punishment e.g. spam
  236. * @param {string} payload.type - the type of punishment (enum: ["banUserId", "banUserIp"])
  237. * @param {string} payload.value - the user id/ip address for the ban (depends on punishment type)
  238. * @param {Date} payload.expiresAt - the date at which the punishment expires at
  239. * @param {string} payload.punishedBy - the userId of the who initiated the punishment
  240. * @returns {Promise} - returns promise (reject, resolve)
  241. */
  242. ADD_PUNISHMENT(payload) {
  243. return new Promise((resolve, reject) =>
  244. async.waterfall(
  245. [
  246. next => {
  247. const punishment = new PunishmentsModule.PunishmentModel({
  248. type: payload.type,
  249. value: payload.value,
  250. reason: payload.reason,
  251. active: true,
  252. expiresAt: payload.expiresAt,
  253. punishedAt: Date.now(),
  254. punishedBy: payload.punishedBy
  255. });
  256. punishment.save((err, punishment) => {
  257. if (err) return next(err);
  258. return next(null, punishment);
  259. });
  260. },
  261. (punishment, next) => {
  262. CacheModule.runJob(
  263. "HSET",
  264. {
  265. table: "punishments",
  266. key: punishment._id,
  267. value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
  268. },
  269. this
  270. )
  271. .then(() => next())
  272. .catch(next);
  273. },
  274. (punishment, next) => {
  275. // DISCORD MESSAGE
  276. next(null, punishment);
  277. }
  278. ],
  279. (err, punishment) => {
  280. if (err) return reject(new Error(err));
  281. return resolve(punishment);
  282. }
  283. )
  284. );
  285. }
  286. }
  287. export default new _PunishmentsModule();