Move functions to prepare for async/await

GitOrigin-RevId: b0f43d14a214d1f7695b3b312d63827d2c82470c
This commit is contained in:
andrew rumble 2024-10-02 14:31:52 +01:00 committed by Copybot
parent 50723d0f8f
commit 696bfa3756

View file

@ -46,6 +46,109 @@ const { plainTextResponse } = require('../../infrastructure/Response')
const ReferencesHandler = require('../References/ReferencesHandler') const ReferencesHandler = require('../References/ReferencesHandler')
const EditorRealTimeController = require('../Editor/EditorRealTimeController') 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 = { module.exports = LinkedFilesController = {
Agents: _.extend( Agents: _.extend(
{ {
@ -71,108 +174,9 @@ module.exports = LinkedFilesController = {
return LinkedFilesController.Agents[provider] return LinkedFilesController.Agents[provider]
}, },
createLinkedFile(req, res, next) { createLinkedFile,
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) refreshLinkedFile,
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 })
}
}
)
}
)
},
handleError(error, req, res, next) { handleError(error, req, res, next) {
if (error instanceof AccessDeniedError) { if (error instanceof AccessDeniedError) {