overleaf/services/chat/app/js/Features/Threads/ThreadManager.js
Christopher Hoskin b3b02941c7 Merge pull request #15261 from overleaf/csh-issue-11625-mongo-ug-5-chat
Upgrade mongodb module for chat from 4.11.0 to 6.1.0

GitOrigin-RevId: d4ce72b093fd09638a575a14f8945c8cb9ec7f3d
2023-10-18 08:04:27 +00:00

111 lines
2.2 KiB
JavaScript

import { db, ObjectId } from '../../mongodb.js'
export const GLOBAL_THREAD = 'GLOBAL'
export async function findOrCreateThread(projectId, threadId) {
let query, update
projectId = new ObjectId(projectId.toString())
if (threadId !== GLOBAL_THREAD) {
threadId = new 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
}
export async function findAllThreadRooms(projectId) {
return db.rooms
.find(
{
project_id: new ObjectId(projectId.toString()),
thread_id: { $exists: true },
},
{
thread_id: 1,
resolved: 1,
}
)
.toArray()
}
export async function findAllThreadRoomsAndGlobalThread(projectId) {
return db.rooms
.find(
{
project_id: new ObjectId(projectId.toString()),
},
{
thread_id: 1,
resolved: 1,
}
)
.toArray()
}
export async function resolveThread(projectId, threadId, userId) {
await db.rooms.updateOne(
{
project_id: new ObjectId(projectId.toString()),
thread_id: new ObjectId(threadId.toString()),
},
{
$set: {
resolved: {
user_id: userId,
ts: new Date(),
},
},
}
)
}
export async function reopenThread(projectId, threadId) {
await db.rooms.updateOne(
{
project_id: new ObjectId(projectId.toString()),
thread_id: new ObjectId(threadId.toString()),
},
{
$unset: {
resolved: true,
},
}
)
}
export async function deleteThread(projectId, threadId) {
const room = await findOrCreateThread(projectId, threadId)
await db.rooms.deleteOne({
_id: room._id,
})
return room._id
}
export async function deleteAllThreadsInProject(projectId) {
await db.rooms.deleteMany({
project_id: new ObjectId(projectId.toString()),
})
}