2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const request = require('request').defaults({ jar: false })
|
2020-08-11 05:35:08 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const Errors = require('../Errors/Errors')
|
2020-03-02 07:31:50 -05:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-25 08:48:39 -04:00
|
|
|
const TIMEOUT = 30 * 1000 // request timeout
|
|
|
|
|
2020-03-02 07:31:50 -05:00
|
|
|
const DocstoreManager = {
|
2019-05-29 05:21:06 -04:00
|
|
|
deleteDoc(project_id, doc_id, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error) {}
|
|
|
|
}
|
|
|
|
const url = `${
|
|
|
|
settings.apis.docstore.url
|
|
|
|
}/project/${project_id}/doc/${doc_id}`
|
2019-09-25 08:48:39 -04:00
|
|
|
return request.del({ url: url, timeout: TIMEOUT }, function(
|
|
|
|
error,
|
|
|
|
res,
|
|
|
|
body
|
|
|
|
) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
return callback(null)
|
|
|
|
} else if (res.statusCode === 404) {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new Errors.NotFoundError({
|
|
|
|
message: 'tried to delete doc not in docstore',
|
|
|
|
info: {
|
|
|
|
project_id,
|
|
|
|
doc_id
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error) // maybe suppress the error when delete doc which is not present?
|
|
|
|
} else {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new OError(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`,
|
|
|
|
{
|
|
|
|
project_id,
|
|
|
|
doc_id
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllDocs(project_id, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error) {}
|
|
|
|
}
|
|
|
|
const url = `${settings.apis.docstore.url}/project/${project_id}/doc`
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url,
|
2019-09-25 08:48:39 -04:00
|
|
|
timeout: TIMEOUT,
|
2019-05-29 05:21:06 -04:00
|
|
|
json: true
|
|
|
|
},
|
|
|
|
function(error, res, docs) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
return callback(null, docs)
|
|
|
|
} else {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new OError(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`,
|
|
|
|
{ project_id }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllRanges(project_id, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error) {}
|
|
|
|
}
|
|
|
|
const url = `${settings.apis.docstore.url}/project/${project_id}/ranges`
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url,
|
2019-09-25 08:48:39 -04:00
|
|
|
timeout: TIMEOUT,
|
2019-05-29 05:21:06 -04:00
|
|
|
json: true
|
|
|
|
},
|
|
|
|
function(error, res, docs) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
return callback(null, docs)
|
|
|
|
} else {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new OError(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`,
|
|
|
|
{ project_id }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDoc(project_id, doc_id, options, callback) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, lines, rev, version) {}
|
|
|
|
}
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
let url = `${
|
|
|
|
settings.apis.docstore.url
|
|
|
|
}/project/${project_id}/doc/${doc_id}`
|
|
|
|
if (options.include_deleted) {
|
|
|
|
url += '?include_deleted=true'
|
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url,
|
2019-09-25 08:48:39 -04:00
|
|
|
timeout: TIMEOUT,
|
2019-05-29 05:21:06 -04:00
|
|
|
json: true
|
|
|
|
},
|
|
|
|
function(error, res, doc) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
logger.log(
|
|
|
|
{ doc_id, project_id, version: doc.version, rev: doc.rev },
|
|
|
|
'got doc from docstore api'
|
|
|
|
)
|
|
|
|
return callback(null, doc.lines, doc.rev, doc.version, doc.ranges)
|
|
|
|
} else if (res.statusCode === 404) {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new Errors.NotFoundError({
|
|
|
|
message: 'doc not found in docstore',
|
|
|
|
info: {
|
|
|
|
project_id,
|
|
|
|
doc_id
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
} else {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new OError(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`,
|
|
|
|
{
|
|
|
|
project_id,
|
|
|
|
doc_id
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
updateDoc(project_id, doc_id, lines, version, ranges, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, modified, rev) {}
|
|
|
|
}
|
|
|
|
const url = `${
|
|
|
|
settings.apis.docstore.url
|
|
|
|
}/project/${project_id}/doc/${doc_id}`
|
|
|
|
return request.post(
|
|
|
|
{
|
|
|
|
url,
|
2019-09-25 08:48:39 -04:00
|
|
|
timeout: TIMEOUT,
|
2019-05-29 05:21:06 -04:00
|
|
|
json: {
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(error, res, result) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
logger.log(
|
|
|
|
{ project_id, doc_id },
|
|
|
|
'update doc in docstore url finished'
|
|
|
|
)
|
|
|
|
return callback(null, result.modified, result.rev)
|
|
|
|
} else {
|
2020-08-11 05:35:08 -04:00
|
|
|
error = new OError(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`,
|
|
|
|
{ project_id, doc_id }
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
archiveProject(project_id, callback) {
|
2019-07-18 10:18:56 -04:00
|
|
|
DocstoreManager._operateOnProject(project_id, 'archive', callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
unarchiveProject(project_id, callback) {
|
2019-07-18 10:18:56 -04:00
|
|
|
DocstoreManager._operateOnProject(project_id, 'unarchive', callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
destroyProject(project_id, callback) {
|
|
|
|
DocstoreManager._operateOnProject(project_id, 'destroy', callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
_operateOnProject(project_id, method, callback) {
|
|
|
|
const url = `${settings.apis.docstore.url}/project/${project_id}/${method}`
|
|
|
|
logger.log({ project_id }, `calling ${method} for project in docstore`)
|
2019-09-25 08:48:39 -04:00
|
|
|
// use default timeout for archiving/unarchiving/destroying
|
2019-07-18 10:18:56 -04:00
|
|
|
request.post(url, function(err, res, docs) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2020-08-11 05:35:08 -04:00
|
|
|
OError.tag(err, `error calling ${method} project in docstore`, {
|
|
|
|
project_id
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2019-07-18 10:18:56 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
2019-07-18 10:18:56 -04:00
|
|
|
callback()
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
const error = new Error(
|
|
|
|
`docstore api responded with non-success code: ${res.statusCode}`
|
|
|
|
)
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err: error, project_id },
|
2019-07-18 10:18:56 -04:00
|
|
|
`error calling ${method} project in docstore`
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2019-07-18 10:18:56 -04:00
|
|
|
callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-02 07:31:50 -05:00
|
|
|
|
|
|
|
module.exports = DocstoreManager
|
|
|
|
module.exports.promises = promisifyAll(DocstoreManager, {
|
|
|
|
multiResult: {
|
|
|
|
getDoc: ['lines', 'rev', 'version', 'ranges'],
|
|
|
|
updateDoc: ['modified', 'rev']
|
|
|
|
}
|
|
|
|
})
|