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

middleware.ts 4.2KB

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