From 2e51b450bf8002cdbe6a1b3a4b3dbafd6641f1be Mon Sep 17 00:00:00 2001 From: Krille Date: Tue, 16 Jul 2024 13:11:35 +0200 Subject: [PATCH] refactor: Better avatar and names for invites Group chats with no user avatar should return null for the room.avatar getter and not a random hero avatar. This could otherwise lead to confusion as it looks like this is a DM which is not the case. For the name we should also not just display the name of the invitor like in a DM even for group chats, but some more additional information. I found a String for invites quite useful here as this would name rooms without a m.room.name like this: "Invited by $senderName" which should be short enough. The alternative "You have been invited by $senderName" could be too long IMO. --- lib/src/room.dart | 26 +++++++------------ .../utils/matrix_default_localizations.dart | 3 +++ lib/src/utils/matrix_localizations.dart | 2 ++ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index b76e39db..e0453883 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -263,11 +263,11 @@ class Room { if (membership == Membership.invite) { final ownMember = unsafeGetUserFromMemoryOrFallback(client.userID!); - unsafeGetUserFromMemoryOrFallback(ownMember.senderId) - .calcDisplayname(i18n: i18n); if (ownMember.senderId != ownMember.stateKey) { - return unsafeGetUserFromMemoryOrFallback(ownMember.senderId) - .calcDisplayname(i18n: i18n); + return i18n.invitedBy( + unsafeGetUserFromMemoryOrFallback(ownMember.senderId) + .calcDisplayname(i18n: i18n), + ); } } if (membership == Membership.leave) { @@ -291,25 +291,19 @@ class Room { /// that you have the room members, call and await `Room.loadHeroUsers()` /// before. Uri? get avatar { + // Check content of `m.room.avatar` final avatarUrl = getState(EventTypes.RoomAvatar)?.content.tryGet('url'); if (avatarUrl != null) { return Uri.tryParse(avatarUrl); } - final heroes = summary.mHeroes; - if (heroes != null && heroes.length == 1) { - final hero = getState(EventTypes.RoomMember, heroes.first); - if (hero != null) { - return hero.asUser(this).avatarUrl; - } - } - if (isDirectChat) { - final user = directChatMatrixID; - if (user != null) { - return unsafeGetUserFromMemoryOrFallback(user).avatarUrl; - } + // Room has no avatar and is not a direct chat + final directChatMatrixID = this.directChatMatrixID; + if (directChatMatrixID != null) { + return unsafeGetUserFromMemoryOrFallback(directChatMatrixID).avatarUrl; } + return null; } diff --git a/lib/src/utils/matrix_default_localizations.dart b/lib/src/utils/matrix_default_localizations.dart index 3d7328d5..ffa0b7b3 100644 --- a/lib/src/utils/matrix_default_localizations.dart +++ b/lib/src/utils/matrix_default_localizations.dart @@ -248,6 +248,9 @@ class MatrixDefaultLocalizations extends MatrixLocalizations { String youInvitedBy(String senderName) => 'You have been invited by $senderName'; + @override + String invitedBy(String senderName) => 'Invited by $senderName'; + @override String youInvitedUser(String targetName) => 'You invited $targetName'; diff --git a/lib/src/utils/matrix_localizations.dart b/lib/src/utils/matrix_localizations.dart index 3ae04fc8..9ef98005 100644 --- a/lib/src/utils/matrix_localizations.dart +++ b/lib/src/utils/matrix_localizations.dart @@ -62,6 +62,8 @@ abstract class MatrixLocalizations { String youInvitedBy(String senderName); + String invitedBy(String senderName); + String youInvitedUser(String targetName); String youUnbannedUser(String targetName);