Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.ts 3.9KB

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