notifications-controller.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import App from '../../base'
  2. const ExposedSettings = window.ExposedSettings
  3. App.controller('NotificationsController', function($scope, $http) {
  4. for (let notification of $scope.notifications || []) {
  5. notification.hide = false
  6. }
  7. $scope.samlInitPath = ExposedSettings.samlInitPath
  8. $scope.dismiss = notification => {
  9. if (!notification._id) {
  10. notification.hide = true
  11. return
  12. }
  13. $http({
  14. url: `/notifications/${notification._id}`,
  15. method: 'DELETE',
  16. headers: {
  17. 'X-Csrf-Token': window.csrfToken
  18. }
  19. }).then(() => (notification.hide = true))
  20. }
  21. })
  22. App.controller('DismissableNotificationsController', function(
  23. $scope,
  24. localStorage
  25. ) {
  26. $scope.shouldShowNotification =
  27. localStorage('dismissed-covid-19-notification-extended') !== true
  28. $scope.dismiss = () => {
  29. localStorage('dismissed-covid-19-notification-extended', true)
  30. $scope.shouldShowNotification = false
  31. }
  32. })
  33. App.controller('ProjectInviteNotificationController', function($scope, $http) {
  34. // Shortcuts for translation keys
  35. $scope.projectName = $scope.notification.messageOpts.projectName
  36. $scope.userName = $scope.notification.messageOpts.userName
  37. $scope.accept = function() {
  38. $scope.notification.inflight = true
  39. return $http({
  40. url: `/project/${$scope.notification.messageOpts.projectId}/invite/token/${$scope.notification.messageOpts.token}/accept`,
  41. method: 'POST',
  42. headers: {
  43. 'X-Csrf-Token': window.csrfToken,
  44. 'X-Requested-With': 'XMLHttpRequest'
  45. }
  46. })
  47. .then(() => {
  48. $scope.notification.accepted = true
  49. })
  50. .catch(({ status }) => {
  51. if (status === 404) {
  52. // 404 probably means the invite has already been accepted and
  53. // deleted. Treat as success
  54. $scope.notification.accepted = true
  55. } else {
  56. $scope.notification.error = true
  57. }
  58. })
  59. .finally(() => {
  60. $scope.notification.inflight = false
  61. })
  62. }
  63. })
  64. App.controller('EmailNotificationController', function(
  65. $scope,
  66. $http,
  67. UserAffiliationsDataService
  68. ) {
  69. $scope.userEmails = window.data.userEmails
  70. const _ssoAvailable = email => {
  71. if (!ExposedSettings.hasSamlFeature) return false
  72. if (email.samlProviderId) return true
  73. if (!email.affiliation || !email.affiliation.institution) return false
  74. if (email.affiliation.institution.ssoEnabled) return true
  75. if (ExposedSettings.hasSamlBeta && email.affiliation.institution.ssoBeta) {
  76. return true
  77. }
  78. return false
  79. }
  80. $scope.showConfirmEmail = email => {
  81. if (ExposedSettings.emailConfirmationDisabled) {
  82. return false
  83. }
  84. if (!email.confirmedAt && !email.hide) {
  85. if (_ssoAvailable(email)) {
  86. return false
  87. }
  88. return true
  89. }
  90. return false
  91. }
  92. for (let userEmail of $scope.userEmails) {
  93. userEmail.hide = false
  94. }
  95. $scope.resendConfirmationEmail = function(userEmail) {
  96. userEmail.confirmationInflight = true
  97. userEmail.error = false
  98. userEmail.errorMessage = null
  99. UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
  100. .then(() => {
  101. userEmail.hide = true
  102. $scope.$emit('project-list:notifications-received')
  103. })
  104. .catch(error => {
  105. userEmail.error = true
  106. userEmail.errorMessage = error.data.message
  107. console.error(error)
  108. $scope.$emit('project-list:notifications-received')
  109. })
  110. .finally(() => {
  111. userEmail.confirmationInflight = false
  112. })
  113. }
  114. })