Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Search on Member List #26273

Merged
merged 7 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Docs: https://github.com/RocketChat/developer-docs/blob/master/reference/api/rest-api/endpoints/team-collaboration-endpoints/im-endpoints
*/
import type { IMessage, IRoom, ISetting, ISubscription, IUpload } from '@rocket.chat/core-typings';
import type { IMessage, IRoom, ISubscription, IUpload } from '@rocket.chat/core-typings';
import {
isDmDeleteProps,
isDmFileProps,
Expand All @@ -12,7 +12,7 @@ import {
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { Subscriptions, Uploads, Messages, Rooms, Settings, Users } from '@rocket.chat/models';
import { Subscriptions, Uploads, Messages, Rooms, Users } from '@rocket.chat/models';

import { canAccessRoomIdAsync } from '../../../authorization/server/functions/canAccessRoom';
import { hasPermission } from '../../../authorization/server';
Expand All @@ -21,6 +21,7 @@ import { API } from '../api';
import { getRoomByNameOrIdWithOptionToJoin } from '../../../lib/server/functions/getRoomByNameOrIdWithOptionToJoin';
import { createDirectMessage } from '../../../../server/methods/createDirectMessage';
import { addUserToFileObj } from '../helpers/addUserToFileObj';
import { settings } from '../../../settings/server';

interface IImFilesObject extends IUpload {
userId: string;
Expand Down Expand Up @@ -310,7 +311,9 @@ API.v1.addRoute(
limit: count,
};

const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(filter, [], options, null, [extraQuery]);
const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');

const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(filter, [], options, searchFields, [extraQuery]);

const [members, total] = await Promise.all([cursor.toArray(), totalCount]);

Expand Down Expand Up @@ -369,13 +372,7 @@ API.v1.addRoute(
{ authRequired: true },
{
async get() {
const settings = await Settings.findOne<ISetting>(
{ _id: 'API_Enable_Direct_Message_History_EndPoint' },
{
projection: { _id: 1, value: 1 },
},
);
if (settings?.value !== true) {
if (settings.get('API_Enable_Direct_Message_History_EndPoint') !== true) {
throw new Meteor.Error('error-endpoint-disabled', 'This endpoint is disabled', {
route: '/api/v1/im.messages.others',
});
Expand Down
4 changes: 3 additions & 1 deletion apps/meteor/server/lib/findUsersOfRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function findUsersOfRoom({
...(limit > 0 && { limit }),
};

return Users.findPaginatedByActiveUsersExcept(filter, undefined, options, undefined, [
const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');

return Users.findPaginatedByActiveUsersExcept(filter, undefined, options, searchFields, [
{
__rooms: rid,
...(status && { status }),
Expand Down
15 changes: 5 additions & 10 deletions apps/meteor/server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ async function getTeams(user, searchTerm, sort, pagination) {
}

async function findUsers({ text, sort, pagination, workspace, viewFullOtherUserInfo }) {
const forcedSearchFields = workspace === 'all' && ['username', 'name', 'emails.address'];
const searchFields =
workspace === 'all' ? ['username', 'name', 'emails.address'] : settings.get('Accounts_SearchFields').trim().split(',');

const options = {
...pagination,
Expand All @@ -178,7 +179,7 @@ async function findUsers({ text, sort, pagination, workspace, viewFullOtherUserI
};

if (workspace === 'all') {
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(text, [], options, forcedSearchFields);
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(text, [], options, searchFields);
const [results, total] = await Promise.all([cursor.toArray(), totalCount]);
return {
total,
Expand All @@ -187,21 +188,15 @@ async function findUsers({ text, sort, pagination, workspace, viewFullOtherUserI
}

if (workspace === 'external') {
const { cursor, totalCount } = Users.findPaginatedByActiveLocalUsersExcept(
text,
[],
options,
forcedSearchFields,
getFederationDomain(),
);
const { cursor, totalCount } = Users.findPaginatedByActiveExternalUsersExcept(text, [], options, searchFields, getFederationDomain());
const [results, total] = await Promise.all([cursor.toArray(), totalCount]);
return {
total,
results,
};
}

const { cursor, totalCount } = Users.findPaginatedByActiveLocalUsersExcept(text, [], options, forcedSearchFields, getFederationDomain());
const { cursor, totalCount } = Users.findPaginatedByActiveLocalUsersExcept(text, [], options, searchFields, getFederationDomain());
const [results, total] = await Promise.all([cursor.toArray(), totalCount]);
return {
total,
Expand Down
23 changes: 23 additions & 0 deletions apps/meteor/tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ describe('[Channels]', function () {
})
.end(done);
});

describe('/channels.members', () => {
it('should return an array of members by channel', (done) => {
request
Expand All @@ -1062,6 +1063,7 @@ describe('[Channels]', function () {
})
.end(done);
});

it('should return an array of members by channel even requested with count and offset params', (done) => {
request
.get(api('channels.members'))
Expand All @@ -1082,6 +1084,27 @@ describe('[Channels]', function () {
})
.end(done);
});

it('should return an filtered array of members by channel', (done) => {
request
.get(api('channels.members'))
.set(credentials)
.query({
roomId: channel._id,
filter: 'rocket.cat',
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('members').and.to.be.an('array');
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('count', 1);
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('offset');
})
.end(done);
});
});

it('/channels.rename', async () => {
Expand Down