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

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