You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.web';
  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. import { WhiteboardStatus } from './types';
  17. const focusWhiteboard = (store: IStore) => {
  18. const { dispatch, getState } = store;
  19. const state = getState();
  20. const conference = getCurrentConference(state);
  21. const stageFilmstrip = isStageFilmstripAvailable(state);
  22. const isPresent = isWhiteboardPresent(state);
  23. if (!isPresent) {
  24. dispatch(participantJoined({
  25. conference,
  26. fakeParticipant: FakeParticipant.Whiteboard,
  27. id: WHITEBOARD_ID,
  28. name: WHITEBOARD_PARTICIPANT_NAME
  29. }));
  30. }
  31. if (stageFilmstrip) {
  32. dispatch(addStageParticipant(WHITEBOARD_ID, true));
  33. } else {
  34. dispatch(pinParticipant(WHITEBOARD_ID));
  35. }
  36. };
  37. /**
  38. * Middleware which intercepts whiteboard actions to handle changes to the related state.
  39. *
  40. * @param {Store} store - The redux store.
  41. * @returns {Function}
  42. */
  43. MiddlewareRegistry.register((store: IStore) => (next: Function) => async (action: any) => {
  44. const { dispatch, getState } = store;
  45. const state = getState();
  46. const conference = getCurrentConference(state);
  47. switch (action.type) {
  48. case SET_WHITEBOARD_OPEN: {
  49. const existingCollabDetails = getCollabDetails(state);
  50. if (!existingCollabDetails) {
  51. const collabLinkData = await generateCollaborationLinkData();
  52. const collabServerUrl = getCollabServerUrl(state);
  53. const roomId = getCurrentRoomId(state);
  54. const collabDetails = {
  55. roomId,
  56. roomKey: collabLinkData.roomKey
  57. };
  58. focusWhiteboard(store);
  59. dispatch(setupWhiteboard({ collabDetails }));
  60. conference?.getMetadataHandler().setMetadata(WHITEBOARD_ID, {
  61. collabServerUrl,
  62. collabDetails
  63. });
  64. raiseWhiteboardNotification(WhiteboardStatus.INSTANTIATED);
  65. return;
  66. }
  67. if (action.isOpen) {
  68. focusWhiteboard(store);
  69. raiseWhiteboardNotification(WhiteboardStatus.SHOWN);
  70. return;
  71. }
  72. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  73. raiseWhiteboardNotification(WhiteboardStatus.HIDDEN);
  74. break;
  75. }
  76. case RESET_WHITEBOARD: {
  77. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  78. raiseWhiteboardNotification(WhiteboardStatus.RESET);
  79. break;
  80. }
  81. }
  82. return next(action);
  83. });
  84. /**
  85. * Raises the whiteboard status notifications changes (if API is enabled).
  86. *
  87. * @param {WhiteboardStatus} status - The whiteboard changed status.
  88. * @returns {Function}
  89. */
  90. function raiseWhiteboardNotification(status: WhiteboardStatus) {
  91. if (typeof APP !== 'undefined') {
  92. APP.API.notifyWhiteboardStatusChanged(status);
  93. }
  94. }
  95. /**
  96. * Set up state change listener to perform maintenance tasks when the conference
  97. * is left or failed, e.g. Disable the whiteboard if it's left open.
  98. */
  99. StateListenerRegistry.register(
  100. state => getCurrentConference(state),
  101. (conference, { dispatch }, previousConference): void => {
  102. if (conference !== previousConference) {
  103. dispatch(resetWhiteboard());
  104. }
  105. if (conference && !previousConference) {
  106. conference.on(JitsiConferenceEvents.METADATA_UPDATED, (metadata: any) => {
  107. if (metadata[WHITEBOARD_ID]) {
  108. dispatch(setupWhiteboard({
  109. collabDetails: metadata[WHITEBOARD_ID].collabDetails
  110. }));
  111. dispatch(setWhiteboardOpen(true));
  112. }
  113. });
  114. }
  115. });