2016-11-19 01:35:28 +08:00
|
|
|
import { check } from 'meteor/check';
|
2019-10-23 09:26:25 +08:00
|
|
|
import AnnotationsStreamer from '/imports/api/annotations/server/streamer';
|
2017-10-12 08:52:57 +08:00
|
|
|
import addAnnotation from '../modifiers/addAnnotation';
|
2020-12-10 23:07:06 +08:00
|
|
|
import Metrics from '/imports/startup/server/metrics';
|
2016-11-19 01:35:28 +08:00
|
|
|
|
2020-12-11 01:07:46 +08:00
|
|
|
const { queueMetrics } = Meteor.settings.private.redis.metrics;
|
|
|
|
|
2021-01-20 22:36:40 +08:00
|
|
|
const {
|
|
|
|
annotationsQueueProcessInterval: ANNOTATION_PROCESS_INTERVAL,
|
|
|
|
} = Meteor.settings.public.whiteboard;
|
2018-05-30 22:19:19 +08:00
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
let annotationsQueue = {};
|
|
|
|
let annotationsRecieverIsRunning = false;
|
|
|
|
|
2021-01-20 22:36:40 +08:00
|
|
|
const process = () => {
|
2018-05-12 00:01:24 +08:00
|
|
|
if (!Object.keys(annotationsQueue).length) {
|
|
|
|
annotationsRecieverIsRunning = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
annotationsRecieverIsRunning = true;
|
2019-10-23 09:26:25 +08:00
|
|
|
Object.keys(annotationsQueue).forEach((meetingId) => {
|
|
|
|
AnnotationsStreamer(meetingId).emit('added', { meetingId, annotations: annotationsQueue[meetingId] });
|
2020-12-16 03:20:32 +08:00
|
|
|
if (queueMetrics) {
|
|
|
|
Metrics.setAnnotationQueueLength(meetingId, 0);
|
|
|
|
}
|
2018-05-12 00:01:24 +08:00
|
|
|
});
|
|
|
|
annotationsQueue = {};
|
|
|
|
|
2021-01-20 22:36:40 +08:00
|
|
|
Meteor.setTimeout(process, ANNOTATION_PROCESS_INTERVAL);
|
2018-05-12 00:01:24 +08:00
|
|
|
};
|
|
|
|
|
2022-09-28 20:03:59 +08:00
|
|
|
export default function handleWhiteboardSend({ envelope, header, body }, meetingId) {
|
2017-10-12 08:52:57 +08:00
|
|
|
const userId = header.userId;
|
2022-05-04 20:40:56 +08:00
|
|
|
const whiteboardId = body.whiteboardId;
|
|
|
|
const annotations = body.annotations;
|
2022-09-28 20:03:59 +08:00
|
|
|
const instanceIdFromMessage = parseInt(envelope.routing.html5InstanceId, 10) || 1;
|
|
|
|
const myInstanceId = parseInt(body.myInstanceId, 10) || 1;
|
2016-11-19 01:35:28 +08:00
|
|
|
|
2017-10-12 08:52:57 +08:00
|
|
|
check(userId, String);
|
2016-11-19 01:35:28 +08:00
|
|
|
check(whiteboardId, String);
|
2022-05-04 20:40:56 +08:00
|
|
|
check(annotations, Array);
|
2016-11-19 01:35:28 +08:00
|
|
|
|
2020-12-11 01:07:46 +08:00
|
|
|
if (!annotationsQueue.hasOwnProperty(meetingId)) {
|
2018-05-12 00:01:24 +08:00
|
|
|
annotationsQueue[meetingId] = [];
|
|
|
|
}
|
2022-09-28 20:03:59 +08:00
|
|
|
|
|
|
|
annotations.forEach(annotation => {
|
2022-05-21 01:25:41 +08:00
|
|
|
annotationsQueue[meetingId].push({ meetingId, whiteboardId, userId: annotation.userId, annotation });
|
2022-09-28 20:03:59 +08:00
|
|
|
if (instanceIdFromMessage === myInstanceId) {
|
|
|
|
addAnnotation(meetingId, whiteboardId, annotation.userId, annotation);
|
|
|
|
}
|
2022-05-04 20:40:56 +08:00
|
|
|
})
|
2020-12-11 01:07:46 +08:00
|
|
|
if (queueMetrics) {
|
|
|
|
Metrics.setAnnotationQueueLength(meetingId, annotationsQueue[meetingId].length);
|
|
|
|
}
|
2021-01-20 22:36:40 +08:00
|
|
|
if (!annotationsRecieverIsRunning) process();
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|