2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const request = require('request')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const { CodedError } = require('./Errors')
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
module.exports = {
|
2020-06-23 13:29:44 -04:00
|
|
|
joinProject(project_id, user, callback) {
|
|
|
|
const user_id = user._id
|
|
|
|
logger.log({ project_id, user_id }, 'sending join project request to web')
|
|
|
|
const url = `${settings.apis.web.url}/project/${project_id}/join`
|
|
|
|
const headers = {}
|
2020-07-07 06:06:02 -04:00
|
|
|
if (user.anonymousAccessToken) {
|
2020-06-23 13:29:44 -04:00
|
|
|
headers['x-sl-anonymous-access-token'] = user.anonymousAccessToken
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
request.post(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
|
|
|
url,
|
|
|
|
qs: { user_id },
|
|
|
|
auth: {
|
|
|
|
user: settings.apis.web.user,
|
|
|
|
pass: settings.apis.web.pass,
|
|
|
|
sendImmediately: true
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
jar: false,
|
|
|
|
headers
|
|
|
|
},
|
|
|
|
function (error, response, data) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
let err
|
2020-06-23 13:29:44 -04:00
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!(data && data.project)) {
|
2020-06-23 13:29:44 -04:00
|
|
|
err = new Error('no data returned from joinProject request')
|
|
|
|
logger.error(
|
|
|
|
{ err, project_id, user_id },
|
|
|
|
'error accessing web api'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(
|
2020-06-23 13:29:44 -04:00
|
|
|
null,
|
|
|
|
data.project,
|
|
|
|
data.privilegeLevel,
|
|
|
|
data.isRestrictedUser
|
|
|
|
)
|
|
|
|
} else if (response.statusCode === 429) {
|
|
|
|
logger.log(project_id, user_id, 'rate-limit hit when joining project')
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(
|
2020-06-23 13:29:44 -04:00
|
|
|
new CodedError(
|
|
|
|
'rate-limit hit when joining project',
|
|
|
|
'TooManyRequests'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err = new Error(
|
|
|
|
`non-success status code from web: ${response.statusCode}`
|
|
|
|
)
|
|
|
|
logger.error({ err, project_id, user_id }, 'error accessing web api')
|
2020-07-07 06:06:02 -04:00
|
|
|
callback(err)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|