Skip to content

Commit 1acf6a3

Browse files
authored
fix: directory search does not return private channels (#37290)
1 parent eb631f6 commit 1acf6a3

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

.changeset/old-cobras-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket.chat/meteor': patch
3+
---
4+
5+
Fixes an issue where private channels that a user belongs to were not shown in Directory search results.

apps/meteor/app/api/server/v1/misc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ API.v1.addRoute(
364364
const sortBy = sort ? Object.keys(sort)[0] : undefined;
365365
const sortDirection = sort && Object.values(sort)[0] === 1 ? 'asc' : 'desc';
366366

367+
const user = await Users.findOneById(this.userId, { projection: { __rooms: 1 } });
367368
const result = await browseChannelsMethod(
368369
{
369370
...filter,
@@ -372,7 +373,7 @@ API.v1.addRoute(
372373
offset: Math.max(0, offset),
373374
limit: Math.max(0, count),
374375
},
375-
this.user,
376+
user,
376377
);
377378

378379
if (!result) {

apps/meteor/server/methods/browseChannels.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Team } from '@rocket.chat/core-services';
2-
import type { IRoom, IUser } from '@rocket.chat/core-typings';
2+
import type { IUser, AtLeast } from '@rocket.chat/core-typings';
33
import type { ServerMethods } from '@rocket.chat/ddp-client';
44
import { Rooms, Users, Subscriptions } from '@rocket.chat/models';
55
import { escapeRegExp } from '@rocket.chat/string-helpers';
@@ -45,7 +45,7 @@ const sortUsers = (field: string, direction: 'asc' | 'desc'): Record<string, Sor
4545
};
4646

4747
const getChannelsAndGroups = async (
48-
user: IUser & { __rooms?: IRoom['_id'][] },
48+
user: AtLeast<IUser, '_id' | '__rooms'>,
4949
canViewAnon: boolean,
5050
searchTerm: string,
5151
sort: Record<string, number>,
@@ -119,7 +119,7 @@ const getChannelsCountForTeam = mem((teamId) => Rooms.countByTeamId(teamId), {
119119
});
120120

121121
const getTeams = async (
122-
user: IUser,
122+
user: AtLeast<IUser, '_id' | '__rooms'>,
123123
searchTerm: string,
124124
sort: Record<string, number>,
125125
pagination: {
@@ -247,7 +247,7 @@ const findUsers = async ({
247247
};
248248

249249
const getUsers = async (
250-
user: IUser | undefined,
250+
user: AtLeast<IUser, '_id' | '__rooms'> | undefined,
251251
text: string,
252252
workspace: string,
253253
sort: Record<string, SortDirection>,
@@ -299,7 +299,7 @@ export const browseChannelsMethod = async (
299299
offset = 0,
300300
limit = 10,
301301
}: BrowseChannelsParams,
302-
user: IUser | undefined | null,
302+
user: AtLeast<IUser, '_id' | '__rooms'> | undefined | null,
303303
) => {
304304
const searchTerm = trim(escapeRegExp(text));
305305

apps/meteor/tests/end-to-end/api/miscellaneous.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ describe('miscellaneous', () => {
215215
describe('/directory', () => {
216216
let user: TestUser<IUser>;
217217
let testChannel: IRoom;
218+
let testGroup: IRoom;
218219
let normalUserCredentials: Credentials;
219220
const teamName = `new-team-name-${Date.now()}` as const;
220221
let teamCreated: ITeam;
@@ -223,15 +224,19 @@ describe('miscellaneous', () => {
223224
await updatePermission('create-team', ['admin', 'user']);
224225
user = await createUser();
225226
normalUserCredentials = await doLogin(user.username, password);
226-
testChannel = (await createRoom({ name: `channel.test.${Date.now()}`, type: 'c' })).body.channel;
227-
teamCreated = await createTeam(normalUserCredentials, teamName, TEAM_TYPE.PUBLIC);
227+
[testChannel, testGroup, teamCreated] = await Promise.all([
228+
createRoom({ name: `channel.test.${Date.now()}`, type: 'c' }).then((res) => res.body.channel),
229+
createRoom({ name: `group.test.${Date.now()}`, type: 'p' }).then((res) => res.body.group),
230+
createTeam(normalUserCredentials, teamName, TEAM_TYPE.PUBLIC),
231+
]);
228232
});
229233

230234
after(async () => {
231235
await Promise.all([
232236
deleteTeam(normalUserCredentials, teamName),
233237
deleteUser(user),
234238
deleteRoom({ type: 'c', roomId: testChannel._id }),
239+
deleteRoom({ type: 'p', roomId: testGroup._id }),
235240
updatePermission('create-team', ['admin', 'user']),
236241
]);
237242
});
@@ -308,6 +313,31 @@ describe('miscellaneous', () => {
308313
})
309314
.end(done);
310315
});
316+
317+
it('should return private group when search by channel and execute successfully', async () => {
318+
await request
319+
.get(api('directory'))
320+
.set(credentials)
321+
.query({
322+
text: testGroup.name,
323+
type: 'channels',
324+
})
325+
.expect('Content-Type', 'application/json')
326+
.expect(200)
327+
.expect((res) => {
328+
expect(res.body).to.have.property('success', true);
329+
expect(res.body).to.have.property('offset');
330+
expect(res.body).to.have.property('total');
331+
expect(res.body).to.have.property('count');
332+
expect(res.body).to.have.property('result').and.to.be.an('array');
333+
expect(res.body.result[0]).to.have.property('_id', testGroup._id);
334+
expect(res.body.result[0]).to.have.property('t', 'p');
335+
expect(res.body.result[0]).to.have.property('name');
336+
expect(res.body.result[0]).to.have.property('usersCount').and.to.be.an('number');
337+
expect(res.body.result[0]).to.have.property('ts');
338+
});
339+
});
340+
311341
it('should return an array(result) when search by channel with sort params correctly and execute successfully', (done) => {
312342
void request
313343
.get(api('directory'))

0 commit comments

Comments
 (0)