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

218 lines
4.5 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';
import { TRACKS, 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,
track: TRACKS[0],
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 }).count();
if (fields.running === false) {
handle.stop();
}
if (fields.ended >= meetingUsers) {
updateTimer({
action: 'stop',
meetingId,
stopwatch: false,
});
}
};
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,
track: TRACKS[0],
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 getTrackModifier = (track) => {
return {
$set: {
track,
},
};
};
const getEndedModifier = () => {
return {
$inc: {
ended: 1,
},
};
};
2020-04-26 03:03:35 +08:00
export default function updateTimer({
action,
meetingId,
requesterUserId = 'SYSTEM_REQUEST',
time = 0,
stopwatch = true,
accumulated = 0,
track = TRACKS[0],
}) {
2023-05-11 04:03:20 +08:00
check(action, String);
check(meetingId, String);
check(requesterUserId, String);
2020-04-26 03:03:35 +08:00
check(time, Number);
check(stopwatch, Boolean);
check(accumulated, Number);
check(track, String);
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 'track':
modifier = getTrackModifier(track);
break;
case 'ended':
modifier = getEndedModifier();
break;
2023-05-11 04:03:20 +08:00
default:
Logger.error(`Unhandled timer action=${action}`);
}
try {
const { numberAffected } = Timer.upsert(selector, modifier);
if (numberAffected) {
Logger.verbose(`Updated timer meetingId=${meetingId}`);
if (action === 'switch'){
if (stopwatch === false){ //required if because timer is a boolean
Logger.info(`Timer: meetingId=${meetingId} requesterUserId=${requesterUserId} action=${action} timer `);
}else{
Logger.info(`Timer: meetingId=${meetingId} requesterUserId=${requesterUserId} action=${action} stopwatch `);
}
}else if (action === 'set' && time !== 0) {
Logger.info(`Timer: meetingId=${meetingId} requesterUserId=${requesterUserId} action=${action} ${time}ms`);
}else if (action == 'track') {
Logger.info(`Timer: meetingId=${meetingId} requesterUserId=${requesterUserId} action=${action} changed to ${track}`);
}else
Logger.info(`Timer: meetingId=${meetingId} requesterUserId=${requesterUserId} action=${action}`);
2023-05-11 04:03:20 +08:00
}
} catch (err) {
Logger.error(`Updating timer: ${err}`);
}
2023-05-11 04:03:20 +08:00
}