2022-10-11 10:47:54 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2022-09-30 14:50:45 +00:00
|
|
|
import { generateCollaborationLinkData } from '@jitsi/excalidraw';
|
|
|
|
|
2022-10-11 10:47:54 +00:00
|
|
|
import { IStore } from '../app/types';
|
2022-09-30 14:50:45 +00:00
|
|
|
import { getCurrentConference } from '../base/conference/functions';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
|
|
|
|
import { participantJoined, participantLeft, pinParticipant } from '../base/participants/actions';
|
|
|
|
import { FakeParticipant } from '../base/participants/types';
|
|
|
|
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
|
|
|
import StateListenerRegistry from '../base/redux/StateListenerRegistry';
|
2022-09-30 14:50:45 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import { addStageParticipant } from '../filmstrip/actions.web';
|
|
|
|
// @ts-ignore
|
|
|
|
import { isStageFilmstripAvailable } from '../filmstrip/functions';
|
2022-10-11 10:47:54 +00:00
|
|
|
|
|
|
|
import { RESET_WHITEBOARD, SET_WHITEBOARD_OPEN } from './actionTypes';
|
|
|
|
import { resetWhiteboard, setWhiteboardOpen, setupWhiteboard } from './actions';
|
|
|
|
import { WHITEBOARD_ID, WHITEBOARD_PARTICIPANT_NAME } from './constants';
|
|
|
|
import { getCollabDetails, getCollabServerUrl, isWhiteboardPresent } from './functions';
|
2022-09-30 14:50:45 +00:00
|
|
|
|
|
|
|
const focusWhiteboard = (store: IStore) => {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
const stageFilmstrip = isStageFilmstripAvailable(state);
|
|
|
|
const isPresent = isWhiteboardPresent(state);
|
|
|
|
|
|
|
|
if (!isPresent) {
|
|
|
|
dispatch(participantJoined({
|
|
|
|
conference,
|
2022-10-06 11:12:57 +00:00
|
|
|
fakeParticipant: FakeParticipant.Whiteboard,
|
2022-09-30 14:50:45 +00:00
|
|
|
id: WHITEBOARD_ID,
|
|
|
|
name: WHITEBOARD_PARTICIPANT_NAME
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (stageFilmstrip) {
|
|
|
|
dispatch(addStageParticipant(WHITEBOARD_ID, true));
|
|
|
|
} else {
|
|
|
|
dispatch(pinParticipant(WHITEBOARD_ID));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware which intercepts whiteboard actions to handle changes to the related state.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register((store: IStore) => (next: Function) => async (action: any) => {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
const conference = getCurrentConference(state);
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case SET_WHITEBOARD_OPEN: {
|
|
|
|
const existingCollabDetails = getCollabDetails(state);
|
|
|
|
|
|
|
|
if (!existingCollabDetails) {
|
|
|
|
const collabDetails = await generateCollaborationLinkData();
|
|
|
|
const collabServerUrl = getCollabServerUrl(state);
|
|
|
|
|
|
|
|
focusWhiteboard(store);
|
2022-10-06 12:22:48 +00:00
|
|
|
dispatch(setupWhiteboard({ collabDetails }));
|
2022-09-30 14:50:45 +00:00
|
|
|
conference.getMetadataHandler().setMetadata(WHITEBOARD_ID, {
|
|
|
|
collabServerUrl,
|
2022-10-06 12:22:48 +00:00
|
|
|
collabDetails
|
2022-09-30 14:50:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.isOpen) {
|
|
|
|
focusWhiteboard(store);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:12:57 +00:00
|
|
|
dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
|
2022-09-30 14:50:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RESET_WHITEBOARD: {
|
2022-10-06 11:12:57 +00:00
|
|
|
dispatch(participantLeft(WHITEBOARD_ID, conference, { fakeParticipant: FakeParticipant.Whiteboard }));
|
2022-09-30 14:50:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up state change listener to perform maintenance tasks when the conference
|
|
|
|
* is left or failed, e.g. Disable the whiteboard if it's left open.
|
|
|
|
*/
|
|
|
|
StateListenerRegistry.register(
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
state => getCurrentConference(state),
|
|
|
|
|
|
|
|
// @ts-ignore
|
2022-10-06 12:22:48 +00:00
|
|
|
(conference, { dispatch }, previousConference): void => {
|
2022-09-30 14:50:45 +00:00
|
|
|
if (conference !== previousConference) {
|
|
|
|
dispatch(resetWhiteboard());
|
|
|
|
}
|
|
|
|
if (conference && !previousConference) {
|
|
|
|
conference.on(JitsiConferenceEvents.METADATA_UPDATED, (metadata: any) => {
|
|
|
|
if (metadata[WHITEBOARD_ID]) {
|
|
|
|
dispatch(setupWhiteboard({
|
2022-10-06 12:22:48 +00:00
|
|
|
collabDetails: metadata[WHITEBOARD_ID].collabDetails
|
2022-09-30 14:50:45 +00:00
|
|
|
}));
|
|
|
|
dispatch(setWhiteboardOpen(true));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|