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.

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