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

164 lines
3.7 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
handle-callback-err,
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
2020-08-19 08:00:01 -04:00
const { ObjectId, getCollection } = require('../../mongodb')
const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
2020-08-19 08:00:01 -04:00
const roomsCollectionPromise = getCollection('rooms')
module.exports = ThreadManager = {
GLOBAL_THREAD: 'GLOBAL',
findOrCreateThread(project_id, thread_id, callback) {
let query, update
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error, thread) {}
}
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,
thread_id: { $exists: false }
}
update = {
project_id
}
} else {
query = {
project_id,
thread_id
}
update = {
project_id,
thread_id
}
}
2020-08-19 08:00:01 -04:00
roomsCollectionPromise.then((rooms) =>
rooms.updateOne(query, { $set: update }, { upsert: true }, function (
error
) {
if (error != null) {
return callback(error)
}
2020-08-19 08:00:01 -04:00
rooms.findOne(query, callback)
})
2020-08-19 08:00:01 -04:00
)
},
2017-03-16 12:08:57 -04:00
findAllThreadRooms(project_id, callback) {
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error, rooms) {}
}
2020-08-19 08:00:01 -04:00
roomsCollectionPromise.then((rooms) =>
rooms
.find(
{
project_id: ObjectId(project_id.toString()),
thread_id: { $exists: true }
},
{
thread_id: 1,
resolved: 1
}
)
.toArray(callback)
)
},
2017-03-16 12:08:57 -04:00
resolveThread(project_id, thread_id, user_id, callback) {
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error) {}
}
2020-08-19 08:00:01 -04:00
roomsCollectionPromise.then((rooms) =>
rooms.updateOne(
{
project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
},
{
$set: {
resolved: {
user_id,
ts: new Date()
}
}
2020-08-19 08:00:01 -04:00
},
callback
)
)
},
reopenThread(project_id, thread_id, callback) {
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error) {}
}
2020-08-19 08:00:01 -04:00
roomsCollectionPromise.then((rooms) =>
rooms.updateOne(
{
project_id: ObjectId(project_id.toString()),
thread_id: ObjectId(thread_id.toString())
},
{
$unset: {
resolved: true
}
},
callback
)
)
},
deleteThread(project_id, thread_id, callback) {
if (callback == null) {
2020-08-10 12:01:11 -04:00
callback = function (error, room_id) {}
}
2020-08-10 12:01:11 -04:00
return this.findOrCreateThread(project_id, thread_id, function (
error,
room
) {
if (error != null) {
return callback(error)
}
2020-08-19 08:00:01 -04:00
roomsCollectionPromise.then((rooms) =>
rooms.deleteOne(
{
_id: room._id
},
function (error) {
if (error != null) {
return callback(error)
}
return callback(null, room._id)
}
2020-08-19 08:00:01 -04:00
)
)
})
}
}
;[
'findOrCreateThread',
'findAllThreadRooms',
'resolveThread',
'reopenThread',
'deleteThread'
2020-08-10 12:01:11 -04:00
].map((method) =>
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
)