2018-08-16 00:45:00 +08:00
|
|
|
import VoiceUsers from '/imports/api/voice-users/';
|
|
|
|
import Meetings from '/imports/api/meetings';
|
2018-08-16 20:21:13 +08:00
|
|
|
import addDialInUser from '/imports/api/users/server/modifiers/addDialInUser';
|
2018-08-16 00:45:00 +08:00
|
|
|
import removeVoiceUser from '../modifiers/removeVoiceUser';
|
|
|
|
import updateVoiceUser from '../modifiers/updateVoiceUser';
|
|
|
|
import addVoiceUser from '../modifiers/addVoiceUser';
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
export default async function handleVoiceUsers({ header, body }) {
|
2018-08-16 00:45:00 +08:00
|
|
|
const { voiceUsers } = body;
|
|
|
|
const { meetingId } = header;
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const meeting = await Meetings.findOneAsync({ meetingId }, { fields: { 'voiceProp.voiceConf': 1 } });
|
|
|
|
const usersIds = voiceUsers.map((m) => m.intId);
|
2018-08-16 00:45:00 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const voiceUsersFetch = await VoiceUsers.find({
|
2018-08-16 00:45:00 +08:00
|
|
|
meetingId,
|
|
|
|
intId: { $in: usersIds },
|
2023-04-01 04:40:41 +08:00
|
|
|
}, { fields: { intId: 1 } }).fetchAsync();
|
2018-08-16 00:45:00 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const voiceUsersIdsToUpdate = voiceUsersFetch.map((m) => m.intId);
|
|
|
|
|
|
|
|
await Promise.all(voiceUsers.map(async (voice) => {
|
2018-08-16 00:45:00 +08:00
|
|
|
if (voiceUsersIdsToUpdate.indexOf(voice.intId) >= 0) {
|
|
|
|
// user already exist, then update
|
2023-04-01 04:40:41 +08:00
|
|
|
await updateVoiceUser(meetingId, {
|
2018-08-16 00:45:00 +08:00
|
|
|
intId: voice.intId,
|
|
|
|
voiceUserId: voice.voiceUserId,
|
|
|
|
talking: voice.talking,
|
|
|
|
muted: voice.muted,
|
|
|
|
voiceConf: meeting.voiceProp.voiceConf,
|
|
|
|
joined: true,
|
2021-04-09 21:08:59 +08:00
|
|
|
});
|
2018-08-16 00:45:00 +08:00
|
|
|
} else {
|
|
|
|
// user doesn't exist yet, then add it
|
2023-04-01 04:40:41 +08:00
|
|
|
await addVoiceUser(meetingId, {
|
2018-08-16 00:45:00 +08:00
|
|
|
voiceUserId: voice.voiceUserId,
|
|
|
|
intId: voice.intId,
|
|
|
|
callerName: voice.callerName,
|
|
|
|
callerNum: voice.callerNum,
|
2023-04-19 23:16:35 +08:00
|
|
|
color: voice.color,
|
2018-08-16 00:45:00 +08:00
|
|
|
muted: voice.muted,
|
|
|
|
talking: voice.talking,
|
|
|
|
callingWith: voice.callingWith,
|
|
|
|
listenOnly: voice.listenOnly,
|
|
|
|
voiceConf: meeting.voiceProp.voiceConf,
|
|
|
|
joined: true,
|
|
|
|
});
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
await addDialInUser(meetingId, voice);
|
2018-08-16 00:45:00 +08:00
|
|
|
}
|
2023-04-01 04:40:41 +08:00
|
|
|
}));
|
2018-08-16 00:45:00 +08:00
|
|
|
|
|
|
|
// removing extra users already existing in Mongo
|
2023-04-01 04:40:41 +08:00
|
|
|
const voiceUsersToRemove = await VoiceUsers.find({
|
2018-08-16 00:45:00 +08:00
|
|
|
meetingId,
|
|
|
|
intId: { $nin: usersIds },
|
2023-04-01 04:40:41 +08:00
|
|
|
}, { fields: { voiceUserId: 1, intId: 1 } }).fetchAsync();
|
|
|
|
|
|
|
|
await Promise.all(voiceUsersToRemove.map(async (user) => {
|
|
|
|
await removeVoiceUser(meetingId, {
|
|
|
|
voiceConf: meeting.voiceProp.voiceConf,
|
|
|
|
voiceUserId: user.voiceUserId,
|
|
|
|
intId: user.intId,
|
|
|
|
});
|
2018-08-16 00:45:00 +08:00
|
|
|
}));
|
|
|
|
}
|