您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 2.7KB

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