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

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