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.

functions.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* @flow */
  2. import { toState } from '../redux';
  3. import { toURLString } from '../util';
  4. /**
  5. * Figures out what's the current conference URL which is supposed to indicate what conference is currently active.
  6. * When not currently in any conference and not trying to join any then 'undefined' is returned.
  7. *
  8. * @param {Object|Function} stateful - Either the whole Redux state object or the Redux store's {@code getState} method.
  9. * @returns {string|undefined}
  10. * @private
  11. */
  12. export function getCurrentConferenceUrl(stateful: Function | Object) {
  13. const state = toState(stateful);
  14. let currentUrl;
  15. if (isInviteURLReady(state)) {
  16. currentUrl = toURLString(getInviteURL(state));
  17. }
  18. // Check if the URL doesn't end with a slash
  19. if (currentUrl && currentUrl.substr(-1) === '/') {
  20. currentUrl = undefined;
  21. }
  22. return currentUrl ? currentUrl : undefined;
  23. }
  24. /**
  25. * Retrieves a simplified version of the conference/location URL stripped of URL params (i.e. Query/search and hash)
  26. * which should be used for sending invites.
  27. * NOTE that the method will throw an error if called too early. That is before the conference is joined or before
  28. * the process of joining one has started. This limitation does not apply to the case when called with the URL object
  29. * instance. Use {@link isInviteURLReady} to check if it's safe to call the method already.
  30. *
  31. * @param {Function|Object} stateOrGetState - The redux state or redux's {@code getState} function or the URL object
  32. * to be stripped.
  33. * @returns {string}
  34. */
  35. export function getInviteURL(stateOrGetState: Function | Object): string {
  36. const state = toState(stateOrGetState);
  37. let locationURL
  38. = state instanceof URL
  39. ? state
  40. : state['features/base/connection'].locationURL;
  41. // If there's no locationURL on the base/connection feature try the base/config where it's set earlier.
  42. if (!locationURL) {
  43. locationURL = state['features/base/config'].locationURL;
  44. }
  45. if (!locationURL) {
  46. throw new Error('Can not get invite URL - the app is not ready');
  47. }
  48. const { inviteDomain } = state['features/dynamic-branding'];
  49. const urlWithoutParams = getURLWithoutParams(locationURL);
  50. if (inviteDomain) {
  51. const meetingId
  52. = state['features/base/config'].brandingRoomAlias || urlWithoutParams.pathname.replace('/', '');
  53. return `${inviteDomain}/${meetingId}`;
  54. }
  55. return urlWithoutParams.href;
  56. }
  57. /**
  58. * Checks whether or not is safe to call the {@link getInviteURL} method already.
  59. *
  60. * @param {Function|Object} stateOrGetState - The redux state or redux's {@code getState} function.
  61. * @returns {boolean}
  62. */
  63. export function isInviteURLReady(stateOrGetState: Function | Object): boolean {
  64. const state = toState(stateOrGetState);
  65. return Boolean(state['features/base/connection'].locationURL || state['features/base/config'].locationURL);
  66. }
  67. /**
  68. * Gets a {@link URL} without hash and query/search params from a specific
  69. * {@code URL}.
  70. *
  71. * @param {URL} url - The {@code URL} which may have hash and query/search
  72. * params.
  73. * @returns {URL}
  74. */
  75. export function getURLWithoutParams(url: URL): URL {
  76. const { hash, search } = url;
  77. if ((hash && hash.length > 1) || (search && search.length > 1)) {
  78. url = new URL(url.href); // eslint-disable-line no-param-reassign
  79. url.hash = '';
  80. url.search = '';
  81. // XXX The implementation of URL at least on React Native appends ? and
  82. // # at the end of the href which is not desired.
  83. let { href } = url;
  84. if (href) {
  85. href.endsWith('#') && (href = href.substring(0, href.length - 1));
  86. href.endsWith('?') && (href = href.substring(0, href.length - 1));
  87. // eslint-disable-next-line no-param-reassign
  88. url.href === href || (url = new URL(href));
  89. }
  90. }
  91. return url;
  92. }
  93. /**
  94. * Gets a URL string without hash and query/search params from a specific
  95. * {@code URL}.
  96. *
  97. * @param {URL} url - The {@code URL} which may have hash and query/search
  98. * params.
  99. * @returns {string}
  100. */
  101. export function getURLWithoutParamsNormalized(url: URL): string {
  102. const urlWithoutParams = getURLWithoutParams(url).href;
  103. if (urlWithoutParams) {
  104. return urlWithoutParams.toLowerCase();
  105. }
  106. return '';
  107. }
  108. /**
  109. * Converts a specific id to jid if it's not jid yet.
  110. *
  111. * @param {string} id - User id or jid.
  112. * @param {Object} configHosts - The {@code hosts} part of the {@code config}
  113. * object.
  114. * @returns {string} A string in the form of a JID (i.e.
  115. * {@code user@server.com}).
  116. */
  117. export function toJid(id: string, { authdomain, domain }: Object): string {
  118. return id.indexOf('@') >= 0 ? id : `${id}@${authdomain || domain}`;
  119. }