2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const request = require('request')
|
2020-08-20 09:05:50 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2020-06-23 13:29:44 -04:00
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-08-20 07:14:02 -04:00
|
|
|
const {
|
|
|
|
CodedError,
|
|
|
|
CorruptedJoinProjectResponseError,
|
2020-08-27 05:55:13 -04:00
|
|
|
NotAuthorizedError,
|
2020-08-20 07:14:02 -04:00
|
|
|
WebApiRequestFailedError
|
|
|
|
} = 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-08-20 09:05:50 -04:00
|
|
|
OError.tag(error, 'join project request failed')
|
2020-06-23 13:29:44 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2020-07-07 06:06:02 -04:00
|
|
|
if (!(data && data.project)) {
|
2020-08-20 07:14:02 -04:00
|
|
|
return callback(new CorruptedJoinProjectResponseError())
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
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) {
|
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'
|
|
|
|
)
|
|
|
|
)
|
2020-08-27 05:55:13 -04:00
|
|
|
} else if (response.statusCode === 403) {
|
|
|
|
callback(new NotAuthorizedError())
|
2020-08-27 06:47:50 -04:00
|
|
|
} else if (response.statusCode === 404) {
|
|
|
|
callback(new CodedError('project not found', 'ProjectNotFound'))
|
2020-06-23 13:29:44 -04:00
|
|
|
} else {
|
2020-08-20 07:03:54 -04:00
|
|
|
callback(new WebApiRequestFailedError(response.statusCode))
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|