notifications-controller.js 3.4 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/${
  41. $scope.notification.messageOpts.projectId
  42. }/invite/token/${$scope.notification.messageOpts.token}/accept`,
  43. method: 'POST',
  44. headers: {
  45. 'X-Csrf-Token': window.csrfToken,
  46. 'X-Requested-With': 'XMLHttpRequest'
  47. }
  48. })
  49. .then(() => {
  50. $scope.notification.accepted = true
  51. })
  52. .catch(({ status }) => {
  53. if (status === 404) {
  54. // 404 probably means the invite has already been accepted and
  55. // deleted. Treat as success
  56. $scope.notification.accepted = true
  57. } else {
  58. $scope.notification.error = true
  59. }
  60. })
  61. .finally(() => {
  62. $scope.notification.inflight = false
  63. })
  64. }
  65. })
  66. App.controller('EmailNotificationController', function(
  67. $scope,
  68. $http,
  69. UserAffiliationsDataService
  70. ) {
  71. $scope.userEmails = []
  72. const _ssoAvailable = email => {
  73. if (!ExposedSettings.hasSamlFeature) return false
  74. if (email.samlProviderId) return true
  75. if (!email.affiliation || !email.affiliation.institution) return false
  76. if (email.affiliation.institution.ssoEnabled) return true
  77. if (ExposedSettings.hasSamlBeta && email.affiliation.institution.ssoBeta) {
  78. return true
  79. }
  80. return false
  81. }
  82. $scope.showConfirmEmail = email => {
  83. if (ExposedSettings.emailConfirmationDisabled) {
  84. return false
  85. }
  86. if (!email.confirmedAt && !email.hide) {
  87. if (_ssoAvailable(email)) {
  88. return false
  89. }
  90. return true
  91. }
  92. return false
  93. }
  94. for (let userEmail of $scope.userEmails) {
  95. userEmail.hide = false
  96. }
  97. const _getUserEmails = () =>
  98. UserAffiliationsDataService.getUserEmails().then(function(emails) {
  99. $scope.userEmails = emails
  100. $scope.$emit('project-list:notifications-received')
  101. })
  102. _getUserEmails()
  103. $scope.resendConfirmationEmail = function(userEmail) {
  104. userEmail.confirmationInflight = true
  105. return UserAffiliationsDataService.resendConfirmationEmail(
  106. userEmail.email
  107. ).then(function() {
  108. userEmail.hide = true
  109. userEmail.confirmationInflight = false
  110. $scope.$emit('project-list:notifications-received')
  111. })
  112. }
  113. })