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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import uuid from 'uuid';
  4. import { BILLING_ID, VPAAS_TENANT_PREFIX } from './constants';
  5. import logger from './logger';
  6. /**
  7. * Returns the full vpaas tenant if available, given a path.
  8. *
  9. * @param {string} path - The meeting url path.
  10. * @returns {string}
  11. */
  12. export function extractVpaasTenantFromPath(path: string) {
  13. const [ , tenant ] = path.split('/');
  14. if (tenant.startsWith(VPAAS_TENANT_PREFIX)) {
  15. return tenant;
  16. }
  17. return '';
  18. }
  19. /**
  20. * Returns true if the current meeting is a vpaas one.
  21. *
  22. * @param {Object} state - The state of the app.
  23. * @returns {boolean}
  24. */
  25. export function isVpaasMeeting(state: Object) {
  26. return Boolean(
  27. state['features/base/config'].billingCounterUrl
  28. && state['features/base/jwt'].jwt
  29. && extractVpaasTenantFromPath(
  30. state['features/base/connection'].locationURL.pathname)
  31. );
  32. }
  33. /**
  34. * Sends a billing counter request.
  35. *
  36. * @param {Object} reqData - The request info.
  37. * @param {string} reqData.baseUrl - The base url for the request.
  38. * @param {string} billingId - The unique id of the client.
  39. * @param {string} jwt - The JWT token.
  40. * @param {string} tenat - The client tenant.
  41. * @returns {void}
  42. */
  43. export async function sendCountRequest({ baseUrl, billingId, jwt, tenant }: {
  44. baseUrl: string,
  45. billingId: string,
  46. jwt: string,
  47. tenant: string
  48. }) {
  49. const fullUrl = `${baseUrl}/${encodeURIComponent(tenant)}/${billingId}`;
  50. const headers = {
  51. 'Authorization': `Bearer ${jwt}`
  52. };
  53. try {
  54. const res = await fetch(fullUrl, {
  55. method: 'GET',
  56. headers
  57. });
  58. if (!res.ok) {
  59. logger.error('Status error:', res.status);
  60. }
  61. } catch (err) {
  62. logger.error('Could not send request', err);
  63. }
  64. }
  65. /**
  66. * Returns the stored billing id (or generates a new one if none is present).
  67. *
  68. * @returns {string}
  69. */
  70. export function getBillingId() {
  71. let billingId = jitsiLocalStorage.getItem(BILLING_ID);
  72. if (!billingId) {
  73. billingId = uuid.v4();
  74. jitsiLocalStorage.setItem(BILLING_ID, billingId);
  75. }
  76. return billingId;
  77. }
  78. /**
  79. * Returns the billing id for vpaas meetings.
  80. *
  81. * @param {Object} state - The state of the app.
  82. * @returns {string | undefined}
  83. */
  84. export function getVpaasBillingId(state: Object) {
  85. if (isVpaasMeeting(state)) {
  86. return getBillingId();
  87. }
  88. }