2020-02-17 12:34:21 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:04 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:34:28 -05:00
|
|
|
let DocumentUpdaterManager
|
|
|
|
const request = require('request')
|
|
|
|
const logger = require('logger-sharelatex')
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2014-03-04 08:02:48 -05:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
module.exports = DocumentUpdaterManager = {
|
2021-08-16 08:09:52 -04:00
|
|
|
_requestDocument(project_id, doc_id, url, callback) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error, content, version) {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-08-16 08:09:52 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
logger.log({ project_id, doc_id }, 'getting doc from document updater')
|
2020-06-04 04:24:21 -04:00
|
|
|
return request.get(url, function (error, res, body) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
try {
|
|
|
|
body = JSON.parse(body)
|
|
|
|
} catch (error1) {
|
|
|
|
error = error1
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
logger.log(
|
|
|
|
{ project_id, doc_id, version: body.version },
|
|
|
|
'got doc from document updater'
|
|
|
|
)
|
|
|
|
return callback(null, body.lines.join('\n'), body.version)
|
|
|
|
} else {
|
|
|
|
error = new Error(
|
|
|
|
`doc updater returned a non-success status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
logger.error(
|
|
|
|
{ err: error, project_id, doc_id, url },
|
|
|
|
'error accessing doc updater'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2014-03-10 12:58:26 -04:00
|
|
|
|
2021-08-16 08:09:52 -04:00
|
|
|
getDocument(project_id, doc_id, callback) {
|
|
|
|
const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
|
|
|
|
DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
peekDocument(project_id, doc_id, callback) {
|
|
|
|
const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}/peek`
|
|
|
|
DocumentUpdaterManager._requestDocument(project_id, doc_id, url, callback)
|
|
|
|
},
|
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
setDocument(project_id, doc_id, content, user_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error) {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const url = `${Settings.apis.documentupdater.url}/project/${project_id}/doc/${doc_id}`
|
|
|
|
logger.log({ project_id, doc_id }, 'setting doc in document updater')
|
|
|
|
return request.post(
|
|
|
|
{
|
|
|
|
url,
|
|
|
|
json: {
|
|
|
|
lines: content.split('\n'),
|
|
|
|
source: 'restore',
|
|
|
|
user_id,
|
2021-07-13 07:04:43 -04:00
|
|
|
undoing: true,
|
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
},
|
2020-06-04 04:24:21 -04:00
|
|
|
function (error, res, body) {
|
2020-02-17 12:34:28 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
return callback(null)
|
|
|
|
} else {
|
|
|
|
error = new Error(
|
|
|
|
`doc updater returned a non-success status code: ${res.statusCode}`
|
|
|
|
)
|
|
|
|
logger.error(
|
|
|
|
{ err: error, project_id, doc_id, url },
|
|
|
|
'error accessing doc updater'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
2021-08-16 08:09:52 -04:00
|
|
|
|
|
|
|
module.exports.promises = {
|
|
|
|
// peekDocument returns two arguments so we can't use util.promisify, which only handles a single argument, we need
|
|
|
|
// to treat this it as a special case.
|
|
|
|
peekDocument: (project_id, doc_id) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
DocumentUpdaterManager.peekDocument(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
(err, content, version) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve([content, version])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|