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.

actions.js 10KB

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