2014-11-10 06:27:08 -05:00
|
|
|
request = require "request"
|
|
|
|
settings = require "settings-sharelatex"
|
|
|
|
logger = require "logger-sharelatex"
|
2019-08-31 09:04:36 -04:00
|
|
|
{ CodedError } = require "./Errors"
|
2014-11-10 06:27:08 -05:00
|
|
|
|
|
|
|
module.exports = WebApiManager =
|
2019-10-30 09:52:36 -04:00
|
|
|
joinProject: (project_id, user, callback = (error, project, privilegeLevel, isRestrictedUser) ->) ->
|
2017-09-26 09:21:41 -04:00
|
|
|
user_id = user._id
|
2014-11-10 06:27:08 -05:00
|
|
|
logger.log {project_id, user_id}, "sending join project request to web"
|
|
|
|
url = "#{settings.apis.web.url}/project/#{project_id}/join"
|
2017-09-26 09:21:41 -04:00
|
|
|
headers = {}
|
2017-10-20 05:10:58 -04:00
|
|
|
if user.anonymousAccessToken?
|
2017-10-26 11:00:06 -04:00
|
|
|
headers['x-sl-anonymous-access-token'] = user.anonymousAccessToken
|
2014-11-10 06:27:08 -05:00
|
|
|
request.post {
|
|
|
|
url: url
|
|
|
|
qs: {user_id}
|
2014-11-17 09:35:07 -05:00
|
|
|
auth:
|
|
|
|
user: settings.apis.web.user
|
|
|
|
pass: settings.apis.web.pass
|
|
|
|
sendImmediately: true
|
2014-11-10 06:27:08 -05:00
|
|
|
json: true
|
|
|
|
jar: false
|
2017-09-26 09:21:41 -04:00
|
|
|
headers: headers
|
2014-11-10 06:27:08 -05:00
|
|
|
}, (error, response, data) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if 200 <= response.statusCode < 300
|
2019-10-30 09:52:36 -04:00
|
|
|
if !data? || !data?.project?
|
|
|
|
err = new Error('no data returned from joinProject request')
|
|
|
|
logger.error {err, project_id, user_id}, "error accessing web api"
|
|
|
|
return callback(err)
|
|
|
|
callback null, data.project, data.privilegeLevel, data.isRestrictedUser
|
2019-08-31 09:04:36 -04:00
|
|
|
else if response.statusCode == 429
|
|
|
|
logger.log(project_id, user_id, "rate-limit hit when joining project")
|
|
|
|
callback(new CodedError("rate-limit hit when joining project", "TooManyRequests"))
|
2014-11-10 06:27:08 -05:00
|
|
|
else
|
|
|
|
err = new Error("non-success status code from web: #{response.statusCode}")
|
2019-08-31 09:04:36 -04:00
|
|
|
logger.error {err, project_id, user_id}, "error accessing web api"
|
2017-09-26 09:21:41 -04:00
|
|
|
callback err
|