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.4KB

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