Add promises export needed for LinkedFilesController

GitOrigin-RevId: 0a4c4b8faf1c6bb94d288cb8019d3966c283ed70
This commit is contained in:
andrew rumble 2024-10-02 15:21:07 +01:00 committed by Copybot
parent 696bfa3756
commit 63ea296453
4 changed files with 78 additions and 53 deletions

View file

@ -1,7 +1,6 @@
/* eslint-disable
n/handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
@ -14,21 +13,18 @@
let ProjectFileAgent
const AuthorizationManager = require('../Authorization/AuthorizationManager')
const ProjectLocator = require('../Project/ProjectLocator')
const ProjectGetter = require('../Project/ProjectGetter')
const DocstoreManager = require('../Docstore/DocstoreManager')
const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
const FileStoreHandler = require('../FileStore/FileStoreHandler')
const _ = require('lodash')
const Settings = require('@overleaf/settings')
const LinkedFilesHandler = require('./LinkedFilesHandler')
const {
BadDataError,
AccessDeniedError,
BadEntityTypeError,
SourceFileNotFoundError,
ProjectNotFoundError,
V1ProjectNotFoundError,
} = require('./LinkedFilesErrors')
const { promisify } = require('@overleaf/promise-utils')
module.exports = ProjectFileAgent = {
createLinkedFile(
@ -39,10 +35,10 @@ module.exports = ProjectFileAgent = {
userId,
callback
) {
if (!this._canCreate(linkedFileData)) {
if (!ProjectFileAgent._canCreate(linkedFileData)) {
return callback(new AccessDeniedError())
}
return this._go(
return ProjectFileAgent._go(
projectId,
linkedFileData,
name,
@ -60,7 +56,7 @@ module.exports = ProjectFileAgent = {
userId,
callback
) {
return this._go(
return ProjectFileAgent._go(
projectId,
linkedFileData,
name,
@ -74,7 +70,7 @@ module.exports = ProjectFileAgent = {
if (callback == null) {
callback = function () {}
}
return this._checkAuth(
return ProjectFileAgent._checkAuth(
projectId,
linkedFileData,
userId,
@ -85,7 +81,7 @@ module.exports = ProjectFileAgent = {
if (!allowed) {
return callback(new AccessDeniedError())
}
if (!this._validate(linkedFileData)) {
if (!ProjectFileAgent._validate(linkedFileData)) {
return callback(new BadDataError())
}
return callback(null, linkedFileData)
@ -94,8 +90,8 @@ module.exports = ProjectFileAgent = {
},
_go(projectId, linkedFileData, name, parentFolderId, userId, callback) {
linkedFileData = this._sanitizeData(linkedFileData)
return this._prepare(
linkedFileData = ProjectFileAgent._sanitizeData(linkedFileData)
return ProjectFileAgent._prepare(
projectId,
linkedFileData,
userId,
@ -103,10 +99,10 @@ module.exports = ProjectFileAgent = {
if (err != null) {
return callback(err)
}
if (!this._validate(linkedFileData)) {
if (!ProjectFileAgent._validate(linkedFileData)) {
return callback(new BadDataError())
}
return this._getEntity(
return ProjectFileAgent._getEntity(
linkedFileData,
userId,
(err, sourceProject, entity, type) => {
@ -177,36 +173,39 @@ module.exports = ProjectFileAgent = {
}
callback = _.once(callback)
const { source_entity_path: sourceEntityPath } = linkedFileData
return this._getSourceProject(linkedFileData, function (err, project) {
if (err != null) {
return callback(err)
}
const sourceProjectId = project._id
return DocumentUpdaterHandler.flushProjectToMongo(
sourceProjectId,
function (err) {
if (err != null) {
return callback(err)
}
return ProjectLocator.findElementByPath(
{
project_id: sourceProjectId,
path: sourceEntityPath,
exactCaseMatch: true,
},
function (err, entity, type) {
if (err != null) {
if (/^not found.*/.test(err.message)) {
err = new SourceFileNotFoundError()
}
return callback(err)
}
return callback(null, project, entity, type)
}
)
return ProjectFileAgent._getSourceProject(
linkedFileData,
function (err, project) {
if (err != null) {
return callback(err)
}
)
})
const sourceProjectId = project._id
return DocumentUpdaterHandler.flushProjectToMongo(
sourceProjectId,
function (err) {
if (err != null) {
return callback(err)
}
return ProjectLocator.findElementByPath(
{
project_id: sourceProjectId,
path: sourceEntityPath,
exactCaseMatch: true,
},
function (err, entity, type) {
if (err != null) {
if (/^not found.*/.test(err.message)) {
err = new SourceFileNotFoundError()
}
return callback(err)
}
return callback(null, project, entity, type)
}
)
}
)
}
)
},
_sanitizeData(data) {
@ -242,7 +241,7 @@ module.exports = ProjectFileAgent = {
if (!ProjectFileAgent._validate(data)) {
return callback(new BadDataError())
}
return this._getSourceProject(data, function (err, project) {
return ProjectFileAgent._getSourceProject(data, function (err, project) {
if (err != null) {
return callback(err)
}
@ -260,3 +259,8 @@ module.exports = ProjectFileAgent = {
})
},
}
ProjectFileAgent.promises = {
createLinkedFile: promisify(ProjectFileAgent.createLinkedFile),
refreshLinkedFile: promisify(ProjectFileAgent.refreshLinkedFile),
}

View file

@ -10,6 +10,7 @@ const {
} = require('./LinkedFilesErrors')
const { OutputFileFetchFailedError } = require('../Errors/Errors')
const LinkedFilesHandler = require('./LinkedFilesHandler')
const { promisify } = require('@overleaf/promise-utils')
function _prepare(projectId, linkedFileData, userId, callback) {
_checkAuth(projectId, linkedFileData, userId, (err, allowed) => {
@ -225,4 +226,11 @@ function _compileAndGetFileStream(linkedFileData, userId, callback) {
})
}
module.exports = { createLinkedFile, refreshLinkedFile }
module.exports = {
createLinkedFile,
refreshLinkedFile,
promises: {
createLinkedFile: promisify(createLinkedFile),
refreshLinkedFile: promisify(refreshLinkedFile),
},
}

View file

@ -24,6 +24,7 @@ const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandle
const _ = require('lodash')
const Async = require('async')
const Errors = require('../Errors/Errors')
const { promisify } = require('@overleaf/promise-utils')
if (!Features.hasFeature('references')) {
logger.debug('references search not enabled')
@ -196,6 +197,10 @@ module.exports = ReferencesHandler = {
},
}
ReferencesHandler.promises = {
indexAll: promisify(ReferencesHandler.indexAll),
}
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)

View file

@ -17,15 +17,19 @@ describe('LinkedFilesController', function () {
beforeEach(function () {
this.userId = 'user-id'
this.Agent = {
createLinkedFile: sinon.stub().yields(),
refreshLinkedFile: sinon.stub().yields(),
promises: {
createLinkedFile: sinon.stub().resolves(),
refreshLinkedFile: sinon.stub().resolves(),
},
}
this.projectId = 'projectId'
this.provider = 'provider'
this.name = 'linked-file-name'
this.data = { customAgentData: 'foo' }
this.LinkedFilesHandler = {
getFileById: sinon.stub(),
promises: {
getFileById: sinon.stub(),
},
}
this.AnalyticsManager = {}
this.SessionManager = {
@ -77,7 +81,7 @@ describe('LinkedFilesController', function () {
this.next = sinon.stub().callsFake(() => done('unexpected error'))
this.res = {
json: () => {
expect(this.Agent.createLinkedFile).to.have.been.calledWith(
expect(this.Agent.promises.createLinkedFile).to.have.been.calledWith(
this.projectId,
{ ...this.data, importedAt: this.fakeTime.toISOString() },
this.name,
@ -100,10 +104,14 @@ describe('LinkedFilesController', function () {
importedAt: new Date(2020, 1, 1).toISOString(),
},
}
this.LinkedFilesHandler.getFileById
this.LinkedFilesHandler.promises.getFileById
.withArgs(this.projectId, 'file-id')
.yields(null, this.file, 'fake-path', {
_id: 'parent-folder-id',
.resolves({
file: this.file,
path: 'fake-path',
parentFolder: {
_id: 'parent-folder-id',
},
})
this.req = {
params: { project_id: this.projectId, file_id: 'file-id' },
@ -116,7 +124,7 @@ describe('LinkedFilesController', function () {
this.next = sinon.stub().callsFake(() => done('unexpected error'))
this.res = {
json: () => {
expect(this.Agent.refreshLinkedFile).to.have.been.calledWith(
expect(this.Agent.promises.refreshLinkedFile).to.have.been.calledWith(
this.projectId,
{
...this.data,