From 0fdfc3ff53e0ba303695bf7834f2505d05979ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Sun, 18 Apr 2021 21:01:53 +0200 Subject: [PATCH 1/6] Rework how disambiguation is handled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/models/room-member.js | 64 ++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/models/room-member.js b/src/models/room-member.js index 444c456091b..1d9d513e58d 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -61,6 +61,7 @@ export function RoomMember(roomId, userId) { }; this._isOutOfBand = false; this._updateModifiedTime(); + this.disambiguate = false; } utils.inherits(RoomMember, EventEmitter); @@ -90,6 +91,8 @@ RoomMember.prototype.isOutOfBand = function() { * @fires module:client~MatrixClient#event:"RoomMember.membership" */ RoomMember.prototype.setMembershipEvent = function(event, roomState) { + const displayName = event.getDirectionalContent().displayname; + if (event.getType() !== "m.room.member") { return; } @@ -101,11 +104,19 @@ RoomMember.prototype.setMembershipEvent = function(event, roomState) { const oldMembership = this.membership; this.membership = event.getDirectionalContent().membership; + this.disambiguate = shouldDisambiguate( + this.userId, + displayName, + roomState, + ); + const oldName = this.name; this.name = calculateDisplayName( this.userId, - event.getDirectionalContent().displayname, - roomState); + displayName, + roomState, + this.disambiguate, + ); this.rawDisplayName = event.getDirectionalContent().displayname || this.userId; if (oldMembership !== this.membership) { @@ -293,43 +304,40 @@ RoomMember.prototype.getMxcAvatarUrl = function() { const MXID_PATTERN = /@.+:.+/; const LTR_RTL_PATTERN = /[\u200E\u200F\u202A-\u202F]/; -function calculateDisplayName(selfUserId, displayName, roomState) { - if (!displayName || displayName === selfUserId) { - return selfUserId; - } +function shouldDisambiguate(selfUserId, displayName, roomState) { + if (!displayName || displayName === selfUserId) return false; // First check if the displayname is something we consider truthy // after stripping it of zero width characters and padding spaces - if (!utils.removeHiddenChars(displayName)) { - return selfUserId; - } + if (!utils.removeHiddenChars(displayName)) return false; - if (!roomState) { - return displayName; - } + if (!roomState) return false; // Next check if the name contains something that look like a mxid // If it does, it may be someone trying to impersonate someone else // Show full mxid in this case - let disambiguate = MXID_PATTERN.test(displayName); + if (MXID_PATTERN.test(displayName)) return true; - if (!disambiguate) { - // Also show mxid if the display name contains any LTR/RTL characters as these - // make it very difficult for us to find similar *looking* display names - // E.g "Mark" could be cloned by writing "kraM" but in RTL. - disambiguate = LTR_RTL_PATTERN.test(displayName); - } + // Also show mxid if the display name contains any LTR/RTL characters as these + // make it very difficult for us to find similar *looking* display names + // E.g "Mark" could be cloned by writing "kraM" but in RTL. + if (LTR_RTL_PATTERN.test(displayName)) return true; - if (!disambiguate) { - // Also show mxid if there are other people with the same or similar - // displayname, after hidden character removal. - const userIds = roomState.getUserIdsWithDisplayName(displayName); - disambiguate = userIds.some((u) => u !== selfUserId); - } + // Also show mxid if there are other people with the same or similar + // displayname, after hidden character removal. + const userIds = roomState.getUserIdsWithDisplayName(displayName); + if (userIds.some((u) => u !== selfUserId)) return true; +} + +function calculateDisplayName(selfUserId, displayName, roomState, disambiguate) { + if (disambiguate) return displayName + " (" + selfUserId + ")"; + + if (!displayName || displayName === selfUserId) return selfUserId; + + // First check if the displayname is something we consider truthy + // after stripping it of zero width characters and padding spaces + if (!utils.removeHiddenChars(displayName)) return selfUserId; - if (disambiguate) { - return displayName + " (" + selfUserId + ")"; - } return displayName; } From e049edd449e96015688386346d45011530229e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 20 Apr 2021 10:44:17 +0200 Subject: [PATCH 2/6] Add docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/models/room-member.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models/room-member.js b/src/models/room-member.js index 1d9d513e58d..73d22f6db45 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -45,6 +45,7 @@ import * as utils from "../utils"; * @prop {string} membership The membership state for this room member e.g. 'join'. * @prop {Object} events The events describing this RoomMember. * @prop {MatrixEvent} events.member The m.room.member event for this RoomMember. + * @prop {boolean} disambiguate True if the client show disambiguate display names */ export function RoomMember(roomId, userId) { this.roomId = roomId; From 71c6d71cae60e5a8a5f60918e0a7ff88ea6c85d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 20 Apr 2021 10:53:39 +0200 Subject: [PATCH 3/6] Fix docs Co-authored-by: Jonathan de Jong --- src/models/room-member.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/room-member.js b/src/models/room-member.js index 73d22f6db45..ba937deab53 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -45,7 +45,7 @@ import * as utils from "../utils"; * @prop {string} membership The membership state for this room member e.g. 'join'. * @prop {Object} events The events describing this RoomMember. * @prop {MatrixEvent} events.member The m.room.member event for this RoomMember. - * @prop {boolean} disambiguate True if the client show disambiguate display names + * @prop {boolean} disambiguate True if the client should disambiguate display names */ export function RoomMember(roomId, userId) { this.roomId = roomId; From 628dd7bf41b3b4388c6aaef9ecc0ebe87af7bddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 20 Apr 2021 11:01:13 +0200 Subject: [PATCH 4/6] Return false by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/models/room-member.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/models/room-member.js b/src/models/room-member.js index 73d22f6db45..81ec5ce560b 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -110,6 +110,7 @@ RoomMember.prototype.setMembershipEvent = function(event, roomState) { displayName, roomState, ); + if (this.userId == "@jboi:jboi.nl") console.log("LOG roomMember", this); const oldName = this.name; this.name = calculateDisplayName( @@ -328,6 +329,8 @@ function shouldDisambiguate(selfUserId, displayName, roomState) { // displayname, after hidden character removal. const userIds = roomState.getUserIdsWithDisplayName(displayName); if (userIds.some((u) => u !== selfUserId)) return true; + + return false; } function calculateDisplayName(selfUserId, displayName, roomState, disambiguate) { From a416fd562bed513c51326ba015061cf37da55075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 20 Apr 2021 11:11:33 +0200 Subject: [PATCH 5/6] Oops - remove log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/models/room-member.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/models/room-member.js b/src/models/room-member.js index e912511c16c..8756e1d5243 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -110,7 +110,6 @@ RoomMember.prototype.setMembershipEvent = function(event, roomState) { displayName, roomState, ); - if (this.userId == "@jboi:jboi.nl") console.log("LOG roomMember", this); const oldName = this.name; this.name = calculateDisplayName( From e4fbbd56a9fc885555b252e3dce6b3229b19c515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Thu, 22 Apr 2021 07:36:53 +0200 Subject: [PATCH 6/6] Improve wording Co-authored-by: Travis Ralston --- src/models/room-member.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/room-member.js b/src/models/room-member.js index 8756e1d5243..4a835f1e577 100644 --- a/src/models/room-member.js +++ b/src/models/room-member.js @@ -45,7 +45,7 @@ import * as utils from "../utils"; * @prop {string} membership The membership state for this room member e.g. 'join'. * @prop {Object} events The events describing this RoomMember. * @prop {MatrixEvent} events.member The m.room.member event for this RoomMember. - * @prop {boolean} disambiguate True if the client should disambiguate display names + * @prop {boolean} disambiguate True if the member's name is disambiguated. */ export function RoomMember(roomId, userId) { this.roomId = roomId;