fix: Show original poster name in room messages, not room server name

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
This commit is contained in:
Janez T
2026-03-19 09:20:22 +01:00
parent f286b0afd1
commit e683ab933f
3 changed files with 27 additions and 11 deletions

View File

@@ -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

View File

@@ -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('');

View File

@@ -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"