Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.ts 5.6KB

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