bigbluebutton-Github/bigbluebutton-html5/imports/api/users/server/modifiers/removeUser.js
2022-11-21 13:07:30 -03:00

82 lines
2.9 KiB
JavaScript
Executable File

import { check } from 'meteor/check';
import Users from '/imports/api/users';
import VideoStreams from '/imports/api/video-streams';
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';
import UsersPersistentData from '/imports/api/users-persistent-data';
import userEjected from '/imports/api/users/server/modifiers/userEjected';
import clearVoiceUser from '/imports/api/voice-users/server/modifiers/clearVoiceUser';
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 function removeUser(body, meetingId) {
const { intId: userId, reasonCode } = body;
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) {
userEjected(meetingId, userId, reasonCode);
}
setloggedOutStatus(userId, meetingId, true);
VideoStreams.remove({ meetingId, userId });
clearUserInfoForRequester(meetingId, userId);
const currentUser = UsersPersistentData.findOne({ userId, meetingId });
const hasMessages = currentUser?.shouldPersist?.hasMessages?.public || currentUser?.shouldPersist?.hasMessages?.private;
const hasConnectionStatus = currentUser?.shouldPersist?.hasConnectionStatus;
if (!hasMessages && !hasConnectionStatus) {
UsersPersistentData.remove(selector);
}
Users.remove(selector);
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);
if (queryCurrentUser.count() === 0) {
disconnectUser(meetingId, userId);
} else {
const queryUserObserver = queryCurrentUser.observeChanges({
removed() {
disconnectUser(meetingId, userId);
queryUserObserver.stop();
},
});
}
}
Logger.info(`Removed user id=${userId} meeting=${meetingId}`);
} catch (err) {
Logger.error(`Removing user from Users collection: ${err}`);
}
}