diff --git a/lib/providers/app_provider.dart b/lib/providers/app_provider.dart index 80ae80c..6166a5e 100644 --- a/lib/providers/app_provider.dart +++ b/lib/providers/app_provider.dart @@ -1134,16 +1134,31 @@ class AppProvider with ChangeNotifier { return; } - // Enrich message with sender name from contacts first + // Enrich message with sender name from contacts. + // For room-forwarded messages, resolve the original poster's name + // from roomPostAuthorPrefix instead of the room server's key. Message enrichedMessage = message; Contact? senderContact; - if (message.senderPublicKeyPrefix != null && message.senderName == null) { - final contact = contactsProvider.findContactByKey( - message.senderPublicKeyPrefix!, - ); - if (contact != null) { - senderContact = contact; - enrichedMessage = message.copyWith(senderName: contact.advName); + if (message.senderName == null) { + // Try room post author first (the actual person who posted) + if (message.roomPostAuthorPrefix != null) { + final author = contactsProvider.findContactByPrefix( + message.roomPostAuthorPrefix!, + ); + if (author != null) { + senderContact = author; + enrichedMessage = message.copyWith(senderName: author.advName); + } + } + // Fall back to sender key (direct messages, or room with unknown author) + if (senderContact == null && message.senderPublicKeyPrefix != null) { + final contact = contactsProvider.findContactByKey( + message.senderPublicKeyPrefix!, + ); + if (contact != null) { + senderContact = contact; + enrichedMessage = message.copyWith(senderName: contact.advName); + } } } senderContact ??= message.senderPublicKeyPrefix != null diff --git a/lib/providers/contacts_provider.dart b/lib/providers/contacts_provider.dart index c43af78..7f3dcbf 100644 --- a/lib/providers/contacts_provider.dart +++ b/lib/providers/contacts_provider.dart @@ -1195,10 +1195,11 @@ class ContactsProvider with ChangeNotifier { /// Find contact by public key prefix (6 bytes) Contact? _findContactByPrefix(Uint8List prefix) { - if (prefix.length < 6) return null; + if (prefix.isEmpty) return null; + final takeLen = prefix.length < 6 ? prefix.length : 6; final prefixHex = prefix - .sublist(0, 6) + .sublist(0, takeLen) .map((b) => b.toRadixString(16).padLeft(2, '0')) .join(''); diff --git a/pubspec.lock b/pubspec.lock index 751dba4..cd77622 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -788,7 +788,7 @@ packages: description: path: "." ref: main - resolved-ref: abff892fac0dd0e8004bfa1f415df74b44b8eb52 + resolved-ref: "3dbc82170583f616b590e5204a307a02f2b8833d" url: "https://github.com/dz0ny/meshcore_client.git" source: git version: "0.1.0"