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 9.1KB

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