overleaf/services/chat/app/js/Features/Threads/ThreadManager.js

155 lines
3.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* 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
*/
let ThreadManager
const { db, ObjectId } = require('../../mongodb')
const logger = require('logger-sharelatex')
const metrics = require('@overleaf/metrics')
module.exports = ThreadManager = {
GLOBAL_THREAD: 'GLOBAL',
findOrCreateThread(project_id, thread_id, callback) {
let query, update
if (callback == null) {
callback = function () {}
}
project_id = ObjectId(project_id.toString())
if (thread_id !== ThreadManager.GLOBAL_THREAD) {
thread_id = ObjectId(thread_id.toString())
}
if (thread_id === ThreadManager.GLOBAL_THREAD) {
query = {
project_id,
2021-07-13 07:04:48 -04:00
thread_id: { $exists: false },
}
update = {
2021-07-13 07:04:48 -04:00
project_id,
}
} else {
query = {
project_id,
2021-07-13 07:04:48 -04:00
thread_id,
}
update = {
project_id,
2021-07-13 07:04:48 -04:00
thread_id,
}
}
db.rooms.findOneAndUpdate(
2021-07-13 07:04:48 -04:00
query,
{ $set: update },
{ upsert: true, returnDocument: 'after' },
function (error, result) {
2021-07-13 07:04:48 -04:00
if (error != null) {
return callback(error)
}
callback(null, result.value)
}
2021-07-13 07:04:48 -04:00
)
},
2017-03-16 12:08:57 -04:00
findAllThreadRooms(project_id, callback) {
if (callback == null) {
callback = function () {}
}
db.rooms
.find(
{
project_id: ObjectId(project_id.toString()),
2021-07-13 07:04:48 -04:00
thread_id: { $exists: true },
},
{
thread_id: 1,
2021-07-13 07:04:48 -04:00
resolved: 1,
}
)
.toArray(callback)
},
2017-03-16 12:08:57 -04:00
resolveThread(project_id, thread_id, user_id, callback) {
if (callback == null) {
callback = function () {}
}
db.rooms.updateOne(
{
project_id: ObjectId(project_id.toString()),
2021-07-13 07:04:48 -04:00
thread_id: ObjectId(thread_id.toString()),
},
{
$set: {
resolved: {
user_id,
2021-07-13 07:04:48 -04:00
ts: new Date(),
},
},
},
callback
)
},
reopenThread(project_id, thread_id, callback) {
if (callback == null) {
callback = function () {}
}
db.rooms.updateOne(
{
project_id: ObjectId(project_id.toString()),
2021-07-13 07:04:48 -04:00
thread_id: ObjectId(thread_id.toString()),
},
{
$unset: {
2021-07-13 07:04:48 -04:00
resolved: true,
},
},
callback
)
},
deleteThread(project_id, thread_id, callback) {
if (callback == null) {
callback = function () {}
}
2021-07-13 07:04:48 -04:00
return this.findOrCreateThread(
project_id,
thread_id,
function (error, room) {
if (error != null) {
return callback(error)
}
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)
}
)
}
)
},
}
;[
'findOrCreateThread',
'findAllThreadRooms',
'resolveThread',
'reopenThread',
2021-07-13 07:04:48 -04:00
'deleteThread',
].map(method =>
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
)