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';
|
2021-09-14 02:16:07 +08:00
|
|
|
import Users from '/imports/api/users';
|
2022-02-02 03:05:02 +08:00
|
|
|
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,
|
2022-02-02 03:05:02 +08:00
|
|
|
track: TRACKS[0],
|
2021-09-14 02:16:07 +08:00
|
|
|
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,
|
2021-09-14 02:16:07 +08:00
|
|
|
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(),
|
2021-09-14 02:16:07 +08:00
|
|
|
ended: 0,
|
2023-05-11 04:03:20 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-14 02:16:07 +08:00
|
|
|
const handleTimerEndedNotifications = (fields, meetingId, handle) => {
|
|
|
|
const meetingUsers = Users.find({
|
|
|
|
meetingId,
|
2021-09-18 04:38:22 +08:00
|
|
|
connectionStatus: 'online',
|
2021-09-14 02:16:07 +08:00
|
|
|
}).count();
|
|
|
|
|
|
|
|
if (fields.running === false) {
|
|
|
|
handle.stop();
|
|
|
|
}
|
|
|
|
|
2021-09-18 04:38:22 +08:00
|
|
|
//Timer is stopped when at least 90% of users online in the meetinng notify that it ended.
|
2021-09-14 02:16:07 +08:00
|
|
|
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(),
|
2021-09-14 02:16:07 +08:00
|
|
|
ended: 0,
|
2023-05-11 04:03:20 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getStopModifier = (accumulated) => {
|
|
|
|
return {
|
|
|
|
$set: {
|
|
|
|
running: false,
|
|
|
|
accumulated,
|
|
|
|
timestamp: 0,
|
2021-09-14 02:16:07 +08:00
|
|
|
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,
|
2022-02-02 03:05:02 +08:00
|
|
|
track: TRACKS[0],
|
2021-09-14 02:16:07 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-02 03:05:02 +08:00
|
|
|
const getTrackModifier = (track) => {
|
2021-09-03 20:44:16 +08:00
|
|
|
return {
|
|
|
|
$set: {
|
2022-02-02 03:05:02 +08:00
|
|
|
track,
|
2021-09-03 20:44:16 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-14 02:16:07 +08:00
|
|
|
const getEndedModifier = () => {
|
|
|
|
return {
|
|
|
|
$inc: {
|
|
|
|
ended: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2020-04-26 03:03:35 +08:00
|
|
|
|
2022-07-14 00:59:02 +08:00
|
|
|
export default function updateTimer(action, meetingId, requesterUserId, time = 0, stopwatch = true, accumulated = 0, track = TRACKS[0]) {
|
2023-05-11 04:03:20 +08:00
|
|
|
check(action, String);
|
|
|
|
check(meetingId, String);
|
2022-07-14 00:59:02 +08:00
|
|
|
check(requesterUserId, String);
|
2020-04-26 03:03:35 +08:00
|
|
|
check(time, Number);
|
|
|
|
check(stopwatch, Boolean);
|
|
|
|
check(accumulated, Number);
|
2022-02-02 03:05:02 +08:00
|
|
|
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':
|
2021-09-14 02:16:07 +08:00
|
|
|
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;
|
2022-02-02 03:05:02 +08:00
|
|
|
case 'track':
|
|
|
|
modifier = getTrackModifier(track);
|
2021-09-03 20:44:16 +08:00
|
|
|
break;
|
2021-09-14 02:16:07 +08:00
|
|
|
case 'ended':
|
|
|
|
modifier = getEndedModifier();
|
|
|
|
break;
|
2023-05-11 04:03:20 +08:00
|
|
|
default:
|
|
|
|
Logger.error(`Unhandled timer action=${action}`);
|
|
|
|
}
|
|
|
|
|
2022-07-14 00:59:02 +08:00
|
|
|
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
|
|
|
}
|
2022-07-14 00:59:02 +08:00
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Updating timer: ${err}`);
|
|
|
|
}
|
2023-05-11 04:03:20 +08:00
|
|
|
}
|