diff --git a/src/components/views/dialogs/AddressPickerDialog.js b/src/components/views/dialogs/AddressPickerDialog.js index 8de76ebfa8..bb9f300f9d 100644 --- a/src/components/views/dialogs/AddressPickerDialog.js +++ b/src/components/views/dialogs/AddressPickerDialog.js @@ -270,34 +270,53 @@ 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 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)) { + 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, });