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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. return getURLWithoutParams(locationURL).href;
  49. }
  50. /**
  51. * Checks whether or not is safe to call the {@link getInviteURL} method already.
  52. *
  53. * @param {Function|Object} stateOrGetState - The redux state or redux's {@code getState} function.
  54. * @returns {boolean}
  55. */
  56. export function isInviteURLReady(stateOrGetState: Function | Object): boolean {
  57. const state = toState(stateOrGetState);
  58. return Boolean(state['features/base/connection'].locationURL || state['features/base/config'].locationURL);
  59. }
  60. /**
  61. * Gets a {@link URL} without hash and query/search params from a specific
  62. * {@code URL}.
  63. *
  64. * @param {URL} url - The {@code URL} which may have hash and query/search
  65. * params.
  66. * @returns {URL}
  67. */
  68. export function getURLWithoutParams(url: URL): URL {
  69. const { hash, search } = url;
  70. if ((hash && hash.length > 1) || (search && search.length > 1)) {
  71. url = new URL(url.href); // eslint-disable-line no-param-reassign
  72. url.hash = '';
  73. url.search = '';
  74. // XXX The implementation of URL at least on React Native appends ? and
  75. // # at the end of the href which is not desired.
  76. let { href } = url;
  77. if (href) {
  78. href.endsWith('#') && (href = href.substring(0, href.length - 1));
  79. href.endsWith('?') && (href = href.substring(0, href.length - 1));
  80. // eslint-disable-next-line no-param-reassign
  81. url.href === href || (url = new URL(href));
  82. }
  83. }
  84. return url;
  85. }
  86. /**
  87. * Gets a URL string without hash and query/search params from a specific
  88. * {@code URL}.
  89. *
  90. * @param {URL} url - The {@code URL} which may have hash and query/search
  91. * params.
  92. * @returns {string}
  93. */
  94. export function getURLWithoutParamsNormalized(url: URL): string {
  95. const urlWithoutParams = getURLWithoutParams(url).href;
  96. if (urlWithoutParams) {
  97. return urlWithoutParams.toLowerCase();
  98. }
  99. return '';
  100. }
  101. /**
  102. * Converts a specific id to jid if it's not jid yet.
  103. *
  104. * @param {string} id - User id or jid.
  105. * @param {Object} configHosts - The {@code hosts} part of the {@code config}
  106. * object.
  107. * @returns {string} A string in the form of a JID (i.e.
  108. * {@code user@server.com}).
  109. */
  110. export function toJid(id: string, { authdomain, domain }: Object): string {
  111. return id.indexOf('@') >= 0 ? id : `${id}@${authdomain || domain}`;
  112. }