HomeController.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-unused-vars,
  5. n/no-deprecated-api,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let HomeController
  16. const Features = require('../../infrastructure/Features')
  17. const Path = require('path')
  18. const fs = require('fs')
  19. const ErrorController = require('../Errors/ErrorController')
  20. const SessionManager = require('../Authentication/SessionManager')
  21. const SplitTestHandler = require('../SplitTests/SplitTestHandler')
  22. const logger = require('@overleaf/logger')
  23. const homepageExists = fs.existsSync(
  24. Path.join(__dirname, '/../../../views/external/home/v2.pug')
  25. )
  26. module.exports = HomeController = {
  27. index(req, res) {
  28. if (SessionManager.isUserLoggedIn(req.session)) {
  29. if (req.query.scribtex_path != null) {
  30. return res.redirect(`/project?scribtex_path=${req.query.scribtex_path}`)
  31. } else {
  32. return res.redirect('/project')
  33. }
  34. } else {
  35. return HomeController.home(req, res)
  36. }
  37. },
  38. async home(req, res) {
  39. if (Features.hasFeature('homepage') && homepageExists) {
  40. try {
  41. const highlightSSOAssignment =
  42. await SplitTestHandler.promises.getAssignment(
  43. req,
  44. res,
  45. 'highlight-sso-2'
  46. )
  47. const homeRegistration = await SplitTestHandler.promises.getAssignment(
  48. req,
  49. res,
  50. 'home-registration'
  51. )
  52. const highlightSSO = highlightSSOAssignment.variant === 'active'
  53. const removeRegistration = homeRegistration.variant
  54. return res.render('external/home/v2', {
  55. highlightSSO,
  56. removeRegistration,
  57. })
  58. } catch (err) {
  59. logger.error(
  60. { err },
  61. "error fetching 'highlight-sso-2' split test assignment"
  62. )
  63. return res.render('external/home/v2', {
  64. highlightSSO: false,
  65. removeRegistration: 'default',
  66. })
  67. }
  68. } else {
  69. return res.redirect('/login')
  70. }
  71. },
  72. externalPage(page, title) {
  73. return function (req, res, next) {
  74. if (next == null) {
  75. next = function () {}
  76. }
  77. const path = Path.join(__dirname, `/../../../views/external/${page}.pug`)
  78. return fs.exists(path, function (exists) {
  79. // No error in this callback - old method in Node.js!
  80. if (exists) {
  81. return res.render(`external/${page}.pug`, { title })
  82. } else {
  83. return ErrorController.notFound(req, res, next)
  84. }
  85. })
  86. }
  87. },
  88. }