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.6KB

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