From 803c7598c6979b386330f52e1349cab9ff4280ec Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 2 Sep 2021 08:10:32 +0200 Subject: [PATCH] refactor: Workarounds for missing mHeroes in rooms Normally we do not need a workaround here at all but we had one in the displayname calculation for historical reasons. A "good" server should always send the mHeroes correctly. Instead of removing this workaround completely we do a compromise and implement a more lightweight alt behaviour by just saying that in a DM room with no heroes, the directChatMatrixId will be used. This is the same behaviour like in Element and needs way less lines than before and also covers the avatar calculation. For Synapse we seem to not need this but for Conduit it might be helpful. --- lib/src/room.dart | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index 74bdcaa0..39365633 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -268,6 +268,12 @@ class Room { .asUser .avatarUrl; } + if (isDirectChat) { + final user = directChatMatrixID; + if (user != null) { + return getUserByMXIDSync(user).avatarUrl; + } + } if (membership == Membership.invite && getState(EventTypes.RoomMember, client.userID) != null) { return getState(EventTypes.RoomMember, client.userID).sender.avatarUrl; @@ -404,26 +410,18 @@ class Room { canonicalAlias.length > 3) { return canonicalAlias.localpart; } - var heroes = []; - if (summary.mHeroes != null && - summary.mHeroes.isNotEmpty && - summary.mHeroes.any((h) => h.isNotEmpty)) { - heroes = summary.mHeroes; - } else { - if (states[EventTypes.RoomMember] is Map) { - for (final entry in states[EventTypes.RoomMember].entries) { - final state = entry.value; - if (state.type == EventTypes.RoomMember && - state.stateKey != client?.userID) heroes.add(state.stateKey); - } - } - } - if (heroes.isNotEmpty) { - return heroes + if (summary.mHeroes != null && summary.mHeroes.isNotEmpty) { + return summary.mHeroes .where((hero) => hero.isNotEmpty) .map((hero) => getUserByMXIDSync(hero).calcDisplayname()) .join(', '); } + if (isDirectChat) { + final user = directChatMatrixID; + if (user != null) { + return getUserByMXIDSync(user).displayName; + } + } if (membership == Membership.invite && getState(EventTypes.RoomMember, client.userID) != null) { return getState(EventTypes.RoomMember, client.userID)