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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. 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. dispatchSetLocationURL();
  75. dispatch(setConfig(config));
  76. }
  77. /**
  78. * Dispatches {@link setLocationURL} in the redux store.
  79. *
  80. * @returns {void}
  81. */
  82. function dispatchSetLocationURL() {
  83. dispatch(setLocationURL(new URL(newLocation.toString())));
  84. }
  85. /**
  86. * Dispatches {@link _setRoomAndNavigate} in the redux store.
  87. *
  88. * @returns {void}
  89. */
  90. function dispatchSetRoom() {
  91. dispatch(setRoom(newLocation.room));
  92. }
  93. }
  94. /**
  95. * Triggers an in-app navigation to a specific or undefined location (URI).
  96. *
  97. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  98. * @param {Function} getState - The redux function that gets/retrieves the redux
  99. * state.
  100. * @param {Object} location - The location (URI) to navigate to. The value may
  101. * be undefined.
  102. * @private
  103. * @returns {void}
  104. */
  105. function _appNavigateToOptionalLocation(
  106. dispatch: Dispatch<*>, getState: Function,
  107. location: Object) {
  108. // If the specified location (URI) does not identify a host, use the app's
  109. // default.
  110. if (!location || !location.host) {
  111. const defaultLocation
  112. = parseURIString(getState()['features/app'].app._getDefaultURL());
  113. if (location) {
  114. location.host = defaultLocation.host;
  115. // FIXME Turn location's host, hostname, and port properties into
  116. // setters in order to reduce the risks of inconsistent state.
  117. location.hostname = defaultLocation.hostname;
  118. location.port = defaultLocation.port;
  119. location.protocol = defaultLocation.protocol;
  120. } else {
  121. // eslint-disable-next-line no-param-reassign
  122. location = defaultLocation;
  123. }
  124. }
  125. location.protocol || (location.protocol = 'https:');
  126. _appNavigateToMandatoryLocation(dispatch, getState, location);
  127. }
  128. /**
  129. * Signals that a specific App will mount (in the terms of React).
  130. *
  131. * @param {App} app - The App which will mount.
  132. * @returns {{
  133. * type: APP_WILL_MOUNT,
  134. * app: App
  135. * }}
  136. */
  137. export function appWillMount(app) {
  138. return (dispatch: Dispatch<*>) => {
  139. dispatch({
  140. type: APP_WILL_MOUNT,
  141. app
  142. });
  143. // TODO There was a redux action creator appInit which I did not like
  144. // because we already had the redux action creator appWillMount and,
  145. // respectively, the redux action APP_WILL_MOUNT. So I set out to remove
  146. // appInit and managed to move everything it was doing but the
  147. // following. Which is not extremely bad because we haven't moved the
  148. // API module into its own feature yet so we're bound to work on that in
  149. // the future.
  150. typeof APP === 'object' && APP.API.init();
  151. };
  152. }
  153. /**
  154. * Signals that a specific App will unmount (in the terms of React).
  155. *
  156. * @param {App} app - The App which will unmount.
  157. * @returns {{
  158. * type: APP_WILL_UNMOUNT,
  159. * app: App
  160. * }}
  161. */
  162. export function appWillUnmount(app) {
  163. return {
  164. type: APP_WILL_UNMOUNT,
  165. app
  166. };
  167. }
  168. /**
  169. * Loads config.js from a specific host.
  170. *
  171. * @param {Object} location - The location URI which specifies the host to load
  172. * the config.js from.
  173. * @returns {Promise<Object>}
  174. */
  175. function _loadConfig(location: Object) {
  176. let protocol = location.protocol.toLowerCase();
  177. // The React Native app supports an app-specific scheme which is sure to not
  178. // be supported by fetch (or whatever loadConfig utilizes).
  179. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  180. // TDOO userinfo
  181. return (
  182. loadConfig(
  183. `${protocol}//${location.host}${location.contextRoot || '/'}`));
  184. }