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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. if (!p.local && !joinLeaveNotificationsDisabled()) {
  33. dispatch(showParticipantJoinedNotification(
  34. getParticipantDisplayName(getState, p.id)
  35. ));
  36. }
  37. if (typeof interfaceConfig === 'object'
  38. && !interfaceConfig.DISABLE_FOCUS_INDICATOR && p.role === PARTICIPANT_ROLE.MODERATOR) {
  39. // Do not show the notification for mobile and also when the focus indicator is disabled.
  40. const displayName = getParticipantDisplayName(getState, p.id);
  41. dispatch(showNotification({
  42. descriptionArguments: { to: displayName || '$t(notify.somebody)' },
  43. descriptionKey: 'notify.grantedTo',
  44. titleKey: 'notify.somebody',
  45. title: displayName
  46. },
  47. NOTIFICATION_TIMEOUT));
  48. }
  49. return result;
  50. }
  51. case PARTICIPANT_LEFT: {
  52. if (!joinLeaveNotificationsDisabled()) {
  53. const participant = getParticipantById(
  54. store.getState(),
  55. action.participant.id
  56. );
  57. if (typeof interfaceConfig === 'object'
  58. && participant
  59. && !participant.local) {
  60. store.dispatch(showNotification({
  61. descriptionKey: 'notify.disconnected',
  62. titleKey: 'notify.somebody',
  63. title: participant.name
  64. }, NOTIFICATION_TIMEOUT));
  65. }
  66. }
  67. return next(action);
  68. }
  69. case PARTICIPANT_UPDATED: {
  70. if (typeof interfaceConfig === 'undefined' || interfaceConfig.DISABLE_FOCUS_INDICATOR) {
  71. // Do not show the notification for mobile and also when the focus indicator is disabled.
  72. return next(action);
  73. }
  74. const { id, role } = action.participant;
  75. const state = store.getState();
  76. const oldParticipant = getParticipantById(state, id);
  77. const oldRole = oldParticipant?.role;
  78. if (oldRole && oldRole !== role && role === PARTICIPANT_ROLE.MODERATOR) {
  79. const displayName = getParticipantDisplayName(state, id);
  80. store.dispatch(showNotification({
  81. descriptionArguments: { to: displayName || '$t(notify.somebody)' },
  82. descriptionKey: 'notify.grantedTo',
  83. titleKey: 'notify.somebody',
  84. title: displayName
  85. },
  86. NOTIFICATION_TIMEOUT));
  87. }
  88. return next(action);
  89. }
  90. }
  91. return next(action);
  92. });
  93. /**
  94. * StateListenerRegistry provides a reliable way to detect the leaving of a
  95. * conference, where we need to clean up the notifications.
  96. */
  97. StateListenerRegistry.register(
  98. /* selector */ state => getCurrentConference(state),
  99. /* listener */ (conference, { dispatch }) => {
  100. if (!conference) {
  101. dispatch(clearNotifications());
  102. }
  103. }
  104. );