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 /* eslint-disable
n/handle-callback-err, n/handle-callback-err,
max-len, max-len,
no-unused-vars,
*/ */
// TODO: This file was created by bulk-decaffeinate. // TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint. // Fix any style issues and re-enable lint.
@ -14,21 +13,18 @@
let ProjectFileAgent let ProjectFileAgent
const AuthorizationManager = require('../Authorization/AuthorizationManager') const AuthorizationManager = require('../Authorization/AuthorizationManager')
const ProjectLocator = require('../Project/ProjectLocator') const ProjectLocator = require('../Project/ProjectLocator')
const ProjectGetter = require('../Project/ProjectGetter')
const DocstoreManager = require('../Docstore/DocstoreManager') const DocstoreManager = require('../Docstore/DocstoreManager')
const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler') const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
const FileStoreHandler = require('../FileStore/FileStoreHandler') const FileStoreHandler = require('../FileStore/FileStoreHandler')
const _ = require('lodash') const _ = require('lodash')
const Settings = require('@overleaf/settings')
const LinkedFilesHandler = require('./LinkedFilesHandler') const LinkedFilesHandler = require('./LinkedFilesHandler')
const { const {
BadDataError, BadDataError,
AccessDeniedError, AccessDeniedError,
BadEntityTypeError, BadEntityTypeError,
SourceFileNotFoundError, SourceFileNotFoundError,
ProjectNotFoundError,
V1ProjectNotFoundError,
} = require('./LinkedFilesErrors') } = require('./LinkedFilesErrors')
const { promisify } = require('@overleaf/promise-utils')
module.exports = ProjectFileAgent = { module.exports = ProjectFileAgent = {
createLinkedFile( createLinkedFile(
@ -39,10 +35,10 @@ module.exports = ProjectFileAgent = {
userId, userId,
callback callback
) { ) {
if (!this._canCreate(linkedFileData)) { if (!ProjectFileAgent._canCreate(linkedFileData)) {
return callback(new AccessDeniedError()) return callback(new AccessDeniedError())
} }
return this._go( return ProjectFileAgent._go(
projectId, projectId,
linkedFileData, linkedFileData,
name, name,
@ -60,7 +56,7 @@ module.exports = ProjectFileAgent = {
userId, userId,
callback callback
) { ) {
return this._go( return ProjectFileAgent._go(
projectId, projectId,
linkedFileData, linkedFileData,
name, name,
@ -74,7 +70,7 @@ module.exports = ProjectFileAgent = {
if (callback == null) { if (callback == null) {
callback = function () {} callback = function () {}
} }
return this._checkAuth( return ProjectFileAgent._checkAuth(
projectId, projectId,
linkedFileData, linkedFileData,
userId, userId,
@ -85,7 +81,7 @@ module.exports = ProjectFileAgent = {
if (!allowed) { if (!allowed) {
return callback(new AccessDeniedError()) return callback(new AccessDeniedError())
} }
if (!this._validate(linkedFileData)) { if (!ProjectFileAgent._validate(linkedFileData)) {
return callback(new BadDataError()) return callback(new BadDataError())
} }
return callback(null, linkedFileData) return callback(null, linkedFileData)
@ -94,8 +90,8 @@ module.exports = ProjectFileAgent = {
}, },
_go(projectId, linkedFileData, name, parentFolderId, userId, callback) { _go(projectId, linkedFileData, name, parentFolderId, userId, callback) {
linkedFileData = this._sanitizeData(linkedFileData) linkedFileData = ProjectFileAgent._sanitizeData(linkedFileData)
return this._prepare( return ProjectFileAgent._prepare(
projectId, projectId,
linkedFileData, linkedFileData,
userId, userId,
@ -103,10 +99,10 @@ module.exports = ProjectFileAgent = {
if (err != null) { if (err != null) {
return callback(err) return callback(err)
} }
if (!this._validate(linkedFileData)) { if (!ProjectFileAgent._validate(linkedFileData)) {
return callback(new BadDataError()) return callback(new BadDataError())
} }
return this._getEntity( return ProjectFileAgent._getEntity(
linkedFileData, linkedFileData,
userId, userId,
(err, sourceProject, entity, type) => { (err, sourceProject, entity, type) => {
@ -177,7 +173,9 @@ module.exports = ProjectFileAgent = {
} }
callback = _.once(callback) callback = _.once(callback)
const { source_entity_path: sourceEntityPath } = linkedFileData const { source_entity_path: sourceEntityPath } = linkedFileData
return this._getSourceProject(linkedFileData, function (err, project) { return ProjectFileAgent._getSourceProject(
linkedFileData,
function (err, project) {
if (err != null) { if (err != null) {
return callback(err) return callback(err)
} }
@ -206,7 +204,8 @@ module.exports = ProjectFileAgent = {
) )
} }
) )
}) }
)
}, },
_sanitizeData(data) { _sanitizeData(data) {
@ -242,7 +241,7 @@ module.exports = ProjectFileAgent = {
if (!ProjectFileAgent._validate(data)) { if (!ProjectFileAgent._validate(data)) {
return callback(new BadDataError()) return callback(new BadDataError())
} }
return this._getSourceProject(data, function (err, project) { return ProjectFileAgent._getSourceProject(data, function (err, project) {
if (err != null) { if (err != null) {
return callback(err) 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') } = require('./LinkedFilesErrors')
const { OutputFileFetchFailedError } = require('../Errors/Errors') const { OutputFileFetchFailedError } = require('../Errors/Errors')
const LinkedFilesHandler = require('./LinkedFilesHandler') const LinkedFilesHandler = require('./LinkedFilesHandler')
const { promisify } = require('@overleaf/promise-utils')
function _prepare(projectId, linkedFileData, userId, callback) { function _prepare(projectId, linkedFileData, userId, callback) {
_checkAuth(projectId, linkedFileData, userId, (err, allowed) => { _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 _ = require('lodash')
const Async = require('async') const Async = require('async')
const Errors = require('../Errors/Errors') const Errors = require('../Errors/Errors')
const { promisify } = require('@overleaf/promise-utils')
if (!Features.hasFeature('references')) { if (!Features.hasFeature('references')) {
logger.debug('references search not enabled') logger.debug('references search not enabled')
@ -196,6 +197,10 @@ module.exports = ReferencesHandler = {
}, },
} }
ReferencesHandler.promises = {
indexAll: promisify(ReferencesHandler.indexAll),
}
function __guard__(value, transform) { function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null return typeof value !== 'undefined' && value !== null
? transform(value) ? transform(value)

View file

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