notifications-controller.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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('ProjectInviteNotificationController', function ($scope, $http) {
  23. // Shortcuts for translation keys
  24. $scope.projectName = $scope.notification.messageOpts.projectName
  25. $scope.userName = $scope.notification.messageOpts.userName
  26. $scope.accept = function () {
  27. $scope.notification.inflight = true
  28. return $http({
  29. url: `/project/${$scope.notification.messageOpts.projectId}/invite/token/${$scope.notification.messageOpts.token}/accept`,
  30. method: 'POST',
  31. headers: {
  32. 'X-Csrf-Token': window.csrfToken,
  33. 'X-Requested-With': 'XMLHttpRequest',
  34. },
  35. })
  36. .then(() => {
  37. $scope.notification.accepted = true
  38. })
  39. .catch(({ status }) => {
  40. if (status === 404) {
  41. // 404 probably means the invite has already been accepted and
  42. // deleted. Treat as success
  43. $scope.notification.accepted = true
  44. } else {
  45. $scope.notification.error = true
  46. }
  47. })
  48. .finally(() => {
  49. $scope.notification.inflight = false
  50. })
  51. }
  52. })
  53. App.controller(
  54. 'EmailNotificationController',
  55. function ($scope, $http, UserAffiliationsDataService) {
  56. $scope.userEmails = window.data.userEmails
  57. const _ssoAvailable = email => {
  58. if (!ExposedSettings.hasSamlFeature) return false
  59. if (email.samlProviderId) return true
  60. if (!email.affiliation || !email.affiliation.institution) return false
  61. if (email.affiliation.institution.ssoEnabled) return true
  62. if (
  63. ExposedSettings.hasSamlBeta &&
  64. email.affiliation.institution.ssoBeta
  65. ) {
  66. return true
  67. }
  68. return false
  69. }
  70. $scope.showConfirmEmail = email => {
  71. if (ExposedSettings.emailConfirmationDisabled) {
  72. return false
  73. }
  74. if (!email.confirmedAt && !email.hide) {
  75. if (_ssoAvailable(email)) {
  76. return false
  77. }
  78. return true
  79. }
  80. return false
  81. }
  82. for (const userEmail of $scope.userEmails) {
  83. userEmail.hide = false
  84. }
  85. $scope.resendConfirmationEmail = function (userEmail) {
  86. userEmail.confirmationInflight = true
  87. userEmail.error = false
  88. userEmail.errorMessage = null
  89. UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
  90. .then(() => {
  91. userEmail.hide = true
  92. $scope.$emit('project-list:notifications-received')
  93. })
  94. .catch(error => {
  95. userEmail.error = true
  96. userEmail.errorMessage = error.data.message
  97. console.error(error)
  98. $scope.$emit('project-list:notifications-received')
  99. })
  100. .finally(() => {
  101. userEmail.confirmationInflight = false
  102. })
  103. }
  104. }
  105. )