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.

actions.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import type { Dispatch } from 'redux';
  4. import { addKnownDomains } from '../known-domains';
  5. import { parseURIString } from '../util';
  6. import { CONFIG_WILL_LOAD, LOAD_CONFIG_ERROR, SET_CONFIG, UPDATE_CONFIG } from './actionTypes';
  7. import { _CONFIG_STORE_PREFIX } from './constants';
  8. import { setConfigFromURLParams } from './functions';
  9. /**
  10. * Updates the config with new options.
  11. *
  12. * @param {Object} config - The new options (to add).
  13. * @returns {Function}
  14. */
  15. export function updateConfig(config: Object) {
  16. return {
  17. type: UPDATE_CONFIG,
  18. config
  19. };
  20. }
  21. /**
  22. * Signals that the configuration (commonly known in Jitsi Meet as config.js)
  23. * for a specific locationURL will be loaded now.
  24. *
  25. * @param {URL} locationURL - The URL of the location which necessitated the
  26. * loading of a configuration.
  27. * @param {string} room - The name of the room (conference) for which we're loading the config for.
  28. * @returns {{
  29. * type: CONFIG_WILL_LOAD,
  30. * locationURL: URL,
  31. * room: string
  32. * }}
  33. */
  34. export function configWillLoad(locationURL: URL, room: string) {
  35. return {
  36. type: CONFIG_WILL_LOAD,
  37. locationURL,
  38. room
  39. };
  40. }
  41. /**
  42. * Signals that a configuration (commonly known in Jitsi Meet as config.js)
  43. * could not be loaded due to a specific error.
  44. *
  45. * @param {Error} error - The {@code Error} which prevented the successful
  46. * loading of a configuration.
  47. * @param {URL} locationURL - The URL of the location which necessitated the
  48. * loading of a configuration.
  49. * @returns {{
  50. * type: LOAD_CONFIG_ERROR,
  51. * error: Error,
  52. * locationURL: URL
  53. * }}
  54. */
  55. export function loadConfigError(error: Error, locationURL: URL) {
  56. return {
  57. type: LOAD_CONFIG_ERROR,
  58. error,
  59. locationURL
  60. };
  61. }
  62. /**
  63. * Sets the configuration represented by the feature base/config. The
  64. * configuration is defined and consumed by the library lib-jitsi-meet but some
  65. * of its properties are consumed by the application jitsi-meet as well.
  66. *
  67. * @param {Object} config - The configuration to be represented by the feature
  68. * base/config.
  69. * @returns {Function}
  70. */
  71. export function setConfig(config: Object = {}) {
  72. return (dispatch: Dispatch<any>, getState: Function) => {
  73. const { locationURL } = getState()['features/base/connection'];
  74. // Now that the loading of the config was successful override the values
  75. // with the parameters passed in the hash part of the location URI.
  76. // TODO We're still in the middle ground between old Web with config,
  77. // interfaceConfig, and loggingConfig used via global variables and new
  78. // Web and mobile reading the respective values from the redux store.
  79. // On React Native there's no interfaceConfig at all yet and
  80. // loggingConfig is not loaded but there's a default value in the redux
  81. // store.
  82. // Only the config will be overridden on React Native, as the other
  83. // globals will be undefined here. It's intentional - we do not care to
  84. // override those configs yet.
  85. locationURL
  86. && setConfigFromURLParams(
  87. // On Web the config also comes from the window.config global,
  88. // but it is resolved in the loadConfig procedure.
  89. config,
  90. window.interfaceConfig,
  91. window.loggingConfig,
  92. locationURL);
  93. dispatch({
  94. type: SET_CONFIG,
  95. config
  96. });
  97. };
  98. }
  99. /**
  100. * Stores a specific Jitsi Meet config.js object into {@code localStorage}.
  101. *
  102. * @param {string} baseURL - The base URL from which the config.js was
  103. * downloaded.
  104. * @param {Object} config - The Jitsi Meet config.js to store.
  105. * @returns {Function}
  106. */
  107. export function storeConfig(baseURL: string, config: Object) {
  108. return (dispatch: Dispatch<any>) => {
  109. // Try to store the configuration in localStorage. If the deployment
  110. // specified 'getroom' as a function, for example, it does not make
  111. // sense to and it will not be stored.
  112. let b = false;
  113. try {
  114. if (typeof window.config === 'undefined' || window.config !== config) {
  115. jitsiLocalStorage.setItem(`${_CONFIG_STORE_PREFIX}/${baseURL}`, JSON.stringify(config));
  116. b = true;
  117. }
  118. } catch (e) {
  119. // Ignore the error because the caching is optional.
  120. }
  121. // If base/config knows a domain, then the app knows it.
  122. if (b) {
  123. try {
  124. dispatch(addKnownDomains(parseURIString(baseURL).host));
  125. } catch (e) {
  126. // Ignore the error because the fiddling with "known domains" is
  127. // a side effect here.
  128. }
  129. }
  130. return b;
  131. };
  132. }