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

actions.js 9.9KB

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