2020-08-28 08:13:19 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2021-07-12 12:47:20 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2021-08-02 06:36:43 -04:00
|
|
|
const Errors = require('./Errors')
|
2024-01-18 08:00:51 -05:00
|
|
|
const { callbackify } = require('util')
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2022-05-17 07:49:13 -04:00
|
|
|
const ARCHIVING_LOCK_DURATION_MS = Settings.archivingLockDurationMs
|
|
|
|
|
2024-01-22 09:45:18 -05:00
|
|
|
async function findDoc(projectId, docId, projection) {
|
2024-01-18 08:00:51 -05:00
|
|
|
const doc = await db.docs.findOne(
|
2022-04-19 08:28:47 -04:00
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId.toString()),
|
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-04-19 08:28:47 -04:00
|
|
|
},
|
2024-01-22 09:45:18 -05:00
|
|
|
{ projection }
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
2024-01-22 09:45:18 -05:00
|
|
|
if (doc && projection.version && !doc.version) {
|
|
|
|
doc.version = 0
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
return doc
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
|
2024-01-22 09:45:18 -05:00
|
|
|
async function getProjectsDeletedDocs(projectId, projection) {
|
2024-01-18 08:00:51 -05:00
|
|
|
const docs = await db.docs
|
2022-04-19 08:28:47 -04:00
|
|
|
.find(
|
2020-08-28 08:13:19 -04:00
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-04-19 08:28:47 -04:00
|
|
|
deleted: true,
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
2020-09-07 05:16:12 -04:00
|
|
|
{
|
2024-01-22 09:45:18 -05:00
|
|
|
projection,
|
2022-04-19 08:28:47 -04:00
|
|
|
sort: { deletedAt: -1 },
|
|
|
|
limit: Settings.max_deleted_docs,
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
2024-01-18 08:00:51 -05:00
|
|
|
.toArray()
|
|
|
|
return docs
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2024-01-22 09:45:18 -05:00
|
|
|
async function getProjectsDocs(projectId, options, projection) {
|
2023-12-13 04:38:54 -05:00
|
|
|
const query = { project_id: new ObjectId(projectId.toString()) }
|
2022-04-19 08:28:47 -04:00
|
|
|
if (!options.include_deleted) {
|
|
|
|
query.deleted = { $ne: true }
|
|
|
|
}
|
|
|
|
const queryOptions = {
|
2024-01-22 09:45:18 -05:00
|
|
|
projection,
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
if (options.limit) {
|
|
|
|
queryOptions.limit = options.limit
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
const docs = await db.docs.find(query, queryOptions).toArray()
|
|
|
|
return docs
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2021-02-18 05:10:14 -05:00
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function getArchivedProjectDocs(projectId, maxResults) {
|
2022-04-19 08:28:47 -04:00
|
|
|
const query = {
|
2023-12-13 04:38:54 -05:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-04-19 08:28:47 -04:00
|
|
|
inS3: true,
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
const docs = await db.docs
|
2022-04-19 08:28:47 -04:00
|
|
|
.find(query, { projection: { _id: 1 }, limit: maxResults })
|
2024-01-18 08:00:51 -05:00
|
|
|
.toArray()
|
|
|
|
return docs
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2014-06-05 08:29:50 -04:00
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function getNonArchivedProjectDocIds(projectId) {
|
|
|
|
const docs = await db.docs
|
2022-05-17 07:49:13 -04:00
|
|
|
.find(
|
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
project_id: new ObjectId(projectId),
|
2022-05-17 07:49:13 -04:00
|
|
|
inS3: { $ne: true },
|
|
|
|
},
|
|
|
|
{ projection: { _id: 1 } }
|
|
|
|
)
|
|
|
|
.map(doc => doc._id)
|
2024-01-18 08:00:51 -05:00
|
|
|
.toArray()
|
|
|
|
return docs
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2021-06-08 07:59:13 -04:00
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function getNonDeletedArchivedProjectDocs(projectId, maxResults) {
|
2022-04-19 08:28:47 -04:00
|
|
|
const query = {
|
2023-12-13 04:38:54 -05:00
|
|
|
project_id: new ObjectId(projectId.toString()),
|
2022-04-19 08:28:47 -04:00
|
|
|
deleted: { $ne: true },
|
|
|
|
inS3: true,
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
const docs = await db.docs
|
2022-04-19 08:28:47 -04:00
|
|
|
.find(query, { projection: { _id: 1 }, limit: maxResults })
|
2024-01-18 08:00:51 -05:00
|
|
|
.toArray()
|
|
|
|
return docs
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function upsertIntoDocCollection(projectId, docId, previousRev, updates) {
|
2023-03-22 09:08:01 -04:00
|
|
|
if (previousRev) {
|
2023-11-15 07:30:14 -05:00
|
|
|
const update = {
|
|
|
|
$set: updates,
|
|
|
|
$unset: { inS3: true },
|
|
|
|
}
|
|
|
|
if (updates.lines || updates.ranges) {
|
|
|
|
update.$inc = { rev: 1 }
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
const result = await db.docs.updateOne(
|
2023-03-22 09:08:01 -04:00
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId),
|
|
|
|
project_id: new ObjectId(projectId),
|
2023-03-22 09:08:01 -04:00
|
|
|
rev: previousRev,
|
|
|
|
},
|
2024-01-18 08:00:51 -05:00
|
|
|
update
|
2023-03-22 09:08:01 -04:00
|
|
|
)
|
2024-01-18 08:00:51 -05:00
|
|
|
if (result.matchedCount !== 1) {
|
|
|
|
throw new Errors.DocRevValueError()
|
|
|
|
}
|
2023-03-22 09:08:01 -04:00
|
|
|
} else {
|
2024-01-18 08:00:51 -05:00
|
|
|
try {
|
|
|
|
await db.docs.insertOne({
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId),
|
|
|
|
project_id: new ObjectId(projectId),
|
2023-03-22 09:08:01 -04:00
|
|
|
rev: 1,
|
|
|
|
...updates,
|
2024-01-18 08:00:51 -05:00
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 11000) {
|
|
|
|
// duplicate doc _id
|
|
|
|
throw new Errors.DocRevValueError()
|
|
|
|
} else {
|
|
|
|
throw err
|
2023-03-22 09:08:01 -04:00
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 14:55:22 -04:00
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function patchDoc(projectId, docId, meta) {
|
|
|
|
await db.docs.updateOne(
|
2022-04-19 08:28:47 -04:00
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId),
|
|
|
|
project_id: new ObjectId(projectId),
|
2022-04-19 08:28:47 -04:00
|
|
|
},
|
2024-01-18 08:00:51 -05:00
|
|
|
{ $set: meta }
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
|
|
|
}
|
2021-02-15 08:13:48 -05:00
|
|
|
|
2022-05-17 07:49:13 -04:00
|
|
|
/**
|
|
|
|
* Fetch a doc and lock it for archiving
|
|
|
|
*
|
|
|
|
* This will return null if the doc is not found, if it's already archived or
|
|
|
|
* if the lock can't be acquired.
|
|
|
|
*/
|
2024-01-18 08:00:51 -05:00
|
|
|
async function getDocForArchiving(projectId, docId) {
|
2022-05-17 07:49:13 -04:00
|
|
|
const archivingUntil = new Date(Date.now() + ARCHIVING_LOCK_DURATION_MS)
|
2024-01-18 08:00:51 -05:00
|
|
|
const result = await db.docs.findOneAndUpdate(
|
2022-05-17 07:49:13 -04:00
|
|
|
{
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId),
|
|
|
|
project_id: new ObjectId(projectId),
|
2022-05-17 07:49:13 -04:00
|
|
|
inS3: { $ne: true },
|
|
|
|
$or: [{ archivingUntil: null }, { archivingUntil: { $lt: new Date() } }],
|
|
|
|
},
|
|
|
|
{ $set: { archivingUntil } },
|
2023-12-13 04:38:54 -05:00
|
|
|
{
|
|
|
|
projection: { lines: 1, ranges: 1, rev: 1 },
|
|
|
|
includeResultMetadata: true,
|
2022-05-17 07:49:13 -04:00
|
|
|
}
|
|
|
|
)
|
2024-01-18 08:00:51 -05:00
|
|
|
return result.value
|
2022-05-17 07:49:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the doc contents from Mongo and release the archiving lock
|
|
|
|
*/
|
2024-01-18 08:00:51 -05:00
|
|
|
async function markDocAsArchived(projectId, docId, rev) {
|
|
|
|
await db.docs.updateOne(
|
2023-12-13 04:38:54 -05:00
|
|
|
{ _id: new ObjectId(docId), rev },
|
2022-05-17 07:49:13 -04:00
|
|
|
{
|
|
|
|
$set: { inS3: true },
|
|
|
|
$unset: { lines: 1, ranges: 1, archivingUntil: 1 },
|
2024-01-18 08:00:51 -05:00
|
|
|
}
|
2022-05-17 07:49:13 -04:00
|
|
|
)
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2015-06-01 18:36:26 -04:00
|
|
|
|
2022-04-26 07:16:36 -04:00
|
|
|
/**
|
|
|
|
* Restore an archived doc
|
|
|
|
*
|
2022-04-26 11:10:15 -04:00
|
|
|
* This checks that the archived doc's rev matches.
|
2022-04-26 07:16:36 -04:00
|
|
|
*/
|
2024-01-18 08:00:51 -05:00
|
|
|
async function restoreArchivedDoc(projectId, docId, archivedDoc) {
|
2022-04-26 07:16:36 -04:00
|
|
|
const query = {
|
2023-12-13 04:38:54 -05:00
|
|
|
_id: new ObjectId(docId),
|
|
|
|
project_id: new ObjectId(projectId),
|
2022-04-26 07:16:36 -04:00
|
|
|
rev: archivedDoc.rev,
|
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
$set: {
|
|
|
|
lines: archivedDoc.lines,
|
|
|
|
ranges: archivedDoc.ranges || {},
|
|
|
|
},
|
|
|
|
$unset: {
|
|
|
|
inS3: true,
|
|
|
|
},
|
|
|
|
}
|
2024-01-18 08:00:51 -05:00
|
|
|
const result = await db.docs.updateOne(query, update)
|
|
|
|
|
|
|
|
if (result.matchedCount === 0) {
|
|
|
|
throw new Errors.DocRevValueError('failed to unarchive doc', {
|
|
|
|
docId,
|
|
|
|
rev: archivedDoc.rev,
|
|
|
|
})
|
|
|
|
}
|
2022-04-26 07:16:36 -04:00
|
|
|
}
|
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function getDocRev(docId) {
|
|
|
|
const doc = await db.docs.findOne(
|
|
|
|
{ _id: new ObjectId(docId.toString()) },
|
|
|
|
{ projection: { rev: 1 } }
|
2022-04-19 08:28:47 -04:00
|
|
|
)
|
2024-01-18 08:00:51 -05:00
|
|
|
return doc && doc.rev
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2021-07-30 11:06:16 -04:00
|
|
|
|
2023-11-08 09:17:19 -05:00
|
|
|
/**
|
|
|
|
* Helper method to support optimistic locking.
|
|
|
|
*
|
|
|
|
* Check that the rev of an existing doc is unchanged. If the rev has
|
|
|
|
* changed, return a DocModifiedError.
|
|
|
|
*/
|
2024-01-18 08:00:51 -05:00
|
|
|
async function checkRevUnchanged(doc) {
|
|
|
|
const currentRev = await getDocRev(doc._id)
|
|
|
|
if (isNaN(currentRev) || isNaN(doc.rev)) {
|
|
|
|
throw new Errors.DocRevValueError('doc rev is NaN', {
|
|
|
|
doc_id: doc._id,
|
|
|
|
rev: doc.rev,
|
|
|
|
currentRev,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (doc.rev !== currentRev) {
|
|
|
|
throw new Errors.DocModifiedError('doc rev has changed', {
|
|
|
|
doc_id: doc._id,
|
|
|
|
rev: doc.rev,
|
|
|
|
currentRev,
|
|
|
|
})
|
|
|
|
}
|
2022-04-19 08:28:47 -04:00
|
|
|
}
|
2021-07-30 11:06:16 -04:00
|
|
|
|
2024-01-18 08:00:51 -05:00
|
|
|
async function destroyProject(projectId) {
|
|
|
|
await db.docs.deleteMany({ project_id: new ObjectId(projectId) })
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-07-31 11:20:07 -04:00
|
|
|
|
2022-04-19 08:28:47 -04:00
|
|
|
module.exports = {
|
2024-01-18 08:00:51 -05:00
|
|
|
findDoc: callbackify(findDoc),
|
|
|
|
getProjectsDeletedDocs: callbackify(getProjectsDeletedDocs),
|
|
|
|
getProjectsDocs: callbackify(getProjectsDocs),
|
|
|
|
getArchivedProjectDocs: callbackify(getArchivedProjectDocs),
|
|
|
|
getNonArchivedProjectDocIds: callbackify(getNonArchivedProjectDocIds),
|
|
|
|
getNonDeletedArchivedProjectDocs: callbackify(
|
|
|
|
getNonDeletedArchivedProjectDocs
|
|
|
|
),
|
|
|
|
upsertIntoDocCollection: callbackify(upsertIntoDocCollection),
|
|
|
|
restoreArchivedDoc: callbackify(restoreArchivedDoc),
|
|
|
|
patchDoc: callbackify(patchDoc),
|
|
|
|
getDocForArchiving: callbackify(getDocForArchiving),
|
|
|
|
markDocAsArchived: callbackify(markDocAsArchived),
|
|
|
|
checkRevUnchanged: callbackify(checkRevUnchanged),
|
|
|
|
destroyProject: callbackify(destroyProject),
|
|
|
|
promises: {
|
|
|
|
findDoc,
|
|
|
|
getProjectsDeletedDocs,
|
|
|
|
getProjectsDocs,
|
|
|
|
getArchivedProjectDocs,
|
|
|
|
getNonArchivedProjectDocIds,
|
|
|
|
getNonDeletedArchivedProjectDocs,
|
|
|
|
upsertIntoDocCollection,
|
|
|
|
restoreArchivedDoc,
|
|
|
|
patchDoc,
|
|
|
|
getDocForArchiving,
|
|
|
|
markDocAsArchived,
|
|
|
|
checkRevUnchanged,
|
|
|
|
destroyProject,
|
|
|
|
},
|
2020-07-31 11:20:07 -04:00
|
|
|
}
|