punishments.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. const cache = require('./cache');
  3. const db = require('./db');
  4. const io = require('./io');
  5. const utils = require('./utils');
  6. const async = require('async');
  7. const mongoose = require('mongoose');
  8. let initialized = false;
  9. let lockdown = false;
  10. module.exports = {
  11. /**
  12. * Initializes the punishments module, and exits if it is unsuccessful
  13. *
  14. * @param {Function} cb - gets called once we're done initializing
  15. */
  16. init: cb => {
  17. async.waterfall([
  18. (next) => {
  19. cache.hgetall('punishments', next);
  20. },
  21. (punishments, next) => {
  22. if (!punishments) return next();
  23. let punishmentIds = Object.keys(punishments);
  24. async.each(punishmentIds, (punishmentId, next) => {
  25. db.models.punishment.findOne({_id: punishmentId}, (err, punishment) => {
  26. if (err) next(err);
  27. else if (!punishment) cache.hdel('punishments', punishmentId, next);
  28. else next();
  29. });
  30. }, next);
  31. },
  32. (next) => {
  33. db.models.punishment.find({}, next);
  34. },
  35. (punishments, next) => {
  36. async.each(punishments, (punishment, next) => {
  37. if (punishment.active === false || punishment.expiresAt < Date.now()) return next();
  38. cache.hset('punishments', punishment._id, cache.schemas.punishment(punishment), next);
  39. }, next);
  40. }
  41. ], (err) => {
  42. if (lockdown) return this._lockdown();
  43. if (err) {
  44. err = utils.getError(err);
  45. cb(err);
  46. } else {
  47. initialized = true;
  48. cb();
  49. }
  50. });
  51. },
  52. /**
  53. * Gets all punishments in the cache that are active, and removes those that have expired
  54. *
  55. * @param {Function} cb - gets called once we're done initializing
  56. */
  57. getPunishments: function(cb) {
  58. if (lockdown) return cb('Lockdown');
  59. let punishmentsToRemove = [];
  60. async.waterfall([
  61. (next) => {
  62. cache.hgetall('punishments', next);
  63. },
  64. (punishmentsObj, next) => {
  65. let punishments = [];
  66. for (let id in punishmentsObj) {
  67. let obj = punishmentsObj[id];
  68. obj.punishmentId = id;
  69. punishments.push(obj);
  70. }
  71. punishments = punishments.filter(punishment => {
  72. if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);
  73. return punishment.expiresAt > Date.now();
  74. });
  75. next(null, punishments);
  76. },
  77. (punishments, next) => {
  78. async.each(
  79. punishmentsToRemove,
  80. (punishment, next2) => {
  81. cache.hdel('punishments', punishment.punishmentId, () => {
  82. next2();
  83. });
  84. },
  85. () => {
  86. next(null, punishments);
  87. }
  88. );
  89. }
  90. ], (err, punishments) => {
  91. if (err && err !== true) return cb(err);
  92. cb(null, punishments);
  93. });
  94. },
  95. /**
  96. * Gets a punishment by id
  97. *
  98. * @param {String} id - the id of the punishment we are trying to get
  99. * @param {Function} cb - gets called once we're done initializing
  100. */
  101. getPunishment: function(id, cb) {
  102. if (lockdown) return cb('Lockdown');
  103. async.waterfall([
  104. (next) => {
  105. if (!mongoose.Types.ObjectId.isValid(id)) return next('Id is not a valid ObjectId.');
  106. cache.hget('punishments', id, next);
  107. },
  108. (punishment, next) => {
  109. if (punishment) return next(true, punishment);
  110. db.models.punishment.findOne({_id: id}, next);
  111. },
  112. (punishment, next) => {
  113. if (punishment) {
  114. cache.hset('punishments', id, punishment, next);
  115. } else next('Punishment not found.');
  116. },
  117. ], (err, punishment) => {
  118. if (err && err !== true) return cb(err);
  119. cb(null, punishment);
  120. });
  121. },
  122. /**
  123. * Gets all punishments from a userId
  124. *
  125. * @param {String} userId - the userId of the punishment(s) we are trying to get
  126. * @param {Function} cb - gets called once we're done initializing
  127. */
  128. getPunishmentsFromUserId: function(userId, cb) {
  129. if (lockdown) return cb('Lockdown');
  130. async.waterfall([
  131. (next) => {
  132. module.exports.getPunishments(next);
  133. },
  134. (punishments, next) => {
  135. punishments = punishments.filter((punishment) => {
  136. return punishment.type === 'banUserId' && punishment.value === userId;
  137. });
  138. next(null, punishments);
  139. }
  140. ], (err, punishments) => {
  141. if (err && err !== true) return cb(err);
  142. cb(null, punishments);
  143. });
  144. },
  145. addPunishment: function(type, value, reason, expiresAt, punishedBy, cb) {
  146. if (lockdown) return cb('Lockdown');
  147. async.waterfall([
  148. (next) => {
  149. const punishment = new db.models.punishment({
  150. type,
  151. value,
  152. reason,
  153. active: true,
  154. expiresAt,
  155. punishedAt: Date.now(),
  156. punishedBy
  157. });
  158. punishment.save((err, punishment) => {
  159. if (err) return next(err);
  160. next(null, punishment);
  161. });
  162. },
  163. (punishment, next) => {
  164. cache.hset('punishments', punishment._id, { type, value, reason, expiresAt }, next);
  165. },
  166. (punishment, next) => {
  167. // DISCORD MESSAGE
  168. next();
  169. }
  170. ], (err) => {
  171. cb(err);
  172. });
  173. },
  174. removePunishmentFromCache: function(punishmentId, cb) {
  175. if (lockdown) return cb('Lockdown');
  176. async.waterfall([
  177. (next) => {
  178. const punishment = new db.models.punishment({
  179. type,
  180. value,
  181. reason,
  182. active: true,
  183. expiresAt,
  184. punishedAt: Date.now(),
  185. punishedBy
  186. });
  187. punishment.save((err, punishment) => {
  188. console.log(err);
  189. if (err) return next(err);
  190. next(null, punishment);
  191. });
  192. },
  193. (punishment, next) => {
  194. cache.hset('punishments', punishment._id, punishment, next);
  195. },
  196. (punishment, next) => {
  197. // DISCORD MESSAGE
  198. next();
  199. }
  200. ], (err) => {
  201. cb(err);
  202. });
  203. },
  204. _lockdown: () => {
  205. lockdown = true;
  206. }
  207. };