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

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