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 = {
|
2023-03-20 14:10:40 +00:00
|
|
|
joinProject(projectId, user, callback) {
|
|
|
|
const userId = user._id
|
|
|
|
logger.debug({ projectId, userId }, 'sending join project request to web')
|
|
|
|
const url = `${settings.apis.web.url}/project/${projectId}/join`
|
2020-06-23 17:29:44 +00:00
|
|
|
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,
|
2023-03-20 14:10:40 +00:00
|
|
|
qs: { user_id: userId },
|
2020-06-23 17:29:44 +00:00
|
|
|
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
|
|
|
}
|
2023-05-30 12:21:55 +00:00
|
|
|
const userMetadata = {
|
|
|
|
isRestrictedUser: data.isRestrictedUser,
|
|
|
|
isTokenMember: data.isTokenMember,
|
|
|
|
isInvitedMember: data.isInvitedMember,
|
|
|
|
}
|
|
|
|
callback(null, data.project, data.privilegeLevel, userMetadata)
|
2020-06-23 17:29:44 +00:00
|
|
|
} 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
|
|
|
}
|