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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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} and {@code room} defined values.
  30. * @private
  31. * @returns {void}
  32. */
  33. function _appNavigateToMandatoryLocation(
  34. dispatch: Dispatch<*>, getState: Function,
  35. newLocation: Object) {
  36. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  37. // currently in a conference and ask her if she wants to close the
  38. // current conference and start a new one with the new room name or
  39. // domain.
  40. const oldLocationURL = getState()['features/base/connection'].locationURL;
  41. const oldHost = oldLocationURL ? oldLocationURL.host : undefined;
  42. const newHost = newLocation.host;
  43. if (oldHost === newHost) {
  44. dispatchSetLocationURL()
  45. .then(dispatchSetRoom);
  46. } else {
  47. // If the host has changed, we need to load the config of the new host
  48. // and set it, and only after that we can navigate to a different route.
  49. _loadConfig(newLocation)
  50. .then(
  51. config => configLoaded(/* err */ undefined, config),
  52. err => configLoaded(err, /* config */ undefined))
  53. .then(dispatchSetRoom);
  54. }
  55. /**
  56. * Notifies that an attempt to load the config(uration) of domain has
  57. * completed.
  58. *
  59. * @param {string|undefined} err - If the loading has failed, the error
  60. * detailing the cause of the failure.
  61. * @param {Object|undefined} config - If the loading has succeeded, the
  62. * loaded config(uration).
  63. * @returns {void}
  64. */
  65. function configLoaded(err, config) {
  66. if (err) {
  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 even if we use the default
  70. // config here.
  71. // The function loadConfig will log the err.
  72. return;
  73. }
  74. return (
  75. dispatchSetLocationURL()
  76. .then(() => dispatch(setConfig(config))));
  77. }
  78. /**
  79. * Dispatches {@link setLocationURL} in the redux store.
  80. *
  81. * @returns {void}
  82. */
  83. function dispatchSetLocationURL() {
  84. return dispatch(setLocationURL(new URL(newLocation.toString())));
  85. }
  86. /**
  87. * Dispatches {@link _setRoomAndNavigate} in the redux store.
  88. *
  89. * @returns {void}
  90. */
  91. function dispatchSetRoom() {
  92. return dispatch(setRoom(newLocation.room));
  93. }
  94. }
  95. /**
  96. * Triggers an in-app navigation to a specific or undefined location (URI).
  97. *
  98. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  99. * @param {Function} getState - The redux function that gets/retrieves the redux
  100. * state.
  101. * @param {Object} location - The location (URI) to navigate to. The value may
  102. * be undefined.
  103. * @private
  104. * @returns {void}
  105. */
  106. function _appNavigateToOptionalLocation(
  107. dispatch: Dispatch<*>, getState: Function,
  108. location: Object) {
  109. // If the specified location (URI) does not identify a host, use the app's
  110. // default.
  111. if (!location || !location.host) {
  112. const defaultLocation
  113. = parseURIString(getState()['features/app'].app._getDefaultURL());
  114. if (location) {
  115. location.host = defaultLocation.host;
  116. // FIXME Turn location's host, hostname, and port properties into
  117. // setters in order to reduce the risks of inconsistent state.
  118. location.hostname = defaultLocation.hostname;
  119. location.port = defaultLocation.port;
  120. location.protocol = defaultLocation.protocol;
  121. } else {
  122. // eslint-disable-next-line no-param-reassign
  123. location = defaultLocation;
  124. }
  125. }
  126. location.protocol || (location.protocol = 'https:');
  127. _appNavigateToMandatoryLocation(dispatch, getState, location);
  128. }
  129. /**
  130. * Signals that a specific App will mount (in the terms of React).
  131. *
  132. * @param {App} app - The App which will mount.
  133. * @returns {{
  134. * type: APP_WILL_MOUNT,
  135. * app: App
  136. * }}
  137. */
  138. export function appWillMount(app) {
  139. return (dispatch: Dispatch<*>) => {
  140. dispatch({
  141. type: APP_WILL_MOUNT,
  142. app
  143. });
  144. // TODO There was a redux action creator appInit which I did not like
  145. // because we already had the redux action creator appWillMount and,
  146. // respectively, the redux action APP_WILL_MOUNT. So I set out to remove
  147. // appInit and managed to move everything it was doing but the
  148. // following. Which is not extremely bad because we haven't moved the
  149. // API module into its own feature yet so we're bound to work on that in
  150. // the future.
  151. typeof APP === 'object' && APP.API.init();
  152. };
  153. }
  154. /**
  155. * Signals that a specific App will unmount (in the terms of React).
  156. *
  157. * @param {App} app - The App which will unmount.
  158. * @returns {{
  159. * type: APP_WILL_UNMOUNT,
  160. * app: App
  161. * }}
  162. */
  163. export function appWillUnmount(app) {
  164. return {
  165. type: APP_WILL_UNMOUNT,
  166. app
  167. };
  168. }
  169. /**
  170. * Loads config.js from a specific host.
  171. *
  172. * @param {Object} location - The location URI which specifies the host to load
  173. * the config.js from.
  174. * @returns {Promise<Object>}
  175. */
  176. function _loadConfig(location: Object) {
  177. let protocol = location.protocol.toLowerCase();
  178. // The React Native app supports an app-specific scheme which is sure to not
  179. // be supported by fetch (or whatever loadConfig utilizes).
  180. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  181. // TDOO userinfo
  182. return (
  183. loadConfig(
  184. `${protocol}//${location.host}${location.contextRoot || '/'}`));
  185. }