Refactor: Migrate annotations for the Meteor 3.0 api

This commit is contained in:
Tainan Felipe 2023-03-08 10:32:24 -03:00 committed by Ramón Souza
parent 074ce93a4f
commit 77fc5d4067
10 changed files with 46 additions and 39 deletions

View File

@ -1,13 +1,13 @@
import { check } from 'meteor/check';
import _ from "lodash";
import _ from 'lodash';
export default function addAnnotation(meetingId, whiteboardId, userId, annotation, Annotations) {
async function addAnnotation(meetingId, whiteboardId, userId, annotation, Annotations) {
check(meetingId, String);
check(whiteboardId, String);
check(annotation, Object);
const {
id, wbId,
id, wbId,
} = annotation;
let { annotationInfo } = annotation;
@ -17,9 +17,9 @@ export default function addAnnotation(meetingId, whiteboardId, userId, annotatio
id,
};
const oldAnnotation = Annotations.findOne(selector);
const oldAnnotation = await Annotations.findOneAsync(selector);
if (oldAnnotation) {
annotationInfo = _.merge(oldAnnotation.annotationInfo, annotationInfo)
annotationInfo = _.merge(oldAnnotation.annotationInfo, annotationInfo);
}
const modifier = {
@ -34,3 +34,5 @@ export default function addAnnotation(meetingId, whiteboardId, userId, annotatio
return { selector, modifier };
}
export default addAnnotation;

View File

@ -1,10 +1,9 @@
import _ from 'lodash';
import { check } from 'meteor/check';
import modifyWhiteboardAccess from '/imports/api/whiteboard-multi-user/server/modifiers/modifyWhiteboardAccess';
import clearAnnotations from '../modifiers/clearAnnotations';
import addAnnotation from '../modifiers/addAnnotation';
export default function handleWhiteboardAnnotations({ header, body }, meetingId) {
async function handleWhiteboardAnnotations({ header, body }, meetingId) {
check(header, Object);
if (header.userId !== 'nodeJSapp') { return false; }
@ -17,12 +16,14 @@ export default function handleWhiteboardAnnotations({ header, body }, meetingId)
check(whiteboardId, String);
check(multiUser, Array);
clearAnnotations(meetingId, whiteboardId);
_.each(annotations, (annotation) => {
await clearAnnotations(meetingId, whiteboardId);
// we use a for loop here instead of a map because we need to guarantee the order of the annotations.
for (const annotation of annotations) {
const { wbId, userId } = annotation;
addAnnotation(meetingId, wbId, userId, annotation);
});
await addAnnotation(meetingId, wbId, userId, annotation);
}
modifyWhiteboardAccess(meetingId, whiteboardId, multiUser);
}
export default handleWhiteboardAnnotations;

View File

@ -3,7 +3,7 @@ import AnnotationsStreamer from '/imports/api/annotations/server/streamer';
import clearAnnotations from '../modifiers/clearAnnotations';
export default function handleWhiteboardCleared({ body }, meetingId) {
export default async function handleWhiteboardCleared({ body }, meetingId) {
check(body, {
userId: String,
whiteboardId: String,
@ -14,9 +14,11 @@ export default function handleWhiteboardCleared({ body }, meetingId) {
if (fullClear) {
AnnotationsStreamer(meetingId).emit('removed', { meetingId, whiteboardId });
return clearAnnotations(meetingId, whiteboardId);
const result = await clearAnnotations(meetingId, whiteboardId);
return result;
}
AnnotationsStreamer(meetingId).emit('removed', { meetingId, whiteboardId, userId });
return clearAnnotations(meetingId, whiteboardId, userId);
const result = await clearAnnotations(meetingId, whiteboardId, userId);
return result;
}

View File

@ -3,16 +3,16 @@ import { check } from 'meteor/check';
import AnnotationsStreamer from '/imports/api/annotations/server/streamer';
import removeAnnotation from '../modifiers/removeAnnotation';
export default function handleWhiteboardDelete({ body }, meetingId) {
const whiteboardId = body.whiteboardId;
export default async function handleWhiteboardDelete({ body }, meetingId) {
const { whiteboardId } = body;
const shapesIds = body.annotationsIds;
check(whiteboardId, String);
check(shapesIds, Array);
//console.log("!!!!!!!!!!!! handleWhiteboardDelete !!!!!!!!!!!!!!!!!",shapesIds)
shapesIds.map(shapeId => {
const result = await Promise.all(shapesIds.map(async (shapeId) => {
AnnotationsStreamer(meetingId).emit('removed', { meetingId, whiteboardId, shapeId });
removeAnnotation(meetingId, whiteboardId, shapeId);
})
await removeAnnotation(meetingId, whiteboardId, shapeId);
}));
return result;
}

View File

@ -29,7 +29,7 @@ const process = () => {
Meteor.setTimeout(process, ANNOTATION_PROCESS_INTERVAL);
};
export default function handleWhiteboardSend({ envelope, header, body }, meetingId) {
export default async function handleWhiteboardSend({ envelope, header, body }, meetingId) {
const userId = header.userId;
const whiteboardId = body.whiteboardId;
const annotations = body.annotations;
@ -43,13 +43,14 @@ export default function handleWhiteboardSend({ envelope, header, body }, meeting
if (!annotationsQueue.hasOwnProperty(meetingId)) {
annotationsQueue[meetingId] = [];
}
annotations.forEach(annotation => {
// we use a for loop here instead of a map because we need to guarantee the order of the annotations.
for (const annotation of annotations) {
annotationsQueue[meetingId].push({ meetingId, whiteboardId, userId: annotation.userId, annotation });
if (instanceIdFromMessage === myInstanceId) {
addAnnotation(meetingId, whiteboardId, annotation.userId, annotation);
await addAnnotation(meetingId, whiteboardId, annotation.userId, annotation);
}
})
}
if (queueMetrics) {
Metrics.setAnnotationQueueLength(meetingId, annotationsQueue[meetingId].length);
}

View File

@ -3,13 +3,14 @@ import { check } from 'meteor/check';
import AnnotationsStreamer from '/imports/api/annotations/server/streamer';
import removeAnnotation from '../modifiers/removeAnnotation';
export default function handleWhiteboardUndo({ body }, meetingId) {
const whiteboardId = body.whiteboardId;
export default async function handleWhiteboardUndo({ body }, meetingId) {
const { whiteboardId } = body;
const shapeId = body.annotationId;
check(whiteboardId, String);
check(shapeId, String);
AnnotationsStreamer(meetingId).emit('removed', { meetingId, whiteboardId, shapeId });
return removeAnnotation(meetingId, whiteboardId, shapeId);
const result = await removeAnnotation(meetingId, whiteboardId, shapeId);
return result;
}

View File

@ -3,15 +3,15 @@ import Logger from '/imports/startup/server/logger';
import Annotations from '/imports/api/annotations';
import addAnnotationQuery from '/imports/api/annotations/addAnnotation';
export default function addAnnotation(meetingId, whiteboardId, userId, annotation) {
export default async function addAnnotation(meetingId, whiteboardId, userId, annotation) {
check(meetingId, String);
check(whiteboardId, String);
check(annotation, Object);
const query = addAnnotationQuery(meetingId, whiteboardId, userId, annotation, Annotations);
const query = await addAnnotationQuery(meetingId, whiteboardId, userId, annotation, Annotations);
try {
const { insertedId } = Annotations.upsert(query.selector, query.modifier);
const { insertedId } = await Annotations.upsertAsync(query.selector, query.modifier);
if (insertedId) {
Logger.info(`Added annotation id=${annotation.id} whiteboard=${whiteboardId}`);

View File

@ -1,7 +1,7 @@
import Annotations from '/imports/api/annotations';
import Logger from '/imports/startup/server/logger';
export default function clearAnnotations(meetingId, whiteboardId, userId) {
export default async function clearAnnotations(meetingId, whiteboardId, userId) {
const selector = {};
if (meetingId) {
@ -17,7 +17,7 @@ export default function clearAnnotations(meetingId, whiteboardId, userId) {
}
try {
const numberAffected = Annotations.remove(selector);
const numberAffected = await Annotations.removeAsync(selector);
if (numberAffected) {
if (userId) {

View File

@ -2,7 +2,7 @@ import { check } from 'meteor/check';
import Annotations from '/imports/api/annotations';
import Logger from '/imports/startup/server/logger';
export default function removeAnnotation(meetingId, whiteboardId, shapeId) {
export default async function removeAnnotation(meetingId, whiteboardId, shapeId) {
check(meetingId, String);
check(whiteboardId, String);
check(shapeId, String);
@ -14,7 +14,7 @@ export default function removeAnnotation(meetingId, whiteboardId, shapeId) {
};
try {
const numberAffected = Annotations.remove(selector);
const numberAffected = await Annotations.removeAsync(selector);
if (numberAffected) {
Logger.info(`Removed annotation id=${shapeId} whiteboard=${whiteboardId}`);

View File

@ -26,13 +26,13 @@ const intlMessages = defineMessages({
let annotationsStreamListener = null;
function handleAddedAnnotation({
async function handleAddedAnnotation({
meetingId,
whiteboardId,
userId,
annotation,
}) {
const query = addAnnotationQuery(meetingId, whiteboardId, userId, annotation, Annotations);
const query = await addAnnotationQuery(meetingId, whiteboardId, userId, annotation, Annotations);
Annotations.upsert(query.selector, query.modifier);
}