bigbluebutton-Github/bigbluebutton-html5/imports/api/timer/server/modifiers/updateTimer.js

195 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-05-11 04:03:20 +08:00
import { check } from 'meteor/check';
import Timer from '/imports/api/timer';
import Logger from '/imports/startup/server/logger';
import Users from '/imports/api/users';
2020-04-26 03:03:35 +08:00
import { getDefaultTime } from '/imports/api/timer/server/helpers';
2023-05-11 04:03:20 +08:00
const getActivateModifier = () => {
2020-04-26 03:03:35 +08:00
const time = getDefaultTime();
check(time, Number);
2023-05-11 04:03:20 +08:00
return {
$set: {
stopwatch: true,
active: true,
running: false,
2020-04-26 03:03:35 +08:00
time,
2023-05-11 04:03:20 +08:00
accumulated: 0,
timestamp: 0,
music: false,
ended: 0,
2023-05-11 04:03:20 +08:00
},
};
};
const getDeactivateModifier = () => {
return {
2020-06-13 02:00:24 +08:00
$set: {
active: false,
running: false,
ended: 0,
2020-06-13 02:00:24 +08:00
},
2023-05-11 04:03:20 +08:00
};
};
const getResetModifier = () => {
return {
$set: {
accumulated: 0,
2020-04-26 03:03:35 +08:00
timestamp: Date.now(),
ended: 0,
2023-05-11 04:03:20 +08:00
},
};
};
const handleTimerEndedNotifications = (fields, meetingId, handle) => {
const meetingUsers = Users.find({
meetingId,
validated: true,
}).count();
if (fields.running === false) {
handle.stop();
}
if (fields.ended >= Math.round(0.9 * meetingUsers)) {
const accumulated = 0;
updateTimer('stop', meetingId, 0, false, accumulated);
}
};
const setTimerEndObserver = (meetingId) => {
const { stopwatch } = Timer.findOne({ meetingId });
if (stopwatch === false) {
const meetingTimer = Timer.find(
{ meetingId },
{ fields: { ended: 1, running: 1 } },
);
const handle = meetingTimer.observeChanges({
changed: (id, fields) => {
handleTimerEndedNotifications(fields, meetingId, handle);
},
});
}
};
2020-04-26 03:03:35 +08:00
const getStartModifier = () => {
2023-05-11 04:03:20 +08:00
return {
$set: {
running: true,
timestamp: Date.now(),
ended: 0,
2023-05-11 04:03:20 +08:00
},
};
};
const getStopModifier = (accumulated) => {
return {
$set: {
running: false,
accumulated,
timestamp: 0,
ended: 0,
2023-05-11 04:03:20 +08:00
},
};
};
2020-04-26 03:03:35 +08:00
const getSwitchModifier = (stopwatch) => {
2023-05-11 04:03:20 +08:00
return {
$set: {
2020-04-26 03:03:35 +08:00
stopwatch,
running: false,
accumulated: 0,
timestamp: 0,
music: false,
ended: 0,
2020-04-26 03:03:35 +08:00
},
};
};
const getSetModifier = (time) => {
return {
$set: {
running: false,
accumulated: 0,
timestamp: 0,
time,
2023-05-11 04:03:20 +08:00
},
};
};
const getMusicModifier = (music) => {
return {
$set: {
music,
},
};
};
const getEndedModifier = () => {
return {
$inc: {
ended: 1,
},
};
};
2020-04-26 03:03:35 +08:00
export default function updateTimer(action, meetingId, time = 0, stopwatch = true, accumulated = 0, music = false) {
2023-05-11 04:03:20 +08:00
check(action, String);
check(meetingId, String);
2020-04-26 03:03:35 +08:00
check(time, Number);
check(stopwatch, Boolean);
check(accumulated, Number);
check(music, Boolean);
2023-05-11 04:03:20 +08:00
const selector = {
meetingId,
};
let modifier;
switch(action) {
case 'activate':
modifier = getActivateModifier();
break;
case 'deactivate':
modifier = getDeactivateModifier();
break;
case 'reset':
modifier = getResetModifier();
break;
case 'start':
setTimerEndObserver(meetingId);
2020-04-26 03:03:35 +08:00
modifier = getStartModifier();
2023-05-11 04:03:20 +08:00
break;
case 'stop':
modifier = getStopModifier(accumulated);
break;
2020-04-26 03:03:35 +08:00
case 'switch':
modifier = getSwitchModifier(stopwatch);
break;
case 'set':
modifier = getSetModifier(time);
break;
case 'music':
modifier = getMusicModifier(music);
break;
case 'ended':
modifier = getEndedModifier();
break;
2023-05-11 04:03:20 +08:00
default:
Logger.error(`Unhandled timer action=${action}`);
}
const cb = (err) => {
if (err) {
return Logger.error(`Updating timer at collection: ${err}`);
}
2020-04-27 01:44:01 +08:00
return Logger.debug(`Updated timer action=${action} meetingId=${meetingId}`);
2023-05-11 04:03:20 +08:00
};
return Timer.update(selector, modifier, cb);
}