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.

reducer.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // @flow
  2. import _ from 'lodash';
  3. import { equals, ReducerRegistry, set } from '../redux';
  4. import { CONFIG_WILL_LOAD, LOAD_CONFIG_ERROR, SET_CONFIG } from './actionTypes';
  5. /**
  6. * The initial state of the feature base/config when executing in a
  7. * non-React Native environment. The mandatory configuration to be passed to
  8. * JitsiMeetJS#init(). The app will download config.js from the Jitsi Meet
  9. * deployment and take its values into account but the values bellow will be
  10. * enforced (because they are essential to the correct execution of the
  11. * application).
  12. *
  13. * @type {Object}
  14. */
  15. const INITIAL_NON_RN_STATE = {
  16. };
  17. /**
  18. * The initial state of the feature base/config when executing in a React Native
  19. * environment. The mandatory configuration to be passed to JitsiMeetJS#init().
  20. * The app will download config.js from the Jitsi Meet deployment and take its
  21. * values into account but the values bellow will be enforced (because they are
  22. * essential to the correct execution of the application).
  23. *
  24. * @type {Object}
  25. */
  26. const INITIAL_RN_STATE = {
  27. // FIXME The support for audio levels in lib-jitsi-meet polls the statistics
  28. // of WebRTC at a short interval multiple times a second. Unfortunately,
  29. // React Native is slow to fetch these statistics from the native WebRTC
  30. // API, through the React Native bridge and eventually to JavaScript.
  31. // Because the audio levels are of no interest to the mobile app, it is
  32. // fastest to merely disable them.
  33. disableAudioLevels: true,
  34. p2p: {
  35. disableH264: false,
  36. preferH264: true
  37. }
  38. };
  39. ReducerRegistry.register(
  40. 'features/base/config',
  41. (state = _getInitialState(), action) => {
  42. switch (action.type) {
  43. case CONFIG_WILL_LOAD:
  44. return {
  45. error: undefined,
  46. /**
  47. * The URL of the location associated with/configured by this
  48. * configuration.
  49. *
  50. * @type URL
  51. */
  52. locationURL: action.locationURL
  53. };
  54. case LOAD_CONFIG_ERROR:
  55. // XXX LOAD_CONFIG_ERROR is one of the settlement execution paths of
  56. // the asynchronous "loadConfig procedure/process" started with
  57. // CONFIG_WILL_LOAD. Due to the asynchronous nature of it, whoever
  58. // is settling the process needs to provide proof that they have
  59. // started it and that the iteration of the process being completed
  60. // now is still of interest to the app.
  61. if (state.locationURL === action.locationURL) {
  62. return {
  63. /**
  64. * The {@link Error} which prevented the loading of the
  65. * configuration of the associated {@code locationURL}.
  66. *
  67. * @type Error
  68. */
  69. error: action.error
  70. };
  71. }
  72. break;
  73. case SET_CONFIG:
  74. return _setConfig(state, action);
  75. }
  76. return state;
  77. });
  78. /**
  79. * Gets the initial state of the feature base/config. The mandatory
  80. * configuration to be passed to JitsiMeetJS#init(). The app will download
  81. * config.js from the Jitsi Meet deployment and take its values into account but
  82. * the values bellow will be enforced (because they are essential to the correct
  83. * execution of the application).
  84. *
  85. * @returns {Object}
  86. */
  87. function _getInitialState() {
  88. return (
  89. navigator.product === 'ReactNative'
  90. ? INITIAL_RN_STATE
  91. : INITIAL_NON_RN_STATE);
  92. }
  93. /**
  94. * Reduces a specific Redux action SET_CONFIG of the feature
  95. * base/lib-jitsi-meet.
  96. *
  97. * @param {Object} state - The Redux state of the feature base/lib-jitsi-meet.
  98. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  99. * @private
  100. * @returns {Object} The new state of the feature base/lib-jitsi-meet after the
  101. * reduction of the specified action.
  102. */
  103. function _setConfig(state, { config }) {
  104. // The mobile app bundles jitsi-meet and lib-jitsi-meet at build time and
  105. // does not download them at runtime from the deployment on which it will
  106. // join a conference. The downloading is planned for implementation in the
  107. // future (later rather than sooner) but is not implemented yet at the time
  108. // of this writing and, consequently, we must provide legacy support in the
  109. // meantime.
  110. // eslint-disable-next-line no-param-reassign
  111. config = _translateLegacyConfig(config);
  112. const newState = _.merge(
  113. {},
  114. config,
  115. { error: undefined },
  116. // The config of _getInitialState() is meant to override the config
  117. // downloaded from the Jitsi Meet deployment because the former contains
  118. // values that are mandatory.
  119. _getInitialState()
  120. );
  121. return equals(state, newState) ? state : newState;
  122. }
  123. /**
  124. * Constructs a new config {@code Object}, if necessary, out of a specific
  125. * config {@code Object} which is in the latest format supported by jitsi-meet.
  126. * Such a translation from an old config format to a new/the latest config
  127. * format is necessary because the mobile app bundles jitsi-meet and
  128. * lib-jitsi-meet at build time and does not download them at runtime from the
  129. * deployment on which it will join a conference.
  130. *
  131. * @param {Object} oldValue - The config {@code Object} which may or may not be
  132. * in the latest form supported by jitsi-meet and from which a new config
  133. * {@code Object} is to be constructed if necessary.
  134. * @returns {Object} A config {@code Object} which is in the latest format
  135. * supported by jitsi-meet.
  136. */
  137. function _translateLegacyConfig(oldValue: Object) {
  138. // jitsi/jitsi-meet#3ea2f005787c9f49c48febaeed9dc0340fe0a01b
  139. let newValue = oldValue;
  140. const oldConfigToNewConfig = {
  141. p2p: [
  142. [ 'backToP2PDelay', 'backToP2PDelay' ],
  143. [ 'enableP2P', 'enabled' ],
  144. [ 'p2pStunServers', 'stunServers' ]
  145. ],
  146. analytics: [
  147. [ 'analyticsScriptUrls', 'scriptURLs' ],
  148. [ 'googleAnalyticsTrackingId', 'googleAnalyticsTrackingId' ]
  149. ]
  150. };
  151. // Translate the old config properties into the new config.p2p properties.
  152. Object.keys(oldConfigToNewConfig).forEach(section => {
  153. if (typeof oldValue[section] !== 'object') {
  154. newValue = set(newValue, section, {});
  155. }
  156. for (const [ oldKey, newKey ] of oldConfigToNewConfig[section]) {
  157. if (oldKey in newValue && !(newKey in newValue[section])) {
  158. const v = newValue[oldKey];
  159. // Do not modify oldValue.
  160. if (newValue === oldValue) {
  161. newValue = {
  162. ...newValue
  163. };
  164. }
  165. delete newValue[oldKey];
  166. // Do not modify the section because it may be from oldValue
  167. // i.e. do not modify oldValue.
  168. newValue[section] = {
  169. ...newValue[section],
  170. [newKey]: v
  171. };
  172. }
  173. }
  174. });
  175. return newValue;
  176. }