Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

actions.js 7.7KB

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