punishments.js 7.8 KB

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