mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-29 00:21:41 +00:00
0a12c47b35
Move chat service to ES modules GitOrigin-RevId: c08ae8328b8f3b539e6cfe052834b84bb3756330
111 lines
2.2 KiB
JavaScript
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 = 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
|
|
}
|
|
|
|
export async function findAllThreadRooms(projectId) {
|
|
return db.rooms
|
|
.find(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
thread_id: { $exists: true },
|
|
},
|
|
{
|
|
thread_id: 1,
|
|
resolved: 1,
|
|
}
|
|
)
|
|
.toArray()
|
|
}
|
|
|
|
export async function findAllThreadRoomsAndGlobalThread(projectId) {
|
|
return db.rooms
|
|
.find(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
},
|
|
{
|
|
thread_id: 1,
|
|
resolved: 1,
|
|
}
|
|
)
|
|
.toArray()
|
|
}
|
|
|
|
export 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(),
|
|
},
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
export async function reopenThread(projectId, threadId) {
|
|
await db.rooms.updateOne(
|
|
{
|
|
project_id: ObjectId(projectId.toString()),
|
|
thread_id: 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: ObjectId(projectId.toString()),
|
|
})
|
|
}
|