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')
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-08-20 07:14:02 -04:00
|
|
|
const {
|
|
|
|
CodedError,
|
|
|
|
CorruptedJoinProjectResponseError,
|
2020-08-27 05:55:13 -04:00
|
|
|
NotAuthorizedError,
|
2021-07-13 07:04:45 -04:00
|
|
|
WebApiRequestFailedError,
|
2020-08-20 07:14:02 -04:00
|
|
|
} = require('./Errors')
|
2014-11-10 06:27:08 -05:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
module.exports = {
|
2023-03-20 10:10:40 -04: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 13:29:44 -04:00
|
|
|
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,
|
2023-03-20 10:10:40 -04:00
|
|
|
qs: { user_id: userId },
|
2020-06-23 13:29:44 -04:00
|
|
|
auth: {
|
|
|
|
user: settings.apis.web.user,
|
|
|
|
pass: settings.apis.web.pass,
|
2021-07-13 07:04:45 -04:00
|
|
|
sendImmediately: true,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
jar: false,
|
2021-07-13 07:04:45 -04:00
|
|
|
headers,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
2023-05-30 08:21:55 -04:00
|
|
|
const userMetadata = {
|
|
|
|
isRestrictedUser: data.isRestrictedUser,
|
|
|
|
isTokenMember: data.isTokenMember,
|
|
|
|
isInvitedMember: data.isInvitedMember,
|
|
|
|
}
|
|
|
|
callback(null, data.project, data.privilegeLevel, userMetadata)
|
2020-06-23 13:29:44 -04:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|