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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 { parseURIString, toURLString } from '../base/util';
  15. import { setFatalError } from '../overlay';
  16. import { getDefaultURL } from './functions';
  17. const logger = require('jitsi-meet-logger').getLogger(__filename);
  18. declare var APP: Object;
  19. /**
  20. * Triggers an in-app navigation to a specific route. Allows navigation to be
  21. * abstracted between the mobile/React Native and Web/React applications.
  22. *
  23. * @param {string|undefined} uri - The URI to which to navigate. It may be a
  24. * full URL with an HTTP(S) scheme, a full or partial URI with the app-specific
  25. * scheme, or a mere room name.
  26. * @returns {Function}
  27. */
  28. export function appNavigate(uri: ?string) {
  29. return async (dispatch: Dispatch<any>, getState: Function) => {
  30. let location = parseURIString(uri);
  31. // If the specified location (URI) does not identify a host, use the app's
  32. // default.
  33. if (!location || !location.host) {
  34. const defaultLocation = parseURIString(getDefaultURL(getState));
  35. if (location) {
  36. location.host = defaultLocation.host;
  37. // FIXME Turn location's host, hostname, and port properties into
  38. // setters in order to reduce the risks of inconsistent state.
  39. location.hostname = defaultLocation.hostname;
  40. location.pathname
  41. = defaultLocation.pathname + location.pathname.substr(1);
  42. location.port = defaultLocation.port;
  43. location.protocol = defaultLocation.protocol;
  44. } else {
  45. location = defaultLocation;
  46. }
  47. }
  48. location.protocol || (location.protocol = 'https:');
  49. const { contextRoot, host, room } = location;
  50. const locationURL = new URL(location.toString());
  51. // Disconnect from any current conference.
  52. // FIXME: unify with web.
  53. if (navigator.product === 'ReactNative') {
  54. dispatch(disconnect());
  55. }
  56. dispatch(configWillLoad(locationURL, room));
  57. let protocol = location.protocol.toLowerCase();
  58. // The React Native app supports an app-specific scheme which is sure to not
  59. // be supported by fetch.
  60. protocol !== 'http:' && protocol !== 'https:' && (protocol = 'https:');
  61. const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
  62. let url = `${baseURL}config.js`;
  63. // XXX In order to support multiple shards, tell the room to the deployment.
  64. room && (url += `?room=${room.toLowerCase()}`);
  65. let config;
  66. // Avoid (re)loading the config when there is no room.
  67. if (!room) {
  68. config = restoreConfig(baseURL);
  69. }
  70. if (!config) {
  71. try {
  72. config = await loadConfig(url);
  73. dispatch(storeConfig(baseURL, config));
  74. } catch (error) {
  75. config = restoreConfig(baseURL);
  76. if (!config) {
  77. dispatch(loadConfigError(error, locationURL));
  78. return;
  79. }
  80. }
  81. }
  82. if (getState()['features/base/config'].locationURL !== locationURL) {
  83. dispatch(loadConfigError(new Error('Config no longer needed!'), locationURL));
  84. return;
  85. }
  86. dispatch(setLocationURL(locationURL));
  87. dispatch(setConfig(config));
  88. dispatch(setRoom(room));
  89. // FIXME: unify with web, currently the connection and track creation happens in conference.js.
  90. if (room && navigator.product === 'ReactNative') {
  91. dispatch(createDesiredLocalTracks());
  92. dispatch(connect());
  93. }
  94. };
  95. }
  96. /**
  97. * Redirects to another page generated by replacing the path in the original URL
  98. * with the given path.
  99. *
  100. * @param {(string)} pathname - The path to navigate to.
  101. * @returns {Function}
  102. */
  103. export function redirectWithStoredParams(pathname: string) {
  104. return (dispatch: Dispatch<any>, getState: Function) => {
  105. const { locationURL } = getState()['features/base/connection'];
  106. const newLocationURL = new URL(locationURL.href);
  107. newLocationURL.pathname = pathname;
  108. window.location.assign(newLocationURL.toString());
  109. };
  110. }
  111. /**
  112. * Reloads the page.
  113. *
  114. * @protected
  115. * @returns {Function}
  116. */
  117. export function reloadNow() {
  118. return (dispatch: Dispatch<Function>, getState: Function) => {
  119. dispatch(setFatalError(undefined));
  120. const { locationURL } = getState()['features/base/connection'];
  121. logger.info(`Reloading the conference using URL: ${locationURL}`);
  122. if (navigator.product === 'ReactNative') {
  123. dispatch(appNavigate(toURLString(locationURL)));
  124. } else {
  125. dispatch(reloadWithStoredParams());
  126. }
  127. };
  128. }
  129. /**
  130. * Reloads the page by restoring the original URL.
  131. *
  132. * @returns {Function}
  133. */
  134. export function reloadWithStoredParams() {
  135. return (dispatch: Dispatch<any>, getState: Function) => {
  136. const { locationURL } = getState()['features/base/connection'];
  137. const windowLocation = window.location;
  138. const oldSearchString = windowLocation.search;
  139. windowLocation.replace(locationURL.toString());
  140. if (window.self !== window.top
  141. && locationURL.search === oldSearchString) {
  142. // NOTE: Assuming that only the hash or search part of the URL will
  143. // be changed!
  144. // location.reload will not trigger redirect/reload for iframe when
  145. // only the hash params are changed. That's why we need to call
  146. // reload in addition to replace.
  147. windowLocation.reload();
  148. }
  149. };
  150. }