Merge pull request #9563 from overleaf/em-tpds-merge-metadata
Return metadata from TPDS update endpoint in web GitOrigin-RevId: 9154be67f7f975807c6e986a5d6fb66013c9a384
This commit is contained in:
@@ -43,6 +43,7 @@ module.exports = {
|
||||
'project',
|
||||
'path',
|
||||
'newProject',
|
||||
'newFileRef',
|
||||
]),
|
||||
replaceDocWithFile: callbackify(replaceDocWithFile),
|
||||
replaceFileWithDoc: callbackify(replaceFileWithDoc),
|
||||
@@ -188,7 +189,9 @@ async function replaceFileWithNew(projectId, fileId, newFileRef) {
|
||||
path,
|
||||
})
|
||||
}
|
||||
return { oldFileRef: fileRef, project, path, newProject }
|
||||
// Refresh newFileRef with the version returned from the database
|
||||
newFileRef = ProjectLocator.findElementByMongoPath(newProject, path.mongo)
|
||||
return { oldFileRef: fileRef, project, path, newProject, newFileRef }
|
||||
}
|
||||
|
||||
async function replaceDocWithFile(projectId, docId, fileRef) {
|
||||
|
||||
@@ -343,6 +343,7 @@ const ProjectEntityUpdateHandler = {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
}
|
||||
doc.rev = rev
|
||||
next(
|
||||
projectId,
|
||||
folderId,
|
||||
@@ -576,7 +577,7 @@ const ProjectEntityUpdateHandler = {
|
||||
projectId,
|
||||
fileId,
|
||||
newFileRef,
|
||||
(err, oldFileRef, project, path, newProject) => {
|
||||
(err, oldFileRef, project, path, newProject, newFileRef) => {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
}
|
||||
@@ -597,18 +598,12 @@ const ProjectEntityUpdateHandler = {
|
||||
project.overleaf &&
|
||||
project.overleaf.history &&
|
||||
project.overleaf.history.id
|
||||
// Increment the rev for an in-place update (with the same path) so the third-party-datastore
|
||||
// knows this is a new file.
|
||||
// Ideally we would get this from ProjectEntityMongoUpdateHandler.replaceFileWithNew
|
||||
// but it returns the original oldFileRef (after incrementing the rev value in mongo),
|
||||
// so we add 1 to the rev from that. This isn't atomic and relies on the lock
|
||||
// but it is acceptable for now.
|
||||
TpdsUpdateSender.addFile(
|
||||
{
|
||||
projectId: project._id,
|
||||
fileId: newFileRef._id,
|
||||
path: path.fileSystem,
|
||||
rev: oldFileRef.rev + 1,
|
||||
rev: newFileRef.rev,
|
||||
projectName: project.name,
|
||||
folderId,
|
||||
},
|
||||
@@ -624,7 +619,12 @@ const ProjectEntityUpdateHandler = {
|
||||
userId,
|
||||
{ oldFiles, newFiles, newProject },
|
||||
source,
|
||||
callback
|
||||
err => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
callback(null, newFileRef)
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -670,6 +670,7 @@ const ProjectEntityUpdateHandler = {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
}
|
||||
doc.rev = rev
|
||||
ProjectEntityMongoUpdateHandler.replaceFileWithDoc(
|
||||
projectId,
|
||||
existingFile._id,
|
||||
@@ -936,7 +937,7 @@ const ProjectEntityUpdateHandler = {
|
||||
fileStoreUrl,
|
||||
folderId,
|
||||
source,
|
||||
err => {
|
||||
(err, newFileRef) => {
|
||||
if (err != null) {
|
||||
return callback(err)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const _ = require('underscore')
|
||||
const logger = require('@overleaf/logger')
|
||||
const OError = require('@overleaf/o-error')
|
||||
const async = require('async')
|
||||
const ProjectGetter = require('./ProjectGetter')
|
||||
const Errors = require('../Errors/Errors')
|
||||
@@ -282,10 +283,31 @@ function getIndexOf(searchEntity, id) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Follow the given Mongo path (as returned by findElement) and return the
|
||||
* entity at the end of it.
|
||||
*/
|
||||
function findElementByMongoPath(project, mongoPath) {
|
||||
const components = mongoPath.split('.')
|
||||
let node = project
|
||||
for (const component of components) {
|
||||
const key = Array.isArray(node) ? parseInt(component, 10) : component
|
||||
node = node[key]
|
||||
if (node == null) {
|
||||
throw new OError('entity not found', {
|
||||
projectId: project._id,
|
||||
mongoPath,
|
||||
})
|
||||
}
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findElement,
|
||||
findElementByPath,
|
||||
findRootDoc,
|
||||
findElementByMongoPath,
|
||||
promises: {
|
||||
findElement: promisifyMultiResult(findElement, [
|
||||
'element',
|
||||
|
||||
Reference in New Issue
Block a user