fix: Spotlight search does not find rooms with special characters (#29804)

pull/27860/head^2
Matheus Barbosa Silva 2 years ago committed by GitHub
parent 6eb8cec289
commit 5cee21468e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      .changeset/lovely-snails-drop.md
  2. 2
      apps/meteor/server/lib/spotlight.js
  3. 12
      apps/meteor/server/models/raw/Rooms.ts
  4. 46
      apps/meteor/tests/end-to-end/api/00-miscellaneous.js
  5. 2
      packages/model-typings/src/models/IRoomsModel.ts

@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---
Fix spotlight search does not find rooms with special or non-latin characters

@ -65,7 +65,7 @@ export class Spotlight {
return this.fetchRooms(
userId,
await Rooms.findByNameAndTypesNotInIds(regex, searchableRoomTypeIds, roomIds, roomOptions, includeFederatedRooms).toArray(),
await Rooms.findByNameOrFNameAndTypesNotInIds(regex, searchableRoomTypeIds, roomIds, roomOptions, includeFederatedRooms).toArray(),
);
}

@ -1287,13 +1287,16 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
}
// 3
findByNameAndTypesNotInIds(
findByNameOrFNameAndTypesNotInIds(
name: IRoom['name'] | RegExp,
types: Array<IRoom['t']>,
ids: Array<IRoom['_id']>,
options: FindOptions<IRoom> = {},
includeFederatedRooms = false,
): FindCursor<IRoom> {
const nameCondition: Filter<IRoom> = {
$or: [{ name }, { fname: name }],
};
const query: Filter<IRoom> = {
_id: {
$nin: ids,
@ -1327,9 +1330,12 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
},
includeFederatedRooms
? {
$or: [{ $and: [{ $or: [{ federated: { $exists: false } }, { federated: false }], name }] }, { federated: true, fname: name }],
$or: [
{ $and: [{ $or: [{ federated: { $exists: false } }, { federated: false }] }, nameCondition] },
{ federated: true, fname: name },
],
}
: { $or: [{ federated: { $exists: false } }, { federated: false }], name },
: { $and: [{ $or: [{ federated: { $exists: false } }, { federated: false }] }, nameCondition] },
],
};

@ -2,6 +2,7 @@ import { expect } from 'chai';
import { getCredentials, api, login, request, credentials } from '../../data/api-data.js';
import { updateSetting } from '../../data/permissions.helper';
import { createRoom } from '../../data/rooms.helper';
import { adminEmail, adminUsername, adminPassword, password } from '../../data/user';
import { createUser, login as doLogin } from '../../data/users.helper';
import { IS_EE } from '../../e2e/config/constants';
@ -215,7 +216,7 @@ describe('miscellaneous', function () {
.end(done);
user = undefined;
});
it('create an channel', (done) => {
it('create a channel', (done) => {
request
.post(api('channels.create'))
.set(credentials)
@ -488,17 +489,25 @@ describe('miscellaneous', function () {
})
.end(done);
});
after((done) => {
request
.post(api('users.delete'))
.set(credentials)
.send({
userId: user._id,
let testChannelSpecialChars;
const fnameSpecialCharsRoom = `test ГДΕληνικά`;
before((done) => {
updateSetting('UI_Allow_room_names_with_special_chars', true)
.then(() => {
createRoom({ type: 'c', name: fnameSpecialCharsRoom, credentials: userCredentials }).end((err, res) => {
testChannelSpecialChars = res.body.channel;
});
})
.end(done);
.then(done);
});
after(async () => {
await request.post(api('users.delete')).set(credentials).send({
userId: user._id,
});
user = undefined;
await updateSetting('UI_Allow_room_names_with_special_chars', false);
});
it('create an channel', (done) => {
it('create a channel', (done) => {
request
.post(api('channels.create'))
.set(userCredentials)
@ -591,6 +600,25 @@ describe('miscellaneous', function () {
})
.end(done);
});
it('must return rooms when searching for a valid fname', (done) => {
request
.get(api('spotlight'))
.query({
query: `#${fnameSpecialCharsRoom}`,
})
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('users').and.to.be.an('array');
expect(res.body).to.have.property('rooms').and.to.be.an('array');
expect(res.body.rooms[0]).to.have.property('_id', testChannelSpecialChars._id);
expect(res.body.rooms[0]).to.have.property('name', testChannelSpecialChars.name);
expect(res.body.rooms[0]).to.have.property('t', testChannelSpecialChars.t);
})
.end(done);
});
});
describe('[/instances.get]', () => {

@ -213,7 +213,7 @@ export interface IRoomsModel extends IBaseModel<IRoom> {
options?: FindOptions<IRoom>,
includeFederatedRooms?: boolean,
): FindCursor<IRoom>;
findByNameAndTypesNotInIds(
findByNameOrFNameAndTypesNotInIds(
name: IRoom['name'] | RegExp,
types: IRoom['t'][],
ids: string[],

Loading…
Cancel
Save