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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { setRoom } from '../base/conference';
  2. import { setConfig } from '../base/config';
  3. import { getDomain, setDomain } from '../base/connection';
  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 () => init();
  20. }
  21. /**
  22. * Triggers an in-app navigation to a different route. Allows navigation to be
  23. * abstracted between the mobile and web versions.
  24. *
  25. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  26. * full URL with an http(s) scheme, a full or partial URI with the app-specific
  27. * sheme, or a mere room name.
  28. * @returns {Function}
  29. */
  30. export function appNavigate(uri) {
  31. return (dispatch, getState) => {
  32. const state = getState();
  33. const oldDomain = getDomain(state);
  34. // eslint-disable-next-line prefer-const
  35. let { domain, room } = _parseURIString(uri);
  36. // If the specified URI does not identify a domain, use the app's
  37. // default.
  38. if (typeof domain === 'undefined') {
  39. domain
  40. = _parseURIString(state['features/app'].app._getDefaultURL())
  41. .domain;
  42. }
  43. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  44. // currently in a conference and ask her if she wants to close the
  45. // current conference and start a new one with the new room name or
  46. // domain.
  47. if (typeof domain === 'undefined' || oldDomain === domain) {
  48. dispatchSetRoomAndNavigate();
  49. } else if (oldDomain !== domain) {
  50. // Update domain without waiting for config to be loaded to prevent
  51. // race conditions when we will start to load config multiple times.
  52. dispatch(setDomain(domain));
  53. // If domain has changed, we need to load the config of the new
  54. // domain and set it, and only after that we can navigate to a
  55. // different route.
  56. loadConfig(`https://${domain}`)
  57. .then(
  58. config => configLoaded(/* err */ undefined, config),
  59. err => configLoaded(err, /* config */ undefined))
  60. .then(dispatchSetRoomAndNavigate);
  61. }
  62. /**
  63. * Notifies that an attempt to load the config(uration) of domain has
  64. * completed.
  65. *
  66. * @param {string|undefined} err - If the loading has failed, the error
  67. * detailing the cause of the failure.
  68. * @param {Object|undefined} config - If the loading has succeeded, the
  69. * loaded config(uration).
  70. * @returns {void}
  71. */
  72. function configLoaded(err, config) {
  73. if (err) {
  74. // XXX The failure could be, for example, because of a
  75. // certificate-related error. In which case the connection will
  76. // fail later in Strophe anyway even if we use the default
  77. // config here.
  78. // The function loadConfig will log the err.
  79. return;
  80. }
  81. dispatch(setConfig(config));
  82. }
  83. /**
  84. * Dispatches _setRoomAndNavigate in the Redux store.
  85. *
  86. * @returns {void}
  87. */
  88. function dispatchSetRoomAndNavigate() {
  89. // If both domain and room vars became undefined, that means we're
  90. // actually dealing with just room name and not with URL.
  91. dispatch(
  92. _setRoomAndNavigate(
  93. typeof room === 'undefined' && typeof domain === 'undefined'
  94. ? uri
  95. : room));
  96. }
  97. };
  98. }
  99. /**
  100. * Signals that a specific App will mount (in the terms of React).
  101. *
  102. * @param {App} app - The App which will mount.
  103. * @returns {{
  104. * type: APP_WILL_MOUNT,
  105. * app: App
  106. * }}
  107. */
  108. export function appWillMount(app) {
  109. return {
  110. type: APP_WILL_MOUNT,
  111. app
  112. };
  113. }
  114. /**
  115. * Signals that a specific App will unmount (in the terms of React).
  116. *
  117. * @param {App} app - The App which will unmount.
  118. * @returns {{
  119. * type: APP_WILL_UNMOUNT,
  120. * app: App
  121. * }}
  122. */
  123. export function appWillUnmount(app) {
  124. return {
  125. type: APP_WILL_UNMOUNT,
  126. app
  127. };
  128. }
  129. /**
  130. * Navigates to a route in accord with a specific Redux state.
  131. *
  132. * @param {Object} state - The Redux state which determines/identifies the route
  133. * to navigate to.
  134. * @private
  135. * @returns {void}
  136. */
  137. function _navigate(state) {
  138. const app = state['features/app'].app;
  139. const routeToRender = _getRouteToRender(state);
  140. app._navigate(routeToRender);
  141. }
  142. /**
  143. * Sets room and navigates to new route if needed.
  144. *
  145. * @param {string} newRoom - New room name.
  146. * @private
  147. * @returns {Function}
  148. */
  149. function _setRoomAndNavigate(newRoom) {
  150. return (dispatch, getState) => {
  151. dispatch(setRoom(newRoom));
  152. _navigate(getState());
  153. };
  154. }