asyncify ThreadManager

GitOrigin-RevId: 24fc2d5e04238a7e4e23826a19b69a9fbb2f9637
This commit is contained in:
Tim Alby 2022-01-07 13:04:23 +01:00 committed by Copybot
parent f3a5e8a0e8
commit 49e2e02105

View file

@ -2,10 +2,11 @@ let ThreadManager
const { db, ObjectId } = require('../../mongodb') const { db, ObjectId } = require('../../mongodb')
const logger = require('@overleaf/logger') const logger = require('@overleaf/logger')
const metrics = require('@overleaf/metrics') const metrics = require('@overleaf/metrics')
const { callbackify } = require('util')
const GLOBAL_THREAD = 'GLOBAL' const GLOBAL_THREAD = 'GLOBAL'
function findOrCreateThread(projectId, threadId, callback) { async function findOrCreateThread(projectId, threadId) {
let query, update let query, update
projectId = ObjectId(projectId.toString()) projectId = ObjectId(projectId.toString())
if (threadId !== GLOBAL_THREAD) { if (threadId !== GLOBAL_THREAD) {
@ -31,21 +32,16 @@ function findOrCreateThread(projectId, threadId, callback) {
} }
} }
db.rooms.findOneAndUpdate( const result = await db.rooms.findOneAndUpdate(
query, query,
{ $set: update }, { $set: update },
{ upsert: true, returnDocument: 'after' }, { upsert: true, returnDocument: 'after' }
function (error, result) {
if (error) {
return callback(error)
}
callback(null, result.value)
}
) )
return result.value
} }
function findAllThreadRooms(projectId, callback) { async function findAllThreadRooms(projectId) {
db.rooms return db.rooms
.find( .find(
{ {
project_id: ObjectId(projectId.toString()), project_id: ObjectId(projectId.toString()),
@ -56,11 +52,11 @@ function findAllThreadRooms(projectId, callback) {
resolved: 1, resolved: 1,
} }
) )
.toArray(callback) .toArray()
} }
function resolveThread(projectId, threadId, userId, callback) { async function resolveThread(projectId, threadId, userId) {
db.rooms.updateOne( await db.rooms.updateOne(
{ {
project_id: ObjectId(projectId.toString()), project_id: ObjectId(projectId.toString()),
thread_id: ObjectId(threadId.toString()), thread_id: ObjectId(threadId.toString()),
@ -72,13 +68,12 @@ function resolveThread(projectId, threadId, userId, callback) {
ts: new Date(), ts: new Date(),
}, },
}, },
}, }
callback
) )
} }
function reopenThread(projectId, threadId, callback) { async function reopenThread(projectId, threadId) {
db.rooms.updateOne( await db.rooms.updateOne(
{ {
project_id: ObjectId(projectId.toString()), project_id: ObjectId(projectId.toString()),
thread_id: ObjectId(threadId.toString()), thread_id: ObjectId(threadId.toString()),
@ -91,32 +86,21 @@ function reopenThread(projectId, threadId, callback) {
) )
} }
function deleteThread(projectId, threadId, callback) { async function deleteThread(projectId, threadId) {
findOrCreateThread(projectId, threadId, function (error, room) { const room = await findOrCreateThread(projectId, threadId)
if (error) { await db.rooms.deleteOne({
return callback(error) _id: room._id,
}
db.rooms.deleteOne(
{
_id: room._id,
},
function (error) {
if (error) {
return callback(error)
}
callback(null, room._id)
}
)
}) })
return room._id
} }
module.exports = ThreadManager = { module.exports = ThreadManager = {
GLOBAL_THREAD, GLOBAL_THREAD,
findOrCreateThread, findOrCreateThread: callbackify(findOrCreateThread),
findAllThreadRooms, findAllThreadRooms: callbackify(findAllThreadRooms),
resolveThread, resolveThread: callbackify(resolveThread),
reopenThread, reopenThread: callbackify(reopenThread),
deleteThread, deleteThread: callbackify(deleteThread),
} }
;[ ;[
'findOrCreateThread', 'findOrCreateThread',