2014-03-05 11:31:52 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
request = require "request"
|
|
|
|
settings = require "settings-sharelatex"
|
2014-03-11 14:01:14 -04:00
|
|
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
2014-03-05 11:31:52 -05:00
|
|
|
|
2016-12-09 10:43:08 -05:00
|
|
|
module.exports = HistoryController =
|
2017-10-24 06:48:21 -04:00
|
|
|
initializeProject: (callback = (error, history_id) ->) ->
|
|
|
|
return callback() if !settings.apis.project_history?.enabled
|
|
|
|
request.post {
|
|
|
|
url: "#{settings.apis.project_history.url}/project"
|
|
|
|
}, (error, res, body)->
|
|
|
|
return callback(error) if error?
|
|
|
|
|
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
|
|
|
try
|
|
|
|
project = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
overleaf_id = project?.project?.id
|
|
|
|
if !overleaf_id
|
|
|
|
error = new Error("project-history did not provide an id", project)
|
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
callback null, { overleaf_id }
|
|
|
|
else
|
|
|
|
error = new Error("project-history returned a non-success status code: #{res.statusCode}")
|
|
|
|
callback error
|
|
|
|
|
2016-12-09 10:43:08 -05:00
|
|
|
proxyToHistoryApi: (req, res, next = (error) ->) ->
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
2017-10-11 06:18:34 -04:00
|
|
|
url = HistoryController.buildHistoryServiceUrl() + req.url
|
|
|
|
|
2016-09-05 10:58:31 -04:00
|
|
|
logger.log url: url, "proxying to track-changes api"
|
|
|
|
getReq = request(
|
|
|
|
url: url
|
|
|
|
method: req.method
|
|
|
|
headers:
|
|
|
|
"X-User-Id": user_id
|
|
|
|
)
|
|
|
|
getReq.pipe(res)
|
|
|
|
getReq.on "error", (error) ->
|
|
|
|
logger.error err: error, "track-changes API error"
|
|
|
|
next(error)
|
2017-10-11 06:18:34 -04:00
|
|
|
|
|
|
|
buildHistoryServiceUrl: () ->
|
2017-10-24 04:47:14 -04:00
|
|
|
if settings.apis.project_history?.enabled
|
2017-10-11 06:18:34 -04:00
|
|
|
return settings.apis.project_history.url
|
|
|
|
else
|
|
|
|
return settings.apis.trackchanges.url
|