2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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
|
|
|
|
*/
|
2024-01-30 10:35:54 -05:00
|
|
|
const { promisifyAll } = require('@overleaf/promise-utils')
|
2020-08-25 07:32:16 -04:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
const SnapshotManager = {
|
2023-03-21 08:06:13 -04:00
|
|
|
recordSnapshot(projectId, docId, version, pathname, lines, ranges, callback) {
|
2020-05-06 06:08:21 -04:00
|
|
|
try {
|
2023-12-14 09:28:19 -05:00
|
|
|
projectId = new ObjectId(projectId)
|
|
|
|
docId = new ObjectId(docId)
|
2020-05-06 06:08:21 -04:00
|
|
|
} catch (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return callback(error)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-08-25 07:32:16 -04:00
|
|
|
db.docSnapshots.insertOne(
|
2020-05-06 06:09:33 -04:00
|
|
|
{
|
2023-03-21 08:06:13 -04:00
|
|
|
project_id: projectId,
|
|
|
|
doc_id: docId,
|
2020-05-06 06:09:33 -04:00
|
|
|
version,
|
|
|
|
lines,
|
|
|
|
pathname,
|
|
|
|
ranges: SnapshotManager.jsonRangesToMongo(ranges),
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
// Suggested indexes:
|
|
|
|
// db.docSnapshots.createIndex({project_id:1, doc_id:1})
|
|
|
|
// db.docSnapshots.createIndex({ts:1},{expiresAfterSeconds: 30*24*3600)) # expires after 30 days
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
jsonRangesToMongo(ranges) {
|
2020-05-06 06:09:33 -04:00
|
|
|
if (ranges == null) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateMetadata = function (metadata) {
|
2020-05-06 06:08:21 -04:00
|
|
|
if ((metadata != null ? metadata.ts : undefined) != null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
metadata.ts = new Date(metadata.ts)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
|
|
|
if ((metadata != null ? metadata.user_id : undefined) != null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return (metadata.user_id = SnapshotManager._safeObjectId(
|
|
|
|
metadata.user_id
|
|
|
|
))
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const change of Array.from(ranges.changes || [])) {
|
2020-05-06 06:09:33 -04:00
|
|
|
change.id = SnapshotManager._safeObjectId(change.id)
|
|
|
|
updateMetadata(change.metadata)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:15 -04:00
|
|
|
for (const comment of Array.from(ranges.comments || [])) {
|
2020-05-06 06:09:33 -04:00
|
|
|
comment.id = SnapshotManager._safeObjectId(comment.id)
|
2020-05-06 06:08:21 -04:00
|
|
|
if ((comment.op != null ? comment.op.t : undefined) != null) {
|
2020-05-06 06:09:33 -04:00
|
|
|
comment.op.t = SnapshotManager._safeObjectId(comment.op.t)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
updateMetadata(comment.metadata)
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2020-05-06 06:09:33 -04:00
|
|
|
return ranges
|
2020-05-06 06:08:21 -04:00
|
|
|
},
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
_safeObjectId(data) {
|
|
|
|
try {
|
2023-12-14 09:28:19 -05:00
|
|
|
return new ObjectId(data)
|
2020-05-06 06:08:21 -04:00
|
|
|
} catch (error) {
|
2020-05-06 06:09:33 -04:00
|
|
|
return data
|
2020-05-06 06:08:21 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-01-30 10:35:54 -05:00
|
|
|
|
|
|
|
module.exports = SnapshotManager
|
|
|
|
module.exports.promises = promisifyAll(SnapshotManager, {
|
|
|
|
without: ['jsonRangesToMongo', '_safeObjectId'],
|
|
|
|
})
|