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')
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
module.exports = ThreadManager = {
|
|
|
|
GLOBAL_THREAD: 'GLOBAL',
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
findOrCreateThread(projectId, threadId, callback) {
|
2018-12-20 14:13:59 -05:00
|
|
|
let query, update
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
projectId = ObjectId(projectId.toString())
|
|
|
|
if (threadId !== ThreadManager.GLOBAL_THREAD) {
|
|
|
|
threadId = ObjectId(threadId.toString())
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
if (threadId === ThreadManager.GLOBAL_THREAD) {
|
2018-12-20 14:13:59 -05:00
|
|
|
query = {
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: projectId,
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id: { $exists: false },
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
update = {
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: projectId,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = {
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
update = {
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: projectId,
|
|
|
|
thread_id: threadId,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2021-11-19 08:12:17 -05:00
|
|
|
db.rooms.findOneAndUpdate(
|
2021-07-13 07:04:48 -04:00
|
|
|
query,
|
|
|
|
{ $set: update },
|
2021-11-19 08:12:17 -05:00
|
|
|
{ upsert: true, returnDocument: 'after' },
|
|
|
|
function (error, result) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2021-07-13 07:04:48 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-11-19 08:12:17 -05:00
|
|
|
callback(null, result.value)
|
2020-08-27 11:31:27 -04:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
findAllThreadRooms(projectId, callback) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms
|
|
|
|
.find(
|
|
|
|
{
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id: { $exists: true },
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
thread_id: 1,
|
2021-07-13 07:04:48 -04:00
|
|
|
resolved: 1,
|
2020-08-27 11:31:27 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.toArray(callback)
|
2018-12-20 14:13:59 -05:00
|
|
|
},
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
resolveThread(projectId, threadId, userId, callback) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms.updateOne(
|
|
|
|
{
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
resolved: {
|
2022-01-06 11:32:46 -05:00
|
|
|
user_id: userId,
|
2021-07-13 07:04:48 -04:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
|
|
|
},
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
callback
|
2018-12-20 14:13:59 -05:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
reopenThread(projectId, threadId, callback) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms.updateOne(
|
|
|
|
{
|
2022-01-06 11:32:46 -05:00
|
|
|
project_id: ObjectId(projectId.toString()),
|
|
|
|
thread_id: ObjectId(threadId.toString()),
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$unset: {
|
2021-07-13 07:04:48 -04:00
|
|
|
resolved: true,
|
|
|
|
},
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
callback
|
2018-12-20 14:13:59 -05:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-01-06 11:32:46 -05:00
|
|
|
deleteThread(projectId, threadId, callback) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (!callback) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
this.findOrCreateThread(projectId, threadId, function (error, room) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-06 11:32:46 -05:00
|
|
|
return callback(error)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
2022-01-06 11:32:46 -05:00
|
|
|
db.rooms.deleteOne(
|
|
|
|
{
|
|
|
|
_id: room._id,
|
|
|
|
},
|
|
|
|
function (error) {
|
2022-01-07 05:29:50 -05:00
|
|
|
if (error) {
|
2022-01-06 11:32:46 -05:00
|
|
|
return callback(error)
|
|
|
|
}
|
2022-01-07 05:41:23 -05:00
|
|
|
callback(null, room._id)
|
2022-01-06 11:32:46 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
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)
|
|
|
|
)
|