您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 6.6KB

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