2018-06-28 22:27:40 +08:00
|
|
|
import Users from '/imports/api/users';
|
2018-05-12 00:01:24 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2018-06-28 22:27:40 +08:00
|
|
|
import WhiteboardMultiUser from '/imports/api/whiteboard-multi-user/';
|
2018-05-12 00:01:24 +08:00
|
|
|
import addAnnotationQuery from '/imports/api/annotations/addAnnotation';
|
2018-07-12 06:03:56 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2019-02-14 00:15:09 +08:00
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
2018-06-26 21:38:43 +08:00
|
|
|
import { isEqual } from 'lodash';
|
2018-05-12 00:01:24 +08:00
|
|
|
|
|
|
|
const Annotations = new Mongo.Collection(null);
|
2018-08-02 04:15:52 +08:00
|
|
|
const ANNOTATION_CONFIG = Meteor.settings.public.whiteboard.annotations;
|
|
|
|
const DRAW_START = ANNOTATION_CONFIG.status.start;
|
|
|
|
const DRAW_END = ANNOTATION_CONFIG.status.end;
|
2018-08-15 02:47:09 +08:00
|
|
|
const discardedList = [];
|
2018-08-23 23:42:34 +08:00
|
|
|
|
2019-10-23 09:26:25 +08:00
|
|
|
|
|
|
|
let annotationsStreamListener = null;
|
|
|
|
|
2018-08-23 23:42:34 +08:00
|
|
|
export function addAnnotationToDiscardedList(annotation) {
|
2018-08-24 00:35:26 +08:00
|
|
|
if (!discardedList.includes(annotation)) discardedList.push(annotation);
|
2018-08-23 23:42:34 +08:00
|
|
|
}
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2018-06-28 22:27:40 +08:00
|
|
|
function clearFakeAnnotations() {
|
|
|
|
Annotations.remove({ id: /-fake/g });
|
|
|
|
}
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
function handleAddedAnnotation({
|
|
|
|
meetingId, whiteboardId, userId, annotation,
|
|
|
|
}) {
|
|
|
|
const isOwn = Auth.meetingID === meetingId && Auth.userID === userId;
|
|
|
|
const query = addAnnotationQuery(meetingId, whiteboardId, userId, annotation);
|
|
|
|
|
|
|
|
if (!isOwn) {
|
|
|
|
Annotations.upsert(query.selector, query.modifier);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fakeAnnotation = Annotations.findOne({ id: `${annotation.id}-fake` });
|
2018-09-25 06:43:54 +08:00
|
|
|
let fakePoints;
|
|
|
|
|
|
|
|
if (fakeAnnotation) {
|
|
|
|
fakePoints = fakeAnnotation.annotationInfo.points;
|
|
|
|
const { points: lastPoints } = annotation.annotationInfo;
|
|
|
|
|
|
|
|
if (annotation.annotationType !== 'pencil') {
|
|
|
|
Annotations.update(fakeAnnotation._id, {
|
|
|
|
$set: {
|
|
|
|
position: annotation.position,
|
2019-06-29 05:45:50 +08:00
|
|
|
'annotationInfo.color': isEqual(fakePoints, lastPoints) || annotation.status === DRAW_END
|
|
|
|
? annotation.annotationInfo.color : fakeAnnotation.annotationInfo.color,
|
2018-09-25 06:43:54 +08:00
|
|
|
},
|
|
|
|
$inc: { version: 1 }, // TODO: Remove all this version stuff
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Annotations.upsert(query.selector, query.modifier, (err) => {
|
|
|
|
if (err) {
|
2019-06-29 05:45:50 +08:00
|
|
|
logger.error({
|
|
|
|
logCode: 'whiteboard_annotation_upsert_error',
|
|
|
|
extraInfo: { error: err },
|
|
|
|
}, 'Error on adding an annotation');
|
2018-05-12 00:01:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove fake annotation for pencil on draw end
|
2018-08-02 04:15:52 +08:00
|
|
|
if (annotation.status === DRAW_END) {
|
2018-05-12 00:01:24 +08:00
|
|
|
Annotations.remove({ id: `${annotation.id}-fake` });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 04:15:52 +08:00
|
|
|
if (annotation.status === DRAW_START) {
|
2018-05-12 00:01:24 +08:00
|
|
|
Annotations.update(fakeAnnotation._id, {
|
|
|
|
$set: {
|
|
|
|
position: annotation.position - 1,
|
|
|
|
},
|
|
|
|
$inc: { version: 1 }, // TODO: Remove all this version stuff
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleRemovedAnnotation({
|
|
|
|
meetingId, whiteboardId, userId, shapeId,
|
|
|
|
}) {
|
|
|
|
const query = { meetingId, whiteboardId };
|
|
|
|
|
2018-08-23 23:42:34 +08:00
|
|
|
addAnnotationToDiscardedList(shapeId);
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
if (userId) {
|
|
|
|
query.userId = userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shapeId) {
|
2018-06-26 21:50:19 +08:00
|
|
|
query.id = { $in: [shapeId, `${shapeId}-fake`] };
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Annotations.remove(query);
|
|
|
|
}
|
|
|
|
|
2019-10-23 09:26:25 +08:00
|
|
|
export function initAnnotationsStreamListener() {
|
|
|
|
if (!annotationsStreamListener) {
|
|
|
|
annotationsStreamListener = new Meteor.Streamer(`annotations-${Auth.meetingID}`, { retransmit: false });
|
|
|
|
|
|
|
|
annotationsStreamListener.on('removed', handleRemovedAnnotation);
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2019-10-23 09:26:25 +08:00
|
|
|
annotationsStreamListener.on('added', ({ annotations }) => {
|
|
|
|
// Call handleAddedAnnotation when this annotation is not in discardedList
|
|
|
|
annotations
|
|
|
|
.filter(({ annotation }) => !discardedList.includes(annotation.id))
|
|
|
|
.forEach(annotation => handleAddedAnnotation(annotation));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2018-06-26 21:38:43 +08:00
|
|
|
function increaseBrightness(realHex, percent) {
|
|
|
|
let hex = parseInt(realHex, 10).toString(16).padStart(6, 0);
|
2018-05-12 00:01:24 +08:00
|
|
|
// strip the leading # if it's there
|
|
|
|
hex = hex.replace(/^\s*#|\s*$/g, '');
|
|
|
|
|
|
|
|
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
|
2018-06-26 21:38:43 +08:00
|
|
|
if (hex.length === 3) {
|
2018-05-12 00:01:24 +08:00
|
|
|
hex = hex.replace(/(.)/g, '$1$1');
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:38:43 +08:00
|
|
|
const r = parseInt(hex.substr(0, 2), 16);
|
|
|
|
const g = parseInt(hex.substr(2, 2), 16);
|
|
|
|
const b = parseInt(hex.substr(4, 2), 16);
|
2018-05-12 00:01:24 +08:00
|
|
|
|
2018-06-26 21:38:43 +08:00
|
|
|
/* eslint-disable no-bitwise, no-mixed-operators */
|
2019-06-29 05:45:50 +08:00
|
|
|
return parseInt(((0 | (1 << 8) + r + ((256 - r) * percent) / 100).toString(16)).substr(1)
|
|
|
|
+ ((0 | (1 << 8) + g + ((256 - g) * percent) / 100).toString(16)).substr(1)
|
|
|
|
+ ((0 | (1 << 8) + b + ((256 - b) * percent) / 100).toString(16)).substr(1), 16);
|
2018-06-26 21:38:43 +08:00
|
|
|
/* eslint-enable no-bitwise, no-mixed-operators */
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
2019-02-14 00:15:09 +08:00
|
|
|
const annotationsQueue = [];
|
2018-05-12 00:01:24 +08:00
|
|
|
// How many packets we need to have to use annotationsBufferTimeMax
|
|
|
|
const annotationsMaxDelayQueueSize = 60;
|
|
|
|
// Minimum bufferTime
|
|
|
|
const annotationsBufferTimeMin = 30;
|
|
|
|
// Maximum bufferTime
|
|
|
|
const annotationsBufferTimeMax = 200;
|
|
|
|
let annotationsSenderIsRunning = false;
|
|
|
|
|
2019-02-14 00:15:09 +08:00
|
|
|
const proccessAnnotationsQueue = async () => {
|
2018-05-12 00:01:24 +08:00
|
|
|
annotationsSenderIsRunning = true;
|
|
|
|
const queueSize = annotationsQueue.length;
|
|
|
|
|
|
|
|
if (!queueSize) {
|
|
|
|
annotationsSenderIsRunning = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-14 00:15:09 +08:00
|
|
|
const annotations = annotationsQueue.splice(0, queueSize);
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
// console.log('annotationQueue.length', annotationsQueue, annotationsQueue.length);
|
2019-06-29 05:45:50 +08:00
|
|
|
await makeCall('sendBulkAnnotations', annotations.filter(({ id }) => !discardedList.includes(id)));
|
2019-02-14 00:15:09 +08:00
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
// ask tiago
|
2019-06-29 05:45:50 +08:00
|
|
|
const delayPerc = Math.min(annotationsMaxDelayQueueSize, queueSize) / annotationsMaxDelayQueueSize;
|
2018-05-12 00:01:24 +08:00
|
|
|
const delayDelta = annotationsBufferTimeMax - annotationsBufferTimeMin;
|
|
|
|
const delayTime = annotationsBufferTimeMin + (delayDelta * delayPerc);
|
|
|
|
setTimeout(proccessAnnotationsQueue, delayTime);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function sendAnnotation(annotation) {
|
2018-06-26 21:28:58 +08:00
|
|
|
// Prevent sending annotations while disconnected
|
|
|
|
if (!Meteor.status().connected) return;
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
annotationsQueue.push(annotation);
|
|
|
|
if (!annotationsSenderIsRunning) setTimeout(proccessAnnotationsQueue, annotationsBufferTimeMin);
|
|
|
|
|
|
|
|
// skip optimistic for draw end since the smoothing is done in akka
|
2018-08-02 04:15:52 +08:00
|
|
|
if (annotation.status === DRAW_END) return;
|
2018-05-12 00:01:24 +08:00
|
|
|
|
|
|
|
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,
|
2018-06-26 21:38:43 +08:00
|
|
|
color: increaseBrightness(annotation.annotationInfo.color, 40),
|
2018-05-12 00:01:24 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2018-06-26 21:38:43 +08:00
|
|
|
|
|
|
|
Annotations.upsert(queryFake.selector, queryFake.modifier);
|
2018-05-12 00:01:24 +08:00
|
|
|
}
|
|
|
|
|
2018-06-28 22:27:40 +08:00
|
|
|
WhiteboardMultiUser.find({ meetingId: Auth.meetingID }).observeChanges({
|
|
|
|
changed: clearFakeAnnotations,
|
|
|
|
});
|
|
|
|
|
2019-07-25 01:59:04 +08:00
|
|
|
Users.find({ userId: Auth.userID }, { fields: { presenter: 1 } }).observeChanges({
|
2018-06-28 22:27:40 +08:00
|
|
|
changed(id, { presenter }) {
|
|
|
|
if (presenter === false) clearFakeAnnotations();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-05-12 00:01:24 +08:00
|
|
|
export default Annotations;
|