notifications-controller.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.affiliation || !email.affiliation.institution) return false
  62. if (email.affiliation.institution.ssoEnabled) return true
  63. if (
  64. ExposedSettings.hasSamlBeta &&
  65. email.affiliation.institution.ssoBeta
  66. ) {
  67. return true
  68. }
  69. return false
  70. }
  71. $scope.showConfirmEmail = email => {
  72. if (ExposedSettings.emailConfirmationDisabled) {
  73. return false
  74. }
  75. if (!email.confirmedAt && !email.hide) {
  76. if (_ssoAvailable(email)) {
  77. return false
  78. }
  79. return true
  80. }
  81. return false
  82. }
  83. for (let userEmail of $scope.userEmails) {
  84. userEmail.hide = false
  85. }
  86. const _getUserEmails = () =>
  87. UserAffiliationsDataService.getUserEmails().then(function(emails) {
  88. $scope.userEmails = emails
  89. $scope.$emit('project-list:notifications-received')
  90. })
  91. _getUserEmails()
  92. $scope.resendConfirmationEmail = function(userEmail) {
  93. userEmail.confirmationInflight = true
  94. return UserAffiliationsDataService.resendConfirmationEmail(
  95. userEmail.email
  96. ).then(function() {
  97. userEmail.hide = true
  98. userEmail.confirmationInflight = false
  99. $scope.$emit('project-list:notifications-received')
  100. })
  101. }
  102. })
  103. })