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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { setRoom } from '../base/conference';
  4. import {
  5. configWillLoad,
  6. createFakeConfig,
  7. loadConfigError,
  8. restoreConfig,
  9. setConfig,
  10. storeConfig
  11. } from '../base/config';
  12. import { connect, disconnect, setLocationURL } from '../base/connection';
  13. import { loadConfig } from '../base/lib-jitsi-meet';
  14. import { MEDIA_TYPE } from '../base/media';
  15. import { toState } from '../base/redux';
  16. import { createDesiredLocalTracks, isLocalVideoTrackMuted, isLocalTrackMuted } from '../base/tracks';
  17. import {
  18. addHashParamsToURL,
  19. getBackendSafeRoomName,
  20. getLocationContextRoot,
  21. parseURIString,
  22. toURLString
  23. } from '../base/util';
  24. import { showNotification } from '../notifications';
  25. import { setFatalError } from '../overlay';
  26. import {
  27. getDefaultURL,
  28. getName
  29. } from './functions';
  30. import logger from './logger';
  31. declare var APP: Object;
  32. declare var interfaceConfig: Object;
  33. /**
  34. * Triggers an in-app navigation to a specific route. Allows navigation to be
  35. * abstracted between the mobile/React Native and Web/React applications.
  36. *
  37. * @param {string|undefined} uri - The URI to which to navigate. It may be a
  38. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  39. * scheme, or a mere room name.
  40. * @returns {Function}
  41. */
  42. export function appNavigate(uri: ?string) {
  43. return async (dispatch: Dispatch<any>, getState: Function) => {
  44. let location = parseURIString(uri);
  45. // If the specified location (URI) does not identify a host, use the app's
  46. // default.
  47. if (!location || !location.host) {
  48. const defaultLocation = parseURIString(getDefaultURL(getState));
  49. if (location) {
  50. location.host = defaultLocation.host;
  51. // FIXME Turn location's host, hostname, and port properties into
  52. // setters in order to reduce the risks of inconsistent state.
  53. location.hostname = defaultLocation.hostname;
  54. location.pathname
  55. = defaultLocation.pathname + location.pathname.substr(1);
  56. location.port = defaultLocation.port;
  57. location.protocol = defaultLocation.protocol;
  58. } else {
  59. location = defaultLocation;
  60. }
  61. }
  62. location.protocol || (location.protocol = 'https:');
  63. const { contextRoot, host, room } = location;
  64. const locationURL = new URL(location.toString());
  65. // Disconnect from any current conference.
  66. // FIXME: unify with web.
  67. if (navigator.product === 'ReactNative') {
  68. dispatch(disconnect());
  69. }
  70. dispatch(configWillLoad(locationURL, room));
  71. let protocol = location.protocol.toLowerCase();
  72. // The React Native app supports an app-specific scheme which is sure to not
  73. // be supported by fetch.
  74. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  75. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  76. let url = `${baseURL}config.js`;
  77. // XXX In order to support multiple shards, tell the room to the deployment.
  78. room && (url += `?room=${getBackendSafeRoomName(room)}`);
  79. let config;
  80. // Avoid (re)loading the config when there is no room.
  81. if (!room) {
  82. config = restoreConfig(baseURL);
  83. }
  84. if (!config) {
  85. try {
  86. config = await loadConfig(url);
  87. dispatch(storeConfig(baseURL, config));
  88. } catch (error) {
  89. config = restoreConfig(baseURL);
  90. if (!config) {
  91. if (room) {
  92. dispatch(loadConfigError(error, locationURL));
  93. return;
  94. }
  95. // If there is no room (we are on the welcome page), don't fail, just create a fake one.
  96. logger.warn('Failed to load config but there is no room, applying a fake one');
  97. config = createFakeConfig(baseURL);
  98. }
  99. }
  100. }
  101. if (getState()['features/base/config'].locationURL !== locationURL) {
  102. dispatch(loadConfigError(new Error('Config no longer needed!'), locationURL));
  103. return;
  104. }
  105. dispatch(setLocationURL(locationURL));
  106. dispatch(setConfig(config));
  107. dispatch(setRoom(room));
  108. // FIXME: unify with web, currently the connection and track creation happens in conference.js.
  109. if (room && navigator.product === 'ReactNative') {
  110. dispatch(createDesiredLocalTracks());
  111. dispatch(connect());
  112. }
  113. };
  114. }
  115. /**
  116. * Redirects to another page generated by replacing the path in the original URL
  117. * with the given path.
  118. *
  119. * @param {(string)} pathname - The path to navigate to.
  120. * @returns {Function}
  121. */
  122. export function redirectWithStoredParams(pathname: string) {
  123. return (dispatch: Dispatch<any>, getState: Function) => {
  124. const { locationURL } = getState()['features/base/connection'];
  125. const newLocationURL = new URL(locationURL.href);
  126. newLocationURL.pathname = pathname;
  127. window.location.assign(newLocationURL.toString());
  128. };
  129. }
  130. /**
  131. * Assigns a specific pathname to window.location.pathname taking into account
  132. * the context root of the Web app.
  133. *
  134. * @param {string} pathname - The pathname to assign to
  135. * window.location.pathname. If the specified pathname is relative, the context
  136. * root of the Web app will be prepended to the specified pathname before
  137. * assigning it to window.location.pathname.
  138. * @returns {Function}
  139. */
  140. export function redirectToStaticPage(pathname: string) {
  141. return () => {
  142. const windowLocation = window.location;
  143. let newPathname = pathname;
  144. if (!newPathname.startsWith('/')) {
  145. // A pathname equal to ./ specifies the current directory. It will be
  146. // fine but pointless to include it because contextRoot is the current
  147. // directory.
  148. newPathname.startsWith('./')
  149. && (newPathname = newPathname.substring(2));
  150. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  151. }
  152. windowLocation.pathname = newPathname;
  153. };
  154. }
  155. /**
  156. * Reloads the page.
  157. *
  158. * @protected
  159. * @returns {Function}
  160. */
  161. export function reloadNow() {
  162. return (dispatch: Dispatch<Function>, getState: Function) => {
  163. dispatch(setFatalError(undefined));
  164. const state = getState();
  165. const { locationURL } = state['features/base/connection'];
  166. // Preserve the local tracks muted state after the reload.
  167. const newURL = addTrackStateToURL(locationURL, state);
  168. logger.info(`Reloading the conference using URL: ${locationURL}`);
  169. if (navigator.product === 'ReactNative') {
  170. dispatch(appNavigate(toURLString(newURL)));
  171. } else {
  172. dispatch(reloadWithStoredParams());
  173. }
  174. };
  175. }
  176. /**
  177. * Adds the current track state to the passed URL.
  178. *
  179. * @param {URL} url - The URL that will be modified.
  180. * @param {Function|Object} stateful - The redux store or {@code getState} function.
  181. * @returns {URL} - Returns the modified URL.
  182. */
  183. function addTrackStateToURL(url, stateful) {
  184. const state = toState(stateful);
  185. const tracks = state['features/base/tracks'];
  186. const isVideoMuted = isLocalVideoTrackMuted(tracks);
  187. const isAudioMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  188. return addHashParamsToURL(new URL(url), { // use new URL object in order to not pollute the passed parameter.
  189. 'config.startWithAudioMuted': isAudioMuted,
  190. 'config.startWithVideoMuted': isVideoMuted
  191. });
  192. }
  193. /**
  194. * Reloads the page by restoring the original URL.
  195. *
  196. * @returns {Function}
  197. */
  198. export function reloadWithStoredParams() {
  199. return (dispatch: Dispatch<any>, getState: Function) => {
  200. const state = getState();
  201. const { locationURL } = state['features/base/connection'];
  202. // Preserve the local tracks muted states.
  203. const newURL = addTrackStateToURL(locationURL, state);
  204. const windowLocation = window.location;
  205. const oldSearchString = windowLocation.search;
  206. windowLocation.replace(newURL.toString());
  207. if (newURL.search === oldSearchString) {
  208. // NOTE: Assuming that only the hash or search part of the URL will
  209. // be changed!
  210. // location.replace will not trigger redirect/reload when
  211. // only the hash params are changed. That's why we need to call
  212. // reload in addition to replace.
  213. windowLocation.reload();
  214. }
  215. };
  216. }
  217. /**
  218. * Check if the welcome page is enabled and redirects to it.
  219. * If requested show a thank you dialog before that.
  220. * If we have a close page enabled, redirect to it without
  221. * showing any other dialog.
  222. *
  223. * @param {Object} options - Used to decide which particular close page to show
  224. * or if close page is disabled, whether we should show the thankyou dialog.
  225. * @param {boolean} options.showThankYou - Whether we should
  226. * show thank you dialog.
  227. * @param {boolean} options.feedbackSubmitted - Whether feedback was submitted.
  228. * @returns {Function}
  229. */
  230. export function maybeRedirectToWelcomePage(options: Object = {}) {
  231. return (dispatch: Dispatch<any>, getState: Function) => {
  232. const {
  233. enableClosePage
  234. } = getState()['features/base/config'];
  235. // if close page is enabled redirect to it, without further action
  236. if (enableClosePage) {
  237. const { isGuest } = getState()['features/base/jwt'];
  238. // save whether current user is guest or not, before navigating
  239. // to close page
  240. window.sessionStorage.setItem('guest', isGuest);
  241. let path = 'close.html';
  242. if (interfaceConfig.SHOW_PROMOTIONAL_CLOSE_PAGE) {
  243. path = 'close3.html';
  244. } else if (!options.feedbackSubmitted) {
  245. path = 'close2.html';
  246. }
  247. dispatch(redirectToStaticPage(`static/${path}`));
  248. return;
  249. }
  250. // else: show thankYou dialog only if there is no feedback
  251. if (options.showThankYou) {
  252. dispatch(showNotification({
  253. titleArguments: { appName: getName() },
  254. titleKey: 'dialog.thankYou'
  255. }));
  256. }
  257. // if Welcome page is enabled redirect to welcome page after 3 sec, if
  258. // there is a thank you message to be shown, 0.5s otherwise.
  259. if (getState()['features/base/config'].enableWelcomePage) {
  260. setTimeout(
  261. () => {
  262. dispatch(redirectWithStoredParams('/'));
  263. },
  264. options.showThankYou ? 3000 : 500);
  265. }
  266. };
  267. }