2018-12-20 14:13:59 -05:00
|
|
|
let ThreadManager
|
2020-08-27 11:31:27 -04:00
|
|
|
const { db, ObjectId } = require('../../mongodb')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-09-09 05:37:56 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2022-01-07 07:04:23 -05:00
|
|
|
const { callbackify } = require('util')
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
const GLOBAL_THREAD = 'GLOBAL'
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
async function findOrCreateThread(projectId, threadId) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let query, update
|
|
|
|
projectId = ObjectId(projectId.toString())
|
|
|
|
if (threadId !== GLOBAL_THREAD) {
|
|
|
|
threadId = ObjectId(threadId.toString())
|
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
if (threadId === GLOBAL_THREAD) {
|
|
|
|
query = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: { $exists: false },
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
update = {
|
|
|
|
project_id: projectId,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
|
|
|
}
|
|
|
|
update = {
|
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
const result = await db.rooms.findOneAndUpdate(
|
2022-01-07 06:48:39 -05:00
|
|
|
query,
|
|
|
|
{ $set: update },
|
2022-01-07 07:04:23 -05:00
|
|
|
{ upsert: true, returnDocument: 'after' }
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
2022-01-07 07:04:23 -05:00
|
|
|
return result.value
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
async function findAllThreadRooms(projectId) {
|
|
|
|
return db.rooms
|
2022-01-07 06:48:39 -05:00
|
|
|
.find(
|
2020-08-27 11:31:27 -04:00
|
|
|
{
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
2022-01-07 06:48:39 -05:00
|
|
|
thread_id: { $exists: true },
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
2022-01-07 06:48:39 -05:00
|
|
|
thread_id: 1,
|
|
|
|
resolved: 1,
|
|
|
|
}
|
|
|
|
)
|
2022-01-07 07:04:23 -05:00
|
|
|
.toArray()
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
async function resolveThread(projectId, threadId, userId) {
|
|
|
|
await db.rooms.updateOne(
|
2022-01-07 06:48:39 -05:00
|
|
|
{
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
resolved: {
|
|
|
|
user_id: userId,
|
|
|
|
ts: new Date(),
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
2022-01-07 07:04:23 -05:00
|
|
|
}
|
2022-01-07 06:48:39 -05:00
|
|
|
)
|
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
async function reopenThread(projectId, threadId) {
|
|
|
|
await db.rooms.updateOne(
|
2022-01-07 06:48:39 -05:00
|
|
|
{
|
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$unset: {
|
|
|
|
resolved: true,
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-07 07:04:23 -05:00
|
|
|
async function deleteThread(projectId, threadId) {
|
|
|
|
const room = await findOrCreateThread(projectId, threadId)
|
|
|
|
await db.rooms.deleteOne({
|
|
|
|
_id: room._id,
|
2022-01-07 06:48:39 -05:00
|
|
|
})
|
2022-01-07 07:04:23 -05:00
|
|
|
return room._id
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2018-12-20 14:13:59 -05:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
module.exports = ThreadManager = {
|
|
|
|
GLOBAL_THREAD,
|
2022-01-07 07:04:23 -05:00
|
|
|
findOrCreateThread: callbackify(findOrCreateThread),
|
|
|
|
findAllThreadRooms: callbackify(findAllThreadRooms),
|
|
|
|
resolveThread: callbackify(resolveThread),
|
|
|
|
reopenThread: callbackify(reopenThread),
|
|
|
|
deleteThread: callbackify(deleteThread),
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
;[
|
|
|
|
'findOrCreateThread',
|
|
|
|
'findAllThreadRooms',
|
|
|
|
'resolveThread',
|
|
|
|
'reopenThread',
|
2021-07-13 07:04:48 -04:00
|
|
|
'deleteThread',
|
|
|
|
].map(method =>
|
2018-12-20 14:13:59 -05:00
|
|
|
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
|
|
|
|
)
|