HistoryController.coffee 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. logger = require "logger-sharelatex"
  2. request = require "request"
  3. settings = require "settings-sharelatex"
  4. AuthenticationController = require "../Authentication/AuthenticationController"
  5. module.exports = HistoryController =
  6. initializeProject: (callback = (error, history_id) ->) ->
  7. return callback() if !settings.apis.project_history?.enabled
  8. request.post {
  9. url: "#{settings.apis.project_history.url}/project"
  10. }, (error, res, body)->
  11. return callback(error) if error?
  12. if res.statusCode >= 200 and res.statusCode < 300
  13. try
  14. project = JSON.parse(body)
  15. catch error
  16. return callback(error)
  17. overleaf_id = project?.project?.id
  18. if !overleaf_id
  19. error = new Error("project-history did not provide an id", project)
  20. return callback(error)
  21. callback null, { overleaf_id }
  22. else
  23. error = new Error("project-history returned a non-success status code: #{res.statusCode}")
  24. callback error
  25. proxyToHistoryApi: (req, res, next = (error) ->) ->
  26. user_id = AuthenticationController.getLoggedInUserId req
  27. url = HistoryController.buildHistoryServiceUrl() + req.url
  28. logger.log url: url, "proxying to track-changes api"
  29. getReq = request(
  30. url: url
  31. method: req.method
  32. headers:
  33. "X-User-Id": user_id
  34. )
  35. getReq.pipe(res)
  36. getReq.on "error", (error) ->
  37. logger.error err: error, "track-changes API error"
  38. next(error)
  39. buildHistoryServiceUrl: () ->
  40. if settings.apis.project_history?.enabled
  41. return settings.apis.project_history.url
  42. else
  43. return settings.apis.trackchanges.url