Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.any.ts 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @ts-ignore
  2. // eslint-disable-next-line
  3. import { openTokenAuthUrl } from '../authentication/actions';
  4. // @ts-ignore
  5. import { getTokenAuthUrl, isTokenAuthEnabled } from '../authentication/functions';
  6. import { getJwtExpirationDate } from '../base/jwt/functions';
  7. import { MEDIA_TYPE } from '../base/media/constants';
  8. import { isLocalTrackMuted } from '../base/tracks/functions.any';
  9. import { getLocationContextRoot, parseURIString } from '../base/util/uri';
  10. import { addTrackStateToURL } from './functions.any';
  11. import logger from './logger';
  12. import { IStore } from './types';
  13. /**
  14. * Redirects to another page generated by replacing the path in the original URL
  15. * with the given path.
  16. *
  17. * @param {(string)} pathname - The path to navigate to.
  18. * @returns {Function}
  19. */
  20. export function redirectWithStoredParams(pathname: string) {
  21. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  22. const { locationURL } = getState()['features/base/connection'];
  23. const newLocationURL = new URL(locationURL?.href ?? '');
  24. newLocationURL.pathname = pathname;
  25. window.location.assign(newLocationURL.toString());
  26. };
  27. }
  28. /**
  29. * Assigns a specific pathname to window.location.pathname taking into account
  30. * the context root of the Web app.
  31. *
  32. * @param {string} pathname - The pathname to assign to
  33. * window.location.pathname. If the specified pathname is relative, the context
  34. * root of the Web app will be prepended to the specified pathname before
  35. * assigning it to window.location.pathname.
  36. * @param {string} hashParam - Optional hash param to assign to
  37. * window.location.hash.
  38. * @returns {Function}
  39. */
  40. export function redirectToStaticPage(pathname: string, hashParam?: string) {
  41. return () => {
  42. const windowLocation = window.location;
  43. let newPathname = pathname;
  44. if (!newPathname.startsWith('/')) {
  45. // A pathname equal to ./ specifies the current directory. It will be
  46. // fine but pointless to include it because contextRoot is the current
  47. // directory.
  48. newPathname.startsWith('./')
  49. && (newPathname = newPathname.substring(2));
  50. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  51. }
  52. if (hashParam) {
  53. windowLocation.hash = hashParam;
  54. }
  55. windowLocation.pathname = newPathname;
  56. };
  57. }
  58. /**
  59. * Reloads the page by restoring the original URL.
  60. *
  61. * @returns {Function}
  62. */
  63. export function reloadWithStoredParams() {
  64. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  65. const state = getState();
  66. const { locationURL } = state['features/base/connection'];
  67. // Preserve the local tracks muted states.
  68. // @ts-ignore
  69. const newURL = addTrackStateToURL(locationURL, state);
  70. const windowLocation = window.location;
  71. const oldSearchString = windowLocation.search;
  72. windowLocation.replace(newURL.toString());
  73. if (newURL.search === oldSearchString) {
  74. // NOTE: Assuming that only the hash or search part of the URL will
  75. // be changed!
  76. // location.replace will not trigger redirect/reload when
  77. // only the hash params are changed. That's why we need to call
  78. // reload in addition to replace.
  79. windowLocation.reload();
  80. }
  81. };
  82. }
  83. /**
  84. * Checks whether tokenAuthUrl is set, we have a jwt token that will expire soon
  85. * and redirect to the auth url to obtain new token if this is the case.
  86. *
  87. * @param {Dispatch} dispatch - The Redux dispatch function.
  88. * @param {Function} getState - The Redux state.
  89. * @param {Function} failureCallback - The callback on failure to obtain auth url.
  90. * @returns {boolean} Whether we will redirect or not.
  91. */
  92. export function maybeRedirectToTokenAuthUrl(
  93. dispatch: IStore['dispatch'], getState: IStore['getState'], failureCallback: Function) {
  94. const state = getState();
  95. const config = state['features/base/config'];
  96. const { enabled: audioOnlyEnabled } = state['features/base/audio-only'];
  97. const { startAudioOnly } = config;
  98. const { locationURL = { href: '' } as URL } = state['features/base/connection'];
  99. const audioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  100. const videoMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.VIDEO);
  101. if (!isTokenAuthEnabled(config)) {
  102. return false;
  103. }
  104. // if tokenAuthUrl check jwt if is about to expire go through the url to get new token
  105. const jwt = state['features/base/jwt'].jwt;
  106. const expirationDate = getJwtExpirationDate(jwt);
  107. // if there is jwt and its expiration time is less than 3 minutes away
  108. // let's obtain new token
  109. if (expirationDate && expirationDate.getTime() - Date.now() < 3 * 60 * 1000) {
  110. const room = state['features/base/conference'].room;
  111. const { tenant } = parseURIString(locationURL.href) || {};
  112. getTokenAuthUrl(
  113. config,
  114. locationURL,
  115. {
  116. audioMuted,
  117. audioOnlyEnabled: audioOnlyEnabled || startAudioOnly,
  118. skipPrejoin: true,
  119. videoMuted
  120. },
  121. room,
  122. tenant
  123. )
  124. .then((tokenAuthServiceUrl: string | undefined) => {
  125. if (!tokenAuthServiceUrl) {
  126. logger.warn('Cannot handle login, token service URL is not set');
  127. return Promise.reject();
  128. }
  129. return dispatch(openTokenAuthUrl(tokenAuthServiceUrl));
  130. })
  131. .catch(() => {
  132. failureCallback();
  133. });
  134. return true;
  135. }
  136. return false;
  137. }