2020-06-23 17:29:38 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 17:29:44 +00:00
|
|
|
const request = require('request')
|
2020-08-20 13:05:50 +00:00
|
|
|
const OError = require('@overleaf/o-error')
|
2021-07-12 16:47:18 +00:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-08-20 11:14:02 +00:00
|
|
|
const {
|
|
|
|
CodedError,
|
|
|
|
CorruptedJoinProjectResponseError,
|
2020-08-27 09:55:13 +00:00
|
|
|
NotAuthorizedError,
|
2021-07-13 11:04:45 +00:00
|
|
|
WebApiRequestFailedError,
|
2020-08-20 11:14:02 +00:00
|
|
|
} = require('./Errors')
|
2014-11-10 11:27:08 +00:00
|
|
|
|
2020-07-07 10:06:02 +00:00
|
|
|
module.exports = {
|
2020-06-23 17:29:44 +00:00
|
|
|
joinProject(project_id, user, callback) {
|
|
|
|
const user_id = user._id
|
2021-09-14 08:36:24 +00:00
|
|
|
logger.debug({ project_id, user_id }, 'sending join project request to web')
|
2020-06-23 17:29:44 +00:00
|
|
|
const url = `${settings.apis.web.url}/project/${project_id}/join`
|
|
|
|
const headers = {}
|
2020-07-07 10:06:02 +00:00
|
|
|
if (user.anonymousAccessToken) {
|
2020-06-23 17:29:44 +00:00
|
|
|
headers['x-sl-anonymous-access-token'] = user.anonymousAccessToken
|
|
|
|
}
|
2020-07-07 10:06:02 +00:00
|
|
|
request.post(
|
2020-06-23 17:29:44 +00:00
|
|
|
{
|
|
|
|
url,
|
|
|
|
qs: { user_id },
|
|
|
|
auth: {
|
|
|
|
user: settings.apis.web.user,
|
|
|
|
pass: settings.apis.web.pass,
|
2021-07-13 11:04:45 +00:00
|
|
|
sendImmediately: true,
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
jar: false,
|
2021-07-13 11:04:45 +00:00
|
|
|
headers,
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
|
|
|
function (error, response, data) {
|
2020-07-07 10:06:02 +00:00
|
|
|
if (error) {
|
2020-08-20 13:05:50 +00:00
|
|
|
OError.tag(error, 'join project request failed')
|
2020-06-23 17:29:44 +00:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
2020-07-07 10:06:02 +00:00
|
|
|
if (!(data && data.project)) {
|
2020-08-20 11:14:02 +00:00
|
|
|
return callback(new CorruptedJoinProjectResponseError())
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(
|
2020-06-23 17:29:44 +00:00
|
|
|
null,
|
|
|
|
data.project,
|
|
|
|
data.privilegeLevel,
|
|
|
|
data.isRestrictedUser
|
|
|
|
)
|
|
|
|
} else if (response.statusCode === 429) {
|
2020-07-07 10:06:02 +00:00
|
|
|
callback(
|
2020-06-23 17:29:44 +00:00
|
|
|
new CodedError(
|
|
|
|
'rate-limit hit when joining project',
|
|
|
|
'TooManyRequests'
|
|
|
|
)
|
|
|
|
)
|
2020-08-27 09:55:13 +00:00
|
|
|
} else if (response.statusCode === 403) {
|
|
|
|
callback(new NotAuthorizedError())
|
2020-08-27 10:47:50 +00:00
|
|
|
} else if (response.statusCode === 404) {
|
|
|
|
callback(new CodedError('project not found', 'ProjectNotFound'))
|
2020-06-23 17:29:44 +00:00
|
|
|
} else {
|
2020-08-20 11:03:54 +00:00
|
|
|
callback(new WebApiRequestFailedError(response.statusCode))
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 11:04:45 +00:00
|
|
|
},
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|