punishments.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 = 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. for (
  110. let id = 0, punishmentKeys = Object.keys(punishments);
  111. id < punishmentKeys.length;
  112. id += 1
  113. ) {
  114. const punishment = punishments[id];
  115. punishment.punishmentId = id;
  116. punishments.push(punishment);
  117. }
  118. filteredPunishments = punishments.filter(punishment => {
  119. if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);
  120. return punishment.expiresAt > Date.now();
  121. });
  122. next(null, filteredPunishments);
  123. },
  124. (punishments, next) => {
  125. async.each(
  126. punishmentsToRemove,
  127. (punishment, next2) => {
  128. CacheModule.runJob(
  129. "HDEL",
  130. {
  131. table: "punishments",
  132. key: punishment.punishmentId
  133. },
  134. this
  135. ).finally(() => next2());
  136. },
  137. () => {
  138. next(null, punishments);
  139. }
  140. );
  141. }
  142. ],
  143. (err, punishments) => {
  144. if (err && err !== true) return reject(new Error(err));
  145. return resolve(punishments);
  146. }
  147. );
  148. });
  149. }
  150. /**
  151. * Gets a punishment by id
  152. *
  153. * @param {object} payload - object containing the payload
  154. * @param {string} payload.id - the id of the punishment we are trying to get
  155. * @returns {Promise} - returns promise (reject, resolve)
  156. */
  157. GET_PUNISHMENT(payload) {
  158. return new Promise((resolve, reject) =>
  159. async.waterfall(
  160. [
  161. next => {
  162. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  163. return CacheModule.runJob(
  164. "HGET",
  165. {
  166. table: "punishments",
  167. key: payload.id
  168. },
  169. this
  170. )
  171. .then(punishment => next(null, punishment))
  172. .catch(next);
  173. },
  174. (punishment, next) => {
  175. if (punishment) return next(true, punishment);
  176. return PunishmentsModule.punishmentModel.findOne({ _id: payload.id }, next);
  177. },
  178. (punishment, next) => {
  179. if (punishment) {
  180. CacheModule.runJob(
  181. "HSET",
  182. {
  183. table: "punishments",
  184. key: payload.id,
  185. value: punishment
  186. },
  187. this
  188. )
  189. .then(punishment => {
  190. next(null, punishment);
  191. })
  192. .catch(next);
  193. } else next("Punishment not found.");
  194. }
  195. ],
  196. (err, punishment) => {
  197. if (err && err !== true) return reject(new Error(err));
  198. return resolve(punishment);
  199. }
  200. )
  201. );
  202. }
  203. /**
  204. * Gets all punishments from a userId
  205. *
  206. * @param {object} payload - object containing the payload
  207. * @param {string} payload.userId - the userId of the punishment(s) we are trying to get
  208. * @returns {Promise} - returns promise (reject, resolve)
  209. */
  210. GET_PUNISHMENTS_FROM_USER_ID(payload) {
  211. return new Promise((resolve, reject) => {
  212. async.waterfall(
  213. [
  214. next => {
  215. PunishmentsModule.runJob("GET_PUNISHMENTS", {}, this)
  216. .then(punishments => {
  217. next(null, punishments);
  218. })
  219. .catch(next);
  220. },
  221. (punishments, next) => {
  222. const filteredPunishments = punishments.filter(
  223. punishment => punishment.type === "banUserId" && punishment.value === payload.userId
  224. );
  225. next(null, filteredPunishments);
  226. }
  227. ],
  228. (err, punishments) => {
  229. if (err && err !== true) return reject(new Error(err));
  230. return resolve(punishments);
  231. }
  232. );
  233. });
  234. }
  235. /**
  236. * Adds a new punishment to the database
  237. *
  238. * @param {object} payload - object containing the payload
  239. * @param {string} payload.reason - the reason for the punishment e.g. spam
  240. * @param {string} payload.type - the type of punishment (enum: ["banUserId", "banUserIp"])
  241. * @param {string} payload.value - the user id/ip address for the ban (depends on punishment type)
  242. * @param {Date} payload.expiresAt - the date at which the punishment expires at
  243. * @param {string} payload.punishedBy - the userId of the who initiated the punishment
  244. * @returns {Promise} - returns promise (reject, resolve)
  245. */
  246. ADD_PUNISHMENT(payload) {
  247. return new Promise((resolve, reject) =>
  248. async.waterfall(
  249. [
  250. next => {
  251. const punishment = new PunishmentsModule.punishmentModel({
  252. type: payload.type,
  253. value: payload.value,
  254. reason: payload.reason,
  255. active: true,
  256. expiresAt: payload.expiresAt,
  257. punishedAt: Date.now(),
  258. punishedBy: payload.punishedBy
  259. });
  260. punishment.save((err, punishment) => {
  261. if (err) return next(err);
  262. return next(null, punishment);
  263. });
  264. },
  265. (punishment, next) => {
  266. CacheModule.runJob(
  267. "HSET",
  268. {
  269. table: "punishments",
  270. key: punishment._id,
  271. value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
  272. },
  273. this
  274. )
  275. .then(() => next())
  276. .catch(next);
  277. },
  278. (punishment, next) => {
  279. // DISCORD MESSAGE
  280. next(null, punishment);
  281. }
  282. ],
  283. (err, punishment) => {
  284. if (err) return reject(new Error(err));
  285. return resolve(punishment);
  286. }
  287. )
  288. );
  289. }
  290. }
  291. export default new _PunishmentsModule();