2020-02-16 09:01:47 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-16 09:01:46 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-16 09:02:21 -05:00
|
|
|
let MongoManager
|
2020-08-28 08:13:19 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2020-02-16 09:02:21 -05:00
|
|
|
const logger = require('logger-sharelatex')
|
2020-09-10 13:07:14 -04:00
|
|
|
const metrics = require('@overleaf/metrics')
|
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')
|
2020-07-31 11:20:07 -04:00
|
|
|
const { promisify } = require('util')
|
2014-04-28 11:45:59 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
module.exports = MongoManager = {
|
|
|
|
findDoc(project_id, doc_id, filter, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error, doc) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docs.findOne(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
project_id: ObjectId(project_id.toString()),
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
2020-09-07 05:16:12 -04:00
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
projection: filter,
|
2020-09-07 05:16:12 -04:00
|
|
|
},
|
2020-08-28 08:13:19 -04:00
|
|
|
callback
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
|
|
|
},
|
2014-04-28 12:43:19 -04:00
|
|
|
|
2021-02-18 05:10:14 -05:00
|
|
|
getProjectsDeletedDocs(project_id, filter, callback) {
|
|
|
|
db.docs
|
|
|
|
.find(
|
|
|
|
{
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
deleted: true,
|
2021-02-18 05:10:14 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
projection: filter,
|
|
|
|
sort: { deletedAt: -1 },
|
2021-07-13 07:04:48 -04:00
|
|
|
limit: Settings.max_deleted_docs,
|
2021-02-18 05:10:14 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.toArray(callback)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
getProjectsDocs(project_id, options, filter, callback) {
|
|
|
|
const query = { project_id: ObjectId(project_id.toString()) }
|
|
|
|
if (!options.include_deleted) {
|
|
|
|
query.deleted = { $ne: true }
|
|
|
|
}
|
2021-04-16 04:27:09 -04:00
|
|
|
const queryOptions = {
|
2021-07-13 07:04:48 -04:00
|
|
|
projection: filter,
|
2021-04-16 04:27:09 -04:00
|
|
|
}
|
|
|
|
if (options.limit) {
|
|
|
|
queryOptions.limit = options.limit
|
|
|
|
}
|
|
|
|
db.docs.find(query, queryOptions).toArray(callback)
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
2014-06-05 08:29:50 -04:00
|
|
|
|
2021-06-08 14:29:10 -04:00
|
|
|
getArchivedProjectDocs(project_id, maxResults, callback) {
|
2020-02-16 09:02:21 -05:00
|
|
|
const query = {
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2021-06-08 14:29:10 -04:00
|
|
|
db.docs
|
|
|
|
.find(query, { projection: { _id: 1 }, limit: maxResults })
|
|
|
|
.toArray(callback)
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
2014-04-29 10:07:22 -04:00
|
|
|
|
2021-06-08 14:29:10 -04:00
|
|
|
getNonArchivedProjectDocs(project_id, maxResults, callback) {
|
2021-06-08 07:59:13 -04:00
|
|
|
const query = {
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: { $ne: true },
|
2021-06-08 07:59:13 -04:00
|
|
|
}
|
2021-06-08 14:29:10 -04:00
|
|
|
db.docs.find(query, { limit: maxResults }).toArray(callback)
|
2021-06-08 07:59:13 -04:00
|
|
|
},
|
|
|
|
|
2021-06-08 14:29:10 -04:00
|
|
|
getNonDeletedArchivedProjectDocs(project_id, maxResults, callback) {
|
2021-01-04 12:04:33 -05:00
|
|
|
const query = {
|
|
|
|
project_id: ObjectId(project_id.toString()),
|
|
|
|
deleted: { $ne: true },
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
2021-01-04 12:04:33 -05:00
|
|
|
}
|
2021-06-08 14:29:10 -04:00
|
|
|
db.docs
|
|
|
|
.find(query, { projection: { _id: 1 }, limit: maxResults })
|
|
|
|
.toArray(callback)
|
2021-01-04 12:04:33 -05:00
|
|
|
},
|
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
upsertIntoDocCollection(project_id, doc_id, updates, callback) {
|
|
|
|
const update = {
|
|
|
|
$set: updates,
|
|
|
|
$inc: {
|
2021-07-13 07:04:48 -04:00
|
|
|
rev: 1,
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
|
|
|
$unset: {
|
2021-07-13 07:04:48 -04:00
|
|
|
inS3: true,
|
|
|
|
},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
update.$set.project_id = ObjectId(project_id)
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docs.updateOne(
|
|
|
|
{ _id: ObjectId(doc_id) },
|
|
|
|
update,
|
|
|
|
{ upsert: true },
|
|
|
|
callback
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
|
|
|
},
|
2015-06-02 14:55:22 -04:00
|
|
|
|
2021-02-15 08:13:48 -05:00
|
|
|
patchDoc(project_id, doc_id, meta, callback) {
|
|
|
|
db.docs.updateOne(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id),
|
2021-07-13 07:04:48 -04:00
|
|
|
project_id: ObjectId(project_id),
|
2021-02-15 08:13:48 -05:00
|
|
|
},
|
|
|
|
{ $set: meta },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
markDocAsArchived(doc_id, rev, callback) {
|
|
|
|
const update = {
|
|
|
|
$set: {},
|
2021-07-13 07:04:48 -04:00
|
|
|
$unset: {},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
|
|
|
update.$set.inS3 = true
|
|
|
|
update.$unset.lines = true
|
|
|
|
update.$unset.ranges = true
|
|
|
|
const query = {
|
|
|
|
_id: doc_id,
|
2021-07-13 07:04:48 -04:00
|
|
|
rev,
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docs.updateOne(query, update, callback)
|
2020-02-16 09:02:21 -05:00
|
|
|
},
|
2015-06-01 18:36:26 -04:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
getDocVersion(doc_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error, version) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docOps.findOne(
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
doc_id: ObjectId(doc_id),
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
{
|
2020-09-07 05:16:12 -04:00
|
|
|
projection: {
|
2021-07-13 07:04:48 -04:00
|
|
|
version: 1,
|
|
|
|
},
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
function (error, doc) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
callback(null, (doc && doc.version) || 0)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
|
|
|
},
|
2016-11-28 09:55:16 -05:00
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
setDocVersion(doc_id, version, callback) {
|
|
|
|
if (callback == null) {
|
2020-05-28 09:20:54 -04:00
|
|
|
callback = function (error) {}
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docOps.updateOne(
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
doc_id: ObjectId(doc_id),
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
$set: { version },
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
upsert: true,
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
callback
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
|
|
|
},
|
2017-03-16 12:46:58 -04:00
|
|
|
|
2021-07-30 11:06:16 -04:00
|
|
|
getDocRev(doc_id, callback) {
|
|
|
|
db.docs.findOne(
|
|
|
|
{
|
|
|
|
_id: ObjectId(doc_id.toString()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
projection: { rev: 1 },
|
|
|
|
},
|
|
|
|
function (err, doc) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, doc && doc.rev)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
// Helper method to support optimistic locking. Call the provided method for
|
|
|
|
// an existing doc and return the result if the rev in mongo is unchanged when
|
|
|
|
// checked afterwards. If the rev has changed, return a DocModifiedError.
|
2021-08-02 06:02:12 -04:00
|
|
|
withRevCheck(doc, method, callback) {
|
2021-07-30 11:06:16 -04:00
|
|
|
method(doc._id, function (err, result) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
MongoManager.getDocRev(doc._id, function (err, currentRev) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
if (doc.rev !== currentRev) {
|
|
|
|
return callback(
|
2021-08-02 06:36:43 -04:00
|
|
|
new Errors.DocModifiedError('doc rev has changed', {
|
2021-07-30 11:06:16 -04:00
|
|
|
doc_id: doc._id,
|
|
|
|
rev: doc.rev,
|
|
|
|
currentRev,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return callback(null, result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-02-16 09:02:21 -05:00
|
|
|
destroyDoc(doc_id, callback) {
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docs.deleteOne(
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
_id: ObjectId(doc_id),
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-08-28 08:13:19 -04:00
|
|
|
db.docOps.deleteOne(
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
doc_id: ObjectId(doc_id),
|
2020-08-28 08:13:19 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
2020-02-16 09:02:21 -05:00
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-16 09:02:21 -05:00
|
|
|
}
|
2020-07-31 11:20:07 -04:00
|
|
|
|
|
|
|
const methods = Object.getOwnPropertyNames(MongoManager)
|
|
|
|
|
|
|
|
module.exports.promises = {}
|
|
|
|
for (const method of methods) {
|
2020-02-16 09:02:21 -05:00
|
|
|
metrics.timeAsyncMethod(MongoManager, method, 'mongo.MongoManager', logger)
|
2020-07-31 11:20:07 -04:00
|
|
|
module.exports.promises[method] = promisify(module.exports[method])
|
|
|
|
}
|