bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/whiteboard/service.js

431 lines
12 KiB
JavaScript
Raw Normal View History

import Users from '/imports/api/users';
import Auth from '/imports/ui/services/auth';
import WhiteboardMultiUser from '/imports/api/whiteboard-multi-user';
import addAnnotationQuery from '/imports/api/annotations/addAnnotation';
import { Slides } from '/imports/api/slides';
import { makeCall } from '/imports/ui/services/api';
import PresentationService from '/imports/ui/components/presentation/service';
2022-06-01 21:47:28 +08:00
import PollService from '/imports/ui/components/poll/service';
import logger from '/imports/startup/client/logger';
import { defineMessages } from 'react-intl';
import { notify } from '/imports/ui/services/notification';
import caseInsensitiveReducer from '/imports/utils/caseInsensitiveReducer';
const Annotations = new Mongo.Collection(null);
2020-04-07 04:34:08 +08:00
const UnsentAnnotations = new Mongo.Collection(null);
const ANNOTATION_CONFIG = Meteor.settings.public.whiteboard.annotations;
const DRAW_END = ANNOTATION_CONFIG.status.end;
2019-10-23 09:26:25 +08:00
const intlMessages = defineMessages({
notifyNotAllowedChange: {
id: 'app.whiteboard.annotations.notAllowed',
description: 'Label shown in toast when the user make a change on a shape he doesnt have permission',
},
});
2019-10-23 09:26:25 +08:00
let annotationsStreamListener = null;
export function addAnnotationToDiscardedList(annotation) {
if (!discardedList.includes(annotation)) discardedList.push(annotation);
}
2020-04-07 04:34:08 +08:00
const clearPreview = (annotation) => {
UnsentAnnotations.remove({ id: annotation });
};
const clearFakeAnnotations = () => {
2020-04-07 04:34:08 +08:00
UnsentAnnotations.remove({});
Annotations.remove({ id: /-fake/g });
}
function handleAddedAnnotation({
2022-04-05 22:49:13 +08:00
meetingId,
whiteboardId,
userId,
annotation,
}) {
const isOwn = Auth.meetingID === meetingId && Auth.userID === userId;
const query = addAnnotationQuery(meetingId, whiteboardId, userId, annotation, Annotations);
2020-04-07 04:34:08 +08:00
Annotations.upsert(query.selector, query.modifier);
2018-09-25 06:43:54 +08:00
2020-04-07 04:34:08 +08:00
if (isOwn) {
UnsentAnnotations.remove({ id: `${annotation.id}` });
}
}
2022-04-05 22:49:13 +08:00
function handleRemovedAnnotation({ meetingId, whiteboardId, userId, shapeId }) {
const query = { meetingId, whiteboardId };
if (userId) {
query.userId = userId;
}
if (shapeId) {
2020-04-07 04:34:08 +08:00
query.id = shapeId;
}
const annotationIsFake = Annotations.remove(query) === 0;
if (annotationIsFake) {
query.id = { $in: [shapeId, `${shapeId}-fake`] };
Annotations.remove(query);
}
}
2019-10-23 09:26:25 +08:00
export function initAnnotationsStreamListener() {
2022-04-05 22:49:13 +08:00
logger.info(
{ logCode: "init_annotations_stream_listener" },
"initAnnotationsStreamListener called"
);
/**
* We create a promise to add the handlers after a ddp subscription stop.
* The problem was caused because we add handlers to stream before the onStop event happens,
* which set the handlers to undefined.
*/
2022-04-05 22:49:13 +08:00
annotationsStreamListener = new Meteor.Streamer(
`annotations-${Auth.meetingID}`,
{ retransmit: false }
);
const startStreamHandlersPromise = new Promise((resolve) => {
const checkStreamHandlersInterval = setInterval(() => {
2022-04-05 22:49:13 +08:00
const streamHandlersSize = Object.values(
Meteor.StreamerCentral.instances[`annotations-${Auth.meetingID}`]
.handlers
).filter((el) => el !== undefined).length;
2020-01-14 00:43:31 +08:00
if (!streamHandlersSize) {
resolve(clearInterval(checkStreamHandlersInterval));
}
}, 250);
});
2019-10-23 09:26:25 +08:00
startStreamHandlersPromise.then(() => {
2022-04-05 22:49:13 +08:00
logger.debug(
{ logCode: "annotations_stream_handler_attach" },
"Attaching handlers for annotations stream"
);
annotationsStreamListener.on("removed", handleRemovedAnnotation);
annotationsStreamListener.on("added", ({ annotations }) => {
annotations.forEach((annotation) => handleAddedAnnotation(annotation));
2019-10-23 09:26:25 +08:00
});
});
2019-10-23 09:26:25 +08:00
}
const annotationsQueue = [];
// How many packets we need to have to use annotationsBufferTimeMax
const annotationsMaxDelayQueueSize = 60;
// Minimum bufferTime
const annotationsBufferTimeMin = 30;
// Maximum bufferTime
const annotationsBufferTimeMax = 200;
// Time before running 'sendBulkAnnotations' again if user is offline
const annotationsRetryDelay = 1000;
let annotationsSenderIsRunning = false;
const proccessAnnotationsQueue = async () => {
annotationsSenderIsRunning = true;
const queueSize = annotationsQueue.length;
if (!queueSize) {
annotationsSenderIsRunning = false;
return;
}
const annotations = annotationsQueue.splice(0, queueSize);
2022-04-05 22:49:13 +08:00
const isAnnotationSent = await makeCall("sendBulkAnnotations", annotations);
if (!isAnnotationSent) {
// undo splice
annotationsQueue.splice(0, 0, ...annotations);
setTimeout(proccessAnnotationsQueue, annotationsRetryDelay);
} else {
// ask tiago
2022-04-05 22:49:13 +08:00
const delayPerc =
Math.min(annotationsMaxDelayQueueSize, queueSize) /
annotationsMaxDelayQueueSize;
const delayDelta = annotationsBufferTimeMax - annotationsBufferTimeMin;
2022-04-05 22:49:13 +08:00
const delayTime = annotationsBufferTimeMin + delayDelta * delayPerc;
setTimeout(proccessAnnotationsQueue, delayTime);
}
};
2020-04-07 04:34:08 +08:00
const sendAnnotation = (annotation) => {
2018-06-26 21:28:58 +08:00
// Prevent sending annotations while disconnected
2020-04-07 04:34:08 +08:00
// TODO: Change this to add the annotation, but delay the send until we're
// reconnected. With this it will miss things
2018-06-26 21:28:58 +08:00
if (!Meteor.status().connected) return;
const index = annotationsQueue.findIndex(ann => ann.id === annotation.id);
if (index !== -1) {
annotationsQueue[index] = annotation;
} else {
annotationsQueue.push(annotation);
}
if (!annotationsSenderIsRunning)
setTimeout(proccessAnnotationsQueue, annotationsBufferTimeMin);
2020-04-07 04:34:08 +08:00
};
const sendLiveSyncPreviewAnnotation = (annotation) => {
// Prevent sending annotations while disconnected
if (!Meteor.status().connected) return;
annotationsQueue.push(annotation);
if (!annotationsSenderIsRunning) setTimeout(proccessAnnotationsQueue, annotationsBufferTimeMin);
// skip optimistic for draw end since the smoothing is done in akka
if (annotation.status === DRAW_END) return;
const { position, ...relevantAnotation } = annotation;
const queryFake = addAnnotationQuery(
Auth.meetingID, annotation.wbId, Auth.userID,
{
...relevantAnotation,
id: `${annotation.id}-fake`,
position: Number.MAX_SAFE_INTEGER,
annotationInfo: {
...annotation.annotationInfo,
color: increaseBrightness(annotation.annotationInfo.color, 40),
},
},
);
Annotations.upsert(queryFake.selector, queryFake.modifier);
};
WhiteboardMultiUser.find({ meetingId: Auth.meetingID }).observeChanges({
changed: clearFakeAnnotations,
});
2022-04-05 22:49:13 +08:00
Users.find(
{ userId: Auth.userID },
{ fields: { presenter: 1 } }
).observeChanges({
changed(id, { presenter }) {
if (presenter === false) clearFakeAnnotations();
},
});
const getMultiUser = (whiteboardId) => {
const data = WhiteboardMultiUser.findOne(
{
meetingId: Auth.meetingID,
whiteboardId,
2022-04-05 22:49:13 +08:00
},
{ fields: { multiUser: 1 } }
);
if (!data || !data.multiUser || !Array.isArray(data.multiUser)) return [];
return data.multiUser;
};
const getMultiUserSize = (whiteboardId) => {
const multiUser = getMultiUser(whiteboardId);
if (multiUser.length === 0) return 0;
// Individual whiteboard access is controlled by an array of userIds.
// When an user leaves the meeting or the presenter role moves from an
// user to another we applying a filter at the whiteboard collection.
// Ideally this should change to something more cohese but this would
// require extra changes at multiple backend modules.
const multiUserSize = Users.find(
{
meetingId: Auth.meetingID,
$or: [
{
userId: { $in: multiUser },
presenter: false,
},
{ presenter: true },
],
2022-04-05 22:49:13 +08:00
},
{ fields: { userId: 1 } }
).fetch();
return multiUserSize.length;
};
const getCurrentWhiteboardId = () => {
2022-04-05 22:49:13 +08:00
const podId = "DEFAULT_PRESENTATION_POD";
const currentPresentation = PresentationService.getCurrentPresentation(podId);
if (!currentPresentation) return null;
const currentSlide = Slides.findOne(
{
podId,
presentationId: currentPresentation.id,
current: true,
2022-04-05 22:49:13 +08:00
},
{ fields: { id: 1 } }
);
return currentSlide && currentSlide.id;
2022-04-05 22:49:13 +08:00
};
const isMultiUserActive = (whiteboardId) => {
const multiUser = getMultiUser(whiteboardId);
return multiUser.length !== 0;
};
const hasMultiUserAccess = (whiteboardId, userId) => {
const multiUser = getMultiUser(whiteboardId);
return multiUser.includes(userId);
};
const changeWhiteboardAccess = (userId, access) => {
const whiteboardId = getCurrentWhiteboardId();
if (!whiteboardId) return;
if (access) {
addIndividualAccess(whiteboardId, userId);
} else {
removeIndividualAccess(whiteboardId, userId);
}
};
const addGlobalAccess = (whiteboardId) => {
2022-04-05 22:49:13 +08:00
makeCall("addGlobalAccess", whiteboardId);
};
const addIndividualAccess = (whiteboardId, userId) => {
2022-04-05 22:49:13 +08:00
makeCall("addIndividualAccess", whiteboardId, userId);
};
const removeGlobalAccess = (whiteboardId) => {
2022-04-05 22:49:13 +08:00
makeCall("removeGlobalAccess", whiteboardId);
};
const removeIndividualAccess = (whiteboardId, userId) => {
2022-04-05 22:49:13 +08:00
makeCall("removeIndividualAccess", whiteboardId, userId);
};
2022-05-07 00:37:43 +08:00
const persistShape = (shape, whiteboardId) => {
const annotation = {
id: shape.id,
annotationInfo: shape,
wbId: whiteboardId,
userId: Auth.userID,
};
2022-05-13 22:51:41 +08:00
sendAnnotation(annotation);
2022-04-05 22:49:13 +08:00
};
2022-05-07 00:37:43 +08:00
const removeShapes = (shapes, whiteboardId) => makeCall("deleteAnnotations", shapes, whiteboardId);
2022-04-05 22:49:13 +08:00
const changeCurrentSlide = (s) => {
makeCall("changeCurrentSlide", s);
}
const getShapes = (whiteboardId, curPageId, intl) => {
2022-05-13 23:41:10 +08:00
const annotations = Annotations.find(
{
whiteboardId,
},
{
fields: { annotationInfo: 1, userId: 1, },
},
).fetch();
let result = {};
annotations.forEach((annotation) => {
if (annotation.annotationInfo.questionType) {
// poll result, convert it to text and create tldraw shape
annotation.annotationInfo.answers = annotation.annotationInfo.answers.reduce(caseInsensitiveReducer, []);
const pollResult = PollService.getPollResultString(annotation.annotationInfo, intl)
.split('<br/>').join('\n').replace( /(<([^>]+)>)/ig, '');
annotation.annotationInfo = {
childIndex: 2,
id: annotation.annotationInfo.id,
name: `poll-result-${annotation.annotationInfo.id}`,
type: "text",
text: pollResult,
parentId: `${curPageId}`,
point: [0, 0],
rotation: 0,
style: {
isFilled: false,
size: "medium",
scale: 1,
color: "black",
textAlign: "start",
font: "script",
dash: "draw"
},
}
annotation.annotationInfo.questionType = false;
}
result[annotation.annotationInfo.id] = annotation.annotationInfo;
});
return result;
2022-04-05 22:49:13 +08:00
};
const getCurrentPres = () => {
const podId = "DEFAULT_PRESENTATION_POD";
return PresentationService.getCurrentPresentation(podId);
}
const initDefaultPages = (count = 1) => {
2022-04-05 22:49:13 +08:00
const pages = {};
const pageStates = {};
let i = 1;
while (i < count + 1) {
2022-04-05 22:49:13 +08:00
pages[`${i}`] = {
id: `${i}`,
name: `Slide ${i}`,
shapes: {},
bindings: {},
};
pageStates[`${i}`] = {
id: `${i}`,
selectedIds: [],
camera: {
point: [0, 0],
zoom: 1,
},
};
i++;
}
return { pages, pageStates };
};
const notifyNotAllowedChange = (intl) => {
if (intl) notify(intl.formatMessage(intlMessages.notifyNotAllowedChange), 'warning', 'whiteboard');
};
2020-04-07 04:34:08 +08:00
export {
2022-04-05 22:49:13 +08:00
initDefaultPages,
2020-04-07 04:34:08 +08:00
Annotations,
UnsentAnnotations,
sendAnnotation,
sendLiveSyncPreviewAnnotation,
2020-04-07 04:34:08 +08:00
clearPreview,
getMultiUser,
getMultiUserSize,
getCurrentWhiteboardId,
isMultiUserActive,
hasMultiUserAccess,
changeWhiteboardAccess,
addGlobalAccess,
addIndividualAccess,
removeGlobalAccess,
removeIndividualAccess,
2022-04-05 22:49:13 +08:00
persistShape,
getShapes,
getCurrentPres,
removeShapes,
changeCurrentSlide,
clearFakeAnnotations,
notifyNotAllowedChange,
2020-04-07 04:34:08 +08:00
};