From 2f03177b5dc4ff77da2d1556ac95a8e377931a49 Mon Sep 17 00:00:00 2001 From: Abhinav Kumar Date: Sat, 17 Aug 2024 06:17:04 +0530 Subject: [PATCH] refactor!: removed roomNameExists method (#32484) Co-authored-by: Marcos Spessatto Defendi --- apps/meteor/app/api/server/v1/rooms.ts | 4 ++- apps/meteor/server/methods/index.ts | 1 - apps/meteor/server/methods/roomNameExists.ts | 34 -------------------- apps/meteor/tests/end-to-end/api/rooms.ts | 17 +++++++++- 4 files changed, 19 insertions(+), 37 deletions(-) delete mode 100644 apps/meteor/server/methods/roomNameExists.ts diff --git a/apps/meteor/app/api/server/v1/rooms.ts b/apps/meteor/app/api/server/v1/rooms.ts index eaf350c0f188..4a42adf6f5bd 100644 --- a/apps/meteor/app/api/server/v1/rooms.ts +++ b/apps/meteor/app/api/server/v1/rooms.ts @@ -89,7 +89,9 @@ API.v1.addRoute( async get() { const { roomName } = this.queryParams; - return API.v1.success({ exists: await Meteor.callAsync('roomNameExists', roomName) }); + const room = await Rooms.findOneByName(roomName, { projection: { _id: 1 } }); + + return API.v1.success({ exists: !!room }); }, }, ); diff --git a/apps/meteor/server/methods/index.ts b/apps/meteor/server/methods/index.ts index a3b4b747f715..8182c10f5008 100644 --- a/apps/meteor/server/methods/index.ts +++ b/apps/meteor/server/methods/index.ts @@ -41,7 +41,6 @@ import './removeUserFromRoom'; import './reportMessage'; import './requestDataDownload'; import './resetAvatar'; -import './roomNameExists'; import './saveUserPreferences'; import './saveUserProfile'; import './sendConfirmationEmail'; diff --git a/apps/meteor/server/methods/roomNameExists.ts b/apps/meteor/server/methods/roomNameExists.ts deleted file mode 100644 index f510391ddd8e..000000000000 --- a/apps/meteor/server/methods/roomNameExists.ts +++ /dev/null @@ -1,34 +0,0 @@ -import type { ServerMethods } from '@rocket.chat/ddp-client'; -import { Rooms } from '@rocket.chat/models'; -import { check } from 'meteor/check'; -import { Meteor } from 'meteor/meteor'; - -import { methodDeprecationLogger } from '../../app/lib/server/lib/deprecationWarningLogger'; - -declare module '@rocket.chat/ddp-client' { - // eslint-disable-next-line @typescript-eslint/naming-convention - interface ServerMethods { - roomNameExists(roomName: string): boolean; - } -} - -Meteor.methods({ - async roomNameExists(roomName) { - check(roomName, String); - - methodDeprecationLogger.method('roomNameExists', '7.0.0'); - - if (!Meteor.userId()) { - throw new Meteor.Error('error-invalid-user', 'Invalid user', { - method: 'roomExists', - }); - } - const room = await Rooms.findOneByName(roomName, { projection: { _id: 1 } }); - - if (room === undefined || room === null) { - return false; - } - - return true; - }, -}); diff --git a/apps/meteor/tests/end-to-end/api/rooms.ts b/apps/meteor/tests/end-to-end/api/rooms.ts index 5047af7956d8..e5a62137d42c 100644 --- a/apps/meteor/tests/end-to-end/api/rooms.ts +++ b/apps/meteor/tests/end-to-end/api/rooms.ts @@ -941,7 +941,22 @@ describe('[Rooms]', () => { .end(done); }); - it('should return an error when send an invalid room', (done) => { + it('should return false if this room name does not exist', (done) => { + void request + .get(api('rooms.nameExists')) + .set(credentials) + .query({ + roomName: 'foo', + }) + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('exists', false); + }) + .end(done); + }); + + it('should return an error when the require parameter (roomName) is not provided', (done) => { void request .get(api('rooms.nameExists')) .set(credentials)