bigbluebutton-Github/bigbluebutton-html5/imports/api/voice-users/server/modifiers/updateVoiceUser.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-08-01 03:36:41 +08:00
import { Match, check } from 'meteor/check';
import Logger from '/imports/startup/server/logger';
import VoiceUsers from '/imports/api/voice-users';
2017-08-01 03:36:41 +08:00
import flat from 'flat';
2019-11-15 01:07:02 +08:00
import { spokeTimeoutHandles, clearSpokeTimeout } from '/imports/api/common/server/helpers';
2017-08-01 03:36:41 +08:00
2019-11-20 23:08:36 +08:00
const TALKING_TIMEOUT = 6000;
2019-11-14 00:24:38 +08:00
export default async function updateVoiceUser(meetingId, voiceUser) {
2017-08-01 03:36:41 +08:00
check(meetingId, String);
check(voiceUser, {
intId: String,
voiceUserId: String,
talking: Match.Maybe(Boolean),
muted: Match.Maybe(Boolean),
2017-08-01 21:10:12 +08:00
voiceConf: String,
joined: Match.Maybe(Boolean),
floor: Match.Maybe(Boolean),
lastFloorTime: Match.Maybe(String),
2017-08-01 03:36:41 +08:00
});
2017-08-02 00:04:20 +08:00
const { intId } = voiceUser;
2017-08-01 03:36:41 +08:00
const selector = {
meetingId,
2017-08-02 00:04:20 +08:00
intId,
2017-08-01 03:36:41 +08:00
};
const modifier = {
2017-08-01 03:36:41 +08:00
$set: Object.assign(
flat(voiceUser),
),
};
if (voiceUser.talking) {
const user = await VoiceUsers.findOneAsync({ meetingId, intId }, {
2019-11-20 23:08:36 +08:00
fields: {
startTime: 1,
},
});
if (user && !user.startTime) modifier.$set.startTime = Date.now();
2019-11-14 00:24:38 +08:00
modifier.$set.spoke = true;
modifier.$set.endTime = null;
2019-11-15 01:07:02 +08:00
clearSpokeTimeout(meetingId, intId);
}
2019-11-14 00:24:38 +08:00
if (!voiceUser.talking) {
const timeoutHandle = Meteor.setTimeout(async () => {
const user = await VoiceUsers.findOneAsync({ meetingId, intId }, {
2019-11-14 00:24:38 +08:00
fields: {
endTime: 1,
talking: 1,
},
});
if (user) {
const { endTime, talking } = user;
2019-11-14 06:11:46 +08:00
const spokeDelay = ((Date.now() - endTime) < TALKING_TIMEOUT);
2019-11-14 00:24:38 +08:00
if (talking || spokeDelay) return;
modifier.$set.spoke = false;
2019-11-20 23:08:36 +08:00
modifier.$set.startTime = null;
try {
const numberAffected = await VoiceUsers.updateAsync(selector, modifier);
if (numberAffected) {
Logger.debug('Update voiceUser', { voiceUser: intId, meetingId });
}
} catch (err) {
Logger.error(`Update voiceUser=${intId}: ${err}`);
}
2019-11-14 00:24:38 +08:00
}
}, TALKING_TIMEOUT);
2019-11-15 01:07:02 +08:00
spokeTimeoutHandles[`${meetingId}-${intId}`] = timeoutHandle;
2019-11-14 06:11:46 +08:00
modifier.$set.endTime = Date.now();
2019-11-14 00:24:38 +08:00
}
try {
const numberAffected = await VoiceUsers.updateAsync(selector, modifier);
if (numberAffected) {
Logger.debug('Update voiceUser', { voiceUser: intId, meetingId });
}
} catch (err) {
Logger.error(`Update voiceUser=${intId}: ${err}`);
}
2017-08-01 03:36:41 +08:00
}