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

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