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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { generateCollaborationLinkData } from '@jitsi/excalidraw';
  2. import { AnyAction } from 'redux';
  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. import { getCurrentRoomId } from '../breakout-rooms/functions';
  11. import { addStageParticipant } from '../filmstrip/actions.web';
  12. import { isStageFilmstripAvailable } from '../filmstrip/functions.web';
  13. import { RESET_WHITEBOARD, SET_WHITEBOARD_OPEN } from './actionTypes';
  14. import { resetWhiteboard, setWhiteboardOpen, setupWhiteboard } from './actions';
  15. import { WHITEBOARD_ID, WHITEBOARD_PARTICIPANT_NAME } from './constants';
  16. import { getCollabDetails, getCollabServerUrl, isWhiteboardPresent } from './functions';
  17. import { WhiteboardStatus } from './types';
  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: AnyAction) => {
  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 collabLinkData = await generateCollaborationLinkData();
  53. const collabServerUrl = getCollabServerUrl(state);
  54. const roomId = getCurrentRoomId(state);
  55. const collabDetails = {
  56. roomId,
  57. roomKey: collabLinkData.roomKey
  58. };
  59. focusWhiteboard(store);
  60. dispatch(setupWhiteboard({ collabDetails }));
  61. conference?.getMetadataHandler().setMetadata(WHITEBOARD_ID, {
  62. collabServerUrl,
  63. collabDetails
  64. });
  65. raiseWhiteboardNotification(WhiteboardStatus.INSTANTIATED);
  66. return;
  67. }
  68. if (action.isOpen) {
  69. focusWhiteboard(store);
  70. raiseWhiteboardNotification(WhiteboardStatus.SHOWN);
  71. return;
  72. }
  73. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  74. raiseWhiteboardNotification(WhiteboardStatus.HIDDEN);
  75. break;
  76. }
  77. case RESET_WHITEBOARD: {
  78. dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
  79. raiseWhiteboardNotification(WhiteboardStatus.RESET);
  80. break;
  81. }
  82. }
  83. return next(action);
  84. });
  85. /**
  86. * Raises the whiteboard status notifications changes (if API is enabled).
  87. *
  88. * @param {WhiteboardStatus} status - The whiteboard changed status.
  89. * @returns {Function}
  90. */
  91. function raiseWhiteboardNotification(status: WhiteboardStatus) {
  92. if (typeof APP !== 'undefined') {
  93. APP.API.notifyWhiteboardStatusChanged(status);
  94. }
  95. }
  96. /**
  97. * Set up state change listener to perform maintenance tasks when the conference
  98. * is left or failed, e.g. Disable the whiteboard if it's left open.
  99. */
  100. StateListenerRegistry.register(
  101. state => getCurrentConference(state),
  102. (conference, { dispatch }, previousConference): void => {
  103. if (conference !== previousConference) {
  104. dispatch(resetWhiteboard());
  105. }
  106. if (conference && !previousConference) {
  107. conference.on(JitsiConferenceEvents.METADATA_UPDATED, (metadata: any) => {
  108. if (metadata[WHITEBOARD_ID]) {
  109. dispatch(setupWhiteboard({
  110. collabDetails: metadata[WHITEBOARD_ID].collabDetails
  111. }));
  112. dispatch(setWhiteboardOpen(true));
  113. }
  114. });
  115. }
  116. });