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.

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