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.

refactor: normalize JID handling and clean up unused code

Aldino Kemal1b7bd4072873
diff --git a/src/domains/send/sticker.go b/src/domains/send/sticker.go
index 376fbfe5..22ab4260 100644
--- a/src/domains/send/sticker.go
+++ b/src/domains/send/sticker.go
@@ -6,4 +6,4 @@ type StickerRequest struct {
BaseRequest
Sticker *multipart.FileHeader `json:"sticker" form:"sticker"`
StickerURL *string `json:"sticker_url" form:"sticker_url"`
-}
\ No newline at end of file
+}
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index 1f91629d..f0679b80 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -11,7 +11,6 @@ import (
"github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/whatsapp"
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
"github.com/sirupsen/logrus"
- "go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
@@ -26,37 +25,6 @@ func NewStorageRepository(db *sql.DB) domainChatStorage.IChatStorageRepository {
return &SQLiteRepository{db: db}
}
-// normalizeJIDFromLID converts @lid JIDs to their corresponding @s.whatsapp.net JIDs
-// Returns the original JID if it's not an @lid or if LID lookup fails
-func normalizeJIDFromLID(ctx context.Context, jid types.JID, client *whatsmeow.Client) types.JID {
- // Only process @lid JIDs
- if jid.Server != "lid" {
- return jid
- }
-
- // Safety check
- if client == nil || client.Store == nil || client.Store.LIDs == nil {
- logrus.Warnf("Cannot resolve LID %s: client not available", jid.String())
- return jid
- }
-
- // Attempt to get the phone number for this LID
- pn, err := client.Store.LIDs.GetPNForLID(ctx, jid)
- if err != nil {
- logrus.Debugf("Failed to resolve LID %s to phone number: %v", jid.String(), err)
- return jid
- }
-
- // If we got a valid phone number, use it
- if !pn.IsEmpty() {
- logrus.Debugf("Resolved LID %s to phone number %s", jid.String(), pn.String())
- return pn
- }
-
- // Fallback to original JID
- return jid
-}
-
// StoreChat creates or updates a chat
func (r *SQLiteRepository) StoreChat(chat *domainChatStorage.Chat) error {
now := time.Now()
@@ -548,8 +516,8 @@ func (r *SQLiteRepository) CreateMessage(ctx context.Context, evt *events.Messag
client := whatsapp.GetClient()
// Normalize chat and sender JIDs (convert @lid to @s.whatsapp.net)
- normalizedChatJID := normalizeJIDFromLID(ctx, evt.Info.Chat, client)
- normalizedSender := normalizeJIDFromLID(ctx, evt.Info.Sender, client)
+ normalizedChatJID := whatsapp.NormalizeJIDFromLID(ctx, evt.Info.Chat, client)
+ normalizedSender := whatsapp.NormalizeJIDFromLID(ctx, evt.Info.Sender, client)
chatJID := normalizedChatJID.String()
// Store the full sender JID (user@server) to ensure consistency between received and sent messages
@@ -685,7 +653,7 @@ func (r *SQLiteRepository) StoreSentMessageWithContext(ctx context.Context, mess
client := whatsapp.GetClient()
// Normalize recipient JID (convert @lid to @s.whatsapp.net)
- normalizedJID := normalizeJIDFromLID(ctx, jid, client)
+ normalizedJID := whatsapp.NormalizeJIDFromLID(ctx, jid, client)
chatJID := normalizedJID.String()
// Get chat name (no pushname available for sent messages)
diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go
index b56ff51d..3d69d193 100644
--- a/src/infrastructure/whatsapp/init.go
+++ b/src/infrastructure/whatsapp/init.go
@@ -73,6 +73,37 @@ func initDatabase(ctx context.Context, dbLog waLog.Logger, DBURI string) (*sqlst
return nil, fmt.Errorf("unknown database type: %s. Currently only sqlite3(file:) and postgres are supported", DBURI)
}
+// NormalizeJIDFromLID converts @lid JIDs to their corresponding @s.whatsapp.net JIDs
+// Returns the original JID if it's not an @lid or if LID lookup fails
+func NormalizeJIDFromLID(ctx context.Context, jid types.JID, client *whatsmeow.Client) types.JID {
+ // Only process @lid JIDs
+ if jid.Server != "lid" {
+ return jid
+ }
+
+ // Safety check
+ if client == nil || client.Store == nil || client.Store.LIDs == nil {
+ log.Warnf("Cannot resolve LID %s: client not available", jid.String())
+ return jid
+ }
+
+ // Attempt to get the phone number for this LID
+ pn, err := client.Store.LIDs.GetPNForLID(ctx, jid)
+ if err != nil {
+ log.Debugf("Failed to resolve LID %s to phone number: %v", jid.String(), err)
+ return jid
+ }
+
+ // If we got a valid phone number, use it
+ if !pn.IsEmpty() {
+ log.Debugf("Resolved LID %s to phone number %s", jid.String(), pn.String())
+ return pn
+ }
+
+ // Fallback to original JID
+ return jid
+}
+
func syncKeysDevice(ctx context.Context, db, keysDB *sqlstore.Container) {
if keysDB == nil {
return
@@ -635,9 +666,9 @@ func buildMessageMetaParts(evt *events.Message) []string {
}
func handleImageMessage(ctx context.Context, evt *events.Message) {
- if !config.WhatsappAutoDownloadMedia {
- return
- }
+ if !config.WhatsappAutoDownloadMedia {
+ return
+ }
client := GetClient()
if client == nil {
return
@@ -908,25 +939,29 @@ func processHistorySync(ctx context.Context, data *waHistorySync.HistorySync, ch
}
// processConversationMessages processes and stores conversation messages from history sync
-func processConversationMessages(_ context.Context, data *waHistorySync.HistorySync, chatStorageRepo domainChatStorage.IChatStorageRepository) error {
+func processConversationMessages(ctx context.Context, data *waHistorySync.HistorySync, chatStorageRepo domainChatStorage.IChatStorageRepository) error {
conversations := data.GetConversations()
log.Infof("Processing %d conversations from history sync", len(conversations))
client := GetClient()
for _, conv := range conversations {
- chatJID := conv.GetID()
- if chatJID == "" {
+ rawChatJID := conv.GetID()
+ if rawChatJID == "" {
continue
}
// Parse JID to get proper format
- jid, err := types.ParseJID(chatJID)
+ jid, err := types.ParseJID(rawChatJID)
if err != nil {
- log.Warnf("Failed to parse JID %s: %v", chatJID, err)
+ log.Warnf("Failed to parse JID %s: %v", rawChatJID, err)
continue
}
+ // Normalize JID (convert @lid to @s.whatsapp.net if possible)
+ jid = NormalizeJIDFromLID(ctx, jid, client)
+ chatJID := jid.String()
+
displayName := conv.GetDisplayName()
// Get or create chat
@@ -986,6 +1021,8 @@ func processConversationMessages(_ context.Context, data *waHistorySync.HistoryS
if participant != "" {
// For group messages, participant contains the actual sender
if senderJID, err := types.ParseJID(participant); err == nil {
+ // Normalize sender JID (convert @lid to @s.whatsapp.net if possible)
+ senderJID = NormalizeJIDFromLID(ctx, senderJID, client)
sender = senderJID.String() // Use full JID format for consistency
} else {
// Fallback to participant string, but ensure it's not empty
@@ -1059,17 +1096,28 @@ func processConversationMessages(_ context.Context, data *waHistorySync.HistoryS
}
// processPushNames processes push names from history sync to update chat names
-func processPushNames(_ context.Context, data *waHistorySync.HistorySync, chatStorageRepo domainChatStorage.IChatStorageRepository) error {
+func processPushNames(ctx context.Context, data *waHistorySync.HistorySync, chatStorageRepo domainChatStorage.IChatStorageRepository) error {
pushnames := data.GetPushnames()
log.Infof("Processing %d push names from history sync", len(pushnames))
+ client := GetClient()
+
for _, pushname := range pushnames {
- jidStr := pushname.GetID()
+ rawJIDStr := pushname.GetID()
name := pushname.GetPushname()
- if jidStr == "" || name == "" {
+ if rawJIDStr == "" || name == "" {
+ continue
+ }
+
+ // Parse and normalize JID (convert @lid to @s.whatsapp.net if possible)
+ jid, err := types.ParseJID(rawJIDStr)
+ if err != nil {
+ log.Warnf("Failed to parse JID %s in push names: %v", rawJIDStr, err)
continue
}
+ jid = NormalizeJIDFromLID(ctx, jid, client)
+ jidStr := jid.String()
// Check if chat exists
existingChat, err := chatStorageRepo.GetChat(jidStr)
diff --git a/src/validations/send_validation_test_sticker.go b/src/validations/send_validation_test_sticker.go
index 08192f1f..077fdbbb 100644
--- a/src/validations/send_validation_test_sticker.go
+++ b/src/validations/send_validation_test_sticker.go
@@ -160,4 +160,4 @@ func TestValidateSendSticker(t *testing.T) {
assert.Equal(t, tt.err, err)
})
}
-}
\ No newline at end of file
+}