2020-02-17 12:34:21 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// 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 WebApiManager
|
|
|
|
const request = require('requestretry') // allow retry on error https://github.com/FGRibreau/node-request-retry
|
|
|
|
const logger = require('logger-sharelatex')
|
2021-07-12 12:47:16 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2014-03-06 13:04:00 -05:00
|
|
|
|
2020-02-17 12:34:04 -05:00
|
|
|
// Don't let HTTP calls hang for a long time
|
2020-02-17 12:34:28 -05:00
|
|
|
const MAX_HTTP_REQUEST_LENGTH = 15000 // 15 seconds
|
2016-04-13 11:30:20 -04:00
|
|
|
|
2020-02-17 12:34:04 -05:00
|
|
|
// DEPRECATED! This method of getting user details via track-changes is deprecated
|
|
|
|
// in the way we lay out our services.
|
|
|
|
// Instead, web should be responsible for collecting the raw data (user_ids) and
|
|
|
|
// filling it out with calls to other services. All API calls should create a
|
|
|
|
// tree-like structure as much as possible, with web as the root.
|
2020-02-17 12:34:28 -05:00
|
|
|
module.exports = WebApiManager = {
|
|
|
|
sendRequest(url, callback) {
|
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error, body) {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
return request.get(
|
|
|
|
{
|
|
|
|
url: `${Settings.apis.web.url}${url}`,
|
|
|
|
timeout: MAX_HTTP_REQUEST_LENGTH,
|
|
|
|
maxAttempts: 2, // for node-request-retry
|
|
|
|
auth: {
|
|
|
|
user: Settings.apis.web.user,
|
|
|
|
pass: Settings.apis.web.pass,
|
2021-07-13 07:04:43 -04:00
|
|
|
sendImmediately: 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 === 404) {
|
|
|
|
logger.log({ url }, 'got 404 from web api')
|
|
|
|
return callback(null, null)
|
|
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
return callback(null, body)
|
|
|
|
} else {
|
|
|
|
error = new Error(
|
|
|
|
`web returned a non-success status code: ${res.statusCode} (attempts: ${res.attempts})`
|
|
|
|
)
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2014-03-28 09:05:16 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
getUserInfo(user_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error, userInfo) {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const url = `/user/${user_id}/personal_info`
|
|
|
|
logger.log({ user_id }, 'getting user info from web')
|
2020-06-04 04:24:21 -04:00
|
|
|
return WebApiManager.sendRequest(url, function (error, body) {
|
2020-02-17 12:34:28 -05:00
|
|
|
let user
|
|
|
|
if (error != null) {
|
|
|
|
logger.error({ err: error, user_id, url }, 'error accessing web')
|
|
|
|
return callback(error)
|
|
|
|
}
|
2014-03-28 09:05:16 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
if (body === null) {
|
|
|
|
logger.error({ user_id, url }, 'no user found')
|
|
|
|
return callback(null, null)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
user = JSON.parse(body)
|
|
|
|
} catch (error1) {
|
|
|
|
error = error1
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, {
|
|
|
|
id: user.id,
|
|
|
|
email: user.email,
|
|
|
|
first_name: user.first_name,
|
2021-07-13 07:04:43 -04:00
|
|
|
last_name: user.last_name,
|
2020-02-17 12:34:28 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2014-03-28 09:05:16 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
getProjectDetails(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2020-06-04 04:24:21 -04:00
|
|
|
callback = function (error, details) {}
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|
|
|
|
const url = `/project/${project_id}/details`
|
|
|
|
logger.log({ project_id }, 'getting project details from web')
|
2020-06-04 04:24:21 -04:00
|
|
|
return WebApiManager.sendRequest(url, function (error, body) {
|
2020-02-17 12:34:28 -05:00
|
|
|
let project
|
|
|
|
if (error != null) {
|
|
|
|
logger.error({ err: error, project_id, url }, 'error accessing web')
|
|
|
|
return callback(error)
|
|
|
|
}
|
2014-03-28 09:05:16 -04:00
|
|
|
|
2020-02-17 12:34:28 -05:00
|
|
|
try {
|
|
|
|
project = JSON.parse(body)
|
|
|
|
} catch (error1) {
|
|
|
|
error = error1
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, project)
|
|
|
|
})
|
2021-07-13 07:04:43 -04:00
|
|
|
},
|
2020-02-17 12:34:28 -05:00
|
|
|
}
|