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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { setRoom } from '../base/conference';
  2. import { setLocationURL } from '../base/connection';
  3. import { setConfig } from '../base/config';
  4. import { loadConfig } from '../base/lib-jitsi-meet';
  5. import { parseURIString } from '../base/util';
  6. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
  7. declare var APP: Object;
  8. /**
  9. * Triggers an in-app navigation to a specific route. Allows navigation to be
  10. * abstracted between the mobile/React Native and Web/React applications.
  11. *
  12. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  13. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  14. * scheme, or a mere room name.
  15. * @returns {Function}
  16. */
  17. export function appNavigate(uri: ?string) {
  18. return (dispatch: Dispatch<*>, getState: Function) =>
  19. _appNavigateToOptionalLocation(dispatch, getState, parseURIString(uri));
  20. }
  21. /**
  22. * Triggers an in-app navigation to a specific location URI.
  23. *
  24. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  25. * @param {Function} getState - The redux function that gets/retrieves the redux
  26. * state.
  27. * @param {Object} newLocation - The location URI to navigate to. The value
  28. * cannot be undefined and is assumed to have all properties such as
  29. * {@code host}, {@code contextRoot}, and {@code room} defined. Depending on the
  30. * property, it may have a value equal to {@code undefined} and that may be
  31. * acceptable.
  32. * @private
  33. * @returns {void}
  34. */
  35. function _appNavigateToMandatoryLocation(
  36. dispatch: Dispatch<*>, getState: Function,
  37. newLocation: Object) {
  38. _loadConfig(newLocation)
  39. .then(
  40. config => configLoaded(/* err */ undefined, config),
  41. err => configLoaded(err, /* config */ undefined))
  42. .then(() => dispatch(setRoom(newLocation.room)));
  43. /**
  44. * Notifies that an attempt to load a configuration has completed. Due to
  45. * the asynchronous nature of the loading, the specified <tt>config</tt> may
  46. * or may not be required by the time the notification arrives.
  47. *
  48. * @param {string|undefined} error - If the loading has failed, the error
  49. * detailing the cause of the failure.
  50. * @param {Object|undefined} config - If the loading has succeeded, the
  51. * loaded configuration.
  52. * @returns {void}
  53. */
  54. function configLoaded(error, config) {
  55. // FIXME Due to the asynchronous nature of the loading, the specified
  56. // config may or may not be required by the time the notification
  57. // arrives.
  58. if (error) {
  59. // XXX The failure could be, for example, because of a
  60. // certificate-related error. In which case the connection will
  61. // fail later in Strophe anyway even if we use the default
  62. // config here.
  63. // The function loadConfig will log the err.
  64. return;
  65. }
  66. return (
  67. dispatch(setLocationURL(new URL(newLocation.toString())))
  68. .then(() => dispatch(setConfig(config))));
  69. }
  70. }
  71. /**
  72. * Triggers an in-app navigation to a specific or undefined location (URI).
  73. *
  74. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  75. * @param {Function} getState - The redux function that gets/retrieves the redux
  76. * state.
  77. * @param {Object} location - The location (URI) to navigate to. The value may
  78. * be undefined.
  79. * @private
  80. * @returns {void}
  81. */
  82. function _appNavigateToOptionalLocation(
  83. dispatch: Dispatch<*>, getState: Function,
  84. location: Object) {
  85. // If the specified location (URI) does not identify a host, use the app's
  86. // default.
  87. if (!location || !location.host) {
  88. const defaultLocation
  89. = parseURIString(getState()['features/app'].app._getDefaultURL());
  90. if (location) {
  91. location.host = defaultLocation.host;
  92. // FIXME Turn location's host, hostname, and port properties into
  93. // setters in order to reduce the risks of inconsistent state.
  94. location.hostname = defaultLocation.hostname;
  95. location.port = defaultLocation.port;
  96. location.protocol = defaultLocation.protocol;
  97. } else {
  98. // eslint-disable-next-line no-param-reassign
  99. location = defaultLocation;
  100. }
  101. }
  102. location.protocol || (location.protocol = 'https:');
  103. _appNavigateToMandatoryLocation(dispatch, getState, location);
  104. }
  105. /**
  106. * Signals that a specific App will mount (in the terms of React).
  107. *
  108. * @param {App} app - The App which will mount.
  109. * @returns {{
  110. * type: APP_WILL_MOUNT,
  111. * app: App
  112. * }}
  113. */
  114. export function appWillMount(app) {
  115. return (dispatch: Dispatch<*>) => {
  116. dispatch({
  117. type: APP_WILL_MOUNT,
  118. app
  119. });
  120. // TODO There was a redux action creator appInit which I did not like
  121. // because we already had the redux action creator appWillMount and,
  122. // respectively, the redux action APP_WILL_MOUNT. So I set out to remove
  123. // appInit and managed to move everything it was doing but the
  124. // following. Which is not extremely bad because we haven't moved the
  125. // API module into its own feature yet so we're bound to work on that in
  126. // the future.
  127. typeof APP === 'object' && APP.API.init();
  128. };
  129. }
  130. /**
  131. * Signals that a specific App will unmount (in the terms of React).
  132. *
  133. * @param {App} app - The App which will unmount.
  134. * @returns {{
  135. * type: APP_WILL_UNMOUNT,
  136. * app: App
  137. * }}
  138. */
  139. export function appWillUnmount(app) {
  140. return {
  141. type: APP_WILL_UNMOUNT,
  142. app
  143. };
  144. }
  145. /**
  146. * Loads config.js from a specific host.
  147. *
  148. * @param {Object} location - The location URI which specifies the host to load
  149. * the config.js from.
  150. * @private
  151. * @returns {Promise<Object>}
  152. */
  153. function _loadConfig({ contextRoot, host, protocol, room }) {
  154. /* eslint-disable no-param-reassign */
  155. protocol = protocol.toLowerCase();
  156. // The React Native app supports an app-specific scheme which is sure to not
  157. // be supported by fetch (or whatever loadConfig utilizes).
  158. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  159. // TDOO userinfo
  160. let url = `${protocol}//${host}${contextRoot || '/'}config.js`;
  161. // XXX In order to support multiple shards, tell the room to the deployment.
  162. room && (url += `?room=${room.toLowerCase()}`);
  163. /* eslint-enable no-param-reassign */
  164. return loadConfig(url);
  165. }