Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { IReduxState } from '../app/types';
  2. import { IJitsiConference } from '../base/conference/reducer';
  3. import { VPAAS_TENANT_PREFIX } from './constants';
  4. import logger from './logger';
  5. /**
  6. * Returns the full vpaas tenant if available, given a path.
  7. *
  8. * @param {string} path - The meeting url path.
  9. * @returns {string}
  10. */
  11. function extractVpaasTenantFromPath(path: string) {
  12. const [ , tenant ] = path.split('/');
  13. if (tenant.startsWith(VPAAS_TENANT_PREFIX)) {
  14. return tenant;
  15. }
  16. return '';
  17. }
  18. /**
  19. * Returns the vpaas tenant.
  20. *
  21. * @param {IReduxState} state - The global state.
  22. * @returns {string}
  23. */
  24. export function getVpaasTenant(state: IReduxState) {
  25. return extractVpaasTenantFromPath(state['features/base/connection'].locationURL?.pathname ?? '');
  26. }
  27. /**
  28. * Returns true if the current meeting is a vpaas one.
  29. *
  30. * @param {IReduxState} state - The state of the app.
  31. * @returns {boolean}
  32. */
  33. export function isVpaasMeeting(state: IReduxState) {
  34. const connection = state['features/base/connection'];
  35. if (connection?.locationURL?.pathname) {
  36. return Boolean(
  37. extractVpaasTenantFromPath(connection?.locationURL?.pathname)
  38. );
  39. }
  40. return false;
  41. }
  42. /**
  43. * Sends a request for retrieving the conference creator's customer id.
  44. *
  45. * @param {IJitsiConference} conference - The conference state.
  46. * @param {IReduxState} state - The state of the app.
  47. * @returns {Object} - Object containing customerId field.
  48. */
  49. export async function sendGetCustomerIdRequest(conference: IJitsiConference, state: IReduxState) {
  50. const { jaasConferenceCreatorUrl } = state['features/base/config'];
  51. const roomJid = conference?.room?.roomjid;
  52. if (jaasConferenceCreatorUrl && roomJid) {
  53. const fullUrl = `${jaasConferenceCreatorUrl}?conference=${encodeURIComponent(roomJid)}`;
  54. const response = await fetch(fullUrl);
  55. const responseBody = await response.json();
  56. if (response.ok) {
  57. return responseBody;
  58. }
  59. logger.error(`Failed to fetch ${fullUrl}. with: ${JSON.stringify(responseBody)}`);
  60. }
  61. }
  62. /**
  63. * Sends a request for retrieving jaas customer details.
  64. *
  65. * @param {Object} reqData - The request info.
  66. * @param {string} reqData.appId - The client appId.
  67. * @param {string} reqData.baseUrl - The base url for the request.
  68. * @returns {void}
  69. */
  70. export async function sendGetDetailsRequest({ appId, baseUrl }: {
  71. appId: string;
  72. baseUrl: string;
  73. }) {
  74. const fullUrl = `${baseUrl}/v1/public/tenants/${encodeURIComponent(appId)}`;
  75. try {
  76. const res = await fetch(fullUrl);
  77. if (res.ok) {
  78. return res.json();
  79. }
  80. throw new Error('Request not successful');
  81. } catch (err: any) {
  82. throw new Error(err);
  83. }
  84. }
  85. /**
  86. * Returns the billing id for vpaas meetings.
  87. *
  88. * @param {IReduxState} state - The state of the app.
  89. * @param {string} feature - Feature to be looked up for disable state.
  90. * @returns {boolean}
  91. */
  92. export function isFeatureDisabled(state: IReduxState, feature: string) {
  93. return state['features/jaas'].disabledFeatures.includes(feature);
  94. }
  95. /**
  96. * Sends a request for retrieving jaas JWT.
  97. *
  98. * @param {Object} reqData - The request info.
  99. * @param {string} reqData.appId - The client appId.
  100. * @param {string} reqData.baseUrl - The base url for the request.
  101. * @returns {void}
  102. */
  103. export async function sendGetJWTRequest({ appId, baseUrl }: {
  104. appId: string;
  105. baseUrl: string;
  106. }) {
  107. const fullUrl = `${baseUrl}/v1/public/token/${encodeURIComponent(appId)}`;
  108. try {
  109. const res = await fetch(fullUrl, {
  110. method: 'GET'
  111. });
  112. if (res.ok) {
  113. return res.json();
  114. }
  115. throw new Error('Request not successful');
  116. } catch (err: any) {
  117. throw new Error(err);
  118. }
  119. }
  120. /**
  121. * Gets a jaas JWT.
  122. *
  123. * @param {IReduxState} state - Redux state.
  124. * @returns {string} The JWT.
  125. */
  126. export async function getJaasJWT(state: IReduxState) {
  127. const baseUrl = state['features/base/config'].jaasTokenUrl;
  128. const appId = getVpaasTenant(state);
  129. const shouldSendRequest = Boolean(baseUrl && appId);
  130. if (shouldSendRequest) {
  131. try {
  132. const jwt = await sendGetJWTRequest({
  133. appId,
  134. baseUrl: baseUrl ?? ''
  135. });
  136. return jwt.token;
  137. } catch (err) {
  138. logger.error('Could not send request', err);
  139. }
  140. }
  141. }