GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(history-sync): name 1:1 chats from the contact store, not DisplayName (#845)
Adrián Rivelli5648c999bb94
diff --git a/src/infrastructure/whatsapp/history_sync.go b/src/infrastructure/whatsapp/history_sync.goindex f6336fdd..ea8cf7cb 100644--- a/src/infrastructure/whatsapp/history_sync.go+++ b/src/infrastructure/whatsapp/history_sync.go@@ -98,6 +98,10 @@ func processConversationMessages(ctx context.Context, data *waHistorySync.HistordeviceID = client.Store.ID.ToNonAD().String()}+ // The contact store is authoritative for chat names; the conversation's own+ // DisplayName is not. The resolver caches contacts, so build it once.+ chatNameResolver := NewChatDisplayNameResolver(ctx, client)+for _, conv := range conversations {rawChatJID := conv.GetID()if rawChatJID == "" {@@ -115,7 +119,7 @@ func processConversationMessages(ctx context.Context, data *waHistorySync.Historjid = NormalizeJIDFromLID(ctx, jid, client)chatJID := jid.String()- displayName := conv.GetDisplayName()+ displayName := conversationChatName(ctx, chatNameResolver, chatJID, jid, conv.GetDisplayName())// Get or create chatchatName := chatStorageRepo.GetChatNameWithPushName(jid, chatJID, "", displayName)@@ -379,3 +383,27 @@ func processPushNames(ctx context.Context, data *waHistorySync.HistorySync, chatreturn nil}++// conversationChatName picks the name to store for a synced conversation.+//+// A conversation's own DisplayName cannot be trusted for one-to-one chats: it+// arrives empty for many of them, and for some it carries the account owner's+// own push name, which then gets written as the name of somebody else's chat.+// Neither is repairable later — the read path's resolver only replaces names+// that look like a JID or a bare number, so an owner's name sticks forever, and+// chat search filters on the stored name in SQL before any resolver runs, which+// makes those contacts unsearchable.+//+// So ask the contact store first, passing no stored name so the resolver cannot+// short-circuit on one. When it knows nobody it answers the bare user part,+// which is no better than what we already have, so keep DisplayName then.+func conversationChatName(ctx context.Context, resolver *ChatDisplayNameResolver, chatJID string, jid types.JID, displayName string) string {+ if resolver == nil {+ return displayName+ }+ resolved := resolver.Resolve(ctx, chatJID, "")+ if resolved == "" || resolved == chatJID || resolved == jid.ToNonAD().User {+ return displayName+ }+ return resolved+}diff --git a/src/infrastructure/whatsapp/history_sync_name_test.go b/src/infrastructure/whatsapp/history_sync_name_test.gonew file mode 100644index 00000000..75650f44--- /dev/null+++ b/src/infrastructure/whatsapp/history_sync_name_test.go@@ -0,0 +1,52 @@+package whatsapp++import (+ "context"+ "testing"++ "go.mau.fi/whatsmeow/types"+)++type nameContactsSpy struct{ contacts map[types.JID]types.ContactInfo }++func (s nameContactsSpy) GetContact(_ context.Context, jid types.JID) (types.ContactInfo, error) {+ return s.contacts[jid], nil+}++func (s nameContactsSpy) GetAllContacts(_ context.Context) (map[types.JID]types.ContactInfo, error) {+ return s.contacts, nil+}++func TestConversationChatNamePrefersContactOverDisplayName(t *testing.T) {+ jid := types.NewJID("5491137991166", types.DefaultUserServer)+ spy := nameContactsSpy{contacts: map[types.JID]types.ContactInfo{+ jid: {Found: true, FullName: "Tuty Rivelli"},+ }}+ resolver := newChatDisplayNameResolver(spy, nil)++ // The owner's own push name arriving as a conversation DisplayName is the+ // case that leaves unrelated chats all named after the account owner.+ if got := conversationChatName(context.Background(), resolver, jid.String(), jid, "Adrian"); got != "Tuty Rivelli" {+ t.Fatalf("owner push name won over the contact: got %q", got)+ }+ // An empty DisplayName is what leaves a bare phone number as the chat name.+ if got := conversationChatName(context.Background(), resolver, jid.String(), jid, ""); got != "Tuty Rivelli" {+ t.Fatalf("empty display name did not fall back to the contact: got %q", got)+ }+}++func TestConversationChatNameKeepsDisplayNameWhenContactUnknown(t *testing.T) {+ jid := types.NewJID("5491100000000", types.DefaultUserServer)+ resolver := newChatDisplayNameResolver(nameContactsSpy{contacts: map[types.JID]types.ContactInfo{}}, nil)++ if got := conversationChatName(context.Background(), resolver, jid.String(), jid, "Some Label"); got != "Some Label" {+ t.Fatalf("discarded the display name with no contact to replace it: got %q", got)+ }+}++func TestConversationChatNameWithoutResolver(t *testing.T) {+ jid := types.NewJID("5491100000000", types.DefaultUserServer)+ if got := conversationChatName(context.Background(), nil, jid.String(), jid, "Some Label"); got != "Some Label" {+ t.Fatalf("nil resolver should be a no-op: got %q", got)+ }+}