2023-05-11 04:03:20 +08:00
|
|
|
import { check } from 'meteor/check';
|
2023-06-09 03:24:25 +08:00
|
|
|
import Timer from '/imports/api/timer';
|
2023-05-03 04:46:33 +08:00
|
|
|
import RedisPubSub from '/imports/startup/server/redis';
|
2023-05-11 04:03:20 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
|
|
|
|
|
|
|
export default function stopTimer() {
|
2023-05-03 04:46:33 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'StopTimerReqMsg';
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { meetingId, requesterUserId } = extractCredentials(this.userId);
|
|
|
|
check(meetingId, String);
|
|
|
|
check(requesterUserId, String);
|
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
const timer = Timer.findOne(
|
|
|
|
{ meetingId },
|
2023-06-09 03:24:25 +08:00
|
|
|
{
|
|
|
|
fields:
|
2023-05-03 04:46:33 +08:00
|
|
|
{
|
|
|
|
stopwatch: 1,
|
|
|
|
time: 1,
|
|
|
|
accumulated: 1,
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
2023-05-11 04:03:20 +08:00
|
|
|
},
|
2023-05-03 04:46:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
const {
|
|
|
|
timestamp,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const accumulated = timer.accumulated + (now - timestamp);
|
|
|
|
|
|
|
|
const payload = {
|
2023-06-09 03:24:25 +08:00
|
|
|
accumulated,
|
2023-05-03 04:46:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
|
|
|
|
} else {
|
2023-06-09 03:24:25 +08:00
|
|
|
Logger.warn(`Could not stop timer for meeting=${meetingId}, timer not found`);
|
2023-05-03 04:46:33 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Stopping timer: ${err}`);
|
2023-05-11 04:03:20 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 04:46:33 +08:00
|
|
|
|
|
|
|
// This method should only be used by the server
|
|
|
|
export function sysStopTimer(meetingId) {
|
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'StopTimerReqMsg';
|
|
|
|
const USER_ID = 'nodeJSapp';
|
|
|
|
|
|
|
|
try {
|
|
|
|
check(meetingId, String);
|
|
|
|
const now = Date.now();
|
|
|
|
const timer = Timer.findOne(
|
|
|
|
{ meetingId },
|
2023-06-09 03:24:25 +08:00
|
|
|
{
|
|
|
|
fields:
|
2023-05-03 04:46:33 +08:00
|
|
|
{
|
|
|
|
stopwatch: 1,
|
|
|
|
time: 1,
|
|
|
|
accumulated: 1,
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
const {
|
|
|
|
timestamp,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
const accumulated = timer.accumulated + (now - timestamp);
|
|
|
|
|
|
|
|
const payload = {
|
2023-06-09 03:24:25 +08:00
|
|
|
accumulated,
|
2023-05-03 04:46:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, USER_ID, payload);
|
|
|
|
} else {
|
2023-06-09 03:24:25 +08:00
|
|
|
Logger.warn(`Could not stop timer for meeting=${meetingId}, timer not found`);
|
2023-05-03 04:46:33 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Stopping timer: ${err}`);
|
|
|
|
}
|
2023-06-09 03:24:25 +08:00
|
|
|
}
|