overleaf/services/web/app/coffee/Features/History/HistoryController.coffee

72 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2014-03-05 11:31:52 -05:00
logger = require "logger-sharelatex"
request = require "request"
settings = require "settings-sharelatex"
AuthenticationController = require "../Authentication/AuthenticationController"
ProjectDetailsHandler = require "../Project/ProjectDetailsHandler"
ProjectEntityUpdateHandler = require "../Project/ProjectEntityUpdateHandler"
HistoryManager = require "./HistoryManager"
2014-03-05 11:31:52 -05:00
module.exports = HistoryController =
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?
history = project.overleaf?.history
if history?.id? and history?.display
req.useProjectHistory = true
else
req.useProjectHistory = false
next()
proxyToHistoryApi: (req, res, next = (error) ->) ->
2016-09-05 10:58:31 -04:00
user_id = AuthenticationController.getLoggedInUserId req
url = HistoryController.buildHistoryServiceUrl(req.useProjectHistory) + req.url
2017-10-11 06:18:34 -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) ->
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
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-02 04:43:57 -05:00
if 200 <= response.statusCode < 300
HistoryManager.injectUserDetails body, (error, data) ->
return next(error) if error?
res.json data
else
error = new Error("history api responded with non-success code: #{response.statusCode}")
logger.error err: error, user_id: user_id, "error proxying request to history api"
next(error)
buildHistoryServiceUrl: (useProjectHistory) ->
# choose a history service, either document-level (trackchanges)
# or project-level (project_history)
if useProjectHistory
2017-10-11 06:18:34 -04:00
return settings.apis.project_history.url
else
return settings.apis.trackchanges.url
2018-02-28 12:31:26 -05:00
2018-03-07 06:19:47 -05:00
resyncProjectHistory: (req, res, next = (error) ->) ->
2018-02-28 12:31:26 -05:00
project_id = req.params.Project_id
2018-03-07 06:19:47 -05:00
ProjectEntityUpdateHandler.resyncProjectHistory project_id, (error) ->
2018-02-28 12:31:26 -05:00
return next(error) if error?
res.sendStatus 204