SystemMessageManager.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable
  2. handle-callback-err,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. let SystemMessageManager
  13. const { SystemMessage } = require('../../models/SystemMessage')
  14. module.exports = SystemMessageManager = {
  15. getMessages(callback) {
  16. if (callback == null) {
  17. callback = function(error, messages) {}
  18. }
  19. callback(null, this._cachedMessages)
  20. },
  21. getMessagesFromDB(callback) {
  22. if (callback == null) {
  23. callback = function(error, messages) {}
  24. }
  25. return SystemMessage.find({}, callback)
  26. },
  27. clearMessages(callback) {
  28. if (callback == null) {
  29. callback = function(error) {}
  30. }
  31. return SystemMessage.deleteMany({}, callback)
  32. },
  33. createMessage(content, callback) {
  34. if (callback == null) {
  35. callback = function(error) {}
  36. }
  37. const message = new SystemMessage({ content })
  38. return message.save(callback)
  39. },
  40. refreshCache() {
  41. this.getMessagesFromDB((error, messages) => {
  42. if (!error) {
  43. this._cachedMessages = messages
  44. }
  45. })
  46. }
  47. }
  48. const CACHE_TIMEOUT = 10 * 1000 * (Math.random() + 2) // 20-30 seconds
  49. SystemMessageManager.refreshCache()
  50. setInterval(() => SystemMessageManager.refreshCache(), CACHE_TIMEOUT)