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

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