2018-12-20 19:13:59 +00:00
|
|
|
let MessageManager
|
2020-08-27 15:31:27 +00:00
|
|
|
const { db, ObjectId } = require('../../mongodb')
|
2020-09-09 09:37:56 +00:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2014-08-15 09:50:36 +00:00
|
|
|
|
2022-01-07 12:10:54 +00:00
|
|
|
async function createMessage(roomId, userId, content, timestamp) {
|
2022-01-07 11:48:39 +00:00
|
|
|
let newMessageOpts = {
|
|
|
|
content,
|
|
|
|
room_id: roomId,
|
|
|
|
user_id: userId,
|
|
|
|
timestamp,
|
|
|
|
}
|
|
|
|
newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts)
|
2022-01-07 12:10:54 +00:00
|
|
|
const confirmation = await db.messages.insertOne(newMessageOpts)
|
|
|
|
newMessageOpts._id = confirmation.insertedId
|
|
|
|
return newMessageOpts
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
2014-08-15 09:50:36 +00:00
|
|
|
|
2022-01-07 12:10:54 +00:00
|
|
|
async function getMessages(roomId, limit, before) {
|
2022-01-07 11:48:39 +00:00
|
|
|
let query = { room_id: roomId }
|
|
|
|
if (before) {
|
|
|
|
query.timestamp = { $lt: before }
|
|
|
|
}
|
|
|
|
query = _ensureIdsAreObjectIds(query)
|
2022-01-07 12:10:54 +00:00
|
|
|
return db.messages.find(query).sort({ timestamp: -1 }).limit(limit).toArray()
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
2017-01-24 14:44:32 +00:00
|
|
|
|
2022-01-07 12:10:54 +00:00
|
|
|
async function findAllMessagesInRooms(roomIds) {
|
|
|
|
return db.messages
|
2022-01-07 11:48:39 +00:00
|
|
|
.find({
|
|
|
|
room_id: { $in: roomIds },
|
|
|
|
})
|
2022-01-07 12:10:54 +00:00
|
|
|
.toArray()
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
2017-01-24 14:44:32 +00:00
|
|
|
|
2022-01-07 12:10:54 +00:00
|
|
|
async function deleteAllMessagesInRoom(roomId) {
|
|
|
|
await db.messages.deleteMany({
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 12:20:52 +00:00
|
|
|
async function deleteAllMessagesInRooms(roomIds) {
|
|
|
|
await db.messages.deleteMany({
|
|
|
|
room_id: { $in: roomIds },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-31 07:54:33 +00:00
|
|
|
async function updateMessage(roomId, messageId, userId, content, timestamp) {
|
2022-01-07 11:48:39 +00:00
|
|
|
const query = _ensureIdsAreObjectIds({
|
|
|
|
_id: messageId,
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-03-31 07:54:33 +00:00
|
|
|
if (userId) {
|
|
|
|
query.user_id = ObjectId(userId)
|
|
|
|
}
|
2022-03-31 09:23:22 +00:00
|
|
|
const res = await db.messages.updateOne(query, {
|
2022-01-07 12:10:54 +00:00
|
|
|
$set: {
|
|
|
|
content,
|
|
|
|
edited_at: timestamp,
|
2022-01-07 11:48:39 +00:00
|
|
|
},
|
2022-01-07 12:10:54 +00:00
|
|
|
})
|
2022-03-31 09:23:22 +00:00
|
|
|
return res.modifiedCount === 1
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
2017-03-16 16:08:57 +00:00
|
|
|
|
2022-01-07 12:10:54 +00:00
|
|
|
async function deleteMessage(roomId, messageId) {
|
2022-01-07 11:48:39 +00:00
|
|
|
const query = _ensureIdsAreObjectIds({
|
|
|
|
_id: messageId,
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-01-07 12:10:54 +00:00
|
|
|
await db.messages.deleteOne(query)
|
2022-01-07 11:48:39 +00:00
|
|
|
}
|
2017-03-17 15:16:38 +00:00
|
|
|
|
2022-01-07 11:48:39 +00:00
|
|
|
function _ensureIdsAreObjectIds(query) {
|
|
|
|
if (query.user_id && !(query.user_id instanceof ObjectId)) {
|
|
|
|
query.user_id = ObjectId(query.user_id)
|
|
|
|
}
|
|
|
|
if (query.room_id && !(query.room_id instanceof ObjectId)) {
|
|
|
|
query.room_id = ObjectId(query.room_id)
|
|
|
|
}
|
|
|
|
if (query._id && !(query._id instanceof ObjectId)) {
|
|
|
|
query._id = ObjectId(query._id)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MessageManager = {
|
2022-01-07 14:30:29 +00:00
|
|
|
createMessage,
|
|
|
|
getMessages,
|
|
|
|
findAllMessagesInRooms,
|
|
|
|
deleteAllMessagesInRoom,
|
2022-03-16 12:20:52 +00:00
|
|
|
deleteAllMessagesInRooms,
|
2022-01-07 14:30:29 +00:00
|
|
|
updateMessage,
|
|
|
|
deleteMessage,
|
2018-12-20 19:13:59 +00:00
|
|
|
}
|
|
|
|
;[
|
|
|
|
'createMessage',
|
|
|
|
'getMessages',
|
|
|
|
'findAllMessagesInRooms',
|
|
|
|
'updateMessage',
|
2021-07-13 11:04:48 +00:00
|
|
|
'deleteMessage',
|
|
|
|
].map(method =>
|
2018-12-20 19:13:59 +00:00
|
|
|
metrics.timeAsyncMethod(
|
|
|
|
MessageManager,
|
|
|
|
method,
|
|
|
|
'mongo.MessageManager',
|
|
|
|
logger
|
|
|
|
)
|
|
|
|
)
|