mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
4cc888ce2f
Add endpoints to chat for deleting expired project chat data GitOrigin-RevId: e4eb7c7a79472bb116b2095a76c870e204590288
134 lines
2.7 KiB
JavaScript
134 lines
2.7 KiB
JavaScript
let ThreadManager
|
|
const { db, ObjectId } = require('../../mongodb')
|
|
const logger = require('@overleaf/logger')
|
|
const metrics = require('@overleaf/metrics')
|
|
|
|
const GLOBAL_THREAD = 'GLOBAL'
|
|
|
|
async function findOrCreateThread(projectId, threadId) {
|
|
let query, update
|
|
projectId = ObjectId(projectId.toString())
|
|
if (threadId !== GLOBAL_THREAD) {
|
|
threadId = ObjectId(threadId.toString())
|
|
}
|
|
|
|
if (threadId === GLOBAL_THREAD) {
|
|
query = {
|
|
project_id: projectId,
|
|
thread_id: { $exists: false },
|
|
}
|
|
update = {
|
|
project_id: projectId,
|
|
}
|
|
} else {
|
|
query = {
|
|
project_id: projectId,
|
|
thread_id: threadId,
|
|
}
|
|
update = {
|
|
project_id: projectId,
|
|
thread_id: threadId,
|
|
}
|
|
}
|
|
|
|
const result = await db.rooms.findOneAndUpdate(
|
|
query,
|
|
{ $set: update },
|
|
{ upsert: true, returnDocument: 'after' }
|
|
)
|
|
return result.value
|
|
}
|
|
|
|
async function findAllThreadRooms(projectId) {
|
|
return db.rooms
|
|
.find(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
thread_id: { $exists: true },
|
|
},
|
|
{
|
|
thread_id: 1,
|
|
resolved: 1,
|
|
}
|
|
)
|
|
.toArray()
|
|
}
|
|
|
|
async function findAllThreadRoomsAndGlobalThread(projectId) {
|
|
return db.rooms
|
|
.find(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
},
|
|
{
|
|
thread_id: 1,
|
|
resolved: 1,
|
|
}
|
|
)
|
|
.toArray()
|
|
}
|
|
|
|
async function resolveThread(projectId, threadId, userId) {
|
|
await db.rooms.updateOne(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
thread_id: ObjectId(threadId.toString()),
|
|
},
|
|
{
|
|
$set: {
|
|
resolved: {
|
|
user_id: userId,
|
|
ts: new Date(),
|
|
},
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
async function reopenThread(projectId, threadId) {
|
|
await db.rooms.updateOne(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
thread_id: ObjectId(threadId.toString()),
|
|
},
|
|
{
|
|
$unset: {
|
|
resolved: true,
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
async function deleteThread(projectId, threadId) {
|
|
const room = await findOrCreateThread(projectId, threadId)
|
|
await db.rooms.deleteOne({
|
|
_id: room._id,
|
|
})
|
|
return room._id
|
|
}
|
|
|
|
async function deleteAllThreadsInProject(projectId) {
|
|
await db.rooms.deleteMany({
|
|
project_id: ObjectId(projectId.toString()),
|
|
})
|
|
}
|
|
|
|
module.exports = ThreadManager = {
|
|
GLOBAL_THREAD,
|
|
findOrCreateThread,
|
|
findAllThreadRooms,
|
|
findAllThreadRoomsAndGlobalThread,
|
|
resolveThread,
|
|
reopenThread,
|
|
deleteThread,
|
|
deleteAllThreadsInProject,
|
|
}
|
|
;[
|
|
'findOrCreateThread',
|
|
'findAllThreadRooms',
|
|
'resolveThread',
|
|
'reopenThread',
|
|
'deleteThread',
|
|
].map(method =>
|
|
metrics.timeAsyncMethod(ThreadManager, method, 'mongo.ThreadManager', logger)
|
|
)
|