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
|
|
|
|
*/
|
|
|
|
let DocstoreManager
|
|
|
|
const request = require('request').defaults({ jar: false })
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
|
2019-09-25 08:48:39 -04:00
|
|
|
const TIMEOUT = 30 * 1000 // request timeout
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
module.exports = DocstoreManager = {
|
|
|
|
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) {
|
|
|
|
error = new Errors.NotFoundError('tried to delete doc not in docstore')
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err: error, project_id, doc_id },
|
|
|
|
'tried to delete doc not in docstore'
|
|
|
|
)
|
|
|
|
return callback(error) // maybe suppress the error when delete doc which is not present?
|
|
|
|
} else {
|
|
|
|
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, doc_id },
|
|
|
|
'error deleting doc in docstore'
|
|
|
|
)
|
|
|
|
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 {
|
|
|
|
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 },
|
|
|
|
'error getting all docs from docstore'
|
|
|
|
)
|
|
|
|
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 {
|
|
|
|
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 },
|
|
|
|
'error getting all doc ranges from docstore'
|
|
|
|
)
|
|
|
|
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) {
|
|
|
|
error = new Errors.NotFoundError('doc not found in docstore')
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err: error, project_id, doc_id },
|
|
|
|
'doc not found in docstore'
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
} else {
|
|
|
|
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, doc_id },
|
|
|
|
'error getting doc from docstore'
|
|
|
|
)
|
|
|
|
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 {
|
|
|
|
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, doc_id },
|
|
|
|
'error updating doc in docstore'
|
|
|
|
)
|
|
|
|
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) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
|
|
|
{ err, project_id },
|
2019-07-18 10:18:56 -04:00
|
|
|
`error calling ${method} project in docstore`
|
2019-07-01 09:48:09 -04:00
|
|
|
)
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|