notifications-controller.js 4.4 KB

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