2018-12-20 14:13:56 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-12-20 14:13:53 -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
|
|
|
|
*/
|
2018-12-20 14:13:59 -05:00
|
|
|
let MessageManager
|
2020-08-27 11:31:27 -04:00
|
|
|
const { db, ObjectId } = require('../../mongodb')
|
2020-09-09 05:37:56 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2018-12-20 14:13:59 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
module.exports = MessageManager = {
|
|
|
|
createMessage(room_id, user_id, content, timestamp, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, message) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
let newMessageOpts = {
|
|
|
|
content,
|
|
|
|
room_id,
|
|
|
|
user_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
timestamp,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages.insertOne(newMessageOpts, function (error, confirmation) {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
newMessageOpts._id = confirmation.insertedId
|
|
|
|
callback(null, newMessageOpts)
|
|
|
|
})
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
getMessages(room_id, limit, before, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, messages) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
let query = { room_id }
|
|
|
|
if (before != null) {
|
|
|
|
query.timestamp = { $lt: before }
|
|
|
|
}
|
|
|
|
query = this._ensureIdsAreObjectIds(query)
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages
|
|
|
|
.find(query)
|
|
|
|
.sort({ timestamp: -1 })
|
|
|
|
.limit(limit)
|
|
|
|
.toArray(callback)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
findAllMessagesInRooms(room_ids, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, messages) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages
|
|
|
|
.find({
|
2021-07-13 07:04:48 -04:00
|
|
|
room_id: { $in: room_ids },
|
2020-08-27 11:31:27 -04:00
|
|
|
})
|
|
|
|
.toArray(callback)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
deleteAllMessagesInRoom(room_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages.deleteMany(
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
room_id,
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
callback
|
2018-12-20 14:13:59 -05:00
|
|
|
)
|
|
|
|
},
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
updateMessage(room_id, message_id, content, timestamp, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, message) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
const query = this._ensureIdsAreObjectIds({
|
|
|
|
_id: message_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
room_id,
|
2018-12-20 14:13:59 -05:00
|
|
|
})
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages.updateOne(
|
|
|
|
query,
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
content,
|
2021-07-13 07:04:48 -04:00
|
|
|
edited_at: timestamp,
|
|
|
|
},
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
callback
|
2018-12-20 14:13:59 -05:00
|
|
|
)
|
|
|
|
},
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
deleteMessage(room_id, message_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
const query = this._ensureIdsAreObjectIds({
|
|
|
|
_id: message_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
room_id,
|
2018-12-20 14:13:59 -05:00
|
|
|
})
|
2020-08-27 11:31:27 -04:00
|
|
|
db.messages.deleteOne(query, callback)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2017-03-17 11:16:38 -04:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
_ensureIdsAreObjectIds(query) {
|
|
|
|
if (query.user_id != null && !(query.user_id instanceof ObjectId)) {
|
|
|
|
query.user_id = ObjectId(query.user_id)
|
|
|
|
}
|
|
|
|
if (query.room_id != null && !(query.room_id instanceof ObjectId)) {
|
|
|
|
query.room_id = ObjectId(query.room_id)
|
|
|
|
}
|
|
|
|
if (query._id != null && !(query._id instanceof ObjectId)) {
|
|
|
|
query._id = ObjectId(query._id)
|
|
|
|
}
|
|
|
|
return query
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
;[
|
|
|
|
'createMessage',
|
|
|
|
'getMessages',
|
|
|
|
'findAllMessagesInRooms',
|
|
|
|
'updateMessage',
|
2021-07-13 07:04:48 -04:00
|
|
|
'deleteMessage',
|
|
|
|
].map(method =>
|
2018-12-20 14:13:59 -05:00
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
MessageManager,
|
|
|
|
method,
|
|
|
|
'mongo.MessageManager',
|
|
|
|
logger
|
|
|
|
)
|
|
|
|
)
|