From 776fc1e53629a7328ff5eee998674b0e68a94595 Mon Sep 17 00:00:00 2001
From: Guilherme Gazzo <guilhermegazzo@gmail.com>
Date: Fri, 26 Jul 2024 15:55:49 -0300
Subject: [PATCH] chore(client): stop replacing  `Meteor.user` (#32910)

---
 apps/meteor/app/models/client/models/Users.ts    | 10 +++++++++-
 .../client/models/CachedCollectionManager.ts     | 16 +---------------
 apps/meteor/client/startup/startup.ts            |  5 +++++
 apps/meteor/client/views/root/SAMLLoginRoute.tsx | 16 +---------------
 .../client/views/root/SAMLLoginRoute.spec.tsx    |  8 ++++----
 5 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/apps/meteor/app/models/client/models/Users.ts b/apps/meteor/app/models/client/models/Users.ts
index 089486f3a20..26d333cc8bb 100644
--- a/apps/meteor/app/models/client/models/Users.ts
+++ b/apps/meteor/app/models/client/models/Users.ts
@@ -30,5 +30,13 @@ class UsersCollection extends Mongo.Collection<IUser> {
 	}
 }
 
+Object.assign(Meteor.users, {
+	_connection: undefined,
+	findOneById: UsersCollection.prototype.findOneById,
+	isUserInRole: UsersCollection.prototype.isUserInRole,
+	findUsersInRoles: UsersCollection.prototype.findUsersInRoles,
+	remove: UsersCollection.prototype.remove,
+});
+
 /** @deprecated */
-export const Users = new UsersCollection();
+export const Users = Meteor.users as UsersCollection;
diff --git a/apps/meteor/app/ui-cached-collection/client/models/CachedCollectionManager.ts b/apps/meteor/app/ui-cached-collection/client/models/CachedCollectionManager.ts
index 845731e2450..52d6661d706 100644
--- a/apps/meteor/app/ui-cached-collection/client/models/CachedCollectionManager.ts
+++ b/apps/meteor/app/ui-cached-collection/client/models/CachedCollectionManager.ts
@@ -11,15 +11,12 @@ class CachedCollectionManager extends Emitter<{ reconnect: void; login: string |
 
 	private _syncEnabled: boolean;
 
-	private logged: boolean;
-
 	private step: number;
 
 	constructor() {
 		super();
 		this.items = [];
 		this._syncEnabled = false;
-		this.logged = false;
 
 		const { _unstoreLoginToken } = Accounts;
 		Accounts._unstoreLoginToken = (...args) => {
@@ -40,14 +37,6 @@ class CachedCollectionManager extends Emitter<{ reconnect: void; login: string |
 					return connected && this.emit('reconnect');
 			}
 		});
-
-		Tracker.autorun(() => {
-			const uid = Meteor.userId();
-			this.logged = uid !== null;
-			if (this.logged) {
-				this.emit('login', uid);
-			}
-		});
 	}
 
 	register(cachedCollection: CachedCollection<any>) {
@@ -80,10 +69,7 @@ class CachedCollectionManager extends Emitter<{ reconnect: void; login: string |
 	}
 
 	onLogin(cb: () => void) {
-		this.on('login', cb);
-		if (this.logged) {
-			cb();
-		}
+		Accounts.onLogin(cb);
 	}
 }
 
diff --git a/apps/meteor/client/startup/startup.ts b/apps/meteor/client/startup/startup.ts
index 2541529a1ae..45c102a72bf 100644
--- a/apps/meteor/client/startup/startup.ts
+++ b/apps/meteor/client/startup/startup.ts
@@ -28,10 +28,15 @@ Meteor.startup(() => {
 			removeLocalUserData();
 			return;
 		}
+
 		if (!Meteor.status().connected) {
 			return;
 		}
 
+		if (Meteor.loggingIn()) {
+			return;
+		}
+
 		const user = await synchronizeUserData(uid);
 		if (!user) {
 			return;
diff --git a/apps/meteor/client/views/root/SAMLLoginRoute.tsx b/apps/meteor/client/views/root/SAMLLoginRoute.tsx
index 61ceb82a5ee..2e12b036a4b 100644
--- a/apps/meteor/client/views/root/SAMLLoginRoute.tsx
+++ b/apps/meteor/client/views/root/SAMLLoginRoute.tsx
@@ -1,5 +1,5 @@
 import type { LocationPathname } from '@rocket.chat/ui-contexts';
-import { useRouter, useToastMessageDispatch, useUserId, useAbsoluteUrl } from '@rocket.chat/ui-contexts';
+import { useRouter, useToastMessageDispatch, useAbsoluteUrl } from '@rocket.chat/ui-contexts';
 import { Meteor } from 'meteor/meteor';
 import { useEffect } from 'react';
 
@@ -38,20 +38,6 @@ const SAMLLoginRoute = () => {
 		});
 	}, [dispatchToastMessage, rootUrl, router]);
 
-	const userId = useUserId();
-	useEffect(() => {
-		if (!userId) {
-			return;
-		}
-
-		router.navigate(
-			{
-				pathname: '/home',
-			},
-			{ replace: true },
-		);
-	}, [userId, router]);
-
 	return null;
 };
 
diff --git a/apps/meteor/tests/unit/client/views/root/SAMLLoginRoute.spec.tsx b/apps/meteor/tests/unit/client/views/root/SAMLLoginRoute.spec.tsx
index bfd0d9ec0a6..bca7538e46c 100644
--- a/apps/meteor/tests/unit/client/views/root/SAMLLoginRoute.spec.tsx
+++ b/apps/meteor/tests/unit/client/views/root/SAMLLoginRoute.spec.tsx
@@ -19,7 +19,7 @@ describe('views/root/SAMLLoginRoute', () => {
 		loginWithSamlTokenStub.callsFake((_token, callback) => callback());
 	});
 
-	it('should redirect to /home when userId is not null', async () => {
+	it('should redirect to /home', async () => {
 		render(
 			<MockedServerContext>
 				<MockedUserContext>
@@ -30,7 +30,7 @@ describe('views/root/SAMLLoginRoute', () => {
 			</MockedServerContext>,
 		);
 
-		expect(navigateStub.calledTwice).toBe(true);
+		expect(navigateStub.calledOnce).toBe(true);
 		expect(
 			navigateStub.calledWith(
 				sinon.match({
@@ -40,7 +40,7 @@ describe('views/root/SAMLLoginRoute', () => {
 		).toBe(true);
 	});
 
-	it('should redirect to /home when userId is null and redirectUrl is not within the workspace domain', async () => {
+	it('should redirect to /home when redirectUrl is not within the workspace domain', async () => {
 		render(
 			<MockedServerContext>
 				<RouterContextMock searchParameters={{ redirectUrl: 'http://rocket.chat' }} navigate={navigateStub}>
@@ -58,7 +58,7 @@ describe('views/root/SAMLLoginRoute', () => {
 		).toBe(true);
 	});
 
-	it('should redirect to the provided redirectUrl when userId is null and redirectUrl is within the workspace domain', async () => {
+	it('should redirect to the provided redirectUrl when redirectUrl is within the workspace domain', async () => {
 		render(
 			<MockedServerContext>
 				<RouterContextMock searchParameters={{ redirectUrl: 'http://localhost:3000/invite/test' }} navigate={navigateStub}>
-- 
GitLab