punishments.js 7.8 KB

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