notifications-controller.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. define(['base'], function(App) {
  15. App.controller('NotificationsController', function($scope, $http) {
  16. for (let notification of Array.from($scope.notifications)) {
  17. notification.hide = false
  18. }
  19. $scope.samlInitPath = ExposedSettings.samlInitPath
  20. return ($scope.dismiss = notification =>
  21. $http({
  22. url: `/notifications/${notification._id}`,
  23. method: 'DELETE',
  24. headers: {
  25. 'X-Csrf-Token': window.csrfToken
  26. }
  27. }).then(() => (notification.hide = true)))
  28. })
  29. App.controller('ProjectInviteNotificationController', function(
  30. $scope,
  31. $http
  32. ) {
  33. // Shortcuts for translation keys
  34. $scope.projectName = $scope.notification.messageOpts.projectName
  35. $scope.userName = $scope.notification.messageOpts.userName
  36. return ($scope.accept = function() {
  37. $scope.notification.inflight = true
  38. return $http({
  39. url: `/project/${
  40. $scope.notification.messageOpts.projectId
  41. }/invite/token/${$scope.notification.messageOpts.token}/accept`,
  42. method: 'POST',
  43. headers: {
  44. 'X-Csrf-Token': window.csrfToken,
  45. 'X-Requested-With': 'XMLHttpRequest'
  46. }
  47. })
  48. .then(() => {
  49. $scope.notification.accepted = true
  50. })
  51. .catch(({ status }) => {
  52. if (status === 404) {
  53. // 404 probably means the invite has already been accepted and
  54. // deleted. Treat as success
  55. $scope.notification.accepted = true
  56. } else {
  57. $scope.notification.error = true
  58. }
  59. })
  60. .finally(() => {
  61. $scope.notification.inflight = false
  62. })
  63. })
  64. })
  65. App.controller('OverleafV2NotificationController', function(
  66. $scope,
  67. localStorage
  68. ) {
  69. $scope.visible = !localStorage('overleaf_v2_2_notification_hidden_at')
  70. return ($scope.dismiss = function() {
  71. $scope.visible = false
  72. return localStorage('overleaf_v2_2_notification_hidden_at', Date.now())
  73. })
  74. })
  75. App.controller('OverleafV1NotificationController', function(
  76. $scope,
  77. localStorage
  78. ) {
  79. $scope.visible = !localStorage('overleaf_v1_notification_hidden_at')
  80. return ($scope.toggle = function() {
  81. $scope.visible = !$scope.visible
  82. if (!$scope.visible) {
  83. return localStorage('overleaf_v1_notification_hidden_at', Date.now())
  84. } else {
  85. return localStorage('overleaf_v1_notification_hidden_at', null)
  86. }
  87. })
  88. })
  89. return App.controller('EmailNotificationController', function(
  90. $scope,
  91. $http,
  92. UserAffiliationsDataService
  93. ) {
  94. $scope.userEmails = []
  95. const _ssoAvailable = email => {
  96. if (!ExposedSettings.hasSamlFeature) return false
  97. if (!email.affiliation || !email.affiliation.institution) return false
  98. if (email.affiliation.institution.ssoEnabled) return true
  99. if (
  100. ExposedSettings.hasSamlBeta &&
  101. email.affiliation.institution.ssoBeta
  102. ) {
  103. return true
  104. }
  105. return false
  106. }
  107. $scope.showConfirmEmail = email => {
  108. if (ExposedSettings.emailConfirmationDisabled) {
  109. return false
  110. }
  111. if (!email.confirmedAt && !email.hide) {
  112. if (_ssoAvailable(email)) {
  113. return false
  114. }
  115. return true
  116. }
  117. return false
  118. }
  119. for (let userEmail of Array.from($scope.userEmails)) {
  120. userEmail.hide = false
  121. }
  122. const _getUserEmails = () =>
  123. UserAffiliationsDataService.getUserEmails().then(function(emails) {
  124. $scope.userEmails = emails
  125. return $scope.$emit('project-list:notifications-received')
  126. })
  127. _getUserEmails()
  128. return ($scope.resendConfirmationEmail = function(userEmail) {
  129. userEmail.confirmationInflight = true
  130. return UserAffiliationsDataService.resendConfirmationEmail(
  131. userEmail.email
  132. ).then(function() {
  133. userEmail.hide = true
  134. userEmail.confirmationInflight = false
  135. return $scope.$emit('project-list:notifications-received')
  136. })
  137. })
  138. })
  139. })