notifications-controller.js 4.0 KB

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