From 696bfa3756eec80b8522e4d3a93ccbe8194cbd6b Mon Sep 17 00:00:00 2001 From: andrew rumble Date: Wed, 2 Oct 2024 14:31:52 +0100 Subject: [PATCH] Move functions to prepare for async/await GitOrigin-RevId: b0f43d14a214d1f7695b3b312d63827d2c82470c --- .../LinkedFiles/LinkedFilesController.js | 206 +++++++++--------- 1 file changed, 105 insertions(+), 101 deletions(-) diff --git a/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js b/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js index a20cbd9e68..8f8b785a37 100644 --- a/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js +++ b/services/web/app/src/Features/LinkedFiles/LinkedFilesController.js @@ -46,6 +46,109 @@ const { plainTextResponse } = require('../../infrastructure/Response') const ReferencesHandler = require('../References/ReferencesHandler') const EditorRealTimeController = require('../Editor/EditorRealTimeController') +function createLinkedFile(req, res, next) { + const { project_id: projectId } = req.params + const { name, provider, data, parent_folder_id: parentFolderId } = req.body + const userId = SessionManager.getLoggedInUserId(req.session) + + const Agent = LinkedFilesController._getAgent(provider) + if (Agent == null) { + return res.sendStatus(400) + } + + data.provider = provider + data.importedAt = new Date().toISOString() + + return Agent.createLinkedFile( + projectId, + data, + name, + parentFolderId, + userId, + function (err, newFileId) { + if (err != null) { + return LinkedFilesController.handleError(err, req, res, next) + } + if (name.endsWith('.bib')) { + AnalyticsManager.recordEventForUserInBackground( + userId, + 'linked-bib-file', + { + integration: provider, + } + ) + } + return res.json({ new_file_id: newFileId }) + } + ) +} + +function refreshLinkedFile(req, res, next) { + const { project_id: projectId, file_id: fileId } = req.params + const userId = SessionManager.getLoggedInUserId(req.session) + + return LinkedFilesHandler.getFileById( + projectId, + fileId, + function (err, file, path, parentFolder) { + if (err != null) { + return next(err) + } + if (file == null) { + return res.sendStatus(404) + } + const { name } = file + const { linkedFileData } = file + if ( + linkedFileData == null || + (linkedFileData != null ? linkedFileData.provider : undefined) == null + ) { + return res.sendStatus(409) + } + const { provider } = linkedFileData + const parentFolderId = parentFolder._id + const Agent = LinkedFilesController._getAgent(provider) + if (Agent == null) { + return res.sendStatus(400) + } + + linkedFileData.importedAt = new Date().toISOString() + + Agent.refreshLinkedFile( + projectId, + linkedFileData, + name, + parentFolderId, + userId, + function (err, newFileId) { + if (err != null) { + return LinkedFilesController.handleError(err, req, res, next) + } + if (req.body.shouldReindexReferences) { + ReferencesHandler.indexAll(projectId, function (error, data) { + if (error) { + OError.tag(error, 'failed to index references', { + projectId, + }) + return next(error) + } + EditorRealTimeController.emitToRoom( + projectId, + 'references:keys:updated', + data.keys, + true + ) + res.json({ new_file_id: newFileId }) + }) + } else { + res.json({ new_file_id: newFileId }) + } + } + ) + } + ) +} + module.exports = LinkedFilesController = { Agents: _.extend( { @@ -71,108 +174,9 @@ module.exports = LinkedFilesController = { return LinkedFilesController.Agents[provider] }, - createLinkedFile(req, res, next) { - const { project_id: projectId } = req.params - const { name, provider, data, parent_folder_id: parentFolderId } = req.body - const userId = SessionManager.getLoggedInUserId(req.session) + createLinkedFile, - const Agent = LinkedFilesController._getAgent(provider) - if (Agent == null) { - return res.sendStatus(400) - } - - data.provider = provider - data.importedAt = new Date().toISOString() - - return Agent.createLinkedFile( - projectId, - data, - name, - parentFolderId, - userId, - function (err, newFileId) { - if (err != null) { - return LinkedFilesController.handleError(err, req, res, next) - } - if (name.endsWith('.bib')) { - AnalyticsManager.recordEventForUserInBackground( - userId, - 'linked-bib-file', - { - integration: provider, - } - ) - } - return res.json({ new_file_id: newFileId }) - } - ) - }, - - refreshLinkedFile(req, res, next) { - const { project_id: projectId, file_id: fileId } = req.params - const userId = SessionManager.getLoggedInUserId(req.session) - - return LinkedFilesHandler.getFileById( - projectId, - fileId, - function (err, file, path, parentFolder) { - if (err != null) { - return next(err) - } - if (file == null) { - return res.sendStatus(404) - } - const { name } = file - const { linkedFileData } = file - if ( - linkedFileData == null || - (linkedFileData != null ? linkedFileData.provider : undefined) == null - ) { - return res.sendStatus(409) - } - const { provider } = linkedFileData - const parentFolderId = parentFolder._id - const Agent = LinkedFilesController._getAgent(provider) - if (Agent == null) { - return res.sendStatus(400) - } - - linkedFileData.importedAt = new Date().toISOString() - - Agent.refreshLinkedFile( - projectId, - linkedFileData, - name, - parentFolderId, - userId, - function (err, newFileId) { - if (err != null) { - return LinkedFilesController.handleError(err, req, res, next) - } - if (req.body.shouldReindexReferences) { - ReferencesHandler.indexAll(projectId, function (error, data) { - if (error) { - OError.tag(error, 'failed to index references', { - projectId, - }) - return next(error) - } - EditorRealTimeController.emitToRoom( - projectId, - 'references:keys:updated', - data.keys, - true - ) - res.json({ new_file_id: newFileId }) - }) - } else { - res.json({ new_file_id: newFileId }) - } - } - ) - } - ) - }, + refreshLinkedFile, handleError(error, req, res, next) { if (error instanceof AccessDeniedError) {