2018-12-20 14:13:56 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-12-20 14:13:53 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2018-12-20 14:13:59 -05:00
|
|
|
let ThreadManager
|
2020-08-27 11:31:27 -04:00
|
|
|
const { db, ObjectId } = require('../../mongodb')
|
2018-12-20 14:13:59 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
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
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
findOrCreateThread(project_id, thread_id, callback) {
|
|
|
|
let query, update
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, thread) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
project_id = ObjectId(project_id.toString())
|
|
|
|
if (thread_id !== ThreadManager.GLOBAL_THREAD) {
|
|
|
|
thread_id = ObjectId(thread_id.toString())
|
|
|
|
}
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
if (thread_id === ThreadManager.GLOBAL_THREAD) {
|
|
|
|
query = {
|
|
|
|
project_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id: { $exists: false },
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
update = {
|
2021-07-13 07:04:48 -04:00
|
|
|
project_id,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query = {
|
|
|
|
project_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
update = {
|
|
|
|
project_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id,
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
db.rooms.updateOne(
|
|
|
|
query,
|
|
|
|
{ $set: update },
|
|
|
|
{ upsert: true },
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
db.rooms.findOne(query, callback)
|
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
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
findAllThreadRooms(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, rooms) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms
|
|
|
|
.find(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(project_id.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
|
|
|
|
2018-12-20 14:13:59 -05:00
|
|
|
resolveThread(project_id, thread_id, user_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms.updateOne(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id: ObjectId(thread_id.toString()),
|
2020-08-27 11:31:27 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
resolved: {
|
|
|
|
user_id,
|
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
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
reopenThread(project_id, thread_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2020-08-27 11:31:27 -04:00
|
|
|
db.rooms.updateOne(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
thread_id: ObjectId(thread_id.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
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteThread(project_id, thread_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, room_id) {}
|
2018-12-20 14:13:59 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return this.findOrCreateThread(
|
|
|
|
project_id,
|
|
|
|
thread_id,
|
|
|
|
function (error, room) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
2020-08-27 11:31:27 -04:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
db.rooms.deleteOne(
|
|
|
|
{
|
|
|
|
_id: room._id,
|
|
|
|
},
|
|
|
|
function (error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, room._id)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
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)
|
|
|
|
)
|