Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

middleware.js 4.0KB

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