2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const ProjectGetter = require('../Project/ProjectGetter')
|
2020-08-19 05:11:32 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const ProjectLocator = require('../Project/ProjectLocator')
|
|
|
|
const ProjectEntityHandler = require('../Project/ProjectEntityHandler')
|
|
|
|
const ProjectEntityUpdateHandler = require('../Project/ProjectEntityUpdateHandler')
|
|
|
|
const logger = require('logger-sharelatex')
|
2019-11-21 09:38:06 -05:00
|
|
|
const _ = require('lodash')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDocument(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
next = function (error) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const project_id = req.params.Project_id
|
|
|
|
const { doc_id } = req.params
|
|
|
|
const plain =
|
|
|
|
__guard__(req != null ? req.query : undefined, x => x.plain) === 'true'
|
|
|
|
return ProjectGetter.getProject(
|
|
|
|
project_id,
|
|
|
|
{ rootFolder: true, overleaf: true },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, project) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
return ProjectLocator.findElement(
|
|
|
|
{ project, element_id: doc_id, type: 'doc' },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, doc, path) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(error, 'error finding element for getDocument', {
|
|
|
|
doc_id,
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2020-08-19 05:11:32 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectEntityHandler.getDoc(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
function (error, lines, rev, version, ranges) {
|
|
|
|
if (error != null) {
|
|
|
|
OError.tag(
|
|
|
|
error,
|
|
|
|
'error finding doc contents for getDocument',
|
|
|
|
{
|
|
|
|
doc_id,
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (plain) {
|
|
|
|
res.type('text/plain')
|
|
|
|
return res.send(lines.join('\n'))
|
|
|
|
} else {
|
|
|
|
const projectHistoryId = _.get(project, 'overleaf.history.id')
|
2021-08-18 08:38:23 -04:00
|
|
|
const projectHistoryDisplay = _.get(
|
2021-04-14 09:17:21 -04:00
|
|
|
project,
|
|
|
|
'overleaf.history.display'
|
|
|
|
)
|
2021-08-18 08:38:23 -04:00
|
|
|
const sendToBothHistorySystems = _.get(
|
|
|
|
project,
|
|
|
|
'overleaf.history.allowDowngrade'
|
|
|
|
)
|
|
|
|
// if project has been switched but has 'allowDowngrade' set
|
|
|
|
// then leave projectHistoryType undefined to (temporarily)
|
|
|
|
// continue sending updates to both SL and full project history
|
|
|
|
const projectHistoryType =
|
|
|
|
projectHistoryDisplay && !sendToBothHistorySystems
|
|
|
|
? 'project-history'
|
|
|
|
: undefined // for backwards compatibility, don't send anything if the project is still on track-changes
|
2021-04-14 09:17:21 -04:00
|
|
|
return res.json({
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
pathname: path.fileSystem,
|
|
|
|
projectHistoryId,
|
2021-04-27 03:52:58 -04:00
|
|
|
projectHistoryType,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
setDocument(req, res, next) {
|
|
|
|
if (next == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
next = function (error) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const project_id = req.params.Project_id
|
|
|
|
const { doc_id } = req.params
|
|
|
|
const { lines, version, ranges, lastUpdatedAt, lastUpdatedBy } = req.body
|
|
|
|
return ProjectEntityUpdateHandler.updateDocLines(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(error, 'error finding element for getDocument', {
|
|
|
|
doc_id,
|
2021-04-27 03:52:58 -04:00
|
|
|
project_id,
|
2020-08-19 05:11:32 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ doc_id, project_id },
|
|
|
|
'finished receiving set document request from api (docupdater)'
|
|
|
|
)
|
|
|
|
return res.sendStatus(200)
|
|
|
|
}
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|