2020-05-06 06:09:33 -04:00
|
|
|
const DocumentManager = require('./DocumentManager')
|
|
|
|
const HistoryManager = require('./HistoryManager')
|
|
|
|
const ProjectManager = require('./ProjectManager')
|
|
|
|
const Errors = require('./Errors')
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-05-11 11:15:37 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
2020-05-06 06:09:33 -04:00
|
|
|
const Metrics = require('./Metrics')
|
|
|
|
const ProjectFlusher = require('./ProjectFlusher')
|
|
|
|
const DeleteQueueManager = require('./DeleteQueueManager')
|
|
|
|
const async = require('async')
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
module.exports = {
|
|
|
|
getDoc,
|
|
|
|
getProjectDocsAndFlushIfOld,
|
|
|
|
clearProjectState,
|
|
|
|
setDoc,
|
|
|
|
flushDocIfLoaded,
|
|
|
|
deleteDoc,
|
|
|
|
flushProject,
|
|
|
|
deleteProject,
|
|
|
|
deleteMultipleProjects,
|
|
|
|
acceptChanges,
|
|
|
|
deleteComment,
|
|
|
|
updateProject,
|
|
|
|
resyncProjectHistory,
|
|
|
|
flushAllProjects,
|
|
|
|
flushQueuedProjects
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function getDoc(req, res, next) {
|
|
|
|
let fromVersion
|
|
|
|
const docId = req.params.doc_id
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
logger.log({ projectId, docId }, 'getting doc via http')
|
|
|
|
const timer = new Metrics.Timer('http.getDoc')
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
if (req.query.fromVersion != null) {
|
|
|
|
fromVersion = parseInt(req.query.fromVersion, 10)
|
|
|
|
} else {
|
|
|
|
fromVersion = -1
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
DocumentManager.getDocAndRecentOpsWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
fromVersion,
|
2020-05-11 11:14:37 -04:00
|
|
|
(error, lines, version, ops, ranges, pathname) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log({ projectId, docId }, 'got doc via http')
|
|
|
|
if (lines == null || version == null) {
|
|
|
|
return next(new Errors.NotFoundError('document not found'))
|
|
|
|
}
|
|
|
|
res.json({
|
|
|
|
id: docId,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ops,
|
|
|
|
ranges,
|
|
|
|
pathname
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function _getTotalSizeOfLines(lines) {
|
|
|
|
let size = 0
|
|
|
|
for (const line of lines) {
|
|
|
|
size += line.length + 1
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function getProjectDocsAndFlushIfOld(req, res, next) {
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const projectStateHash = req.query.state
|
|
|
|
// exclude is string of existing docs "id:version,id:version,..."
|
|
|
|
const excludeItems =
|
|
|
|
req.query.exclude != null ? req.query.exclude.split(',') : []
|
|
|
|
logger.log({ projectId, exclude: excludeItems }, 'getting docs via http')
|
|
|
|
const timer = new Metrics.Timer('http.getAllDocs')
|
|
|
|
const excludeVersions = {}
|
|
|
|
for (const item of excludeItems) {
|
|
|
|
const [id, version] = item.split(':')
|
|
|
|
excludeVersions[id] = version
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ projectId, projectStateHash, excludeVersions },
|
|
|
|
'excluding versions'
|
|
|
|
)
|
|
|
|
ProjectManager.getProjectDocsAndFlushIfOld(
|
|
|
|
projectId,
|
|
|
|
projectStateHash,
|
|
|
|
excludeVersions,
|
2020-05-11 11:14:37 -04:00
|
|
|
(error, result) => {
|
2020-05-06 06:09:33 -04:00
|
|
|
timer.done()
|
2020-05-11 11:12:15 -04:00
|
|
|
if (error instanceof Errors.ProjectStateChangedError) {
|
|
|
|
res.sendStatus(409) // conflict
|
|
|
|
} else if (error) {
|
2020-05-11 10:45:39 -04:00
|
|
|
next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
} else {
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log(
|
|
|
|
{
|
|
|
|
projectId,
|
|
|
|
result: result.map((doc) => `${doc._id}:${doc.v}`)
|
|
|
|
},
|
|
|
|
'got docs via http'
|
|
|
|
)
|
|
|
|
res.send(result)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function clearProjectState(req, res, next) {
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const timer = new Metrics.Timer('http.clearProjectState')
|
|
|
|
logger.log({ projectId }, 'clearing project state via http')
|
2020-05-11 11:14:37 -04:00
|
|
|
ProjectManager.clearProjectState(projectId, (error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
next(error)
|
|
|
|
} else {
|
|
|
|
res.sendStatus(200)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDoc(req, res, next) {
|
|
|
|
const docId = req.params.doc_id
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const { lines, source, user_id: userId, undoing } = req.body
|
|
|
|
const lineSize = _getTotalSizeOfLines(lines)
|
2020-05-11 11:15:37 -04:00
|
|
|
if (lineSize > Settings.max_doc_length) {
|
2020-05-06 06:09:33 -04:00
|
|
|
logger.log(
|
2020-05-11 11:12:15 -04:00
|
|
|
{ projectId, docId, source, lineSize, userId },
|
|
|
|
'document too large, returning 406 response'
|
2020-05-06 06:09:33 -04:00
|
|
|
)
|
2020-05-11 11:12:15 -04:00
|
|
|
return res.sendStatus(406)
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ projectId, docId, lines, source, userId, undoing },
|
|
|
|
'setting doc via http'
|
|
|
|
)
|
|
|
|
const timer = new Metrics.Timer('http.setDoc')
|
|
|
|
DocumentManager.setDocWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
lines,
|
|
|
|
source,
|
|
|
|
userId,
|
|
|
|
undoing,
|
2020-05-11 11:14:37 -04:00
|
|
|
(error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId, docId }, 'set doc via http')
|
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function flushDocIfLoaded(req, res, next) {
|
|
|
|
const docId = req.params.doc_id
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
logger.log({ projectId, docId }, 'flushing doc via http')
|
|
|
|
const timer = new Metrics.Timer('http.flushDoc')
|
2020-05-11 11:14:37 -04:00
|
|
|
DocumentManager.flushDocIfLoadedWithLock(projectId, docId, (error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log({ projectId, docId }, 'flushed doc via http')
|
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteDoc(req, res, next) {
|
|
|
|
const docId = req.params.doc_id
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const ignoreFlushErrors = req.query.ignore_flush_errors === 'true'
|
|
|
|
const timer = new Metrics.Timer('http.deleteDoc')
|
|
|
|
logger.log({ projectId, docId }, 'deleting doc via http')
|
|
|
|
DocumentManager.flushAndDeleteDocWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
{ ignoreFlushErrors },
|
2020-05-11 11:14:37 -04:00
|
|
|
(error) => {
|
2020-05-11 10:45:39 -04:00
|
|
|
timer.done()
|
2020-05-11 11:12:15 -04:00
|
|
|
// There is no harm in flushing project history if the previous call
|
|
|
|
// failed and sometimes it is required
|
|
|
|
HistoryManager.flushProjectChangesAsync(projectId)
|
|
|
|
|
2020-05-11 10:52:06 -04:00
|
|
|
if (error) {
|
2020-05-11 10:45:39 -04:00
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId, docId }, 'deleted doc via http')
|
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function flushProject(req, res, next) {
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
logger.log({ projectId }, 'flushing project via http')
|
|
|
|
const timer = new Metrics.Timer('http.flushProject')
|
2020-05-11 11:14:37 -04:00
|
|
|
ProjectManager.flushProjectWithLocks(projectId, (error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log({ projectId }, 'flushed project via http')
|
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
})
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function deleteProject(req, res, next) {
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
logger.log({ projectId }, 'deleting project via http')
|
|
|
|
const options = {}
|
|
|
|
if (req.query.background) {
|
|
|
|
options.background = true
|
|
|
|
} // allow non-urgent flushes to be queued
|
|
|
|
if (req.query.shutdown) {
|
|
|
|
options.skip_history_flush = true
|
|
|
|
} // don't flush history when realtime shuts down
|
|
|
|
if (req.query.background) {
|
2020-05-11 11:14:37 -04:00
|
|
|
ProjectManager.queueFlushAndDeleteProject(projectId, (error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId }, 'queue delete of project via http')
|
|
|
|
res.sendStatus(204)
|
|
|
|
}) // No Content
|
|
|
|
} else {
|
|
|
|
const timer = new Metrics.Timer('http.deleteProject')
|
2020-05-11 11:14:37 -04:00
|
|
|
ProjectManager.flushAndDeleteProjectWithLocks(
|
|
|
|
projectId,
|
|
|
|
options,
|
|
|
|
(error) => {
|
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log({ projectId }, 'deleted project via http')
|
|
|
|
res.sendStatus(204) // No Content
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:14:37 -04:00
|
|
|
)
|
2020-05-11 11:12:15 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function deleteMultipleProjects(req, res, next) {
|
|
|
|
const projectIds = req.body.project_ids || []
|
|
|
|
logger.log({ projectIds }, 'deleting multiple projects via http')
|
|
|
|
async.eachSeries(
|
|
|
|
projectIds,
|
2020-05-11 11:14:37 -04:00
|
|
|
(projectId, cb) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId }, 'queue delete of project via http')
|
|
|
|
ProjectManager.queueFlushAndDeleteProject(projectId, cb)
|
|
|
|
},
|
2020-05-11 11:14:37 -04:00
|
|
|
(error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function acceptChanges(req, res, next) {
|
|
|
|
const { project_id: projectId, doc_id: docId } = req.params
|
|
|
|
let changeIds = req.body.change_ids
|
|
|
|
if (changeIds == null) {
|
|
|
|
changeIds = [req.params.change_id]
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ projectId, docId },
|
|
|
|
`accepting ${changeIds.length} changes via http`
|
|
|
|
)
|
|
|
|
const timer = new Metrics.Timer('http.acceptChanges')
|
2020-05-11 11:14:37 -04:00
|
|
|
DocumentManager.acceptChangesWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
changeIds,
|
|
|
|
(error) => {
|
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ projectId, docId },
|
|
|
|
`accepted ${changeIds.length} changes via http`
|
|
|
|
)
|
|
|
|
res.sendStatus(204) // No Content
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:14:37 -04:00
|
|
|
)
|
2020-05-11 11:12:15 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function deleteComment(req, res, next) {
|
|
|
|
const {
|
|
|
|
project_id: projectId,
|
|
|
|
doc_id: docId,
|
|
|
|
comment_id: commentId
|
|
|
|
} = req.params
|
|
|
|
logger.log({ projectId, docId, commentId }, 'deleting comment via http')
|
|
|
|
const timer = new Metrics.Timer('http.deleteComment')
|
2020-05-11 11:14:37 -04:00
|
|
|
DocumentManager.deleteCommentWithLock(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
commentId,
|
|
|
|
(error) => {
|
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
logger.log({ projectId, docId, commentId }, 'deleted comment via http')
|
|
|
|
res.sendStatus(204) // No Content
|
2020-05-11 11:12:15 -04:00
|
|
|
}
|
2020-05-11 11:14:37 -04:00
|
|
|
)
|
2020-05-11 11:12:15 -04:00
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function updateProject(req, res, next) {
|
|
|
|
const timer = new Metrics.Timer('http.updateProject')
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const {
|
|
|
|
projectHistoryId,
|
|
|
|
userId,
|
|
|
|
docUpdates,
|
|
|
|
fileUpdates,
|
|
|
|
version
|
|
|
|
} = req.body
|
|
|
|
logger.log(
|
|
|
|
{ projectId, docUpdates, fileUpdates, version },
|
|
|
|
'updating project via http'
|
|
|
|
)
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
ProjectManager.updateProjectWithLocks(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
userId,
|
|
|
|
docUpdates,
|
|
|
|
fileUpdates,
|
|
|
|
version,
|
2020-05-11 11:14:37 -04:00
|
|
|
(error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
timer.done()
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId }, 'updated project via http')
|
|
|
|
res.sendStatus(204) // No Content
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function resyncProjectHistory(req, res, next) {
|
|
|
|
const projectId = req.params.project_id
|
|
|
|
const { projectHistoryId, docs, files } = req.body
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log(
|
|
|
|
{ projectId, docs, files },
|
|
|
|
'queuing project history resync via http'
|
|
|
|
)
|
|
|
|
HistoryManager.resyncProjectHistory(
|
|
|
|
projectId,
|
|
|
|
projectHistoryId,
|
|
|
|
docs,
|
|
|
|
files,
|
2020-05-11 11:14:37 -04:00
|
|
|
(error) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
logger.log({ projectId }, 'queued project history resync via http')
|
|
|
|
res.sendStatus(204)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:08:21 -04:00
|
|
|
|
2020-05-11 11:12:15 -04:00
|
|
|
function flushAllProjects(req, res, next) {
|
|
|
|
res.setTimeout(5 * 60 * 1000)
|
|
|
|
const options = {
|
|
|
|
limit: req.query.limit || 1000,
|
|
|
|
concurrency: req.query.concurrency || 5,
|
|
|
|
dryRun: req.query.dryRun || false
|
|
|
|
}
|
2020-05-11 11:14:37 -04:00
|
|
|
ProjectFlusher.flushAllProjects(options, (err, projectIds) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
if (err) {
|
|
|
|
logger.err({ err }, 'error bulk flushing projects')
|
|
|
|
res.sendStatus(500)
|
|
|
|
} else {
|
|
|
|
res.send(projectIds)
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:12:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function flushQueuedProjects(req, res, next) {
|
|
|
|
res.setTimeout(10 * 60 * 1000)
|
|
|
|
const options = {
|
|
|
|
limit: req.query.limit || 1000,
|
|
|
|
timeout: 5 * 60 * 1000,
|
|
|
|
min_delete_age: req.query.min_delete_age || 5 * 60 * 1000
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-05-11 11:14:37 -04:00
|
|
|
DeleteQueueManager.flushAndDeleteOldProjects(options, (err, flushed) => {
|
2020-05-11 11:12:15 -04:00
|
|
|
if (err) {
|
|
|
|
logger.err({ err }, 'error flushing old projects')
|
|
|
|
res.sendStatus(500)
|
|
|
|
} else {
|
|
|
|
logger.log({ flushed }, 'flush of queued projects completed')
|
|
|
|
res.send({ flushed })
|
|
|
|
}
|
|
|
|
})
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|