Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { setRoom } from '../base/conference';
  2. import { getDomain, setDomain } from '../base/connection';
  3. import { loadConfig, setConfig } from '../base/lib-jitsi-meet';
  4. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
  5. import {
  6. _getRoomAndDomainFromUrlString,
  7. _getRouteToRender,
  8. areRoutesEqual,
  9. init
  10. } from './functions';
  11. import './reducer';
  12. /**
  13. * Variable saving current route of the app. Later it will be extracted as
  14. * a property of the class with Router specific logic.
  15. *
  16. * @type {Object}
  17. */
  18. let currentRoute = {};
  19. /**
  20. * Temporary solution. Should dispatch actions related to initial settings of
  21. * the app like setting log levels, reading the config parameters from query
  22. * string etc.
  23. *
  24. * @returns {Function}
  25. */
  26. export function appInit() {
  27. return () => init();
  28. }
  29. /**
  30. * Triggers an in-app navigation to a different route. Allows navigation to be
  31. * abstracted between the mobile and web versions.
  32. *
  33. * @param {(string|undefined)} urlOrRoom - The URL or room name to which to
  34. * navigate.
  35. * @returns {Function}
  36. */
  37. export function appNavigate(urlOrRoom) {
  38. return (dispatch, getState) => {
  39. const state = getState();
  40. const oldDomain = getDomain(state);
  41. const { domain, room } = _getRoomAndDomainFromUrlString(urlOrRoom);
  42. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  43. // currently in a conference and ask her if she wants to close the
  44. // current conference and start a new one with the new room name or
  45. // domain.
  46. if (typeof domain === 'undefined' || oldDomain === domain) {
  47. // If both domain and room vars became undefined, that means we're
  48. // actually dealing with just room name and not with URL.
  49. dispatch(
  50. _setRoomAndNavigate(
  51. typeof room === 'undefined' && typeof domain === 'undefined'
  52. ? urlOrRoom
  53. : room));
  54. } else if (oldDomain !== domain) {
  55. // Update domain without waiting for config to be loaded to prevent
  56. // race conditions when we will start to load config multiple times.
  57. dispatch(setDomain(domain));
  58. // If domain has changed, we need to load the config of the new
  59. // domain and set it, and only after that we can navigate to
  60. // different route.
  61. loadConfig(`https://${domain}`)
  62. .then(
  63. config => configLoaded(/* err */ undefined, config),
  64. err => configLoaded(err, /* config */ undefined))
  65. .then(() => {
  66. const link = typeof room === 'undefined'
  67. && typeof domain === 'undefined'
  68. ? urlOrRoom
  69. : room;
  70. dispatch(_setRoomAndNavigate(link));
  71. });
  72. }
  73. /**
  74. * Notifies that an attempt to load the config(uration) of domain has
  75. * completed.
  76. *
  77. * @param {string|undefined} err - If the loading has failed, the error
  78. * detailing the cause of the failure.
  79. * @param {Object|undefined} config - If the loading has succeeded, the
  80. * loaded config(uration).
  81. * @returns {void}
  82. */
  83. function configLoaded(err, config) {
  84. if (err) {
  85. // XXX The failure could be, for example, because of a
  86. // certificate-related error. In which case the connection will
  87. // fail later in Strophe anyway even if we use the default
  88. // config here.
  89. // The function loadConfig will log the err.
  90. return;
  91. }
  92. dispatch(setConfig(config));
  93. }
  94. };
  95. }
  96. /**
  97. * Signals that a specific App will mount (in the terms of React).
  98. *
  99. * @param {App} app - The App which will mount.
  100. * @returns {{
  101. * type: APP_WILL_MOUNT,
  102. * app: App
  103. * }}
  104. */
  105. export function appWillMount(app) {
  106. return {
  107. type: APP_WILL_MOUNT,
  108. app
  109. };
  110. }
  111. /**
  112. * Signals that a specific App will unmount (in the terms of React).
  113. *
  114. * @param {App} app - The App which will unmount.
  115. * @returns {{
  116. * type: APP_WILL_UNMOUNT,
  117. * app: App
  118. * }}
  119. */
  120. export function appWillUnmount(app) {
  121. return {
  122. type: APP_WILL_UNMOUNT,
  123. app
  124. };
  125. }
  126. /**
  127. * Sets room and navigates to new route if needed.
  128. *
  129. * @param {string} newRoom - New room name.
  130. * @private
  131. * @returns {Function}
  132. */
  133. function _setRoomAndNavigate(newRoom) {
  134. return (dispatch, getState) => {
  135. dispatch(setRoom(newRoom));
  136. const state = getState();
  137. const { app } = state['features/app'];
  138. const newRoute = _getRouteToRender(state);
  139. if (!areRoutesEqual(newRoute, currentRoute)) {
  140. currentRoute = newRoute;
  141. app._navigate(newRoute);
  142. }
  143. };
  144. }