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

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