overleaf/services/web/app/src/Features/SystemMessages/SystemMessageManager.js
Alasdair Smith 0ca81de78c Merge pull request #1717 from overleaf/as-decaffeinate-backend
Decaffeinate backend

GitOrigin-RevId: 4ca9f94fc809cab6f47cec8254cacaf1bb3806fa
2019-05-29 09:32:21 +00:00

61 lines
1.6 KiB
JavaScript

/* eslint-disable
handle-callback-err,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let SystemMessageManager
const { SystemMessage } = require('../../models/SystemMessage')
module.exports = SystemMessageManager = {
getMessages(callback) {
if (callback == null) {
callback = function(error, messages) {}
}
if (this._cachedMessages != null) {
return callback(null, this._cachedMessages)
} else {
return this.getMessagesFromDB((error, messages) => {
if (error != null) {
return callback(error)
}
this._cachedMessages = messages
return callback(null, messages)
})
}
},
getMessagesFromDB(callback) {
if (callback == null) {
callback = function(error, messages) {}
}
return SystemMessage.find({}, callback)
},
clearMessages(callback) {
if (callback == null) {
callback = function(error) {}
}
return SystemMessage.remove({}, callback)
},
createMessage(content, callback) {
if (callback == null) {
callback = function(error) {}
}
const message = new SystemMessage({ content })
return message.save(callback)
},
clearCache() {
return delete this._cachedMessages
}
}
const CACHE_TIMEOUT = 20 * 1000 // 20 seconds
setInterval(() => SystemMessageManager.clearCache(), CACHE_TIMEOUT)