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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. const { billingCounterUrl, iAmRecorder, iAmSipGateway } = state['features/base/config'];
  27. const { jwt } = state['features/base/jwt'];
  28. const isAllowed = iAmRecorder || iAmSipGateway || Boolean(jwt);
  29. return Boolean(
  30. billingCounterUrl
  31. && extractVpaasTenantFromPath(
  32. state['features/base/connection'].locationURL.pathname)
  33. && isAllowed
  34. );
  35. }
  36. /**
  37. * Sends a billing counter request.
  38. *
  39. * @param {Object} reqData - The request info.
  40. * @param {string} reqData.baseUrl - The base url for the request.
  41. * @param {string} billingId - The unique id of the client.
  42. * @param {string} jwt - The JWT token.
  43. * @param {string} tenat - The client tenant.
  44. * @returns {void}
  45. */
  46. export async function sendCountRequest({ baseUrl, billingId, jwt, tenant }: {
  47. baseUrl: string,
  48. billingId: string,
  49. jwt: string,
  50. tenant: string
  51. }) {
  52. const fullUrl = `${baseUrl}/${encodeURIComponent(tenant)}/${billingId}`;
  53. const headers = {
  54. 'Authorization': `Bearer ${jwt}`
  55. };
  56. try {
  57. const res = await fetch(fullUrl, {
  58. method: 'GET',
  59. headers
  60. });
  61. if (!res.ok) {
  62. logger.error('Status error:', res.status);
  63. }
  64. } catch (err) {
  65. logger.error('Could not send request', err);
  66. }
  67. }
  68. /**
  69. * Returns the stored billing id (or generates a new one if none is present).
  70. *
  71. * @returns {string}
  72. */
  73. export function getBillingId() {
  74. let billingId = jitsiLocalStorage.getItem(BILLING_ID);
  75. if (!billingId) {
  76. billingId = uuid.v4();
  77. jitsiLocalStorage.setItem(BILLING_ID, billingId);
  78. }
  79. return billingId;
  80. }
  81. /**
  82. * Returns the billing id for vpaas meetings.
  83. *
  84. * @param {Object} state - The state of the app.
  85. * @returns {string | undefined}
  86. */
  87. export function getVpaasBillingId(state: Object) {
  88. if (isVpaasMeeting(state)) {
  89. return getBillingId();
  90. }
  91. }