From e683ab933fa0770448afd597ea011d23a400373c Mon Sep 17 00:00:00 2001 From: Janez T Date: Thu, 19 Mar 2026 09:20:22 +0100 Subject: [PATCH] fix: Show original poster name in room messages, not room server name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Room servers forward messages with the original author's 4-byte public key prefix (roomPostAuthorPrefix). Was ignoring this — all room messages showed the room server's name as sender. Now resolves the actual poster's name from roomPostAuthorPrefix by matching against the contacts list. Falls back to room server name if the author isn't a known contact. Also fixes findContactByPrefix to accept 4-byte prefixes (was rejecting anything shorter than 6 bytes). Fixes #22 --- lib/providers/app_provider.dart | 31 +++++++++++++++++++++------- lib/providers/contacts_provider.dart | 5 +++-- pubspec.lock | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) 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"