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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { setRoom } from '../base/conference';
  2. import { getDomain, setDomain } from '../base/connection';
  3. import { loadConfig, setConfig } from '../base/lib-jitsi-meet';
  4. import {
  5. detectAndroid,
  6. detectIOS
  7. } from '../base/util';
  8. import {
  9. APP_WILL_MOUNT,
  10. APP_WILL_UNMOUNT,
  11. APP_SET_PLATFORM
  12. } from './actionTypes';
  13. import {
  14. _getRoomAndDomainFromUrlString,
  15. _getRouteToRender,
  16. init
  17. } from './functions';
  18. import './reducer';
  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 (room === 'mobile-app') {
  47. return;
  48. } else if (typeof domain === 'undefined' || oldDomain === domain) {
  49. // If both domain and room vars became undefined, that means we're
  50. // actually dealing with just room name and not with URL.
  51. dispatch(
  52. _setRoomAndNavigate(
  53. typeof room === 'undefined' && typeof domain === 'undefined'
  54. ? urlOrRoom
  55. : room));
  56. } else if (oldDomain !== domain) {
  57. // Update domain without waiting for config to be loaded to prevent
  58. // race conditions when we will start to load config multiple times.
  59. dispatch(setDomain(domain));
  60. // If domain has changed, we need to load the config of the new
  61. // domain and set it, and only after that we can navigate to
  62. // different route.
  63. loadConfig(`https://${domain}`)
  64. .then(
  65. config => configLoaded(/* err */ undefined, config),
  66. err => configLoaded(err, /* config */ undefined));
  67. }
  68. /**
  69. * Notifies that an attempt to load the config(uration) of domain has
  70. * completed.
  71. *
  72. * @param {string|undefined} err - If the loading has failed, the error
  73. * detailing the cause of the failure.
  74. * @param {Object|undefined} config - If the loading has succeeded, the
  75. * loaded config(uration).
  76. * @returns {void}
  77. */
  78. function configLoaded(err, config) {
  79. if (err) {
  80. // XXX The failure could be, for example, because of a
  81. // certificate-related error. In which case the connection will
  82. // fail later in Strophe anyway even if we use the default
  83. // config here.
  84. // The function loadConfig will log the err.
  85. return;
  86. }
  87. // We set room name only here to prevent race conditions on app
  88. // start to not make app re-render conference page for two times.
  89. dispatch(setRoom(room));
  90. dispatch(setConfig(config));
  91. }
  92. };
  93. }
  94. /**
  95. * Signals that a specific App will mount (in the terms of React).
  96. *
  97. * @param {App} app - The App which will mount.
  98. * @returns {{
  99. * type: APP_WILL_MOUNT,
  100. * app: App
  101. * }}
  102. */
  103. export function appWillMount(app) {
  104. return {
  105. type: APP_WILL_MOUNT,
  106. app
  107. };
  108. }
  109. /**
  110. * Signals that a specific App will unmount (in the terms of React).
  111. *
  112. * @param {App} app - The App which will unmount.
  113. * @returns {{
  114. * type: APP_WILL_UNMOUNT,
  115. * app: App
  116. * }}
  117. */
  118. export function appWillUnmount(app) {
  119. return {
  120. type: APP_WILL_UNMOUNT,
  121. app
  122. };
  123. }
  124. /**
  125. * Detects the platform of user agent and signals that platform detected.
  126. *
  127. * @returns {Function}
  128. */
  129. export function detectPlatform() {
  130. return dispatch => {
  131. if (detectAndroid()) {
  132. dispatch(_setPlatform('android'));
  133. } else if (detectIOS()) {
  134. dispatch(_setPlatform('ios'));
  135. }
  136. };
  137. }
  138. /**
  139. * Navigates to route corresponding to current room name.
  140. *
  141. * @param {Object} state - Redux state.
  142. * @private
  143. * @returns {void}
  144. */
  145. function _navigate(state) {
  146. const app = state['features/app'].app;
  147. const routeToRender = _getRouteToRender(state);
  148. app._navigate(routeToRender);
  149. }
  150. /**
  151. * Signals that user agent platform is mobile and it has been already detected.
  152. *
  153. * @param {string} platform - Mobile user agent platform.
  154. * @returns {{
  155. * type: APP_SET_PLATFORM,
  156. * platform: string
  157. * }}
  158. * @private
  159. */
  160. function _setPlatform(platform) {
  161. return {
  162. type: APP_SET_PLATFORM,
  163. platform
  164. };
  165. }
  166. /**
  167. * Sets room and navigates to new route if needed.
  168. *
  169. * @param {string} newRoom - New room name.
  170. * @private
  171. * @returns {Function}
  172. */
  173. function _setRoomAndNavigate(newRoom) {
  174. return (dispatch, getState) => {
  175. const oldRoom = getState()['features/base/conference'].room;
  176. dispatch(setRoom(newRoom));
  177. const state = getState();
  178. const { platform } = state['features/app'];
  179. const { room } = state['features/base/conference'];
  180. const { landingIsShown } = state['features/landing'];
  181. // If user agent is mobile browser and landing wasn't shown we
  182. // should recheck which component to render.
  183. if ((platform && !landingIsShown) || room !== oldRoom) {
  184. _navigate(state);
  185. }
  186. };
  187. }