diff --git a/.changeset/old-cobras-serve.md b/.changeset/old-cobras-serve.md new file mode 100644 index 0000000000000..6998eb7a2b6fe --- /dev/null +++ b/.changeset/old-cobras-serve.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': patch +--- + +Fixes an issue where private channels that a user belongs to were not shown in Directory search results. diff --git a/apps/meteor/app/api/server/v1/misc.ts b/apps/meteor/app/api/server/v1/misc.ts index 00c7262acdad9..ecdd8c58b052c 100644 --- a/apps/meteor/app/api/server/v1/misc.ts +++ b/apps/meteor/app/api/server/v1/misc.ts @@ -364,6 +364,7 @@ API.v1.addRoute( const sortBy = sort ? Object.keys(sort)[0] : undefined; const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc'; + const user = await Users.findOneById(this.userId, { projection: { __rooms: 1 } }); const result = await browseChannelsMethod( { ...filter, @@ -372,7 +373,7 @@ API.v1.addRoute( offset: Math.max(0, offset), limit: Math.max(0, count), }, - this.user, + user, ); if (!result) { diff --git a/apps/meteor/server/methods/browseChannels.ts b/apps/meteor/server/methods/browseChannels.ts index 2b22d2cd74ed1..965aa7669ea7a 100644 --- a/apps/meteor/server/methods/browseChannels.ts +++ b/apps/meteor/server/methods/browseChannels.ts @@ -1,5 +1,5 @@ import { Team } from '@rocket.chat/core-services'; -import type { IRoom, IUser } from '@rocket.chat/core-typings'; +import type { IUser, AtLeast } from '@rocket.chat/core-typings'; import type { ServerMethods } from '@rocket.chat/ddp-client'; import { Rooms, Users, Subscriptions } from '@rocket.chat/models'; import { escapeRegExp } from '@rocket.chat/string-helpers'; @@ -45,7 +45,7 @@ const sortUsers = (field: string, direction: 'asc' | 'desc'): Record, canViewAnon: boolean, searchTerm: string, sort: Record, @@ -119,7 +119,7 @@ const getChannelsCountForTeam = mem((teamId) => Rooms.countByTeamId(teamId), { }); const getTeams = async ( - user: IUser, + user: AtLeast, searchTerm: string, sort: Record, pagination: { @@ -247,7 +247,7 @@ const findUsers = async ({ }; const getUsers = async ( - user: IUser | undefined, + user: AtLeast | undefined, text: string, workspace: string, sort: Record, @@ -299,7 +299,7 @@ export const browseChannelsMethod = async ( offset = 0, limit = 10, }: BrowseChannelsParams, - user: IUser | undefined | null, + user: AtLeast | undefined | null, ) => { const searchTerm = trim(escapeRegExp(text)); diff --git a/apps/meteor/tests/end-to-end/api/miscellaneous.ts b/apps/meteor/tests/end-to-end/api/miscellaneous.ts index ffa8f9d4586e1..de022f7a2f7dd 100644 --- a/apps/meteor/tests/end-to-end/api/miscellaneous.ts +++ b/apps/meteor/tests/end-to-end/api/miscellaneous.ts @@ -215,6 +215,7 @@ describe('miscellaneous', () => { describe('/directory', () => { let user: TestUser; let testChannel: IRoom; + let testGroup: IRoom; let normalUserCredentials: Credentials; const teamName = `new-team-name-${Date.now()}` as const; let teamCreated: ITeam; @@ -223,8 +224,11 @@ describe('miscellaneous', () => { await updatePermission('create-team', ['admin', 'user']); user = await createUser(); normalUserCredentials = await doLogin(user.username, password); - testChannel = (await createRoom({ name: `channel.test.${Date.now()}`, type: 'c' })).body.channel; - teamCreated = await createTeam(normalUserCredentials, teamName, TEAM_TYPE.PUBLIC); + [testChannel, testGroup, teamCreated] = await Promise.all([ + createRoom({ name: `channel.test.${Date.now()}`, type: 'c' }).then((res) => res.body.channel), + createRoom({ name: `group.test.${Date.now()}`, type: 'p' }).then((res) => res.body.group), + createTeam(normalUserCredentials, teamName, TEAM_TYPE.PUBLIC), + ]); }); after(async () => { @@ -232,6 +236,7 @@ describe('miscellaneous', () => { deleteTeam(normalUserCredentials, teamName), deleteUser(user), deleteRoom({ type: 'c', roomId: testChannel._id }), + deleteRoom({ type: 'p', roomId: testGroup._id }), updatePermission('create-team', ['admin', 'user']), ]); }); @@ -308,6 +313,31 @@ describe('miscellaneous', () => { }) .end(done); }); + + it('should return private group when search by channel and execute successfully', async () => { + await request + .get(api('directory')) + .set(credentials) + .query({ + text: testGroup.name, + type: 'channels', + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('offset'); + expect(res.body).to.have.property('total'); + expect(res.body).to.have.property('count'); + expect(res.body).to.have.property('result').and.to.be.an('array'); + expect(res.body.result[0]).to.have.property('_id', testGroup._id); + expect(res.body.result[0]).to.have.property('t', 'p'); + expect(res.body.result[0]).to.have.property('name'); + expect(res.body.result[0]).to.have.property('usersCount').and.to.be.an('number'); + expect(res.body.result[0]).to.have.property('ts'); + }); + }); + it('should return an array(result) when search by channel with sort params correctly and execute successfully', (done) => { void request .get(api('directory'))