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

middleware.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { generateCollaborationLinkData } from '@jitsi/excalidraw';
  2. import { IStore } from '../app/types';
  3. import { getCurrentConference } from '../base/conference/functions';
  4. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  5. import { participantJoined, participantLeft, pinParticipant } from '../base/participants/actions';
  6. import { FakeParticipant } from '../base/participants/types';
  7. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  8. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  9. import { getCurrentRoomId } from '../breakout-rooms/functions';
  10. import { addStageParticipant } from '../filmstrip/actions.web';
  11. import { isStageFilmstripAvailable } from '../filmstrip/functions';
  12. import { RESET_WHITEBOARD, SET_WHITEBOARD_OPEN } from './actionTypes';
  13. import { resetWhiteboard, setWhiteboardOpen, setupWhiteboard } from './actions';
  14. import { WHITEBOARD_ID, WHITEBOARD_PARTICIPANT_NAME } from './constants';
  15. import { getCollabDetails, getCollabServerUrl, isWhiteboardPresent } from './functions';
  16. const focusWhiteboard = (store: IStore) => {
  17. const { dispatch, getState } = store;
  18. const state = getState();
  19. const conference = getCurrentConference(state);
  20. const stageFilmstrip = isStageFilmstripAvailable(state);
  21. const isPresent = isWhiteboardPresent(state);
  22. if (!isPresent) {
  23. dispatch(participantJoined({
  24. conference,
  25. fakeParticipant: FakeParticipant.Whiteboard,
  26. id: WHITEBOARD_ID,
  27. name: WHITEBOARD_PARTICIPANT_NAME
  28. }));
  29. }
  30. if (stageFilmstrip) {
  31. dispatch(addStageParticipant(WHITEBOARD_ID, true));
  32. } else {
  33. dispatch(pinParticipant(WHITEBOARD_ID));
  34. }
  35. };
  36. /**
  37. * Middleware which intercepts whiteboard actions to handle changes to the related state.
  38. *
  39. * @param {Store} store - The redux store.
  40. * @returns {Function}
  41. */
  42. MiddlewareRegistry.register((store: IStore) => (next: Function) => async (action: any) => {
  43. const { dispatch, getState } = store;
  44. const state = getState();
  45. const conference = getCurrentConference(state);
  46. switch (action.type) {
  47. case SET_WHITEBOARD_OPEN: {
  48. const existingCollabDetails = getCollabDetails(state);
  49. if (!existingCollabDetails) {
  50. const collabLinkData = await generateCollaborationLinkData();
  51. const collabServerUrl = getCollabServerUrl(state);
  52. const roomId = getCurrentRoomId(state);
  53. const collabDetails = {
  54. roomId,
  55. roomKey: collabLinkData.roomKey
  56. };
  57. focusWhiteboard(store);
  58. dispatch(setupWhiteboard({ collabDetails }));
  59. conference.getMetadataHandler().setMetadata(WHITEBOARD_ID, {
  60. collabServerUrl,
  61. collabDetails
  62. });
  63. return;
  64. }
  65. if (action.isOpen) {
  66. focusWhiteboard(store);
  67. return;
  68. }
  69. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  70. break;
  71. }
  72. case RESET_WHITEBOARD: {
  73. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  74. break;
  75. }
  76. }
  77. return next(action);
  78. });
  79. /**
  80. * Set up state change listener to perform maintenance tasks when the conference
  81. * is left or failed, e.g. Disable the whiteboard if it's left open.
  82. */
  83. StateListenerRegistry.register(
  84. // @ts-ignore
  85. state => getCurrentConference(state),
  86. // @ts-ignore
  87. (conference, { dispatch }, previousConference): void => {
  88. if (conference !== previousConference) {
  89. dispatch(resetWhiteboard());
  90. }
  91. if (conference && !previousConference) {
  92. conference.on(JitsiConferenceEvents.METADATA_UPDATED, (metadata: any) => {
  93. if (metadata[WHITEBOARD_ID]) {
  94. dispatch(setupWhiteboard({
  95. collabDetails: metadata[WHITEBOARD_ID].collabDetails
  96. }));
  97. dispatch(setWhiteboardOpen(true));
  98. }
  99. });
  100. }
  101. });