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

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