Promisify LinkedFilesController

GitOrigin-RevId: b81ed1615939671839cbeabc36308fe8b81647cf
This commit is contained in:
andrew rumble 2024-10-02 15:21:52 +01:00 committed by Copybot
parent 63ea296453
commit 8a31146d02

View file

@ -1,6 +1,5 @@
/* eslint-disable
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
@ -13,10 +12,7 @@
*/
let LinkedFilesController
const SessionManager = require('../Authentication/SessionManager')
const EditorController = require('../Editor/EditorController')
const ProjectLocator = require('../Project/ProjectLocator')
const Settings = require('@overleaf/settings')
const logger = require('@overleaf/logger')
const _ = require('lodash')
const AnalyticsManager = require('../../../../app/src/Features/Analytics/AnalyticsManager')
const LinkedFilesHandler = require('./LinkedFilesHandler')
@ -45,8 +41,9 @@ const Modules = require('../../infrastructure/Modules')
const { plainTextResponse } = require('../../infrastructure/Response')
const ReferencesHandler = require('../References/ReferencesHandler')
const EditorRealTimeController = require('../Editor/EditorRealTimeController')
const { expressify } = require('@overleaf/promise-utils')
function createLinkedFile(req, res, next) {
async 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)
@ -59,16 +56,14 @@ function createLinkedFile(req, res, next) {
data.provider = provider
data.importedAt = new Date().toISOString()
return Agent.createLinkedFile(
try {
const newFileId = await Agent.promises.createLinkedFile(
projectId,
data,
name,
parentFolderId,
userId,
function (err, newFileId) {
if (err != null) {
return LinkedFilesController.handleError(err, req, res, next)
}
userId
)
if (name.endsWith('.bib')) {
AnalyticsManager.recordEventForUserInBackground(
userId,
@ -79,21 +74,20 @@ function createLinkedFile(req, res, next) {
)
}
return res.json({ new_file_id: newFileId })
} catch (err) {
return LinkedFilesController.handleError(err, req, res, next)
}
)
}
function refreshLinkedFile(req, res, next) {
async function refreshLinkedFile(req, res, next) {
const { project_id: projectId, file_id: fileId } = req.params
const userId = SessionManager.getLoggedInUserId(req.session)
return LinkedFilesHandler.getFileById(
const { file, parentFolder } = await LinkedFilesHandler.promises.getFileById(
projectId,
fileId,
function (err, file, path, parentFolder) {
if (err != null) {
return next(err)
}
fileId
)
if (file == null) {
return res.sendStatus(404)
}
@ -105,6 +99,7 @@ function refreshLinkedFile(req, res, next) {
) {
return res.sendStatus(409)
}
const { provider } = linkedFileData
const parentFolderId = parentFolder._id
const Agent = LinkedFilesController._getAgent(provider)
@ -113,20 +108,24 @@ function refreshLinkedFile(req, res, next) {
}
linkedFileData.importedAt = new Date().toISOString()
Agent.refreshLinkedFile(
let newFileId
try {
newFileId = await Agent.promises.refreshLinkedFile(
projectId,
linkedFileData,
name,
parentFolderId,
userId,
function (err, newFileId) {
if (err != null) {
userId
)
} catch (err) {
return LinkedFilesController.handleError(err, req, res, next)
}
if (req.body.shouldReindexReferences) {
ReferencesHandler.indexAll(projectId, function (error, data) {
if (error) {
let data
try {
data = await ReferencesHandler.promises.indexAll(projectId)
} catch (error) {
OError.tag(error, 'failed to index references', {
projectId,
})
@ -139,14 +138,9 @@ function refreshLinkedFile(req, res, next) {
true
)
res.json({ new_file_id: newFileId })
})
} else {
res.json({ new_file_id: newFileId })
}
}
)
}
)
}
module.exports = LinkedFilesController = {
@ -174,9 +168,9 @@ module.exports = LinkedFilesController = {
return LinkedFilesController.Agents[provider]
},
createLinkedFile,
createLinkedFile: expressify(createLinkedFile),
refreshLinkedFile,
refreshLinkedFile: expressify(refreshLinkedFile),
handleError(error, req, res, next) {
if (error instanceof AccessDeniedError) {