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

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