2020-01-13 14:00:40 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-01-13 14:00:33 -05:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-08-31 04:58:47 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2024-02-28 06:20:53 -05:00
|
|
|
module.exports = {
|
2023-03-16 08:38:09 -04:00
|
|
|
getUserNotifications(userId, callback) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
const query = {
|
2023-10-23 04:45:57 -04:00
|
|
|
user_id: new ObjectId(userId),
|
2021-07-13 07:04:44 -04:00
|
|
|
templateKey: { $exists: true },
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
2020-08-31 04:58:47 -04:00
|
|
|
db.notifications.find(query).toArray(callback)
|
2020-01-13 14:00:49 -05:00
|
|
|
},
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
_countExistingNotifications(userId, notification, callback) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
const query = {
|
2023-10-23 04:45:57 -04:00
|
|
|
user_id: new ObjectId(userId),
|
2021-07-13 07:04:44 -04:00
|
|
|
key: notification.key,
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
2020-06-04 03:50:05 -04:00
|
|
|
return db.notifications.count(query, function (err, count) {
|
2020-01-13 14:00:49 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return callback(null, count)
|
|
|
|
})
|
|
|
|
},
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
addNotification(userId, notification, callback) {
|
2021-07-13 07:04:44 -04:00
|
|
|
return this._countExistingNotifications(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2021-07-13 07:04:44 -04:00
|
|
|
notification,
|
|
|
|
function (err, count) {
|
|
|
|
if (err != null) {
|
2020-01-13 14:00:49 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2021-07-13 07:04:44 -04:00
|
|
|
if (count !== 0 && !notification.forceCreate) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const doc = {
|
2023-10-23 04:45:57 -04:00
|
|
|
user_id: new ObjectId(userId),
|
2021-07-13 07:04:44 -04:00
|
|
|
key: notification.key,
|
|
|
|
messageOpts: notification.messageOpts,
|
|
|
|
templateKey: notification.templateKey,
|
|
|
|
}
|
|
|
|
// TTL index on the optional `expires` field, which should arrive as an iso date-string, corresponding to
|
|
|
|
// a datetime in the future when the document should be automatically removed.
|
|
|
|
// in Mongo, TTL indexes only work on date fields, and ignore the document when that field is missing
|
|
|
|
// see `README.md` for instruction on creating TTL index
|
|
|
|
if (notification.expires != null) {
|
|
|
|
try {
|
|
|
|
doc.expires = new Date(notification.expires)
|
|
|
|
const _testValue = doc.expires.toISOString()
|
|
|
|
} catch (error) {
|
|
|
|
err = error
|
|
|
|
logger.error(
|
2023-03-16 08:38:09 -04:00
|
|
|
{ userId, expires: notification.expires },
|
2021-07-13 07:04:44 -04:00
|
|
|
'error converting `expires` field to Date'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.notifications.updateOne(
|
|
|
|
{ user_id: doc.user_id, key: notification.key },
|
|
|
|
{ $set: doc },
|
|
|
|
{ upsert: true },
|
|
|
|
callback
|
|
|
|
)
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
2021-07-13 07:04:44 -04:00
|
|
|
)
|
2020-01-13 14:00:49 -05:00
|
|
|
},
|
2016-08-17 05:52:35 -04:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
removeNotificationId(userId, notificationId, callback) {
|
2020-01-13 14:00:49 -05:00
|
|
|
const searchOps = {
|
2023-10-23 04:45:57 -04:00
|
|
|
user_id: new ObjectId(userId),
|
|
|
|
_id: new ObjectId(notificationId),
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
const updateOperation = { $unset: { templateKey: true, messageOpts: true } }
|
2020-08-31 04:58:47 -04:00
|
|
|
db.notifications.updateOne(searchOps, updateOperation, callback)
|
2020-01-13 14:00:49 -05:00
|
|
|
},
|
2016-08-17 05:52:35 -04:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
removeNotificationKey(userId, notificationKey, callback) {
|
2020-01-13 14:00:49 -05:00
|
|
|
const searchOps = {
|
2023-10-23 04:45:57 -04:00
|
|
|
user_id: new ObjectId(userId),
|
2023-03-16 08:38:09 -04:00
|
|
|
key: notificationKey,
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|
|
|
|
const updateOperation = { $unset: { templateKey: true } }
|
2020-08-31 04:58:47 -04:00
|
|
|
db.notifications.updateOne(searchOps, updateOperation, callback)
|
2020-01-13 14:00:49 -05:00
|
|
|
},
|
2016-01-14 07:35:16 -05:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
removeNotificationByKeyOnly(notificationKey, callback) {
|
|
|
|
const searchOps = { key: notificationKey }
|
2020-01-13 14:00:49 -05:00
|
|
|
const updateOperation = { $unset: { templateKey: true } }
|
2020-08-31 04:58:47 -04:00
|
|
|
db.notifications.updateOne(searchOps, updateOperation, callback)
|
2020-01-13 14:00:49 -05:00
|
|
|
},
|
2016-02-05 04:38:32 -05:00
|
|
|
|
2021-10-20 06:18:14 -04:00
|
|
|
countNotificationsByKeyOnly(notificationKey, callback) {
|
|
|
|
const searchOps = { key: notificationKey, templateKey: { $exists: true } }
|
|
|
|
db.notifications.count(searchOps, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteUnreadNotificationsByKeyOnlyBulk(notificationKey, callback) {
|
|
|
|
if (typeof notificationKey !== 'string') {
|
|
|
|
throw new Error('refusing to bulk delete arbitrary notifications')
|
|
|
|
}
|
|
|
|
const searchOps = { key: notificationKey, templateKey: { $exists: true } }
|
|
|
|
db.notifications.deleteMany(searchOps, (err, result) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
callback(null, result.deletedCount)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-01-13 14:00:49 -05:00
|
|
|
// hard delete of doc, rather than removing the templateKey
|
2023-03-16 08:38:09 -04:00
|
|
|
deleteNotificationByKeyOnly(notificationKey, callback) {
|
|
|
|
const searchOps = { key: notificationKey }
|
2020-08-31 04:58:47 -04:00
|
|
|
db.notifications.deleteOne(searchOps, callback)
|
2021-07-13 07:04:44 -04:00
|
|
|
},
|
2020-01-13 14:00:49 -05:00
|
|
|
}
|