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

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