2022-02-09 23:50:01 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import { addWebcamSync } from '../modifiers/sharedWebcam';
|
|
|
|
import VideoStreams from '/imports/api/video-streams/';
|
|
|
|
import updatedVideoStream from '../modifiers/updatedVideoStream';
|
|
|
|
import unsharedWebcam from '../modifiers/unsharedWebcam';
|
|
|
|
|
2023-03-20 20:23:39 +08:00
|
|
|
export default async function handleWebcamSync({ body }, meetingId) {
|
2022-02-09 23:50:01 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(body, Object);
|
|
|
|
const { webcamListSync } = body;
|
|
|
|
check(webcamListSync, Array);
|
|
|
|
|
|
|
|
const streamsIds = webcamListSync.map((webcam) => webcam.stream);
|
|
|
|
|
2023-03-20 20:23:39 +08:00
|
|
|
const webcamStreams = VideoStreams.find({
|
2022-02-09 23:50:01 +08:00
|
|
|
meetingId,
|
|
|
|
stream: { $in: streamsIds },
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
stream: 1,
|
|
|
|
},
|
2023-03-20 20:23:39 +08:00
|
|
|
}).fetchAsync();
|
2022-02-09 23:50:01 +08:00
|
|
|
|
2023-03-20 20:23:39 +08:00
|
|
|
const webcamStreamsToUpdate = webcamStreams.map((m) => m.stream);
|
|
|
|
|
|
|
|
await Promise.all(webcamListSync.map(async (webcam) => {
|
2022-02-09 23:50:01 +08:00
|
|
|
if (webcamStreamsToUpdate.indexOf(webcam.stream) >= 0) {
|
|
|
|
// stream already exist, then update
|
2023-03-20 20:23:39 +08:00
|
|
|
await updatedVideoStream(meetingId, webcam);
|
2022-02-09 23:50:01 +08:00
|
|
|
} else {
|
|
|
|
// stream doesn't exist yet, then add it
|
2023-03-20 20:23:39 +08:00
|
|
|
await addWebcamSync(meetingId, webcam);
|
2022-02-09 23:50:01 +08:00
|
|
|
}
|
2023-03-20 20:23:39 +08:00
|
|
|
}));
|
2022-02-09 23:50:01 +08:00
|
|
|
|
|
|
|
// removing extra video streams already existing in Mongo
|
2023-03-20 20:23:39 +08:00
|
|
|
const videoStreamsToRemove = await VideoStreams.find({
|
2022-02-09 23:50:01 +08:00
|
|
|
meetingId,
|
|
|
|
stream: { $nin: streamsIds },
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
stream: 1,
|
|
|
|
userId: 1,
|
|
|
|
},
|
2023-03-20 20:23:39 +08:00
|
|
|
}).fetchAsynch();
|
2022-02-09 23:50:01 +08:00
|
|
|
|
2023-03-20 20:23:39 +08:00
|
|
|
await Promise.all(videoStreamsToRemove
|
|
|
|
.map(async (videoStream) => {
|
|
|
|
await unsharedWebcam(meetingId, videoStream.userId, videoStream.stream);
|
|
|
|
}));
|
2022-02-09 23:50:01 +08:00
|
|
|
}
|