Add alt_aliases to room completion candidates
but don't match on name otherwise you see multiple entries per room when searching for a room name Also pass the roomId to the composer autocomplete, so it's easier to we don't need to loop through all the rooms and it's also easier accept room with local aliases as well in the future
This commit is contained in:
parent
558ae1128b
commit
8870da6c24
3 changed files with 29 additions and 22 deletions
|
@ -40,11 +40,19 @@ function score(query, space) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function matcherObject(room, displayedAlias, matchName = "") {
|
||||||
|
return {
|
||||||
|
room,
|
||||||
|
matchName,
|
||||||
|
displayedAlias,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default class RoomProvider extends AutocompleteProvider {
|
export default class RoomProvider extends AutocompleteProvider {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(ROOM_REGEX);
|
super(ROOM_REGEX);
|
||||||
this.matcher = new QueryMatcher([], {
|
this.matcher = new QueryMatcher([], {
|
||||||
keys: ['displayedAlias', 'name'],
|
keys: ['displayedAlias', 'matchName'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,16 +64,16 @@ export default class RoomProvider extends AutocompleteProvider {
|
||||||
const {command, range} = this.getCurrentCommand(query, selection, force);
|
const {command, range} = this.getCurrentCommand(query, selection, force);
|
||||||
if (command) {
|
if (command) {
|
||||||
// the only reason we need to do this is because Fuse only matches on properties
|
// the only reason we need to do this is because Fuse only matches on properties
|
||||||
let matcherObjects = client.getVisibleRooms().filter(
|
let matcherObjects = client.getVisibleRooms().reduce((aliases, room) => {
|
||||||
(room) => !!room && !!getDisplayAliasForRoom(room),
|
if (room.getCanonicalAlias()) {
|
||||||
).map((room) => {
|
aliases = aliases.concat(matcherObject(room, room.getCanonicalAlias(), room.name));
|
||||||
return {
|
}
|
||||||
room: room,
|
if (room.getAltAliases().length) {
|
||||||
name: room.name,
|
const altAliases = room.getAltAliases().map(alias => matcherObject(room, alias));
|
||||||
displayedAlias: getDisplayAliasForRoom(room),
|
aliases = aliases.concat(altAliases);
|
||||||
};
|
}
|
||||||
});
|
return aliases;
|
||||||
|
}, []);
|
||||||
// Filter out any matches where the user will have also autocompleted new rooms
|
// Filter out any matches where the user will have also autocompleted new rooms
|
||||||
matcherObjects = matcherObjects.filter((r) => {
|
matcherObjects = matcherObjects.filter((r) => {
|
||||||
const tombstone = r.room.currentState.getStateEvents("m.room.tombstone", "");
|
const tombstone = r.room.currentState.getStateEvents("m.room.tombstone", "");
|
||||||
|
@ -84,16 +92,15 @@ export default class RoomProvider extends AutocompleteProvider {
|
||||||
completions = _sortBy(completions, [
|
completions = _sortBy(completions, [
|
||||||
(c) => score(matchedString, c.displayedAlias),
|
(c) => score(matchedString, c.displayedAlias),
|
||||||
(c) => c.displayedAlias.length,
|
(c) => c.displayedAlias.length,
|
||||||
]).map((room) => {
|
completions = completions.map((room) => {
|
||||||
const displayAlias = getDisplayAliasForRoom(room.room) || room.roomId;
|
|
||||||
return {
|
return {
|
||||||
completion: displayAlias,
|
completion: room.displayedAlias,
|
||||||
completionId: displayAlias,
|
completionId: room.room.roomId,
|
||||||
type: "room",
|
type: "room",
|
||||||
suffix: ' ',
|
suffix: ' ',
|
||||||
href: makeRoomPermalink(displayAlias),
|
href: makeRoomPermalink(room.displayedAlias),
|
||||||
component: (
|
component: (
|
||||||
<PillCompletion initialComponent={<RoomAvatar width={24} height={24} room={room.room} />} title={room.name} description={displayAlias} />
|
<PillCompletion initialComponent={<RoomAvatar width={24} height={24} room={room.room} />} title={room.room.name} description={room.displayedAlias} />
|
||||||
),
|
),
|
||||||
range,
|
range,
|
||||||
};
|
};
|
||||||
|
|
|
@ -102,7 +102,7 @@ export default class AutocompleteWrapperModel {
|
||||||
const text = completion.completion;
|
const text = completion.completion;
|
||||||
switch (completion.type) {
|
switch (completion.type) {
|
||||||
case "room":
|
case "room":
|
||||||
return [this._partCreator.roomPill(completionId), this._partCreator.plain(completion.suffix)];
|
return [this._partCreator.roomPill(text, completionId), this._partCreator.plain(completion.suffix)];
|
||||||
case "at-room":
|
case "at-room":
|
||||||
return [this._partCreator.atRoomPill(completionId), this._partCreator.plain(completion.suffix)];
|
return [this._partCreator.atRoomPill(completionId), this._partCreator.plain(completion.suffix)];
|
||||||
case "user":
|
case "user":
|
||||||
|
|
|
@ -422,14 +422,14 @@ export class PartCreator {
|
||||||
return new PillCandidatePart(text, this._autoCompleteCreator);
|
return new PillCandidatePart(text, this._autoCompleteCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
roomPill(alias) {
|
roomPill(alias, roomId) {
|
||||||
let room;
|
let room;
|
||||||
if (alias[0] === '#') {
|
if (roomId || alias[0] !== "#") {
|
||||||
|
room = this._client.getRoom(roomId || alias);
|
||||||
|
} else {
|
||||||
room = this._client.getRooms().find((r) => {
|
room = this._client.getRooms().find((r) => {
|
||||||
return r.getCanonicalAlias() === alias || r.getAliases().includes(alias);
|
return r.getCanonicalAlias() === alias || r.getAliases().includes(alias);
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
room = this._client.getRoom(alias);
|
|
||||||
}
|
}
|
||||||
return new RoomPillPart(alias, room);
|
return new RoomPillPart(alias, room);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue