Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

fix: strip device suffix from @lid JIDs in FormatJID

Aldino Kemalf9d1e8963894
diff --git a/src/pkg/utils/whatsapp.go b/src/pkg/utils/whatsapp.go
index c2c53eab..2e9410a6 100644
--- a/src/pkg/utils/whatsapp.go
+++ b/src/pkg/utils/whatsapp.go
@@ -819,17 +819,16 @@ func ParseJID(arg string) (types.JID, error) {
return recipient, nil
}
-// FormatJID formats a JID string by removing any :number suffix
+// FormatJID formats a JID string by removing any :device suffix.
+// The suffix is stripped for every server, not just @s.whatsapp.net: messages
+// sent from WhatsApp Web/Desktop arrive with one, and whatsmeow rejects an AD
+// JID as a send recipient (ErrRecipientADJID).
func FormatJID(jid string) types.JID {
- // Remove any :number suffix if present
- if idx := strings.LastIndex(jid, ":"); idx != -1 && strings.Contains(jid, "@s.whatsapp.net") {
- jid = jid[:idx] + jid[strings.Index(jid, "@s.whatsapp.net"):]
- }
formattedJID, err := ParseJID(jid)
if err != nil {
return types.JID{}
}
- return formattedJID
+ return formattedJID.ToNonAD()
}
// ExtractedMedia represents extracted media information
diff --git a/src/pkg/utils/whatsapp_test.go b/src/pkg/utils/whatsapp_test.go
index 99d4acf6..a7c9f0a1 100644
--- a/src/pkg/utils/whatsapp_test.go
+++ b/src/pkg/utils/whatsapp_test.go
@@ -724,3 +724,52 @@ func TestIsForwardableStorageMessage(t *testing.T) {
})
}
}
+
+func TestFormatJIDStripsDeviceSuffix(t *testing.T) {
+ tests := []struct {
+ name string
+ jid string
+ want string
+ }{
+ {
+ name: "PhoneNumberJIDWithoutDevice",
+ },
+ {
+ name: "PhoneNumberJIDWithDevice",
+ jid: "6281234567890:[email protected]",
+ },
+ {
+ name: "LIDWithoutDevice",
+ jid: "123456789012345@lid",
+ want: "123456789012345@lid",
+ },
+ {
+ // Messages sent from WhatsApp Web/Desktop carry a device suffix.
+ // whatsmeow rejects AD JIDs as a send recipient, so it must be stripped
+ // for @lid senders too, not just @s.whatsapp.net ones.
+ name: "LIDWithDevice",
+ jid: "123456789012345:12@lid",
+ want: "123456789012345@lid",
+ },
+ {
+ name: "BareNumberGetsDefaultServer",
+ jid: "6281234567890",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := FormatJID(tt.jid)
+ if got.String() != tt.want {
+ t.Errorf("FormatJID(%q) = %q, want %q", tt.jid, got.String(), tt.want)
+ }
+ if got.Device != 0 {
+ t.Errorf("FormatJID(%q) kept device %d, want 0", tt.jid, got.Device)
+ }
+ })
+ }
+}