From 558ae1128b05962211d07d8c453e3d5ac5448397 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Feb 2020 13:01:36 +0100 Subject: [PATCH 1/9] use alt_aliases rather than aliases for the display alias --- src/Rooms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rooms.js b/src/Rooms.js index f65e0ff218..218e970f35 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -23,7 +23,7 @@ import {MatrixClientPeg} from './MatrixClientPeg'; * of aliases. Otherwise return null; */ export function getDisplayAliasForRoom(room) { - return room.getCanonicalAlias() || room.getAliases()[0]; + return room.getCanonicalAlias() || room.getAltAliases()[0]; } /** From 8870da6c2403074b2d8f41875f1c5522ebdda194 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Feb 2020 12:50:18 +0100 Subject: [PATCH 2/9] 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 --- src/autocomplete/RoomProvider.js | 41 +++++++++++++++++++------------- src/editor/autocomplete.js | 2 +- src/editor/parts.js | 8 +++---- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/autocomplete/RoomProvider.js b/src/autocomplete/RoomProvider.js index fccf1e3524..1074197814 100644 --- a/src/autocomplete/RoomProvider.js +++ b/src/autocomplete/RoomProvider.js @@ -40,11 +40,19 @@ function score(query, space) { } } +function matcherObject(room, displayedAlias, matchName = "") { + return { + room, + matchName, + displayedAlias, + }; +} + export default class RoomProvider extends AutocompleteProvider { constructor() { super(ROOM_REGEX); 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); if (command) { // the only reason we need to do this is because Fuse only matches on properties - let matcherObjects = client.getVisibleRooms().filter( - (room) => !!room && !!getDisplayAliasForRoom(room), - ).map((room) => { - return { - room: room, - name: room.name, - displayedAlias: getDisplayAliasForRoom(room), - }; - }); - + let matcherObjects = client.getVisibleRooms().reduce((aliases, room) => { + if (room.getCanonicalAlias()) { + aliases = aliases.concat(matcherObject(room, room.getCanonicalAlias(), room.name)); + } + if (room.getAltAliases().length) { + const altAliases = room.getAltAliases().map(alias => matcherObject(room, alias)); + aliases = aliases.concat(altAliases); + } + return aliases; + }, []); // Filter out any matches where the user will have also autocompleted new rooms matcherObjects = matcherObjects.filter((r) => { const tombstone = r.room.currentState.getStateEvents("m.room.tombstone", ""); @@ -84,16 +92,15 @@ export default class RoomProvider extends AutocompleteProvider { completions = _sortBy(completions, [ (c) => score(matchedString, c.displayedAlias), (c) => c.displayedAlias.length, - ]).map((room) => { - const displayAlias = getDisplayAliasForRoom(room.room) || room.roomId; + completions = completions.map((room) => { return { - completion: displayAlias, - completionId: displayAlias, + completion: room.displayedAlias, + completionId: room.room.roomId, type: "room", suffix: ' ', - href: makeRoomPermalink(displayAlias), + href: makeRoomPermalink(room.displayedAlias), component: ( - } title={room.name} description={displayAlias} /> + } title={room.room.name} description={room.displayedAlias} /> ), range, }; diff --git a/src/editor/autocomplete.js b/src/editor/autocomplete.js index 1ead3aef07..fcde6e0ce4 100644 --- a/src/editor/autocomplete.js +++ b/src/editor/autocomplete.js @@ -102,7 +102,7 @@ export default class AutocompleteWrapperModel { const text = completion.completion; switch (completion.type) { 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": return [this._partCreator.atRoomPill(completionId), this._partCreator.plain(completion.suffix)]; case "user": diff --git a/src/editor/parts.js b/src/editor/parts.js index 652342405f..083ee38478 100644 --- a/src/editor/parts.js +++ b/src/editor/parts.js @@ -422,14 +422,14 @@ export class PartCreator { return new PillCandidatePart(text, this._autoCompleteCreator); } - roomPill(alias) { + roomPill(alias, roomId) { let room; - if (alias[0] === '#') { + if (roomId || alias[0] !== "#") { + room = this._client.getRoom(roomId || alias); + } else { room = this._client.getRooms().find((r) => { return r.getCanonicalAlias() === alias || r.getAliases().includes(alias); }); - } else { - room = this._client.getRoom(alias); } return new RoomPillPart(alias, room); } From 16815a752b569db1f24d1ff2d92fc2da5f6a6572 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Feb 2020 12:58:41 +0100 Subject: [PATCH 3/9] use alt_aliases instead of aliases to match a pill to a room when deserializing a pill --- src/editor/parts.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/editor/parts.js b/src/editor/parts.js index 083ee38478..0e39e66bad 100644 --- a/src/editor/parts.js +++ b/src/editor/parts.js @@ -428,7 +428,8 @@ export class PartCreator { room = this._client.getRoom(roomId || alias); } else { room = this._client.getRooms().find((r) => { - return r.getCanonicalAlias() === alias || r.getAliases().includes(alias); + return r.getCanonicalAlias() === alias || + r.getAltAliases().includes(alias); }); } return new RoomPillPart(alias, room); From f6313b51e61dd230fd3c895dd5859150f32d719c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Feb 2020 13:00:09 +0100 Subject: [PATCH 4/9] also look in alt_aliases to match a pill to a room --- src/components/views/elements/Pill.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/elements/Pill.js b/src/components/views/elements/Pill.js index cd7277cdeb..6810e776fe 100644 --- a/src/components/views/elements/Pill.js +++ b/src/components/views/elements/Pill.js @@ -129,7 +129,7 @@ const Pill = createReactClass({ const localRoom = resourceId[0] === '#' ? MatrixClientPeg.get().getRooms().find((r) => { return r.getCanonicalAlias() === resourceId || - r.getAliases().includes(resourceId); + r.getAltAliases().includes(resourceId); }) : MatrixClientPeg.get().getRoom(resourceId); room = localRoom; if (!localRoom) { From 4219d2fcf6f31c919ad4a37d1f41d1739858b6ac Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Feb 2020 13:00:32 +0100 Subject: [PATCH 5/9] style a room pill even if we don't find the room --- src/components/views/elements/Pill.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/elements/Pill.js b/src/components/views/elements/Pill.js index 6810e776fe..68e4e29337 100644 --- a/src/components/views/elements/Pill.js +++ b/src/components/views/elements/Pill.js @@ -241,8 +241,8 @@ const Pill = createReactClass({ if (this.props.shouldShowPillAvatar) { avatar =