Skip to content

Commit

Permalink
Regression: Admin UI -> Rooms Search filter not working (#27970)
Browse files Browse the repository at this point in the history
Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com>
  • Loading branch information
heitortanoue and gabriellsh committed Mar 7, 2023
1 parent fb1ab86 commit 75ce955
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 58 deletions.
7 changes: 3 additions & 4 deletions apps/meteor/app/api/server/lib/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ export async function findAdminRooms({
const showTypes = Array.isArray(types) ? types.filter((type) => !typesToRemove.includes(type)) : [];
const options = {
projection: adminFields,
sort: sort || { default: -1, name: 1 },
skip: offset,
limit: count,
};

let result;
if (name && showTypes.length) {
result = Rooms.findByNameContainingAndTypes(name, showTypes, discussion, includeTeams, showOnlyTeams, options);
result = Rooms.findByNameOrFnameContainingAndTypes(name, showTypes, discussion, includeTeams, showOnlyTeams, options);
} else if (showTypes.length) {
result = Rooms.findByTypes(showTypes, discussion, includeTeams, showOnlyTeams, options);
} else {
result = Rooms.findByNameContaining(name, discussion, includeTeams, showOnlyTeams, options);
result = Rooms.findByNameOrFnameContaining(name, discussion, includeTeams, showOnlyTeams, options);
}

const { cursor, totalCount } = result;

const [rooms, total] = await Promise.all([cursor.toArray(), totalCount]);
const [rooms, total] = await Promise.all([cursor.sort(sort || { default: -1, name: 1 }).toArray(), totalCount]);

return {
rooms,
Expand Down
35 changes: 5 additions & 30 deletions apps/meteor/client/views/admin/rooms/RoomsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,6 @@ const getRoomType = (room: IRoom): typeof roomTypeI18nMap[keyof typeof roomTypeI
const getRoomDisplayName = (room: IRoom): string | undefined =>
room.t === 'd' ? room.usernames?.join(' x ') : roomCoordinator.getRoomName(room.t, room);

const useDisplayData = (asyncState: any, sort: [string, 'asc' | 'desc']): IRoom[] =>
useMemo(() => {
const { data = {}, isLoading } = asyncState;

if (isLoading) {
return null;
}

if (sort[0] === 'name' && data.rooms) {
return data.rooms.sort((a: IRoom, b: IRoom) => {
const aName = getRoomDisplayName(a) || '';
const bName = getRoomDisplayName(b) || '';
if (aName === bName) {
return 0;
}
const result = aName < bName ? -1 : 1;
return sort[1] === 'asc' ? result : result * -1;
});
}
return data;
}, [asyncState, sort]);

const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): ReactElement => {
const t = useTranslation();
const mediaQuery = useMediaQuery('(min-width: 1024px)');
Expand All @@ -119,12 +97,9 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
const endpointData = useQuery(
['rooms', query, 'admin'],
async () => {
const { rooms } = await getAdminRooms({ filter: params.text, ...params });
const adminRooms = await getAdminRooms(query);

if (rooms.length === 0) {
throw new Error(t('No_results_found'));
}
return rooms;
return { ...adminRooms, rooms: adminRooms.rooms as IRoom[] };
},
{
onError: (error) => {
Expand All @@ -133,7 +108,7 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
},
);

const { data, refetch } = endpointData;
const { data, refetch, isLoading } = endpointData;

useEffect(() => {
reload.current = refetch;
Expand Down Expand Up @@ -163,7 +138,7 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
[sort, setSort],
);

const displayData = useDisplayData(endpointData, sort);
const displayData = !isLoading && data ? data.rooms : undefined;

const header = useMemo(
() =>
Expand Down Expand Up @@ -266,7 +241,7 @@ const RoomsTable = ({ reload }: { reload: MutableRefObject<() => void> }): React
header={header}
renderRow={renderRow}
results={displayData}
total={data?.length}
total={data?.total}
params={params}
setParams={setParams}
renderFilter={({ onChange, ...props }): ReactElement => <FilterByTypeAndText setFilter={onChange} {...props} />}
Expand Down
28 changes: 6 additions & 22 deletions apps/meteor/server/models/raw/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class RoomsRaw extends BaseRaw {
return statistic;
}

findByNameContainingAndTypes(name, types, discussion = false, teams = false, showOnlyTeams = false, options = {}) {
findByNameOrFnameContainingAndTypes(name, types, discussion = false, teams = false, showOnlyTeams = false, options = {}) {
const nameRegex = new RegExp(escapeRegExp(name).trim(), 'i');

const onlyTeamsQuery = showOnlyTeams ? { teamMain: { $exists: true } } : {};
Expand All @@ -81,16 +81,8 @@ export class RoomsRaw extends BaseRaw {
},
prid: { $exists: discussion },
$or: [
{
$and: [
{
$or: [
{ $and: [{ $or: [{ federated: { $exists: false } }, { federated: false }], name: nameRegex }] },
{ federated: true, fname: nameRegex },
],
},
],
},
{ name: nameRegex, federated: { $ne: true } },
{ fname: nameRegex },
{
t: 'd',
usernames: nameRegex,
Expand Down Expand Up @@ -124,7 +116,7 @@ export class RoomsRaw extends BaseRaw {
return this.findPaginated(query, options);
}

findByNameContaining(name, discussion = false, teams = false, onlyTeams = false, options = {}) {
findByNameOrFnameContaining(name, discussion = false, teams = false, onlyTeams = false, options = {}) {
const nameRegex = new RegExp(escapeRegExp(name).trim(), 'i');

const teamCondition = teams
Expand All @@ -140,16 +132,8 @@ export class RoomsRaw extends BaseRaw {
const query = {
prid: { $exists: discussion },
$or: [
{
$and: [
{
$or: [
{ $and: [{ $or: [{ federated: { $exists: false } }, { federated: false }], name: nameRegex }] },
{ federated: true, fname: nameRegex },
],
},
],
},
{ name: nameRegex, federated: { $ne: true } },
{ fname: nameRegex },
{
t: 'd',
usernames: nameRegex,
Expand Down
52 changes: 52 additions & 0 deletions apps/meteor/tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,16 @@ describe('[Rooms]', function () {
});
});
describe('/rooms.adminRooms', () => {
const suffix = `test-${Date.now()}`;
const fnameRoom = `Ελληνικά-${suffix}`;
const nameRoom = `Ellinika-${suffix}`;

before((done) => {
updateSetting('UI_Allow_room_names_with_special_chars', true).then(() => {
createRoom({ type: 'p', name: fnameRoom }).end(done);
});
});

it('should throw an error when the user tries to gets a list of discussion and he cannot access the room', (done) => {
updatePermission('view-room-administration', []).then(() => {
request
Expand Down Expand Up @@ -1290,6 +1300,48 @@ describe('[Rooms]', function () {
})
.end(done);
});
it('should search the list of admin rooms using non-latin characters when UI_Allow_room_names_with_special_chars setting is toggled', (done) => {
updateSetting('UI_Allow_room_names_with_special_chars', true).then(() => {
request
.get(api('rooms.adminRooms'))
.set(credentials)
.query({
filter: fnameRoom,
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('rooms').and.to.be.an('array');
expect(res.body.rooms).to.have.lengthOf(1);
expect(res.body.rooms[0].fname).to.be.equal(fnameRoom);
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
})
.end(done);
});
});
it('should search the list of admin rooms using latin characters only when UI_Allow_room_names_with_special_chars setting is disabled', (done) => {
updateSetting('UI_Allow_room_names_with_special_chars', false).then(() => {
request
.get(api('rooms.adminRooms'))
.set(credentials)
.query({
filter: nameRoom,
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('rooms').and.to.be.an('array');
expect(res.body.rooms).to.have.lengthOf(1);
expect(res.body.rooms[0].name).to.be.equal(nameRoom);
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
})
.end(done);
});
});
});

describe('update group dms name', () => {
Expand Down
11 changes: 9 additions & 2 deletions packages/model-typings/src/models/IRoomsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ export interface IRoomsModel extends IBaseModel<IRoom> {

getMostRecentAverageChatDurationTime(numberMostRecentChats: any, department: any): Promise<any>;

findByNameContainingAndTypes(name: any, types: any, discussion?: boolean, teams?: boolean, showOnlyTeams?: boolean, options?: any): any;
findByNameOrFnameContainingAndTypes(
name: any,
types: any,
discussion?: boolean,
teams?: boolean,
showOnlyTeams?: boolean,
options?: any,
): any;

findByTypes(types: any, discussion?: boolean, teams?: boolean, onlyTeams?: boolean, options?: any): any;

findByNameContaining(name: any, discussion?: boolean, teams?: boolean, onlyTeams?: boolean, options?: any): any;
findByNameOrFnameContaining(name: any, discussion?: boolean, teams?: boolean, onlyTeams?: boolean, options?: any): any;

findByTeamId(teamId: any, options?: any): any;

Expand Down

0 comments on commit 75ce955

Please sign in to comment.