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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 {
  7. _getRouteToRender,
  8. _parseURIString,
  9. init
  10. } from './functions';
  11. /**
  12. * Temporary solution. Should dispatch actions related to initial settings of
  13. * the app like setting log levels, reading the config parameters from query
  14. * string etc.
  15. *
  16. * @returns {Function}
  17. */
  18. export function appInit() {
  19. return (dispatch: Dispatch<*>, getState: Function) =>
  20. init(getState());
  21. }
  22. /**
  23. * Triggers an in-app navigation to a specific route. Allows navigation to be
  24. * abstracted between the mobile/React Native and Web/React applications.
  25. *
  26. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  27. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  28. * sheme, or a mere room name.
  29. * @returns {Function}
  30. */
  31. export function appNavigate(uri: ?string) {
  32. return (dispatch: Dispatch<*>, getState: Function) =>
  33. _appNavigateToOptionalLocation(
  34. dispatch, getState,
  35. _parseURIString(uri));
  36. }
  37. /**
  38. * Triggers an in-app navigation to a specific location URI.
  39. *
  40. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  41. * @param {Function} getState - The redux function that gets/retrieves the redux
  42. * state.
  43. * @param {Object} newLocation - The location URI to navigate to. The value
  44. * cannot be undefined and is assumed to have all properties such as
  45. * {@code host} and {@code room} defined values.
  46. * @private
  47. * @returns {void}
  48. */
  49. function _appNavigateToMandatoryLocation(
  50. dispatch: Dispatch<*>, getState: Function,
  51. newLocation: Object) {
  52. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  53. // currently in a conference and ask her if she wants to close the
  54. // current conference and start a new one with the new room name or
  55. // domain.
  56. const oldLocationURL = getState()['features/base/connection'].locationURL;
  57. const oldHost = oldLocationURL ? oldLocationURL.host : undefined;
  58. const newHost = newLocation.host;
  59. if (oldHost === newHost) {
  60. dispatchSetLocationURL();
  61. dispatchSetRoomAndNavigate();
  62. } else {
  63. // If the host has changed, we need to load the config of the new host
  64. // and set it, and only after that we can navigate to a different route.
  65. _loadConfig(newLocation)
  66. .then(
  67. config => configLoaded(/* err */ undefined, config),
  68. err => configLoaded(err, /* config */ undefined))
  69. .then(dispatchSetRoomAndNavigate);
  70. }
  71. /**
  72. * Notifies that an attempt to load the config(uration) of domain has
  73. * completed.
  74. *
  75. * @param {string|undefined} err - If the loading has failed, the error
  76. * detailing the cause of the failure.
  77. * @param {Object|undefined} config - If the loading has succeeded, the
  78. * loaded config(uration).
  79. * @returns {void}
  80. */
  81. function configLoaded(err, config) {
  82. if (err) {
  83. // XXX The failure could be, for example, because of a
  84. // certificate-related error. In which case the connection will
  85. // fail later in Strophe anyway even if we use the default
  86. // config here.
  87. // The function loadConfig will log the err.
  88. return;
  89. }
  90. dispatchSetLocationURL();
  91. dispatch(setConfig(config));
  92. }
  93. /**
  94. * Dispatches {@link setLocationURL} in the redux store.
  95. *
  96. * @returns {void}
  97. */
  98. function dispatchSetLocationURL() {
  99. dispatch(
  100. setLocationURL(
  101. new URL(
  102. (newLocation.protocol || 'https:')
  103. // TODO userinfo
  104. + newLocation.host
  105. + (newLocation.pathname || '/')
  106. + newLocation.search
  107. + newLocation.hash)));
  108. }
  109. /**
  110. * Dispatches {@link _setRoomAndNavigate} in the redux store.
  111. *
  112. * @returns {void}
  113. */
  114. function dispatchSetRoomAndNavigate() {
  115. dispatch(_setRoomAndNavigate(newLocation.room));
  116. }
  117. }
  118. /**
  119. * Triggers an in-app navigation to a specific or undefined location (URI).
  120. *
  121. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  122. * @param {Function} getState - The redux function that gets/retrieves the redux
  123. * state.
  124. * @param {Object} location - The location (URI) to navigate to. The value may
  125. * be undefined.
  126. * @private
  127. * @returns {void}
  128. */
  129. function _appNavigateToOptionalLocation(
  130. dispatch: Dispatch<*>, getState: Function,
  131. location: Object) {
  132. // If the specified location (URI) does not identify a host, use the app's
  133. // default.
  134. if (!location || !location.host) {
  135. const defaultLocation
  136. = _parseURIString(getState()['features/app'].app._getDefaultURL());
  137. if (location) {
  138. location.host = defaultLocation.host;
  139. // FIXME Turn location's host, hostname, and port properties into
  140. // setters in order to reduce the risks of inconsistent state.
  141. location.hostname = defaultLocation.hostname;
  142. location.port = defaultLocation.port;
  143. location.protocol = defaultLocation.protocol;
  144. } else {
  145. // eslint-disable-next-line no-param-reassign
  146. location = defaultLocation;
  147. }
  148. }
  149. if (!location.protocol) {
  150. location.protocol = 'https:';
  151. }
  152. _appNavigateToMandatoryLocation(dispatch, getState, location);
  153. }
  154. /**
  155. * Signals that a specific App will mount (in the terms of React).
  156. *
  157. * @param {App} app - The App which will mount.
  158. * @returns {{
  159. * type: APP_WILL_MOUNT,
  160. * app: App
  161. * }}
  162. */
  163. export function appWillMount(app) {
  164. return {
  165. type: APP_WILL_MOUNT,
  166. app
  167. };
  168. }
  169. /**
  170. * Signals that a specific App will unmount (in the terms of React).
  171. *
  172. * @param {App} app - The App which will unmount.
  173. * @returns {{
  174. * type: APP_WILL_UNMOUNT,
  175. * app: App
  176. * }}
  177. */
  178. export function appWillUnmount(app) {
  179. return {
  180. type: APP_WILL_UNMOUNT,
  181. app
  182. };
  183. }
  184. /**
  185. * Loads config.js from a specific host.
  186. *
  187. * @param {Object} location - The loction URI which specifies the host to load
  188. * the config.js from.
  189. * @returns {Promise<Object>}
  190. */
  191. function _loadConfig(location: Object) {
  192. let protocol = location.protocol.toLowerCase();
  193. // The React Native app supports an app-specific scheme which is sure to not
  194. // be supported by fetch (or whatever loadConfig utilizes).
  195. if (protocol !== 'http:' && protocol !== 'https:') {
  196. protocol = 'https:';
  197. }
  198. // TDOO userinfo
  199. return loadConfig(protocol + location.host + (location.contextRoot || '/'));
  200. }
  201. /**
  202. * Navigates to a route in accord with a specific Redux state.
  203. *
  204. * @param {Object} state - The Redux state which determines/identifies the route
  205. * to navigate to.
  206. * @private
  207. * @returns {void}
  208. */
  209. function _navigate(state) {
  210. const app = state['features/app'].app;
  211. const routeToRender = _getRouteToRender(state);
  212. app._navigate(routeToRender);
  213. }
  214. /**
  215. * Sets room and navigates to new route if needed.
  216. *
  217. * @param {string} newRoom - New room name.
  218. * @private
  219. * @returns {Function}
  220. */
  221. function _setRoomAndNavigate(newRoom) {
  222. return (dispatch, getState) => {
  223. dispatch(setRoom(newRoom));
  224. _navigate(getState());
  225. };
  226. }