Fuzzy matching in User and Room providers

pull/21833/head
Aviral Dasgupta 2016-06-21 05:05:23 +05:30
parent 442291c0a4
commit f6a76edfdf
2 changed files with 24 additions and 10 deletions

View File

@ -3,13 +3,16 @@ import Q from 'q';
import MatrixClientPeg from '../MatrixClientPeg'; import MatrixClientPeg from '../MatrixClientPeg';
import Fuse from 'fuse.js'; import Fuse from 'fuse.js';
const ROOM_REGEX = /(?=#)[^\s]*/g; const ROOM_REGEX = /(?=#)([^\s]*)/g;
let instance = null; let instance = null;
export default class RoomProvider extends AutocompleteProvider { export default class RoomProvider extends AutocompleteProvider {
constructor() { constructor() {
super(); super();
this.fuse = new Fuse([], {
keys: ['name', 'roomId', 'aliases']
});
} }
getCompletions(query: String) { getCompletions(query: String) {
@ -18,12 +21,20 @@ export default class RoomProvider extends AutocompleteProvider {
const matches = query.match(ROOM_REGEX); const matches = query.match(ROOM_REGEX);
const command = matches && matches[0]; const command = matches && matches[0];
if(command) { if(command) {
completions = client.getRooms().map(room => { // the only reason we need to do this is because Fuse only matches on properties
this.fuse.set(client.getRooms().filter(room => !!room).map(room => {
return {
name: room.name,
roomId: room.roomId,
aliases: room.getAliases()
};
}));
completions = this.fuse.search(command).map(room => {
return { return {
title: room.name, title: room.name,
subtitle: room.roomId subtitle: room.roomId
}; };
}); }).slice(0, 4);;
} }
return Q.when(completions); return Q.when(completions);
} }

View File

@ -1,6 +1,6 @@
import AutocompleteProvider from './AutocompleteProvider'; import AutocompleteProvider from './AutocompleteProvider';
import Q from 'q'; import Q from 'q';
import MatrixClientPeg from '../MatrixClientPeg'; import Fuse from 'fuse.js';
const ROOM_REGEX = /@[^\s]*/g; const ROOM_REGEX = /@[^\s]*/g;
@ -10,19 +10,23 @@ export default class UserProvider extends AutocompleteProvider {
constructor() { constructor() {
super(); super();
this.users = []; this.users = [];
this.fuse = new Fuse([], {
keys: ['displayName', 'userId']
})
} }
getCompletions(query: String) { getCompletions(query: String) {
let completions = []; let completions = [];
const matches = query.match(ROOM_REGEX); let matches = query.match(ROOM_REGEX);
if(!!matches) { let command = matches && matches[0];
const command = matches[0]; if(command) {
completions = this.users.map(user => { this.fuse.set(this.users);
completions = this.fuse.search(command).map(user => {
return { return {
title: user.displayName || user.userId, title: user.displayName || user.userId,
description: user.userId description: user.userId
}; };
}); }).slice(0, 4);
} }
return Q.when(completions); return Q.when(completions);
} }
@ -32,7 +36,6 @@ export default class UserProvider extends AutocompleteProvider {
} }
setUserList(users) { setUserList(users) {
console.log('setUserList');
this.users = users; this.users = users;
} }