You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* @flow */
  2. import { getCurrentConference } from '../base/conference';
  3. import {
  4. PARTICIPANT_JOINED,
  5. PARTICIPANT_LEFT,
  6. PARTICIPANT_ROLE,
  7. PARTICIPANT_UPDATED,
  8. getParticipantById,
  9. getParticipantDisplayName
  10. } from '../base/participants';
  11. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  12. import {
  13. clearNotifications,
  14. showNotification,
  15. showParticipantJoinedNotification
  16. } from './actions';
  17. import { NOTIFICATION_TIMEOUT } from './constants';
  18. import { joinLeaveNotificationsDisabled } from './functions';
  19. declare var interfaceConfig: Object;
  20. /**
  21. * Middleware that captures actions to display notifications.
  22. *
  23. * @param {Store} store - The redux store.
  24. * @returns {Function}
  25. */
  26. MiddlewareRegistry.register(store => next => action => {
  27. switch (action.type) {
  28. case PARTICIPANT_JOINED: {
  29. const result = next(action);
  30. const { participant: p } = action;
  31. const { dispatch, getState } = store;
  32. const state = getState();
  33. const { conference } = state['features/base/conference'];
  34. if (conference && !p.local && !joinLeaveNotificationsDisabled() && !p.isReplacing) {
  35. dispatch(showParticipantJoinedNotification(
  36. getParticipantDisplayName(state, p.id)
  37. ));
  38. }
  39. if (typeof interfaceConfig === 'object'
  40. && !interfaceConfig.DISABLE_FOCUS_INDICATOR && p.role === PARTICIPANT_ROLE.MODERATOR) {
  41. // Do not show the notification for mobile and also when the focus indicator is disabled.
  42. const displayName = getParticipantDisplayName(state, p.id);
  43. if (!p.isReplacing) {
  44. dispatch(showNotification({
  45. descriptionArguments: { to: displayName || '$t(notify.somebody)' },
  46. descriptionKey: 'notify.grantedTo',
  47. titleKey: 'notify.somebody',
  48. title: displayName
  49. },
  50. NOTIFICATION_TIMEOUT));
  51. }
  52. }
  53. return result;
  54. }
  55. case PARTICIPANT_LEFT: {
  56. if (!joinLeaveNotificationsDisabled()) {
  57. const participant = getParticipantById(
  58. store.getState(),
  59. action.participant.id
  60. );
  61. if (typeof interfaceConfig === 'object'
  62. && participant
  63. && !participant.local
  64. && !action.participant.isReplaced) {
  65. store.dispatch(showNotification({
  66. descriptionKey: 'notify.disconnected',
  67. titleKey: 'notify.somebody',
  68. title: participant.name
  69. }, NOTIFICATION_TIMEOUT));
  70. }
  71. }
  72. return next(action);
  73. }
  74. case PARTICIPANT_UPDATED: {
  75. if (typeof interfaceConfig === 'undefined' || interfaceConfig.DISABLE_FOCUS_INDICATOR) {
  76. // Do not show the notification for mobile and also when the focus indicator is disabled.
  77. return next(action);
  78. }
  79. const { id, role } = action.participant;
  80. const state = store.getState();
  81. const oldParticipant = getParticipantById(state, id);
  82. const oldRole = oldParticipant?.role;
  83. if (oldRole && oldRole !== role && role === PARTICIPANT_ROLE.MODERATOR) {
  84. const displayName = getParticipantDisplayName(state, id);
  85. store.dispatch(showNotification({
  86. descriptionArguments: { to: displayName || '$t(notify.somebody)' },
  87. descriptionKey: 'notify.grantedTo',
  88. titleKey: 'notify.somebody',
  89. title: displayName
  90. },
  91. NOTIFICATION_TIMEOUT));
  92. }
  93. return next(action);
  94. }
  95. }
  96. return next(action);
  97. });
  98. /**
  99. * StateListenerRegistry provides a reliable way to detect the leaving of a
  100. * conference, where we need to clean up the notifications.
  101. */
  102. StateListenerRegistry.register(
  103. /* selector */ state => getCurrentConference(state),
  104. /* listener */ (conference, { dispatch }) => {
  105. if (!conference) {
  106. dispatch(clearNotifications());
  107. }
  108. }
  109. );