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

84 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-02-23 02:59:47 +08:00
import { check } from 'meteor/check';
import Users from '/imports/api/users';
2019-09-07 00:50:31 +08:00
import VideoStreams from '/imports/api/video-streams';
2017-02-23 02:59:47 +08:00
import Logger from '/imports/startup/server/logger';
import setloggedOutStatus from '/imports/api/users-persistent-data/server/modifiers/setloggedOutStatus';
import clearUserInfoForRequester from '/imports/api/users-infos/server/modifiers/clearUserInfoForRequester';
import ClientConnections from '/imports/startup/server/ClientConnections';
2022-02-02 21:53:23 +08:00
import UsersPersistentData from '/imports/api/users-persistent-data';
2022-04-02 04:30:39 +08:00
import userEjected from '/imports/api/users/server/modifiers/userEjected';
import clearVoiceUser from '/imports/api/voice-users/server/modifiers/clearVoiceUser';
2017-02-23 02:59:47 +08:00
const disconnectUser = (meetingId, userId) => {
const sessionUserId = `${meetingId}--${userId}`;
ClientConnections.removeClientConnection(sessionUserId);
const serverSessions = Meteor.server.sessions;
const interable = serverSessions.values();
for (const session of interable) {
if (session.userId === sessionUserId) {
Logger.info(`Removed session id=${userId} meeting=${meetingId}`);
session.close();
}
}
};
export default async function removeUser(body, meetingId) {
const { intId: userId, reasonCode } = body;
2017-02-23 02:59:47 +08:00
check(meetingId, String);
check(userId, String);
try {
const selector = {
meetingId,
userId,
};
// we don't want to fully process the redis message in frontend
// since the backend is supposed to update Mongo
if ((process.env.BBB_HTML5_ROLE !== 'frontend')) {
if (body.eject) {
await userEjected(meetingId, userId, reasonCode);
}
await setloggedOutStatus(userId, meetingId, true);
await VideoStreams.removeAsync({ meetingId, userId });
await clearUserInfoForRequester(meetingId, userId);
2017-02-23 02:59:47 +08:00
const currentUser = await UsersPersistentData.findOneAsync({ userId, meetingId });
const hasMessages = currentUser?.shouldPersist?.hasMessages?.public ||
currentUser?.shouldPersist?.hasMessages?.private;
2022-02-02 21:53:23 +08:00
const hasConnectionStatus = currentUser?.shouldPersist?.hasConnectionStatus;
if (!hasMessages && !hasConnectionStatus) {
await UsersPersistentData.removeAsync(selector);
2022-02-02 21:53:23 +08:00
}
await Users.removeAsync(selector);
await clearVoiceUser(meetingId, userId);
}
if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
// Wait for user removal and then kill user connections and sessions
const queryCurrentUser = Users.find(selector);
const countUser = await queryCurrentUser.countAsync();
if (countUser === 0) {
disconnectUser(meetingId, userId);
} else {
const queryUserObserver = queryCurrentUser.observeChanges({
removed() {
disconnectUser(meetingId, userId);
queryUserObserver.stop();
},
});
}
2021-12-11 03:13:26 +08:00
}
Logger.info(`Removed user id=${userId} meeting=${meetingId}`);
} catch (err) {
Logger.error(`Removing user from Users collection: ${err}`);
}
2017-06-03 03:25:02 +08:00
}