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

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