Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 7.0KB

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