notifications-controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import App from '../../base'
  2. const ExposedSettings = window.ExposedSettings
  3. App.controller('NotificationsController', function ($scope, $http) {
  4. for (const 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(
  23. 'DismissableNotificationsController',
  24. function ($scope, localStorage) {
  25. $scope.shouldShowNotification =
  26. localStorage('dismissed-covid-19-notification-extended') !== true
  27. $scope.dismiss = () => {
  28. localStorage('dismissed-covid-19-notification-extended', true)
  29. $scope.shouldShowNotification = false
  30. }
  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(
  65. 'EmailNotificationController',
  66. function ($scope, $http, UserAffiliationsDataService) {
  67. $scope.userEmails = window.data.userEmails
  68. const _ssoAvailable = email => {
  69. if (!ExposedSettings.hasSamlFeature) return false
  70. if (email.samlProviderId) return true
  71. if (!email.affiliation || !email.affiliation.institution) return false
  72. if (email.affiliation.institution.ssoEnabled) return true
  73. if (
  74. ExposedSettings.hasSamlBeta &&
  75. email.affiliation.institution.ssoBeta
  76. ) {
  77. return true
  78. }
  79. return false
  80. }
  81. $scope.showConfirmEmail = email => {
  82. if (ExposedSettings.emailConfirmationDisabled) {
  83. return false
  84. }
  85. if (!email.confirmedAt && !email.hide) {
  86. if (_ssoAvailable(email)) {
  87. return false
  88. }
  89. return true
  90. }
  91. return false
  92. }
  93. for (const userEmail of $scope.userEmails) {
  94. userEmail.hide = false
  95. }
  96. $scope.resendConfirmationEmail = function (userEmail) {
  97. userEmail.confirmationInflight = true
  98. userEmail.error = false
  99. userEmail.errorMessage = null
  100. UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
  101. .then(() => {
  102. userEmail.hide = true
  103. $scope.$emit('project-list:notifications-received')
  104. })
  105. .catch(error => {
  106. userEmail.error = true
  107. userEmail.errorMessage = error.data.message
  108. console.error(error)
  109. $scope.$emit('project-list:notifications-received')
  110. })
  111. .finally(() => {
  112. userEmail.confirmationInflight = false
  113. })
  114. }
  115. }
  116. )