Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.ts 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import md5 from 'js-md5';
  2. import { getParticipantCount, getPinnedParticipant } from '../../features/base/participants/functions';
  3. import { IReduxState } from '../app/types';
  4. import { getCurrentConference } from '../base/conference/functions';
  5. import { IWhiteboardConfig } from '../base/config/configType';
  6. import { getRemoteParticipants, isLocalParticipantModerator } from '../base/participants/functions';
  7. import { encodeToBase64URL } from '../base/util/httpUtils';
  8. import { appendURLHashParam, appendURLParam, getBackendSafePath } from '../base/util/uri';
  9. import { getCurrentRoomId, isInBreakoutRoom } from '../breakout-rooms/functions';
  10. import { MIN_USER_LIMIT, USER_LIMIT_THRESHOLD, WHITEBOARD_ID, WHITEBOARD_PATH_NAME } from './constants';
  11. import { IWhiteboardState } from './reducer';
  12. const getWhiteboardState = (state: IReduxState): IWhiteboardState => state['features/whiteboard'];
  13. export const getWhiteboardConfig = (state: IReduxState): IWhiteboardConfig =>
  14. state['features/base/config'].whiteboard || {};
  15. const getWhiteboardUserLimit = (state: IReduxState): number => {
  16. const userLimit = getWhiteboardConfig(state).userLimit || Infinity;
  17. return userLimit === Infinity
  18. ? userLimit
  19. : Math.max(Number(getWhiteboardConfig(state).userLimit || 1), MIN_USER_LIMIT);
  20. };
  21. /**
  22. * Returns the whiteboard collaboration details.
  23. *
  24. * @param {IReduxState} state - The state from the Redux store.
  25. * @returns {{ roomId: string, roomKey: string}|undefined}
  26. */
  27. export const getCollabDetails = (state: IReduxState): {
  28. roomId: string; roomKey: string;
  29. } | undefined => getWhiteboardState(state).collabDetails;
  30. /**
  31. * Indicates whether the whiteboard collaboration details are available.
  32. *
  33. * @param {IReduxState} state - The state from the Redux store.
  34. * @returns {boolean}
  35. */
  36. const hasCollabDetails = (state: IReduxState): boolean => Boolean(
  37. getCollabDetails(state)?.roomId && getCollabDetails(state)?.roomKey
  38. );
  39. /**
  40. * Indicates whether the whiteboard is enabled.
  41. *
  42. * @param {IReduxState} state - The state from the Redux store.
  43. * @returns {boolean}
  44. */
  45. export const isWhiteboardEnabled = (state: IReduxState): boolean =>
  46. (getWhiteboardConfig(state).enabled ?? hasCollabDetails(state))
  47. && getWhiteboardConfig(state).collabServerBaseUrl
  48. && getCurrentConference(state)?.getMetadataHandler()
  49. ?.isSupported();
  50. /**
  51. * Indicates whether the whiteboard is open.
  52. *
  53. * @param {IReduxState} state - The state from the Redux store.
  54. * @returns {boolean}
  55. */
  56. export const isWhiteboardOpen = (state: IReduxState): boolean => getWhiteboardState(state).isOpen;
  57. /**
  58. * Indicates whether the whiteboard button is visible.
  59. *
  60. * @param {IReduxState} state - The state from the Redux store.
  61. * @returns {boolean}
  62. */
  63. export const isWhiteboardButtonVisible = (state: IReduxState): boolean =>
  64. isWhiteboardEnabled(state) && (isLocalParticipantModerator(state) || isWhiteboardOpen(state));
  65. /**
  66. * Indicates whether the whiteboard is present as a meeting participant.
  67. *
  68. * @param {IReduxState} state - The state from the Redux store.
  69. * @returns {boolean}
  70. */
  71. export const isWhiteboardPresent = (state: IReduxState): boolean => getRemoteParticipants(state).has(WHITEBOARD_ID);
  72. /**
  73. * Builds the whiteboard collaboration server url.
  74. *
  75. * @param {IReduxState} state - The state from the Redux store.
  76. * @returns {string}
  77. */
  78. export const generateCollabServerUrl = (state: IReduxState): string | undefined => {
  79. const collabServerBaseUrl = getWhiteboardConfig(state).collabServerBaseUrl;
  80. if (!collabServerBaseUrl) {
  81. return;
  82. }
  83. const { locationURL } = state['features/base/connection'];
  84. const inBreakoutRoom = isInBreakoutRoom(state);
  85. const roomId = getCurrentRoomId(state);
  86. const room = md5.hex(
  87. `${getBackendSafePath(locationURL?.pathname)}${inBreakoutRoom ? `|${roomId}` : ''}`
  88. );
  89. return appendURLParam(collabServerBaseUrl, 'room', room);
  90. };
  91. /**
  92. * Returns the whiteboard collaboration server url.
  93. *
  94. * @param {IReduxState} state - The state from the Redux store.
  95. * @returns {string}
  96. */
  97. export const getCollabServerUrl = (state: IReduxState): string | undefined =>
  98. getWhiteboardState(state).collabServerUrl;
  99. /**
  100. * Whether the whiteboard is visible on stage.
  101. *
  102. * @param {IReduxState} state - The state from the Redux store.
  103. * @returns {boolean}
  104. */
  105. export const isWhiteboardVisible = (state: IReduxState): boolean =>
  106. getPinnedParticipant(state)?.id === WHITEBOARD_ID
  107. || state['features/large-video'].participantId === WHITEBOARD_ID;
  108. /**
  109. * Indicates whether the whiteboard is accessible to a participant that has a moderator role.
  110. *
  111. * @param {IReduxState} state - The state from the Redux store.
  112. * @returns {boolean}
  113. */
  114. export const isWhiteboardAllowed = (state: IReduxState): boolean =>
  115. isWhiteboardEnabled(state) && isLocalParticipantModerator(state);
  116. /**
  117. * Whether to enforce the whiteboard user limit.
  118. *
  119. * @param {IReduxState} state - The state from the Redux store.
  120. * @returns {boolean}
  121. */
  122. export const shouldEnforceUserLimit = (state: IReduxState): boolean => {
  123. const userLimit = getWhiteboardUserLimit(state);
  124. if (userLimit === Infinity) {
  125. return false;
  126. }
  127. const participantCount = getParticipantCount(state);
  128. return participantCount > userLimit;
  129. };
  130. /**
  131. * Whether to show a warning about the whiteboard user limit.
  132. *
  133. * @param {IReduxState} state - The state from the Redux store.
  134. * @returns {boolean}
  135. */
  136. export const shouldNotifyUserLimit = (state: IReduxState): boolean => {
  137. const userLimit = getWhiteboardUserLimit(state);
  138. if (userLimit === Infinity) {
  139. return false;
  140. }
  141. const participantCount = getParticipantCount(state);
  142. return participantCount + USER_LIMIT_THRESHOLD > userLimit;
  143. };
  144. /**
  145. * Generates the URL for the static whiteboard page.
  146. *
  147. * @param {string} locationUrl - The window location href.
  148. * @param {string} collabServerUrl - The whiteboard collaboration server url.
  149. * @param {Object} collabDetails - The whiteboard collaboration details.
  150. * @param {string} localParticipantName - The local participant name.
  151. * @returns {string}
  152. */
  153. export function getWhiteboardInfoForURIString(
  154. locationUrl: any,
  155. collabServerUrl: string,
  156. collabDetails: { roomId: string; roomKey: string; },
  157. localParticipantName: string
  158. ): string | undefined {
  159. if (!collabServerUrl || !locationUrl) {
  160. return undefined;
  161. }
  162. let state = {};
  163. let url = `${locationUrl.substring(0, locationUrl.lastIndexOf('/'))}/${WHITEBOARD_PATH_NAME}`;
  164. if (collabDetails?.roomId) {
  165. state = {
  166. ...state,
  167. roomId: collabDetails.roomId
  168. };
  169. }
  170. if (collabDetails?.roomKey) {
  171. state = {
  172. ...state,
  173. roomKey: collabDetails.roomKey
  174. };
  175. }
  176. state = {
  177. ...state,
  178. collabServerUrl,
  179. localParticipantName
  180. };
  181. url = appendURLHashParam(url, 'state', encodeToBase64URL(JSON.stringify(state)));
  182. return url;
  183. }