ThreadManager.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { db, ObjectId } from '../../mongodb.js'
  2. export class MissingThreadError extends Error {}
  3. export const GLOBAL_THREAD = 'GLOBAL'
  4. export async function findOrCreateThread(projectId, threadId) {
  5. let query, update
  6. projectId = new ObjectId(projectId.toString())
  7. if (threadId !== GLOBAL_THREAD) {
  8. threadId = new ObjectId(threadId.toString())
  9. }
  10. if (threadId === GLOBAL_THREAD) {
  11. query = {
  12. project_id: projectId,
  13. thread_id: { $exists: false },
  14. }
  15. update = {
  16. project_id: projectId,
  17. }
  18. } else {
  19. query = {
  20. project_id: projectId,
  21. thread_id: threadId,
  22. }
  23. update = {
  24. project_id: projectId,
  25. thread_id: threadId,
  26. }
  27. }
  28. const result = await db.rooms.findOneAndUpdate(
  29. query,
  30. { $set: update },
  31. { upsert: true, returnDocument: 'after' }
  32. )
  33. return result
  34. }
  35. export async function findAllThreadRooms(projectId) {
  36. return await db.rooms
  37. .find(
  38. {
  39. project_id: new ObjectId(projectId.toString()),
  40. thread_id: { $exists: true },
  41. },
  42. {
  43. thread_id: 1,
  44. resolved: 1,
  45. }
  46. )
  47. .toArray()
  48. }
  49. export async function findAllThreadRoomsAndGlobalThread(projectId) {
  50. return await db.rooms
  51. .find(
  52. {
  53. project_id: new ObjectId(projectId.toString()),
  54. },
  55. {
  56. thread_id: 1,
  57. resolved: 1,
  58. }
  59. )
  60. .toArray()
  61. }
  62. export async function resolveThread(projectId, threadId, userId) {
  63. await db.rooms.updateOne(
  64. {
  65. project_id: new ObjectId(projectId.toString()),
  66. thread_id: new ObjectId(threadId.toString()),
  67. },
  68. {
  69. $set: {
  70. resolved: {
  71. user_id: userId,
  72. ts: new Date(),
  73. },
  74. },
  75. }
  76. )
  77. }
  78. export async function reopenThread(projectId, threadId) {
  79. await db.rooms.updateOne(
  80. {
  81. project_id: new ObjectId(projectId.toString()),
  82. thread_id: new ObjectId(threadId.toString()),
  83. },
  84. {
  85. $unset: {
  86. resolved: true,
  87. },
  88. }
  89. )
  90. }
  91. export async function deleteThread(projectId, threadId) {
  92. const room = await findOrCreateThread(projectId, threadId)
  93. await db.rooms.deleteOne({
  94. _id: room._id,
  95. })
  96. return room._id
  97. }
  98. export async function deleteAllThreadsInProject(projectId) {
  99. await db.rooms.deleteMany({
  100. project_id: new ObjectId(projectId.toString()),
  101. })
  102. }
  103. export async function getResolvedThreadIds(projectId) {
  104. const resolvedThreadIds = await db.rooms
  105. .find(
  106. {
  107. project_id: new ObjectId(projectId),
  108. thread_id: { $exists: true },
  109. resolved: { $exists: true },
  110. },
  111. { projection: { thread_id: 1 } }
  112. )
  113. .map(record => record.thread_id.toString())
  114. .toArray()
  115. return resolvedThreadIds
  116. }
  117. export async function duplicateThread(projectId, threadId) {
  118. const room = await db.rooms.findOne({
  119. project_id: new ObjectId(projectId),
  120. thread_id: new ObjectId(threadId),
  121. })
  122. if (!room) {
  123. throw new MissingThreadError('Trying to duplicate a non-existent thread')
  124. }
  125. const newRoom = {
  126. project_id: room.project_id,
  127. thread_id: new ObjectId(),
  128. }
  129. if (room.resolved) {
  130. newRoom.resolved = room.resolved
  131. }
  132. const confirmation = await db.rooms.insertOne(newRoom)
  133. newRoom._id = confirmation.insertedId
  134. return { oldRoom: room, newRoom }
  135. }
  136. export async function findThreadsById(projectId, threadIds) {
  137. return await db.rooms
  138. .find({
  139. project_id: new ObjectId(projectId),
  140. thread_id: { $in: threadIds.map(id => new ObjectId(id)) },
  141. })
  142. .toArray()
  143. }