punishments.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. let WSModule;
  9. class _PunishmentsModule extends CoreClass {
  10. // eslint-disable-next-line require-jsdoc
  11. constructor() {
  12. super("punishments");
  13. PunishmentsModule = this;
  14. }
  15. /**
  16. * Initialises the punishments module
  17. *
  18. * @returns {Promise} - returns promise (reject, resolve)
  19. */
  20. async initialize() {
  21. this.setStage(1);
  22. CacheModule = this.moduleManager.modules.cache;
  23. DBModule = this.moduleManager.modules.db;
  24. UtilsModule = this.moduleManager.modules.utils;
  25. WSModule = this.moduleManager.modules.ws;
  26. this.punishmentModel = this.PunishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" });
  27. this.punishmentSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "punishment" });
  28. return new Promise((resolve, reject) => {
  29. async.waterfall(
  30. [
  31. next => {
  32. this.setStage(2);
  33. CacheModule.runJob("HGETALL", { table: "punishments" })
  34. .then(punishments => {
  35. next(null, punishments);
  36. })
  37. .catch(next);
  38. },
  39. (punishments, next) => {
  40. this.setStage(3);
  41. if (!punishments) return next();
  42. const punishmentIds = Object.keys(punishments);
  43. return async.each(
  44. punishmentIds,
  45. (punishmentId, cb) => {
  46. PunishmentsModule.punishmentModel.findOne({ _id: punishmentId }, (err, punishment) => {
  47. if (err) next(err);
  48. else if (!punishment)
  49. CacheModule.runJob("HDEL", {
  50. table: "punishments",
  51. key: punishmentId
  52. })
  53. .then(() => cb())
  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(() => {
  131. WSModule.runJob(
  132. "EMIT_TO_ROOM",
  133. {
  134. room: `admin.punishments`,
  135. args: [
  136. "event:admin.punishment.updated",
  137. { data: { punishment: { ...punishment, status: "Inactive" } } }
  138. ]
  139. },
  140. this
  141. ).finally(() => next2());
  142. });
  143. },
  144. () => {
  145. next(null, punishments);
  146. }
  147. );
  148. }
  149. ],
  150. (err, punishments) => {
  151. if (err && err !== true) return reject(new Error(err));
  152. return resolve(punishments);
  153. }
  154. );
  155. });
  156. }
  157. /**
  158. * Gets a punishment by id
  159. *
  160. * @param {object} payload - object containing the payload
  161. * @param {string} payload.id - the id of the punishment we are trying to get
  162. * @returns {Promise} - returns promise (reject, resolve)
  163. */
  164. GET_PUNISHMENT(payload) {
  165. return new Promise((resolve, reject) => {
  166. async.waterfall(
  167. [
  168. next => {
  169. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  170. return CacheModule.runJob(
  171. "HGET",
  172. {
  173. table: "punishments",
  174. key: payload.id
  175. },
  176. this
  177. )
  178. .then(punishment => next(null, punishment))
  179. .catch(next);
  180. },
  181. (punishment, next) => {
  182. if (punishment) return next(true, punishment);
  183. return PunishmentsModule.punishmentModel.findOne({ _id: payload.id }, next);
  184. },
  185. (punishment, next) => {
  186. if (punishment) {
  187. CacheModule.runJob(
  188. "HSET",
  189. {
  190. table: "punishments",
  191. key: payload.id,
  192. value: punishment
  193. },
  194. this
  195. )
  196. .then(punishment => {
  197. next(null, punishment);
  198. })
  199. .catch(next);
  200. } else next("Punishment not found.");
  201. }
  202. ],
  203. (err, punishment) => {
  204. if (err && err !== true) return reject(new Error(err));
  205. return resolve(punishment);
  206. }
  207. );
  208. });
  209. }
  210. /**
  211. * Gets all punishments from a userId
  212. *
  213. * @param {object} payload - object containing the payload
  214. * @param {string} payload.userId - the userId of the punishment(s) we are trying to get
  215. * @returns {Promise} - returns promise (reject, resolve)
  216. */
  217. GET_PUNISHMENTS_FROM_USER_ID(payload) {
  218. return new Promise((resolve, reject) => {
  219. async.waterfall(
  220. [
  221. next => {
  222. PunishmentsModule.runJob("GET_PUNISHMENTS", {}, this)
  223. .then(punishments => {
  224. next(null, punishments);
  225. })
  226. .catch(next);
  227. },
  228. (punishments, next) => {
  229. const filteredPunishments = punishments.filter(
  230. punishment => punishment.type === "banUserId" && punishment.value === payload.userId
  231. );
  232. next(null, filteredPunishments);
  233. }
  234. ],
  235. (err, punishments) => {
  236. if (err && err !== true) return reject(new Error(err));
  237. return resolve(punishments);
  238. }
  239. );
  240. });
  241. }
  242. /**
  243. * Adds a new punishment to the database
  244. *
  245. * @param {object} payload - object containing the payload
  246. * @param {string} payload.reason - the reason for the punishment e.g. spam
  247. * @param {string} payload.type - the type of punishment (enum: ["banUserId", "banUserIp"])
  248. * @param {string} payload.value - the user id/ip address for the ban (depends on punishment type)
  249. * @param {Date} payload.expiresAt - the date at which the punishment expires at
  250. * @param {string} payload.punishedBy - the userId of the who initiated the punishment
  251. * @returns {Promise} - returns promise (reject, resolve)
  252. */
  253. ADD_PUNISHMENT(payload) {
  254. return new Promise((resolve, reject) => {
  255. async.waterfall(
  256. [
  257. next => {
  258. const punishment = new PunishmentsModule.PunishmentModel({
  259. type: payload.type,
  260. value: payload.value,
  261. reason: payload.reason,
  262. active: true,
  263. expiresAt: payload.expiresAt,
  264. punishedAt: Date.now(),
  265. punishedBy: payload.punishedBy
  266. });
  267. punishment.save((err, punishment) => {
  268. if (err) return next(err);
  269. return next(null, punishment);
  270. });
  271. },
  272. (punishment, next) => {
  273. CacheModule.runJob(
  274. "HSET",
  275. {
  276. table: "punishments",
  277. key: punishment._id,
  278. value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
  279. },
  280. this
  281. )
  282. .then(() => next(null, punishment))
  283. .catch(next);
  284. }
  285. ],
  286. (err, punishment) => {
  287. if (err) return reject(new Error(err));
  288. return resolve(punishment);
  289. }
  290. );
  291. });
  292. }
  293. /**
  294. * Deactivates a punishment
  295. *
  296. * @param {object} payload - object containing the payload
  297. * @param {string} payload.punishmentId - the MongoDB id of the punishment
  298. * @returns {Promise} - returns promise (reject, resolve)
  299. */
  300. DEACTIVATE_PUNISHMENT(payload) {
  301. return new Promise((resolve, reject) => {
  302. async.waterfall(
  303. [
  304. next => {
  305. PunishmentsModule.punishmentModel.findOne({ _id: payload.punishmentId }, next);
  306. },
  307. (punishment, next) => {
  308. if (!punishment) next("Punishment does not exist.");
  309. else
  310. PunishmentsModule.punishmentModel.updateOne(
  311. { _id: payload.punishmentId },
  312. { $set: { active: false } },
  313. next
  314. );
  315. },
  316. (res, next) => {
  317. CacheModule.runJob(
  318. "HDEL",
  319. {
  320. table: "punishments",
  321. key: payload.punishmentId
  322. },
  323. this
  324. )
  325. .then(() => next())
  326. .catch(next);
  327. },
  328. next => {
  329. PunishmentsModule.punishmentModel.findOne({ _id: payload.punishmentId }, next);
  330. }
  331. ],
  332. (err, punishment) => {
  333. if (err) return reject(new Error(err));
  334. return resolve(punishment);
  335. }
  336. );
  337. });
  338. }
  339. }
  340. export default new _PunishmentsModule();