notifications-controller.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. const valid = ['did-you-know', 'on-premise', 'people', 'FOMO']
  37. $scope.isVariantValid = valid.includes(
  38. $scope.groupsAndEnterpriseBannerVariant
  39. )
  40. $scope.urlVariantSuffix = $scope.isVariantValid
  41. ? `-${valid.indexOf($scope.groupsAndEnterpriseBannerVariant) + 1}`
  42. : ''
  43. }
  44. )
  45. App.controller('ProjectInviteNotificationController', function ($scope, $http) {
  46. // Shortcuts for translation keys
  47. $scope.projectName = $scope.notification.messageOpts.projectName
  48. $scope.userName = $scope.notification.messageOpts.userName
  49. $scope.accept = function () {
  50. $scope.notification.inflight = true
  51. return $http({
  52. url: `/project/${$scope.notification.messageOpts.projectId}/invite/token/${$scope.notification.messageOpts.token}/accept`,
  53. method: 'POST',
  54. headers: {
  55. 'X-Csrf-Token': window.csrfToken,
  56. 'X-Requested-With': 'XMLHttpRequest',
  57. },
  58. })
  59. .then(() => {
  60. $scope.notification.accepted = true
  61. })
  62. .catch(({ status }) => {
  63. if (status === 404) {
  64. // 404 probably means the invite has already been accepted and
  65. // deleted. Treat as success
  66. $scope.notification.accepted = true
  67. } else {
  68. $scope.notification.error = true
  69. }
  70. })
  71. .finally(() => {
  72. $scope.notification.inflight = false
  73. })
  74. }
  75. })
  76. App.controller(
  77. 'EmailNotificationController',
  78. function ($scope, $http, UserAffiliationsDataService) {
  79. $scope.userEmails = window.data.userEmails
  80. const _ssoAvailable = email => {
  81. if (!ExposedSettings.hasSamlFeature) return false
  82. if (email.samlProviderId) return true
  83. if (!email.affiliation || !email.affiliation.institution) return false
  84. if (email.affiliation.institution.ssoEnabled) return true
  85. if (
  86. ExposedSettings.hasSamlBeta &&
  87. email.affiliation.institution.ssoBeta
  88. ) {
  89. return true
  90. }
  91. return false
  92. }
  93. $scope.showConfirmEmail = email => {
  94. if (ExposedSettings.emailConfirmationDisabled) {
  95. return false
  96. }
  97. if (!email.confirmedAt && !email.hide) {
  98. if (_ssoAvailable(email)) {
  99. return false
  100. }
  101. return true
  102. }
  103. return false
  104. }
  105. for (const userEmail of $scope.userEmails) {
  106. userEmail.hide = false
  107. }
  108. $scope.resendConfirmationEmail = function (userEmail) {
  109. userEmail.confirmationInflight = true
  110. userEmail.error = false
  111. userEmail.errorMessage = null
  112. UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
  113. .then(() => {
  114. userEmail.hide = true
  115. $scope.$emit('project-list:notifications-received')
  116. })
  117. .catch(error => {
  118. userEmail.error = true
  119. userEmail.errorMessage = error.data.message
  120. console.error(error)
  121. $scope.$emit('project-list:notifications-received')
  122. })
  123. .finally(() => {
  124. userEmail.confirmationInflight = false
  125. })
  126. }
  127. }
  128. )