Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

actions.js 9.8KB

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