From b5851aa1524a0b9ff157d3b459b8db35106b3203 Mon Sep 17 00:00:00 2001
From: Anton Georgiev <anto.georgiev@gmail.com>
Date: Wed, 30 Jun 2021 20:08:00 +0000
Subject: [PATCH] removeUser: handle connections in frontends, db changes in
 backend

---
 .../modifiers/clearAuthTokenValidation.js     |  4 +-
 .../api/users/server/modifiers/removeUser.js  | 56 +++++++++----------
 .../startup/server/ClientConnections.js       |  8 ++-
 3 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/bigbluebutton-html5/imports/api/auth-token-validation/server/modifiers/clearAuthTokenValidation.js b/bigbluebutton-html5/imports/api/auth-token-validation/server/modifiers/clearAuthTokenValidation.js
index 75594efe28..2b93b3a10f 100644
--- a/bigbluebutton-html5/imports/api/auth-token-validation/server/modifiers/clearAuthTokenValidation.js
+++ b/bigbluebutton-html5/imports/api/auth-token-validation/server/modifiers/clearAuthTokenValidation.js
@@ -8,7 +8,9 @@ export default function clearAuthTokenValidation(meetingId) {
       Logger.info(`Error when removing auth-token-validation for meeting=${meetingId}`);
     }
 
-    ClientConnections.removeMeeting(meetingId);
+    if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
+      ClientConnections.removeMeeting(meetingId);
+    }
     Logger.info(`Cleared AuthTokenValidation (${meetingId})`);
   });
 }
diff --git a/bigbluebutton-html5/imports/api/users/server/modifiers/removeUser.js b/bigbluebutton-html5/imports/api/users/server/modifiers/removeUser.js
index 060be44012..c6308063ed 100755
--- a/bigbluebutton-html5/imports/api/users/server/modifiers/removeUser.js
+++ b/bigbluebutton-html5/imports/api/users/server/modifiers/removeUser.js
@@ -18,45 +18,39 @@ export default function removeUser(meetingId, userId) {
   check(meetingId, String);
   check(userId, String);
 
-  const userToRemove = Users.findOne({ userId, meetingId });
-
-  if (userToRemove) {
-    const { presenter } = userToRemove;
-    if (presenter) {
-      stopWatchingExternalVideoSystemCall({ meetingId, requesterUserId: 'system-presenter-was-removed' });
+  try {
+    if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
+      const sessionUserId = `${meetingId}-${userId}`;
+      ClientConnections.removeClientConnection(`${meetingId}--${userId}`);
+      clearAllSessions(sessionUserId);
+
+      // 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') {
+        return;
+      }
     }
-  }
 
-  const selector = {
-    meetingId,
-    userId,
-  };
+    const selector = {
+      meetingId,
+      userId,
+    };
 
-  try {
-    setloggedOutStatus(userId, meetingId, true);
-    VideoStreams.remove({ meetingId, userId });
-    const sessionUserId = `${meetingId}-${userId}`;
+    const userToRemove = Users.findOne({ userId, meetingId });
 
-    ClientConnections.removeClientConnection(`${meetingId}--${userId}`);
+    if (userToRemove) {
+      const { presenter } = userToRemove;
+      if (presenter) {
+        stopWatchingExternalVideoSystemCall({ meetingId, requesterUserId: 'system-presenter-was-removed' });
+      }
+    }
 
-    clearAllSessions(sessionUserId);
+    setloggedOutStatus(userId, meetingId, true);
+    VideoStreams.remove({ meetingId, userId });
 
     clearUserInfoForRequester(meetingId, userId);
 
-    /*
-    Timeout added to reduce the probability that "userRemove" happens too close to "ejectUser",
-    redirecting user to the wrong component.
-    This is a workaround and should be removed as soon as a better fix is made
-    see: https://github.com/bigbluebutton/bigbluebutton/pull/12057
-    */
-    const DELAY_USER_REMOVAL_TIMEOUT_MS = 1000;
-
-    Meteor.wrapAsync((callback) => {
-      Meteor.setTimeout(() => {
-        Users.remove(selector);
-        callback();
-      }, DELAY_USER_REMOVAL_TIMEOUT_MS);
-    })();
+    Users.remove(selector);
 
     Logger.info(`Removed user id=${userId} meeting=${meetingId}`);
   } catch (err) {
diff --git a/bigbluebutton-html5/imports/startup/server/ClientConnections.js b/bigbluebutton-html5/imports/startup/server/ClientConnections.js
index a52b1462c6..2e575bee7b 100644
--- a/bigbluebutton-html5/imports/startup/server/ClientConnections.js
+++ b/bigbluebutton-html5/imports/startup/server/ClientConnections.js
@@ -171,6 +171,10 @@ class ClientConnections {
 
 }
 
-const ClientConnectionsSingleton = new ClientConnections();
+if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
+  Logger.info("ClientConnectionsSingleton was created")
 
-export default ClientConnectionsSingleton;
+  const ClientConnectionsSingleton = new ClientConnections();
+
+  export default ClientConnectionsSingleton;
+}
-- 
GitLab