delete_orphaned_doc_comment_ranges.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import minimist from 'minimist'
  2. import ChatApiHandler from '../app/src/Features/Chat/ChatApiHandler.js'
  3. import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js'
  4. import DocumentUpdaterHandler from '../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js'
  5. import { promiseMapWithLimit } from '@overleaf/promise-utils'
  6. import { scriptRunner } from './lib/ScriptRunner.mjs'
  7. const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
  8. /**
  9. * Remove doc comment ranges that are "orphaned" as they do have matching chat
  10. * threads. This can happen when adding comments and the HTTP request fails, but
  11. * the ShareJS op succeeded (eventually). See https://github.com/overleaf/internal/issues/3425
  12. * for more detail.
  13. */
  14. async function main() {
  15. const argv = minimist(process.argv.slice(2))
  16. const { projectId, docId } = argv
  17. const threads = await ChatApiHandler.promises.getThreads(projectId)
  18. const threadIds = Object.keys(threads)
  19. const doc = await DocstoreManager.promises.getDoc(projectId, docId)
  20. const comments = doc.ranges.comments
  21. const orphanedCommentIds = comments.filter(comment => {
  22. const commentThreadId = comment.op.t
  23. return !threadIds.includes(commentThreadId)
  24. })
  25. await promiseMapWithLimit(
  26. WRITE_CONCURRENCY,
  27. orphanedCommentIds,
  28. async comment => {
  29. await DocumentUpdaterHandler.promises.deleteThread(
  30. projectId,
  31. docId,
  32. comment.op.t
  33. )
  34. }
  35. )
  36. await DocumentUpdaterHandler.promises.flushDocToMongo(projectId, docId)
  37. }
  38. try {
  39. await scriptRunner(main)
  40. console.log('Done.')
  41. process.exit(0)
  42. } catch (error) {
  43. console.error({ error })
  44. process.exit(1)
  45. }