mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-14 20:40:17 -05:00
Promisify LinkedFilesController
GitOrigin-RevId: b81ed1615939671839cbeabc36308fe8b81647cf
This commit is contained in:
parent
63ea296453
commit
8a31146d02
1 changed files with 84 additions and 90 deletions
|
@ -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,94 +56,91 @@ function createLinkedFile(req, res, next) {
|
|||
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,
|
||||
try {
|
||||
const newFileId = await Agent.promises.createLinkedFile(
|
||||
projectId,
|
||||
data,
|
||||
name,
|
||||
parentFolderId,
|
||||
userId
|
||||
)
|
||||
if (name.endsWith('.bib')) {
|
||||
AnalyticsManager.recordEventForUserInBackground(
|
||||
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 })
|
||||
}
|
||||
'linked-bib-file',
|
||||
{
|
||||
integration: provider,
|
||||
}
|
||||
)
|
||||
}
|
||||
return res.json({ new_file_id: newFileId })
|
||||
} catch (err) {
|
||||
return LinkedFilesController.handleError(err, req, res, next)
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshLinkedFile(req, res, next) {
|
||||
const { project_id: projectId, file_id: fileId } = req.params
|
||||
const userId = SessionManager.getLoggedInUserId(req.session)
|
||||
|
||||
const { file, parentFolder } = await LinkedFilesHandler.promises.getFileById(
|
||||
projectId,
|
||||
fileId
|
||||
)
|
||||
|
||||
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()
|
||||
let newFileId
|
||||
try {
|
||||
newFileId = await Agent.promises.refreshLinkedFile(
|
||||
projectId,
|
||||
linkedFileData,
|
||||
name,
|
||||
parentFolderId,
|
||||
userId
|
||||
)
|
||||
} catch (err) {
|
||||
return LinkedFilesController.handleError(err, req, res, next)
|
||||
}
|
||||
|
||||
if (req.body.shouldReindexReferences) {
|
||||
let data
|
||||
try {
|
||||
data = await ReferencesHandler.promises.indexAll(projectId)
|
||||
} catch (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 = {
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue