2024-10-18 07:04:57 -04:00
|
|
|
import minimist from 'minimist'
|
|
|
|
import { waitForDb } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import ChatApiHandler from '../app/src/Features/Chat/ChatApiHandler.js'
|
|
|
|
import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js'
|
|
|
|
import DocumentUpdaterHandler from '../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js'
|
|
|
|
import { promiseMapWithLimit } from '@overleaf/promise-utils'
|
2022-08-04 05:42:14 -04:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
|
2022-08-04 05:42:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove doc comment ranges that are "orphaned" as they do have matching chat
|
|
|
|
* threads. This can happen when adding comments and the HTTP request fails, but
|
|
|
|
* the ShareJS op succeeded (eventually). See https://github.com/overleaf/internal/issues/3425
|
|
|
|
* for more detail.
|
|
|
|
*/
|
|
|
|
async function main() {
|
|
|
|
await waitForDb()
|
|
|
|
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
const { projectId, docId } = argv
|
|
|
|
|
|
|
|
const threads = await ChatApiHandler.promises.getThreads(projectId)
|
|
|
|
const threadIds = Object.keys(threads)
|
|
|
|
|
|
|
|
const doc = await DocstoreManager.promises.getDoc(projectId, docId)
|
|
|
|
const comments = doc.ranges.comments
|
|
|
|
|
|
|
|
const orphanedCommentIds = comments.filter(comment => {
|
|
|
|
const commentThreadId = comment.op.t
|
|
|
|
return !threadIds.includes(commentThreadId)
|
|
|
|
})
|
|
|
|
|
|
|
|
await promiseMapWithLimit(
|
|
|
|
WRITE_CONCURRENCY,
|
|
|
|
orphanedCommentIds,
|
|
|
|
async comment => {
|
|
|
|
await DocumentUpdaterHandler.promises.deleteThread(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
comment.op.t
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
await DocumentUpdaterHandler.promises.flushDocToMongo(projectId, docId)
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
try {
|
|
|
|
await main()
|
|
|
|
console.log('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|