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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { getCurrentConference } from '../base/conference';
  5. import {
  6. getLocalParticipant,
  7. getParticipantById,
  8. getParticipantCount,
  9. PARTICIPANT_JOINED,
  10. PARTICIPANT_LEFT,
  11. PARTICIPANT_UPDATED,
  12. participantUpdated,
  13. getRemoteParticipants
  14. } from '../base/participants';
  15. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  16. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  17. import { TOGGLE_E2EE } from './actionTypes';
  18. import { setEveryoneEnabledE2EE, setEveryoneSupportE2EE, toggleE2EE } from './actions';
  19. import { E2EE_OFF_SOUND_ID, E2EE_ON_SOUND_ID } from './constants';
  20. import logger from './logger';
  21. import { E2EE_OFF_SOUND_FILE, E2EE_ON_SOUND_FILE } from './sounds';
  22. /**
  23. * Middleware that captures actions related to E2EE.
  24. *
  25. * @param {Store} store - The redux store.
  26. * @returns {Function}
  27. */
  28. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  29. switch (action.type) {
  30. case APP_WILL_MOUNT:
  31. dispatch(registerSound(
  32. E2EE_OFF_SOUND_ID,
  33. E2EE_OFF_SOUND_FILE));
  34. dispatch(registerSound(
  35. E2EE_ON_SOUND_ID,
  36. E2EE_ON_SOUND_FILE));
  37. break;
  38. case APP_WILL_UNMOUNT:
  39. dispatch(unregisterSound(E2EE_OFF_SOUND_ID));
  40. dispatch(unregisterSound(E2EE_ON_SOUND_ID));
  41. break;
  42. case PARTICIPANT_UPDATED: {
  43. const { id, e2eeEnabled, e2eeSupported } = action.participant;
  44. const oldParticipant = getParticipantById(getState(), id);
  45. const result = next(action);
  46. if (e2eeEnabled !== oldParticipant?.e2eeEnabled
  47. || e2eeSupported !== oldParticipant?.e2eeSupported) {
  48. const state = getState();
  49. let newEveryoneSupportE2EE = true;
  50. let newEveryoneEnabledE2EE = true;
  51. // eslint-disable-next-line no-unused-vars
  52. for (const [ key, p ] of getRemoteParticipants(state)) {
  53. if (!p.e2eeEnabled) {
  54. newEveryoneEnabledE2EE = false;
  55. }
  56. if (!p.e2eeSupported) {
  57. newEveryoneSupportE2EE = false;
  58. }
  59. if (!newEveryoneEnabledE2EE && !newEveryoneSupportE2EE) {
  60. break;
  61. }
  62. }
  63. if (!getLocalParticipant(state)?.e2eeEnabled) {
  64. newEveryoneEnabledE2EE = false;
  65. }
  66. batch(() => {
  67. dispatch(setEveryoneEnabledE2EE(newEveryoneEnabledE2EE));
  68. dispatch(setEveryoneSupportE2EE(newEveryoneSupportE2EE));
  69. });
  70. }
  71. return result;
  72. }
  73. case PARTICIPANT_JOINED: {
  74. const result = next(action);
  75. const { e2eeEnabled, e2eeSupported, local } = action.participant;
  76. const { everyoneEnabledE2EE } = getState()['features/e2ee'];
  77. const participantCount = getParticipantCount(getState());
  78. // the initial values
  79. if (participantCount === 1) {
  80. batch(() => {
  81. dispatch(setEveryoneEnabledE2EE(e2eeEnabled));
  82. dispatch(setEveryoneSupportE2EE(e2eeSupported));
  83. });
  84. }
  85. // if all had it enabled and this one disabled it, change value in store
  86. // otherwise there is no change in the value we store
  87. if (everyoneEnabledE2EE && !e2eeEnabled) {
  88. dispatch(setEveryoneEnabledE2EE(false));
  89. }
  90. if (local) {
  91. return result;
  92. }
  93. const { everyoneSupportE2EE } = getState()['features/e2ee'];
  94. // if all supported it and this one does not, change value in store
  95. // otherwise there is no change in the value we store
  96. if (everyoneSupportE2EE && !e2eeSupported) {
  97. dispatch(setEveryoneSupportE2EE(false));
  98. }
  99. return result;
  100. }
  101. case PARTICIPANT_LEFT: {
  102. const previosState = getState();
  103. const participant = getParticipantById(previosState, action.participant?.id) || {};
  104. const result = next(action);
  105. const newState = getState();
  106. const { e2eeEnabled = false, e2eeSupported = false } = participant;
  107. const { everyoneEnabledE2EE, everyoneSupportE2EE } = newState['features/e2ee'];
  108. // if it was not enabled by everyone, and the participant leaving had it disabled, or if it was not supported
  109. // by everyone, and the participant leaving had it not supported let's check is it enabled for all that stay
  110. if ((!everyoneEnabledE2EE && !e2eeEnabled) || (!everyoneSupportE2EE && !e2eeSupported)) {
  111. let latestEveryoneEnabledE2EE = true;
  112. let latestEveryoneSupportE2EE = true;
  113. // eslint-disable-next-line no-unused-vars
  114. for (const [ key, p ] of getRemoteParticipants(newState)) {
  115. if (!p.e2eeEnabled) {
  116. latestEveryoneEnabledE2EE = false;
  117. }
  118. if (!p.e2eeSupported) {
  119. latestEveryoneSupportE2EE = false;
  120. }
  121. if (!latestEveryoneEnabledE2EE && !latestEveryoneSupportE2EE) {
  122. break;
  123. }
  124. }
  125. if (!getLocalParticipant(newState)?.e2eeEnabled) {
  126. latestEveryoneEnabledE2EE = false;
  127. }
  128. batch(() => {
  129. if (!everyoneEnabledE2EE && latestEveryoneEnabledE2EE) {
  130. dispatch(setEveryoneEnabledE2EE(true));
  131. }
  132. if (!everyoneSupportE2EE && latestEveryoneSupportE2EE) {
  133. dispatch(setEveryoneSupportE2EE(true));
  134. }
  135. });
  136. }
  137. return result;
  138. }
  139. case TOGGLE_E2EE: {
  140. const conference = getCurrentConference(getState);
  141. if (conference && conference.isE2EEEnabled() !== action.enabled) {
  142. logger.debug(`E2EE will be ${action.enabled ? 'enabled' : 'disabled'}`);
  143. conference.toggleE2EE(action.enabled);
  144. // Broadcast that we enabled / disabled E2EE.
  145. const participant = getLocalParticipant(getState);
  146. dispatch(participantUpdated({
  147. e2eeEnabled: action.enabled,
  148. id: participant.id,
  149. local: true
  150. }));
  151. const soundID = action.enabled ? E2EE_ON_SOUND_ID : E2EE_OFF_SOUND_ID;
  152. dispatch(playSound(soundID));
  153. }
  154. break;
  155. }
  156. }
  157. return next(action);
  158. });
  159. /**
  160. * Set up state change listener to perform maintenance tasks when the conference
  161. * is left or failed.
  162. */
  163. StateListenerRegistry.register(
  164. state => getCurrentConference(state),
  165. (conference, { dispatch }, previousConference) => {
  166. if (previousConference) {
  167. dispatch(toggleE2EE(false));
  168. }
  169. });