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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 { clearNotifications, 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. // There are notifications now that gets displayed after we technically left
  71. // the conference, but we're still on the conference screen.
  72. dispatch(clearNotifications());
  73. dispatch(configWillLoad(locationURL, room));
  74. let protocol = location.protocol.toLowerCase();
  75. // The React Native app supports an app-specific scheme which is sure to not
  76. // be supported by fetch.
  77. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  78. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  79. let url = `${baseURL}config.js`;
  80. // XXX In order to support multiple shards, tell the room to the deployment.
  81. room && (url += `?room=${getBackendSafeRoomName(room)}`);
  82. let config;
  83. // Avoid (re)loading the config when there is no room.
  84. if (!room) {
  85. config = restoreConfig(baseURL);
  86. }
  87. if (!config) {
  88. try {
  89. config = await loadConfig(url);
  90. dispatch(storeConfig(baseURL, config));
  91. } catch (error) {
  92. config = restoreConfig(baseURL);
  93. if (!config) {
  94. if (room) {
  95. dispatch(loadConfigError(error, locationURL));
  96. return;
  97. }
  98. // If there is no room (we are on the welcome page), don't fail, just create a fake one.
  99. logger.warn('Failed to load config but there is no room, applying a fake one');
  100. config = createFakeConfig(baseURL);
  101. }
  102. }
  103. }
  104. if (getState()['features/base/config'].locationURL !== locationURL) {
  105. dispatch(loadConfigError(new Error('Config no longer needed!'), locationURL));
  106. return;
  107. }
  108. dispatch(setLocationURL(locationURL));
  109. dispatch(setConfig(config));
  110. dispatch(setRoom(room));
  111. // FIXME: unify with web, currently the connection and track creation happens in conference.js.
  112. if (room && navigator.product === 'ReactNative') {
  113. dispatch(createDesiredLocalTracks());
  114. dispatch(connect());
  115. }
  116. };
  117. }
  118. /**
  119. * Redirects to another page generated by replacing the path in the original URL
  120. * with the given path.
  121. *
  122. * @param {(string)} pathname - The path to navigate to.
  123. * @returns {Function}
  124. */
  125. export function redirectWithStoredParams(pathname: string) {
  126. return (dispatch: Dispatch<any>, getState: Function) => {
  127. const { locationURL } = getState()['features/base/connection'];
  128. const newLocationURL = new URL(locationURL.href);
  129. newLocationURL.pathname = pathname;
  130. window.location.assign(newLocationURL.toString());
  131. };
  132. }
  133. /**
  134. * Assigns a specific pathname to window.location.pathname taking into account
  135. * the context root of the Web app.
  136. *
  137. * @param {string} pathname - The pathname to assign to
  138. * window.location.pathname. If the specified pathname is relative, the context
  139. * root of the Web app will be prepended to the specified pathname before
  140. * assigning it to window.location.pathname.
  141. * @returns {Function}
  142. */
  143. export function redirectToStaticPage(pathname: string) {
  144. return () => {
  145. const windowLocation = window.location;
  146. let newPathname = pathname;
  147. if (!newPathname.startsWith('/')) {
  148. // A pathname equal to ./ specifies the current directory. It will be
  149. // fine but pointless to include it because contextRoot is the current
  150. // directory.
  151. newPathname.startsWith('./')
  152. && (newPathname = newPathname.substring(2));
  153. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  154. }
  155. windowLocation.pathname = newPathname;
  156. };
  157. }
  158. /**
  159. * Reloads the page.
  160. *
  161. * @protected
  162. * @returns {Function}
  163. */
  164. export function reloadNow() {
  165. return (dispatch: Dispatch<Function>, getState: Function) => {
  166. dispatch(setFatalError(undefined));
  167. const state = getState();
  168. const { locationURL } = state['features/base/connection'];
  169. // Preserve the local tracks muted state after the reload.
  170. const newURL = addTrackStateToURL(locationURL, state);
  171. logger.info(`Reloading the conference using URL: ${locationURL}`);
  172. if (navigator.product === 'ReactNative') {
  173. dispatch(appNavigate(toURLString(newURL)));
  174. } else {
  175. dispatch(reloadWithStoredParams());
  176. }
  177. };
  178. }
  179. /**
  180. * Adds the current track state to the passed URL.
  181. *
  182. * @param {URL} url - The URL that will be modified.
  183. * @param {Function|Object} stateful - The redux store or {@code getState} function.
  184. * @returns {URL} - Returns the modified URL.
  185. */
  186. function addTrackStateToURL(url, stateful) {
  187. const state = toState(stateful);
  188. const tracks = state['features/base/tracks'];
  189. const isVideoMuted = isLocalVideoTrackMuted(tracks);
  190. const isAudioMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO);
  191. return addHashParamsToURL(new URL(url), { // use new URL object in order to not pollute the passed parameter.
  192. 'config.startWithAudioMuted': isAudioMuted,
  193. 'config.startWithVideoMuted': isVideoMuted
  194. });
  195. }
  196. /**
  197. * Reloads the page by restoring the original URL.
  198. *
  199. * @returns {Function}
  200. */
  201. export function reloadWithStoredParams() {
  202. return (dispatch: Dispatch<any>, getState: Function) => {
  203. const state = getState();
  204. const { locationURL } = state['features/base/connection'];
  205. // Preserve the local tracks muted states.
  206. const newURL = addTrackStateToURL(locationURL, state);
  207. const windowLocation = window.location;
  208. const oldSearchString = windowLocation.search;
  209. windowLocation.replace(newURL.toString());
  210. if (newURL.search === oldSearchString) {
  211. // NOTE: Assuming that only the hash or search part of the URL will
  212. // be changed!
  213. // location.replace will not trigger redirect/reload when
  214. // only the hash params are changed. That's why we need to call
  215. // reload in addition to replace.
  216. windowLocation.reload();
  217. }
  218. };
  219. }
  220. /**
  221. * Check if the welcome page is enabled and redirects to it.
  222. * If requested show a thank you dialog before that.
  223. * If we have a close page enabled, redirect to it without
  224. * showing any other dialog.
  225. *
  226. * @param {Object} options - Used to decide which particular close page to show
  227. * or if close page is disabled, whether we should show the thankyou dialog.
  228. * @param {boolean} options.showThankYou - Whether we should
  229. * show thank you dialog.
  230. * @param {boolean} options.feedbackSubmitted - Whether feedback was submitted.
  231. * @returns {Function}
  232. */
  233. export function maybeRedirectToWelcomePage(options: Object = {}) {
  234. return (dispatch: Dispatch<any>, getState: Function) => {
  235. const {
  236. enableClosePage
  237. } = getState()['features/base/config'];
  238. // if close page is enabled redirect to it, without further action
  239. if (enableClosePage) {
  240. const { isGuest } = getState()['features/base/jwt'];
  241. // save whether current user is guest or not, before navigating
  242. // to close page
  243. window.sessionStorage.setItem('guest', isGuest);
  244. let path = 'close.html';
  245. if (interfaceConfig.SHOW_PROMOTIONAL_CLOSE_PAGE) {
  246. path = 'close3.html';
  247. } else if (!options.feedbackSubmitted) {
  248. path = 'close2.html';
  249. }
  250. dispatch(redirectToStaticPage(`static/${path}`));
  251. return;
  252. }
  253. // else: show thankYou dialog only if there is no feedback
  254. if (options.showThankYou) {
  255. dispatch(showNotification({
  256. titleArguments: { appName: getName() },
  257. titleKey: 'dialog.thankYou'
  258. }));
  259. }
  260. // if Welcome page is enabled redirect to welcome page after 3 sec, if
  261. // there is a thank you message to be shown, 0.5s otherwise.
  262. if (getState()['features/base/config'].enableWelcomePage) {
  263. setTimeout(
  264. () => {
  265. dispatch(redirectWithStoredParams('/'));
  266. },
  267. options.showThankYou ? 3000 : 500);
  268. }
  269. };
  270. }