asyncify MessageManager

GitOrigin-RevId: 6b89b70f7c19fc9e71d60b634808030f324f40fe
This commit is contained in:
Tim Alby 2022-01-07 13:10:54 +01:00 committed by Copybot
parent 49e2e02105
commit 01cdb4025e

View file

@ -2,8 +2,9 @@ let MessageManager
const { db, ObjectId } = require('../../mongodb') const { db, ObjectId } = require('../../mongodb')
const metrics = require('@overleaf/metrics') const metrics = require('@overleaf/metrics')
const logger = require('@overleaf/logger') const logger = require('@overleaf/logger')
const { callbackify } = require('util')
function createMessage(roomId, userId, content, timestamp, callback) { async function createMessage(roomId, userId, content, timestamp) {
let newMessageOpts = { let newMessageOpts = {
content, content,
room_id: roomId, room_id: roomId,
@ -11,64 +12,53 @@ function createMessage(roomId, userId, content, timestamp, callback) {
timestamp, timestamp,
} }
newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts) newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts)
db.messages.insertOne(newMessageOpts, function (error, confirmation) { const confirmation = await db.messages.insertOne(newMessageOpts)
if (error) { newMessageOpts._id = confirmation.insertedId
return callback(error) return newMessageOpts
}
newMessageOpts._id = confirmation.insertedId
callback(null, newMessageOpts)
})
} }
function getMessages(roomId, limit, before, callback) { async function getMessages(roomId, limit, before) {
let query = { room_id: roomId } let query = { room_id: roomId }
if (before) { if (before) {
query.timestamp = { $lt: before } query.timestamp = { $lt: before }
} }
query = _ensureIdsAreObjectIds(query) query = _ensureIdsAreObjectIds(query)
db.messages.find(query).sort({ timestamp: -1 }).limit(limit).toArray(callback) return db.messages.find(query).sort({ timestamp: -1 }).limit(limit).toArray()
} }
function findAllMessagesInRooms(roomIds, callback) { async function findAllMessagesInRooms(roomIds) {
db.messages return db.messages
.find({ .find({
room_id: { $in: roomIds }, room_id: { $in: roomIds },
}) })
.toArray(callback) .toArray()
} }
function deleteAllMessagesInRoom(roomId, callback) { async function deleteAllMessagesInRoom(roomId) {
db.messages.deleteMany( await db.messages.deleteMany({
{ room_id: roomId,
room_id: roomId, })
},
callback
)
} }
function updateMessage(roomId, messageId, content, timestamp, callback) { async function updateMessage(roomId, messageId, content, timestamp) {
const query = _ensureIdsAreObjectIds({ const query = _ensureIdsAreObjectIds({
_id: messageId, _id: messageId,
room_id: roomId, room_id: roomId,
}) })
db.messages.updateOne( await db.messages.updateOne(query, {
query, $set: {
{ content,
$set: { edited_at: timestamp,
content,
edited_at: timestamp,
},
}, },
callback })
)
} }
function deleteMessage(roomId, messageId, callback) { async function deleteMessage(roomId, messageId) {
const query = _ensureIdsAreObjectIds({ const query = _ensureIdsAreObjectIds({
_id: messageId, _id: messageId,
room_id: roomId, room_id: roomId,
}) })
db.messages.deleteOne(query, callback) await db.messages.deleteOne(query)
} }
function _ensureIdsAreObjectIds(query) { function _ensureIdsAreObjectIds(query) {
@ -85,12 +75,12 @@ function _ensureIdsAreObjectIds(query) {
} }
module.exports = MessageManager = { module.exports = MessageManager = {
createMessage, createMessage: callbackify(createMessage),
getMessages, getMessages: callbackify(getMessages),
findAllMessagesInRooms, findAllMessagesInRooms: callbackify(findAllMessagesInRooms),
deleteAllMessagesInRoom, deleteAllMessagesInRoom: callbackify(deleteAllMessagesInRoom),
updateMessage, updateMessage: callbackify(updateMessage),
deleteMessage, deleteMessage: callbackify(deleteMessage),
} }
;[ ;[
'createMessage', 'createMessage',