2020-02-16 09:02:21 -05:00
|
|
|
const MongoManager = require('./MongoManager')
|
|
|
|
const Errors = require('./Errors')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2022-12-15 07:53:03 -05:00
|
|
|
const _ = require('lodash')
|
2020-02-16 09:02:21 -05:00
|
|
|
const DocArchive = require('./DocArchiveManager')
|
|
|
|
const RangeManager = require('./RangeManager')
|
2021-07-12 12:47:20 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2023-11-08 09:17:19 -05:00
|
|
|
const { callbackifyAll } = require('@overleaf/promise-utils')
|
|
|
|
const { setTimeout } = require('timers/promises')
|
2016-12-05 09:21:49 -05:00
|
|
|
|
2024-05-22 05:37:08 -04:00
|
|
|
/**
|
2024-09-20 09:52:23 -04:00
|
|
|
* @import { Document } from 'mongodb'
|
|
|
|
* @import { WithId } from 'mongodb'
|
2024-05-22 05:37:08 -04:00
|
|
|
*/
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
const DocManager = {
|
2024-05-22 05:37:08 -04:00
|
|
|
/**
|
|
|
|
* @param {string} projectId
|
|
|
|
* @param {string} docId
|
|
|
|
* @param {{inS3: boolean}} filter
|
|
|
|
* @returns {Promise<WithId<Document>>}
|
|
|
|
* @private
|
|
|
|
*/
|
2023-11-08 09:17:19 -05:00
|
|
|
async _getDoc(projectId, docId, filter) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (filter == null) {
|
|
|
|
filter = {}
|
|
|
|
}
|
|
|
|
if (filter.inS3 !== true) {
|
2023-11-08 09:17:19 -05:00
|
|
|
throw new Error('must include inS3 when getting doc')
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
const doc = await MongoManager.promises.findDoc(projectId, docId, filter)
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
if (doc == null) {
|
|
|
|
throw new Errors.NotFoundError(
|
|
|
|
`No such doc: ${docId} in project ${projectId}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.inS3) {
|
|
|
|
await DocArchive.promises.unarchiveDoc(projectId, docId)
|
|
|
|
return await DocManager._getDoc(projectId, docId, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc
|
2021-01-12 12:29:06 -05:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async isDocDeleted(projectId, docId) {
|
|
|
|
const doc = await MongoManager.promises.findDoc(projectId, docId, {
|
|
|
|
deleted: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!doc) {
|
|
|
|
throw new Errors.NotFoundError(
|
|
|
|
`No such project/doc: ${projectId}/${docId}`
|
|
|
|
)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
|
|
|
|
// `doc.deleted` is `undefined` for non deleted docs
|
|
|
|
return Boolean(doc.deleted)
|
|
|
|
},
|
|
|
|
|
|
|
|
async getFullDoc(projectId, docId) {
|
|
|
|
const doc = await DocManager._getDoc(projectId, docId, {
|
|
|
|
lines: true,
|
|
|
|
rev: true,
|
|
|
|
deleted: true,
|
|
|
|
version: true,
|
|
|
|
ranges: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
return doc
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
|
2021-07-30 11:06:16 -04:00
|
|
|
// returns the doc without any version information
|
2023-11-08 09:17:19 -05:00
|
|
|
async _peekRawDoc(projectId, docId) {
|
|
|
|
const doc = await MongoManager.promises.findDoc(projectId, docId, {
|
|
|
|
lines: true,
|
|
|
|
rev: true,
|
|
|
|
deleted: true,
|
|
|
|
version: true,
|
|
|
|
ranges: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (doc == null) {
|
|
|
|
throw new Errors.NotFoundError(
|
|
|
|
`No such doc: ${docId} in project ${projectId}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.inS3) {
|
|
|
|
// skip the unarchiving to mongo when getting a doc
|
|
|
|
const archivedDoc = await DocArchive.promises.getDoc(projectId, docId)
|
|
|
|
Object.assign(doc, archivedDoc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc
|
2021-07-30 11:06:16 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// get the doc from mongo if possible, or from the persistent store otherwise,
|
|
|
|
// without unarchiving it (avoids unnecessary writes to mongo)
|
2023-11-08 09:17:19 -05:00
|
|
|
async peekDoc(projectId, docId) {
|
|
|
|
const doc = await DocManager._peekRawDoc(projectId, docId)
|
2024-01-22 09:45:18 -05:00
|
|
|
await MongoManager.promises.checkRevUnchanged(doc)
|
2023-11-08 09:17:19 -05:00
|
|
|
return doc
|
2021-07-30 11:06:16 -04:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async getDocLines(projectId, docId) {
|
|
|
|
const doc = await DocManager._getDoc(projectId, docId, {
|
|
|
|
lines: true,
|
|
|
|
inS3: true,
|
|
|
|
})
|
|
|
|
return doc
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async getAllDeletedDocs(projectId, filter) {
|
|
|
|
return await MongoManager.promises.getProjectsDeletedDocs(projectId, filter)
|
2021-02-18 05:10:14 -05:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async getAllNonDeletedDocs(projectId, filter) {
|
|
|
|
await DocArchive.promises.unArchiveAllDocs(projectId)
|
|
|
|
const docs = await MongoManager.promises.getProjectsDocs(
|
|
|
|
projectId,
|
|
|
|
{ include_deleted: false },
|
|
|
|
filter
|
|
|
|
)
|
|
|
|
if (docs == null) {
|
|
|
|
throw new Errors.NotFoundError(`No docs for project ${projectId}`)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
return docs
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
|
2024-07-02 11:07:12 -04:00
|
|
|
async projectHasRanges(projectId) {
|
|
|
|
const docs = await MongoManager.promises.getProjectsDocs(
|
|
|
|
projectId,
|
|
|
|
{},
|
|
|
|
{ _id: 1 }
|
|
|
|
)
|
|
|
|
const docIds = docs.map(doc => doc._id)
|
|
|
|
for (const docId of docIds) {
|
|
|
|
const doc = await DocManager.peekDoc(projectId, docId)
|
|
|
|
if (
|
|
|
|
(doc.ranges?.comments != null && doc.ranges.comments.length > 0) ||
|
|
|
|
(doc.ranges?.changes != null && doc.ranges.changes.length > 0)
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async updateDoc(projectId, docId, lines, version, ranges) {
|
|
|
|
const MAX_ATTEMPTS = 2
|
|
|
|
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
|
|
|
try {
|
|
|
|
const { modified, rev } = await DocManager._tryUpdateDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges
|
|
|
|
)
|
|
|
|
return { modified, rev }
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof Errors.DocRevValueError && attempt < MAX_ATTEMPTS) {
|
2023-03-22 09:08:01 -04:00
|
|
|
// Another updateDoc call was racing with ours.
|
|
|
|
// Retry once in a bit.
|
|
|
|
logger.warn(
|
|
|
|
{ projectId, docId, err },
|
|
|
|
'detected concurrent updateDoc call'
|
|
|
|
)
|
2023-11-08 09:17:19 -05:00
|
|
|
await setTimeout(100 + Math.random() * 100)
|
|
|
|
continue
|
2023-03-22 09:08:01 -04:00
|
|
|
} else {
|
2023-11-08 09:17:19 -05:00
|
|
|
throw err
|
2023-03-22 09:08:01 -04:00
|
|
|
}
|
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
}
|
2023-03-22 09:08:01 -04:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async _tryUpdateDoc(projectId, docId, lines, version, ranges) {
|
2020-02-16 09:02:21 -05:00
|
|
|
if (lines == null || version == null || ranges == null) {
|
2023-11-08 09:17:19 -05:00
|
|
|
throw new Error('no lines, version or ranges provided')
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
let doc
|
|
|
|
try {
|
|
|
|
doc = await DocManager._getDoc(projectId, docId, {
|
2020-02-16 09:02:21 -05:00
|
|
|
version: true,
|
|
|
|
rev: true,
|
|
|
|
lines: true,
|
|
|
|
ranges: true,
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
2023-11-08 09:17:19 -05:00
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof Errors.NotFoundError) {
|
|
|
|
doc = null
|
|
|
|
} else {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
ranges = RangeManager.jsonRangesToMongo(ranges)
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
let updateLines, updateRanges, updateVersion
|
|
|
|
if (doc == null) {
|
|
|
|
// If the document doesn't exist, we'll make sure to create/update all parts of it.
|
|
|
|
updateLines = true
|
|
|
|
updateVersion = true
|
|
|
|
updateRanges = true
|
|
|
|
} else {
|
|
|
|
if (doc.version > version) {
|
|
|
|
// Reject update when the version was decremented.
|
|
|
|
// Potential reasons: racing flush, broken history.
|
|
|
|
throw new Errors.DocVersionDecrementedError('rejecting stale update', {
|
|
|
|
updateVersion: version,
|
|
|
|
flushedVersion: doc.version,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
updateLines = !_.isEqual(doc.lines, lines)
|
|
|
|
updateVersion = doc.version !== version
|
|
|
|
updateRanges = RangeManager.shouldUpdateRanges(doc.ranges, ranges)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
let modified = false
|
|
|
|
let rev = doc?.rev || 0
|
2020-02-16 09:02:21 -05:00
|
|
|
|
2023-11-15 07:30:14 -05:00
|
|
|
if (updateLines || updateRanges || updateVersion) {
|
2023-11-08 09:17:19 -05:00
|
|
|
const update = {}
|
|
|
|
if (updateLines) {
|
|
|
|
update.lines = lines
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
if (updateRanges) {
|
|
|
|
update.ranges = ranges
|
|
|
|
}
|
2023-11-15 07:30:14 -05:00
|
|
|
if (updateVersion) {
|
|
|
|
update.version = version
|
|
|
|
}
|
|
|
|
logger.debug(
|
|
|
|
{ projectId, docId, oldVersion: doc?.version, newVersion: version },
|
|
|
|
'updating doc'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (updateLines || updateRanges) {
|
|
|
|
rev += 1 // rev will be incremented in mongo by MongoManager.upsertIntoDocCollection
|
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
|
|
|
|
modified = true
|
|
|
|
await MongoManager.promises.upsertIntoDocCollection(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
doc?.rev,
|
|
|
|
update
|
|
|
|
)
|
|
|
|
} else {
|
2023-11-15 07:30:14 -05:00
|
|
|
logger.debug({ projectId, docId }, 'doc has not changed - not updating')
|
2023-11-08 09:17:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return { modified, rev }
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
async patchDoc(projectId, docId, meta) {
|
2021-02-15 08:13:48 -05:00
|
|
|
const projection = { _id: 1, deleted: true }
|
2023-11-08 09:17:19 -05:00
|
|
|
const doc = await MongoManager.promises.findDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
projection
|
|
|
|
)
|
|
|
|
if (!doc) {
|
|
|
|
throw new Errors.NotFoundError(
|
|
|
|
`No such project/doc to delete: ${projectId}/${docId}`
|
|
|
|
)
|
|
|
|
}
|
2021-02-15 08:13:48 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
if (meta.deleted && Settings.docstore.archiveOnSoftDelete) {
|
|
|
|
// The user will not read this doc anytime soon. Flush it out of mongo.
|
|
|
|
DocArchive.promises.archiveDoc(projectId, docId).catch(err => {
|
|
|
|
logger.warn(
|
|
|
|
{ projectId, docId, err },
|
|
|
|
'archiving a single doc in the background failed'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2021-02-15 08:13:48 -05:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
await MongoManager.promises.patchDoc(projectId, docId, meta)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2023-11-08 09:17:19 -05:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
...callbackifyAll(DocManager, {
|
|
|
|
multiResult: {
|
|
|
|
updateDoc: ['modified', 'rev'],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
promises: DocManager,
|
|
|
|
}
|