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.

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