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"
|
2017-11-03 12:44:37 -04:00
|
|
|
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
|
2018-01-11 06:31:41 -05:00
|
|
|
HistoryManager = require "./HistoryManager"
|
2014-03-05 11:31:52 -05:00
|
|
|
|
2016-12-09 10:43:08 -05:00
|
|
|
module.exports = HistoryController =
|
2017-11-03 12:44:37 -04:00
|
|
|
selectHistoryApi: (req, res, next = (error) ->) ->
|
|
|
|
project_id = req.params?.Project_id
|
|
|
|
# find out which type of history service this project uses
|
|
|
|
ProjectDetailsHandler.getDetails project_id, (err, project) ->
|
|
|
|
return next(err) if err?
|
2017-12-14 10:38:20 -05:00
|
|
|
history = project.overleaf?.history
|
|
|
|
if history?.id? and history?.display
|
2017-11-03 12:44:37 -04:00
|
|
|
req.useProjectHistory = true
|
|
|
|
else
|
|
|
|
req.useProjectHistory = false
|
|
|
|
next()
|
|
|
|
|
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-11-03 12:44:37 -04:00
|
|
|
url = HistoryController.buildHistoryServiceUrl(req.useProjectHistory) + req.url
|
2017-10-11 06:18:34 -04:00
|
|
|
|
2017-11-03 12:44:37 -04:00
|
|
|
logger.log url: url, "proxying to history api"
|
2016-09-05 10:58:31 -04:00
|
|
|
getReq = request(
|
|
|
|
url: url
|
|
|
|
method: req.method
|
|
|
|
headers:
|
|
|
|
"X-User-Id": user_id
|
|
|
|
)
|
|
|
|
getReq.pipe(res)
|
|
|
|
getReq.on "error", (error) ->
|
2017-11-03 12:44:37 -04:00
|
|
|
logger.error url: url, err: error, "history API error"
|
2016-09-05 10:58:31 -04:00
|
|
|
next(error)
|
2017-10-11 06:18:34 -04:00
|
|
|
|
2018-01-11 06:31:41 -05:00
|
|
|
proxyToHistoryApiAndInjectUserDetails: (req, res, next = (error) ->) ->
|
|
|
|
user_id = AuthenticationController.getLoggedInUserId req
|
|
|
|
url = HistoryController.buildHistoryServiceUrl(req.useProjectHistory) + req.url
|
|
|
|
logger.log url: url, "proxying to history api"
|
|
|
|
request {
|
|
|
|
url: url
|
|
|
|
method: req.method
|
|
|
|
json: true
|
|
|
|
headers:
|
|
|
|
"X-User-Id": user_id
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return next(error) if error?
|
2018-02-01 07:49:25 -05:00
|
|
|
HistoryManager.injectUserDetails body, (error, data) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.json data
|
2018-01-11 06:31:41 -05:00
|
|
|
|
2017-11-03 12:44:37 -04:00
|
|
|
buildHistoryServiceUrl: (useProjectHistory) ->
|
|
|
|
# choose a history service, either document-level (trackchanges)
|
|
|
|
# or project-level (project_history)
|
2017-12-14 10:38:20 -05:00
|
|
|
if useProjectHistory
|
2017-10-11 06:18:34 -04:00
|
|
|
return settings.apis.project_history.url
|
|
|
|
else
|
|
|
|
return settings.apis.trackchanges.url
|