Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

middleware.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // @flow
  2. import _ from 'lodash';
  3. import { PREJOIN_INITIALIZED } from '../../prejoin/actionTypes';
  4. import { setPrejoinPageVisibility } from '../../prejoin/actions';
  5. import { APP_WILL_MOUNT } from '../app';
  6. import { setAudioOnly } from '../audio-only';
  7. import { SET_LOCATION_URL } from '../connection/actionTypes'; // minimize imports to avoid circular imports
  8. import { getJwtName } from '../jwt/functions';
  9. import { getLocalParticipant, participantUpdated } from '../participants';
  10. import { MiddlewareRegistry } from '../redux';
  11. import { parseURLParams } from '../util';
  12. import { SETTINGS_UPDATED } from './actionTypes';
  13. import { updateSettings } from './actions';
  14. import { handleCallIntegrationChange, handleCrashReportingChange } from './functions';
  15. /**
  16. * The middleware of the feature base/settings. Distributes changes to the state
  17. * of base/settings to the states of other features computed from the state of
  18. * base/settings.
  19. *
  20. * @param {Store} store - The redux store.
  21. * @returns {Function}
  22. */
  23. MiddlewareRegistry.register(store => next => action => {
  24. const result = next(action);
  25. switch (action.type) {
  26. case APP_WILL_MOUNT:
  27. _initializeCallIntegration(store);
  28. _initializeShowPrejoin(store);
  29. break;
  30. case PREJOIN_INITIALIZED: {
  31. _maybeUpdateDisplayName(store);
  32. break;
  33. }
  34. case SETTINGS_UPDATED:
  35. _maybeHandleCallIntegrationChange(action);
  36. _maybeSetAudioOnly(store, action);
  37. _updateLocalParticipant(store, action);
  38. _maybeCrashReportingChange(action);
  39. break;
  40. case SET_LOCATION_URL:
  41. _updateLocalParticipantFromUrl(store);
  42. break;
  43. }
  44. return result;
  45. });
  46. /**
  47. * Overwrites the showPrejoin flag based on cached used selection for showing prejoin screen.
  48. *
  49. * @param {Store} store - The redux store.
  50. * @private
  51. * @returns {void}
  52. */
  53. function _initializeShowPrejoin({ dispatch, getState }) {
  54. const { userSelectedSkipPrejoin } = getState()['features/base/settings'];
  55. if (userSelectedSkipPrejoin) {
  56. dispatch(setPrejoinPageVisibility(false));
  57. }
  58. }
  59. /**
  60. * Initializes the audio device handler based on the `disableCallIntegration` setting.
  61. *
  62. * @param {Store} store - The redux store.
  63. * @private
  64. * @returns {void}
  65. */
  66. function _initializeCallIntegration({ getState }) {
  67. const { disableCallIntegration } = getState()['features/base/settings'];
  68. if (typeof disableCallIntegration === 'boolean') {
  69. handleCallIntegrationChange(disableCallIntegration);
  70. }
  71. }
  72. /**
  73. * Maps the settings field names to participant names where they don't match.
  74. * Currently there is only one such field, but may be extended in the future.
  75. *
  76. * @private
  77. * @param {string} settingsField - The name of the settings field to map.
  78. * @returns {string}
  79. */
  80. function _mapSettingsFieldToParticipant(settingsField) {
  81. switch (settingsField) {
  82. case 'displayName':
  83. return 'name';
  84. }
  85. return settingsField;
  86. }
  87. /**
  88. * Handles a change in the `disableCallIntegration` setting.
  89. *
  90. * @param {Object} action - The redux action.
  91. * @private
  92. * @returns {void}
  93. */
  94. function _maybeHandleCallIntegrationChange({ settings: { disableCallIntegration } }) {
  95. if (typeof disableCallIntegration === 'boolean') {
  96. handleCallIntegrationChange(disableCallIntegration);
  97. }
  98. }
  99. /**
  100. * Handles a change in the `disableCrashReporting` setting.
  101. *
  102. * @param {Object} action - The redux action.
  103. * @private
  104. * @returns {void}
  105. */
  106. function _maybeCrashReportingChange({ settings: { disableCrashReporting } }) {
  107. if (typeof disableCrashReporting === 'boolean') {
  108. handleCrashReportingChange(disableCrashReporting);
  109. }
  110. }
  111. /**
  112. * Updates {@code startAudioOnly} flag if it's updated in the settings.
  113. *
  114. * @param {Store} store - The redux store.
  115. * @param {Object} action - The redux action.
  116. * @private
  117. * @returns {void}
  118. */
  119. function _maybeSetAudioOnly(
  120. { dispatch },
  121. { settings: { startAudioOnly } }) {
  122. if (typeof startAudioOnly === 'boolean') {
  123. dispatch(setAudioOnly(startAudioOnly));
  124. }
  125. }
  126. /**
  127. * Updates the display name to the one in JWT if there is one.
  128. *
  129. * @param {Store} store - The redux store.
  130. * @private
  131. * @returns {void}
  132. */
  133. function _maybeUpdateDisplayName({ dispatch, getState }) {
  134. const state = getState();
  135. const hasJwt = Boolean(state['features/base/jwt'].jwt);
  136. if (hasJwt) {
  137. const displayName = getJwtName(state);
  138. if (displayName) {
  139. dispatch(updateSettings({
  140. displayName
  141. }));
  142. }
  143. }
  144. }
  145. /**
  146. * Updates the local participant according to settings changes.
  147. *
  148. * @param {Store} store - The redux store.
  149. * @param {Object} action - The dispatched action.
  150. * @private
  151. * @returns {void}
  152. */
  153. function _updateLocalParticipant({ dispatch, getState }, action) {
  154. const { settings } = action;
  155. const localParticipant = getLocalParticipant(getState());
  156. const newLocalParticipant = {
  157. ...localParticipant
  158. };
  159. for (const key in settings) {
  160. if (settings.hasOwnProperty(key)) {
  161. newLocalParticipant[_mapSettingsFieldToParticipant(key)]
  162. = settings[key];
  163. }
  164. }
  165. dispatch(participantUpdated(newLocalParticipant));
  166. }
  167. /**
  168. * Returns the userInfo set in the URL.
  169. *
  170. * @param {Store} store - The redux store.
  171. * @private
  172. * @returns {void}
  173. */
  174. function _updateLocalParticipantFromUrl({ dispatch, getState }) {
  175. const urlParams
  176. = parseURLParams(getState()['features/base/connection'].locationURL);
  177. const urlEmail = urlParams['userInfo.email'];
  178. const urlDisplayName = urlParams['userInfo.displayName'];
  179. if (!urlEmail && !urlDisplayName) {
  180. return;
  181. }
  182. const localParticipant = getLocalParticipant(getState());
  183. if (localParticipant) {
  184. const displayName = _.escape(urlDisplayName);
  185. const email = _.escape(urlEmail);
  186. dispatch(participantUpdated({
  187. ...localParticipant,
  188. email,
  189. name: displayName
  190. }));
  191. dispatch(updateSettings({
  192. displayName,
  193. email
  194. }));
  195. }
  196. }