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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 { getDefaultURL } from './functions';
  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. If
  72. // we receive the config for a location we are no longer interested in,
  73. // "ignore" it - deliver it to the external API, for example, but do not
  74. // 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 fail
  83. // 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 = parseURIString(getDefaultURL(getState));
  107. if (location) {
  108. location.host = defaultLocation.host;
  109. // FIXME Turn location's host, hostname, and port properties into
  110. // setters in order to reduce the risks of inconsistent state.
  111. location.hostname = defaultLocation.hostname;
  112. location.pathname
  113. = defaultLocation.pathname + location.pathname.substr(1);
  114. location.port = defaultLocation.port;
  115. location.protocol = defaultLocation.protocol;
  116. } else {
  117. // eslint-disable-next-line no-param-reassign
  118. location = defaultLocation;
  119. }
  120. }
  121. location.protocol || (location.protocol = 'https:');
  122. return _appNavigateToMandatoryLocation(dispatch, getState, location);
  123. }
  124. /**
  125. * Loads config.js from a specific host.
  126. *
  127. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  128. * @param {Function} getState - The redux {@code getState} function.
  129. * @param {Object} location - The location URI which specifies the host to load
  130. * the config.js from.
  131. * @private
  132. * @returns {Promise<Object>}
  133. */
  134. function _loadConfig(
  135. dispatch: Dispatch<*>,
  136. getState: Function,
  137. { contextRoot, host, protocol, room }) {
  138. // XXX As the mobile/React Native app does not employ config on the
  139. // WelcomePage, do not download config.js from the deployment when
  140. // navigating to the WelcomePage - the perceived/visible navigation will be
  141. // faster.
  142. if (!room && typeof APP === 'undefined') {
  143. return Promise.resolve();
  144. }
  145. /* eslint-disable no-param-reassign */
  146. protocol = protocol.toLowerCase();
  147. // The React Native app supports an app-specific scheme which is sure to not
  148. // be supported by fetch (or whatever loadConfig utilizes).
  149. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  150. // TDOO userinfo
  151. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  152. let url = `${baseURL}config.js`;
  153. // XXX In order to support multiple shards, tell the room to the deployment.
  154. room && (url += `?room=${room.toLowerCase()}`);
  155. /* eslint-enable no-param-reassign */
  156. return loadConfig(url).then(
  157. /* onFulfilled */ config => {
  158. // FIXME If the config is no longer needed (in the terms of
  159. // _loadConfig) and that happened because of an intervening
  160. // _loadConfig for the same baseURL, then the unneeded config may be
  161. // stored after the needed config. Anyway.
  162. dispatch(storeConfig(baseURL, config));
  163. return config;
  164. },
  165. /* onRejected */ error => {
  166. // XXX The (down)loading of config failed. Try to use the last
  167. // successfully fetched for that deployment. It may not match the
  168. // shard.
  169. const config = restoreConfig(baseURL);
  170. if (config) {
  171. return config;
  172. }
  173. throw error;
  174. });
  175. }
  176. /**
  177. * Redirects to another page generated by replacing the path in the original URL
  178. * with the given path.
  179. *
  180. * @param {(string)} pathname - The path to navigate to.
  181. * @returns {Function}
  182. */
  183. export function redirectWithStoredParams(pathname: string) {
  184. return (dispatch: Dispatch<*>, getState: Function) => {
  185. const { locationURL } = getState()['features/base/connection'];
  186. const newLocationURL = new URL(locationURL.href);
  187. newLocationURL.pathname = pathname;
  188. window.location.assign(newLocationURL.toString());
  189. };
  190. }
  191. /**
  192. * Reloads the page.
  193. *
  194. * @protected
  195. * @returns {Function}
  196. */
  197. export function reloadNow() {
  198. return (dispatch: Dispatch<Function>, getState: Function) => {
  199. dispatch(setFatalError(undefined));
  200. const { locationURL } = getState()['features/base/connection'];
  201. logger.info(`Reloading the conference using URL: ${locationURL}`);
  202. if (navigator.product === 'ReactNative') {
  203. dispatch(appNavigate(toURLString(locationURL)));
  204. } else {
  205. dispatch(reloadWithStoredParams());
  206. }
  207. };
  208. }
  209. /**
  210. * Reloads the page by restoring the original URL.
  211. *
  212. * @returns {Function}
  213. */
  214. export function reloadWithStoredParams() {
  215. return (dispatch: Dispatch<*>, getState: Function) => {
  216. const { locationURL } = getState()['features/base/connection'];
  217. const windowLocation = window.location;
  218. const oldSearchString = windowLocation.search;
  219. windowLocation.replace(locationURL.toString());
  220. if (window.self !== window.top
  221. && locationURL.search === oldSearchString) {
  222. // NOTE: Assuming that only the hash or search part of the URL will
  223. // be changed!
  224. // location.reload will not trigger redirect/reload for iframe when
  225. // only the hash params are changed. That's why we need to call
  226. // reload in addition to replace.
  227. windowLocation.reload();
  228. }
  229. };
  230. }