Merge pull request #1589 from matrix-org/luke/improve-room-address-picker
Improve address picker for rooms
This commit is contained in:
commit
7075931c58
1 changed files with 27 additions and 8 deletions
|
@ -270,34 +270,53 @@ module.exports = React.createClass({
|
||||||
const rooms = MatrixClientPeg.get().getRooms();
|
const rooms = MatrixClientPeg.get().getRooms();
|
||||||
const results = [];
|
const results = [];
|
||||||
rooms.forEach((room) => {
|
rooms.forEach((room) => {
|
||||||
|
let rank = Infinity;
|
||||||
const nameEvent = room.currentState.getStateEvents('m.room.name', '');
|
const nameEvent = room.currentState.getStateEvents('m.room.name', '');
|
||||||
const topicEvent = room.currentState.getStateEvents('m.room.topic', '');
|
|
||||||
const name = nameEvent ? nameEvent.getContent().name : '';
|
const name = nameEvent ? nameEvent.getContent().name : '';
|
||||||
const canonicalAlias = room.getCanonicalAlias();
|
const canonicalAlias = room.getCanonicalAlias();
|
||||||
const aliasEvents = room.currentState.getStateEvents('m.room.aliases');
|
const aliasEvents = room.currentState.getStateEvents('m.room.aliases');
|
||||||
const aliases = aliasEvents.map((ev) => ev.getContent().aliases).reduce((a, b) => {
|
const aliases = aliasEvents.map((ev) => ev.getContent().aliases).reduce((a, b) => {
|
||||||
return a.concat(b);
|
return a.concat(b);
|
||||||
}, []);
|
}, []);
|
||||||
const topic = topicEvent ? topicEvent.getContent().topic : '';
|
|
||||||
|
|
||||||
const nameMatch = (name || '').toLowerCase().includes(lowerCaseQuery);
|
const nameMatch = (name || '').toLowerCase().includes(lowerCaseQuery);
|
||||||
const aliasMatch = aliases.some((alias) =>
|
let aliasMatch = false;
|
||||||
(alias || '').toLowerCase().includes(lowerCaseQuery),
|
let shortestMatchingAliasLength = Infinity;
|
||||||
);
|
aliases.forEach((alias) => {
|
||||||
const topicMatch = (topic || '').toLowerCase().includes(lowerCaseQuery);
|
if ((alias || '').toLowerCase().includes(lowerCaseQuery)) {
|
||||||
if (!(nameMatch || topicMatch || aliasMatch)) {
|
aliasMatch = true;
|
||||||
|
if (shortestMatchingAliasLength > alias.length) {
|
||||||
|
shortestMatchingAliasLength = alias.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!(nameMatch || aliasMatch)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (aliasMatch) {
|
||||||
|
// A shorter matching alias will give a better rank
|
||||||
|
rank = shortestMatchingAliasLength;
|
||||||
|
}
|
||||||
|
|
||||||
const avatarEvent = room.currentState.getStateEvents('m.room.avatar', '');
|
const avatarEvent = room.currentState.getStateEvents('m.room.avatar', '');
|
||||||
const avatarUrl = avatarEvent ? avatarEvent.getContent().url : undefined;
|
const avatarUrl = avatarEvent ? avatarEvent.getContent().url : undefined;
|
||||||
|
|
||||||
results.push({
|
results.push({
|
||||||
|
rank,
|
||||||
room_id: room.roomId,
|
room_id: room.roomId,
|
||||||
avatar_url: avatarUrl,
|
avatar_url: avatarUrl,
|
||||||
name: name || canonicalAlias || aliases[0] || _t('Unnamed Room'),
|
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({
|
this.setState({
|
||||||
busy: false,
|
busy: false,
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue