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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* @flow */
  2. import { toState } from '../redux';
  3. /**
  4. * Retrieves a simplified version of the conference/location URL stripped of URL
  5. * params (i.e. query/search and hash) which should be used for sending invites.
  6. *
  7. * @param {Function|Object} stateOrGetState - The redux state or redux's
  8. * {@code getState} function.
  9. * @returns {string|undefined}
  10. */
  11. export function getInviteURL(stateOrGetState: Function | Object): ?string {
  12. const state = toState(stateOrGetState);
  13. const locationURL
  14. = state instanceof URL
  15. ? state
  16. : state['features/base/connection'].locationURL;
  17. return locationURL ? getURLWithoutParams(locationURL).href : undefined;
  18. }
  19. /**
  20. * Gets a {@link URL} without hash and query/search params from a specific
  21. * {@code URL}.
  22. *
  23. * @param {URL} url - The {@code URL} which may have hash and query/search
  24. * params.
  25. * @returns {URL}
  26. */
  27. export function getURLWithoutParams(url: URL): URL {
  28. const { hash, search } = url;
  29. if ((hash && hash.length > 1) || (search && search.length > 1)) {
  30. url = new URL(url.href); // eslint-disable-line no-param-reassign
  31. url.hash = '';
  32. url.search = '';
  33. // XXX The implementation of URL at least on React Native appends ? and
  34. // # at the end of the href which is not desired.
  35. let { href } = url;
  36. if (href) {
  37. href.endsWith('#') && (href = href.substring(0, href.length - 1));
  38. href.endsWith('?') && (href = href.substring(0, href.length - 1));
  39. // eslint-disable-next-line no-param-reassign
  40. url.href === href || (url = new URL(href));
  41. }
  42. }
  43. return url;
  44. }
  45. /**
  46. * Converts a specific id to jid if it's not jid yet.
  47. *
  48. * @param {string} id - User id or jid.
  49. * @param {Object} configHosts - The {@code hosts} part of the {@code config}
  50. * object.
  51. * @returns {string} A string in the form of a JID (i.e.
  52. * {@code user@server.com}).
  53. */
  54. export function toJid(id: string, { authdomain, domain }: Object): string {
  55. return id.indexOf('@') >= 0 ? id : `${id}@${authdomain || domain}`;
  56. }