notifications-controller.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const ExposedSettings = window.ExposedSettings
  2. define(['base'], function(App) {
  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. $http({
  10. url: `/notifications/${notification._id}`,
  11. method: 'DELETE',
  12. headers: {
  13. 'X-Csrf-Token': window.csrfToken
  14. }
  15. }).then(() => (notification.hide = true))
  16. })
  17. App.controller('ProjectInviteNotificationController', function(
  18. $scope,
  19. $http
  20. ) {
  21. // Shortcuts for translation keys
  22. $scope.projectName = $scope.notification.messageOpts.projectName
  23. $scope.userName = $scope.notification.messageOpts.userName
  24. $scope.accept = function() {
  25. $scope.notification.inflight = true
  26. return $http({
  27. url: `/project/${
  28. $scope.notification.messageOpts.projectId
  29. }/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('EmailNotificationController', function(
  54. $scope,
  55. $http,
  56. UserAffiliationsDataService
  57. ) {
  58. $scope.userEmails = []
  59. const _ssoAvailable = email => {
  60. if (!ExposedSettings.hasSamlFeature) return false
  61. if (email.samlProviderId) return true
  62. if (!email.affiliation || !email.affiliation.institution) return false
  63. if (email.affiliation.institution.ssoEnabled) return true
  64. if (
  65. ExposedSettings.hasSamlBeta &&
  66. email.affiliation.institution.ssoBeta
  67. ) {
  68. return true
  69. }
  70. return false
  71. }
  72. $scope.showConfirmEmail = email => {
  73. if (ExposedSettings.emailConfirmationDisabled) {
  74. return false
  75. }
  76. if (!email.confirmedAt && !email.hide) {
  77. if (_ssoAvailable(email)) {
  78. return false
  79. }
  80. return true
  81. }
  82. return false
  83. }
  84. for (let userEmail of $scope.userEmails) {
  85. userEmail.hide = false
  86. }
  87. const _getUserEmails = () =>
  88. UserAffiliationsDataService.getUserEmails().then(function(emails) {
  89. $scope.userEmails = emails
  90. $scope.$emit('project-list:notifications-received')
  91. })
  92. _getUserEmails()
  93. $scope.resendConfirmationEmail = function(userEmail) {
  94. userEmail.confirmationInflight = true
  95. return UserAffiliationsDataService.resendConfirmationEmail(
  96. userEmail.email
  97. ).then(function() {
  98. userEmail.hide = true
  99. userEmail.confirmationInflight = false
  100. $scope.$emit('project-list:notifications-received')
  101. })
  102. }
  103. })
  104. })