From b3674f503dd3e517eb8c5f1b01eb297698c4f18f Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 8 Nov 2017 10:32:48 +0000 Subject: [PATCH 1/2] Do not search through room topics --- src/components/views/dialogs/AddressPickerDialog.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/components/views/dialogs/AddressPickerDialog.js b/src/components/views/dialogs/AddressPickerDialog.js index 8de76ebfa8..077f357591 100644 --- a/src/components/views/dialogs/AddressPickerDialog.js +++ b/src/components/views/dialogs/AddressPickerDialog.js @@ -271,21 +271,18 @@ module.exports = React.createClass({ const results = []; rooms.forEach((room) => { const nameEvent = room.currentState.getStateEvents('m.room.name', ''); - const topicEvent = room.currentState.getStateEvents('m.room.topic', ''); const name = nameEvent ? nameEvent.getContent().name : ''; const canonicalAlias = room.getCanonicalAlias(); const aliasEvents = room.currentState.getStateEvents('m.room.aliases'); const aliases = aliasEvents.map((ev) => ev.getContent().aliases).reduce((a, b) => { return a.concat(b); }, []); - const topic = topicEvent ? topicEvent.getContent().topic : ''; const nameMatch = (name || '').toLowerCase().includes(lowerCaseQuery); const aliasMatch = aliases.some((alias) => (alias || '').toLowerCase().includes(lowerCaseQuery), ); - const topicMatch = (topic || '').toLowerCase().includes(lowerCaseQuery); - if (!(nameMatch || topicMatch || aliasMatch)) { + if (!(nameMatch || aliasMatch)) { return; } const avatarEvent = room.currentState.getStateEvents('m.room.avatar', ''); From 077ca36b05d6896528573333d62a9a58d5299c2c Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 8 Nov 2017 10:47:45 +0000 Subject: [PATCH 2/2] Rank rooms with shorter matching aliases first --- .../views/dialogs/AddressPickerDialog.js | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/components/views/dialogs/AddressPickerDialog.js b/src/components/views/dialogs/AddressPickerDialog.js index 077f357591..bb9f300f9d 100644 --- a/src/components/views/dialogs/AddressPickerDialog.js +++ b/src/components/views/dialogs/AddressPickerDialog.js @@ -270,6 +270,7 @@ module.exports = React.createClass({ const rooms = MatrixClientPeg.get().getRooms(); const results = []; rooms.forEach((room) => { + let rank = Infinity; const nameEvent = room.currentState.getStateEvents('m.room.name', ''); const name = nameEvent ? nameEvent.getContent().name : ''; const canonicalAlias = room.getCanonicalAlias(); @@ -279,22 +280,43 @@ module.exports = React.createClass({ }, []); const nameMatch = (name || '').toLowerCase().includes(lowerCaseQuery); - const aliasMatch = aliases.some((alias) => - (alias || '').toLowerCase().includes(lowerCaseQuery), - ); + let aliasMatch = false; + let shortestMatchingAliasLength = Infinity; + aliases.forEach((alias) => { + if ((alias || '').toLowerCase().includes(lowerCaseQuery)) { + aliasMatch = true; + if (shortestMatchingAliasLength > alias.length) { + shortestMatchingAliasLength = alias.length; + } + } + }); + if (!(nameMatch || aliasMatch)) { return; } + + if (aliasMatch) { + // A shorter matching alias will give a better rank + rank = shortestMatchingAliasLength; + } + const avatarEvent = room.currentState.getStateEvents('m.room.avatar', ''); const avatarUrl = avatarEvent ? avatarEvent.getContent().url : undefined; results.push({ + rank, room_id: room.roomId, avatar_url: avatarUrl, name: name || canonicalAlias || aliases[0] || _t('Unnamed Room'), }); }); - this._processResults(results, query); + + // Sort by rank ascending (a high rank being less relevant) + const sortedResults = results.sort((a, b) => { + return a.rank - b.rank; + }); + + this._processResults(sortedResults, query); this.setState({ busy: false, });