您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { setRoom } from '../base/conference';
  4. import {
  5. configWillLoad,
  6. loadConfigError,
  7. restoreConfig,
  8. setConfig,
  9. storeConfig
  10. } from '../base/config';
  11. import { setLocationURL } from '../base/connection';
  12. import { loadConfig } from '../base/lib-jitsi-meet';
  13. import { parseURIString, toURLString } from '../base/util';
  14. import { setFatalError } from '../overlay';
  15. import { getDefaultURL } from './functions';
  16. const logger = require('jitsi-meet-logger').getLogger(__filename);
  17. declare var APP: Object;
  18. /**
  19. * Triggers an in-app navigation to a specific route. Allows navigation to be
  20. * abstracted between the mobile/React Native and Web/React applications.
  21. *
  22. * @param {string|undefined} uri - The URI to which to navigate. It may be a
  23. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  24. * scheme, or a mere room name.
  25. * @returns {Function}
  26. */
  27. export function appNavigate(uri: ?string) {
  28. return (dispatch: Dispatch<*>, getState: Function) =>
  29. _appNavigateToOptionalLocation(dispatch, getState, parseURIString(uri));
  30. }
  31. /**
  32. * Triggers an in-app navigation to a specific location URI.
  33. *
  34. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  35. * @param {Function} getState - The redux function that gets/retrieves the redux
  36. * state.
  37. * @param {Object} newLocation - The location URI to navigate to. The value
  38. * cannot be undefined and is assumed to have all properties such as
  39. * {@code host}, {@code contextRoot}, and {@code room} defined. Depending on the
  40. * property, it may have a value equal to {@code undefined} and that may be
  41. * acceptable.
  42. * @private
  43. * @returns {Promise<void>}
  44. */
  45. function _appNavigateToMandatoryLocation(
  46. dispatch: Dispatch<*>, getState: Function,
  47. newLocation: Object
  48. ): Promise<void> {
  49. const { room } = newLocation;
  50. const locationURL = new URL(newLocation.toString());
  51. dispatch(configWillLoad(locationURL));
  52. return (
  53. _loadConfig(dispatch, getState, newLocation)
  54. .then(
  55. config => loadConfigSettled(/* error */ undefined, config),
  56. error => loadConfigSettled(error, /* config */ undefined))
  57. .then(() => dispatch(setRoom(room))));
  58. /**
  59. * Notifies that an attempt to load a configuration has completed. Due to
  60. * the asynchronous nature of the loading, the specified {@code config} may
  61. * or may not be required by the time the notification arrives.
  62. *
  63. * @param {string|undefined} error - If the loading has failed, the error
  64. * detailing the cause of the failure.
  65. * @param {Object|undefined} config - If the loading has succeeded, the
  66. * loaded configuration.
  67. * @returns {void}
  68. */
  69. function loadConfigSettled(error, config) {
  70. // Due to the asynchronous nature of the loading, the specified config
  71. // may or may not be required by the time the notification arrives. If
  72. // we receive the config for a location we are no longer interested in,
  73. // "ignore" it - deliver it to the external API, for example, but do not
  74. // proceed with the appNavigate procedure/process.
  75. if (getState()['features/base/config'].locationURL === locationURL) {
  76. dispatch(setLocationURL(locationURL));
  77. dispatch(setConfig(config));
  78. } else {
  79. // eslint-disable-next-line no-param-reassign
  80. error || (error = new Error('Config no longer needed!'));
  81. // XXX The failure could be, for example, because of a
  82. // certificate-related error. In which case the connection will fail
  83. // later in Strophe anyway.
  84. dispatch(loadConfigError(error, locationURL));
  85. throw error;
  86. }
  87. }
  88. }
  89. /**
  90. * Triggers an in-app navigation to a specific or undefined location (URI).
  91. *
  92. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  93. * @param {Function} getState - The redux function that gets/retrieves the redux
  94. * state.
  95. * @param {Object} location - The location (URI) to navigate to. The value may
  96. * be undefined.
  97. * @private
  98. * @returns {void}
  99. */
  100. function _appNavigateToOptionalLocation(
  101. dispatch: Dispatch<*>, getState: Function,
  102. location: Object) {
  103. // If the specified location (URI) does not identify a host, use the app's
  104. // default.
  105. if (!location || !location.host) {
  106. const defaultLocation = parseURIString(getDefaultURL(getState));
  107. if (location) {
  108. location.host = defaultLocation.host;
  109. // FIXME Turn location's host, hostname, and port properties into
  110. // setters in order to reduce the risks of inconsistent state.
  111. location.hostname = defaultLocation.hostname;
  112. location.port = defaultLocation.port;
  113. location.protocol = defaultLocation.protocol;
  114. } else {
  115. // eslint-disable-next-line no-param-reassign
  116. location = defaultLocation;
  117. }
  118. }
  119. location.protocol || (location.protocol = 'https:');
  120. return _appNavigateToMandatoryLocation(dispatch, getState, location);
  121. }
  122. /**
  123. * Loads config.js from a specific host.
  124. *
  125. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  126. * @param {Function} getState - The redux {@code getState} function.
  127. * @param {Object} location - The location URI which specifies the host to load
  128. * the config.js from.
  129. * @private
  130. * @returns {Promise<Object>}
  131. */
  132. function _loadConfig(
  133. dispatch: Dispatch<*>,
  134. getState: Function,
  135. { contextRoot, host, protocol, room }) {
  136. // XXX As the mobile/React Native app does not employ config on the
  137. // WelcomePage, do not download config.js from the deployment when
  138. // navigating to the WelcomePage - the perceived/visible navigation will be
  139. // faster.
  140. if (!room && typeof APP === 'undefined') {
  141. return Promise.resolve();
  142. }
  143. /* eslint-disable no-param-reassign */
  144. protocol = protocol.toLowerCase();
  145. // The React Native app supports an app-specific scheme which is sure to not
  146. // be supported by fetch (or whatever loadConfig utilizes).
  147. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  148. // TDOO userinfo
  149. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  150. let url = `${baseURL}config.js`;
  151. // XXX In order to support multiple shards, tell the room to the deployment.
  152. room && (url += `?room=${room.toLowerCase()}`);
  153. /* eslint-enable no-param-reassign */
  154. return loadConfig(url).then(
  155. /* onFulfilled */ config => {
  156. // FIXME If the config is no longer needed (in the terms of
  157. // _loadConfig) and that happened because of an intervening
  158. // _loadConfig for the same baseURL, then the unneeded config may be
  159. // stored after the needed config. Anyway.
  160. dispatch(storeConfig(baseURL, config));
  161. return config;
  162. },
  163. /* onRejected */ error => {
  164. // XXX The (down)loading of config failed. Try to use the last
  165. // successfully fetched for that deployment. It may not match the
  166. // shard.
  167. const config = restoreConfig(baseURL);
  168. if (config) {
  169. return config;
  170. }
  171. throw error;
  172. });
  173. }
  174. /**
  175. * Redirects to another page generated by replacing the path in the original URL
  176. * with the given path.
  177. *
  178. * @param {(string)} pathname - The path to navigate to.
  179. * @returns {Function}
  180. */
  181. export function redirectWithStoredParams(pathname: string) {
  182. return (dispatch: Dispatch<*>, getState: Function) => {
  183. const { locationURL } = getState()['features/base/connection'];
  184. const newLocationURL = new URL(locationURL.href);
  185. newLocationURL.pathname = pathname;
  186. window.location.assign(newLocationURL.toString());
  187. };
  188. }
  189. /**
  190. * Reloads the page.
  191. *
  192. * @protected
  193. * @returns {Function}
  194. */
  195. export function reloadNow() {
  196. return (dispatch: Dispatch<Function>, getState: Function) => {
  197. dispatch(setFatalError(undefined));
  198. const { locationURL } = getState()['features/base/connection'];
  199. logger.info(`Reloading the conference using URL: ${locationURL}`);
  200. if (navigator.product === 'ReactNative') {
  201. dispatch(appNavigate(toURLString(locationURL)));
  202. } else {
  203. dispatch(reloadWithStoredParams());
  204. }
  205. };
  206. }
  207. /**
  208. * Reloads the page by restoring the original URL.
  209. *
  210. * @returns {Function}
  211. */
  212. export function reloadWithStoredParams() {
  213. return (dispatch: Dispatch<*>, getState: Function) => {
  214. const { locationURL } = getState()['features/base/connection'];
  215. const windowLocation = window.location;
  216. const oldSearchString = windowLocation.search;
  217. windowLocation.replace(locationURL.toString());
  218. if (window.self !== window.top
  219. && locationURL.search === oldSearchString) {
  220. // NOTE: Assuming that only the hash or search part of the URL will
  221. // be changed!
  222. // location.reload will not trigger redirect/reload for iframe when
  223. // only the hash params are changed. That's why we need to call
  224. // reload in addition to replace.
  225. windowLocation.reload();
  226. }
  227. };
  228. }