您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 9.3KB

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