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: API-sent messages missing from chat message list (#683) (#687)
Aldino Kemalf283d236447d
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.goindex df4e351b..ebeb76b6 100644--- a/src/infrastructure/chatstorage/sqlite_repository.go+++ b/src/infrastructure/chatstorage/sqlite_repository.go@@ -1285,6 +1285,9 @@ func (r *SQLiteRepository) StoreSentMessageWithContext(ctx context.Context, messif deviceID == "" && client != nil && client.Store != nil && client.Store.ID != nil {deviceID = client.Store.ID.ToNonAD().String()}+ if deviceID == "" {+ return fmt.Errorf("device_id required: missing device in context: %w", domainChatStorage.ErrMissingDeviceContext)+ }// Normalize recipient JID (convert @lid to @s.whatsapp.net)normalizedJID := whatsapp.NormalizeJIDFromLID(ctx, jid, client)diff --git a/src/infrastructure/chatstorage/sqlite_repository_test.go b/src/infrastructure/chatstorage/sqlite_repository_test.goindex 12852d70..e524f529 100644--- a/src/infrastructure/chatstorage/sqlite_repository_test.go+++ b/src/infrastructure/chatstorage/sqlite_repository_test.go@@ -1,7 +1,9 @@package chatstorageimport (+ "context""database/sql"+ "errors""path/filepath""testing""time"@@ -159,6 +161,29 @@ func TestSQLiteRepositoryDeletesReactionsWithMessagesAndDevices(t *testing.T) {}}+func TestStoreSentMessageWithContextRequiresDeviceInContext(t *testing.T) {+ repo := newTestSQLiteRepository(t)+ deviceID := "[email protected]"+ chatJID := "[email protected]"+ now := time.Date(2026, time.May, 22, 10, 0, 0, 0, time.UTC)++ err := repo.StoreSentMessageWithContext(+ context.Background(),+ "msg-sent-1",+ deviceID,+ chatJID,+ "hello from api",+ now,+ nil,+ )+ if err == nil {+ t.Fatal("expected error when storing sent message without device context")+ }+ if !errors.Is(err, domainChatStorage.ErrMissingDeviceContext) {+ t.Fatalf("expected missing device context error, got %v", err)+ }+}+func seedChatMessage(t *testing.T, repo *SQLiteRepository, deviceID, chatJID, messageID, content string, timestamp time.Time) {t.Helper()if err := repo.StoreChat(&domainChatStorage.Chat{diff --git a/src/infrastructure/whatsapp/chatstorage_wrapper.go b/src/infrastructure/whatsapp/chatstorage_wrapper.goindex 14e10bef..a2ffdc9a 100644--- a/src/infrastructure/whatsapp/chatstorage_wrapper.go+++ b/src/infrastructure/whatsapp/chatstorage_wrapper.go@@ -112,6 +112,9 @@ func (r *deviceChatStorage) DeleteMessageByDevice(deviceID, id, chatJID string)}func (r *deviceChatStorage) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time, msg *waE2E.Message) error {+ if _, ok := DeviceFromContext(ctx); !ok && r.deviceID != "" {+ ctx = ContextWithDevice(ctx, NewDeviceInstance(r.deviceID, nil, nil))+ }return r.base.StoreSentMessageWithContext(ctx, messageID, senderJID, recipientJID, content, timestamp, msg)}diff --git a/src/usecase/send.go b/src/usecase/send.goindex ee0529b3..ac93c6b5 100644--- a/src/usecase/send.go+++ b/src/usecase/send.go@@ -63,10 +63,10 @@ func (service serviceSend) wrapSendMessage(ctx context.Context, client *whatsmeosenderJID = client.Store.ID.String()}- // Store message asynchronously with timeout- // Use a goroutine to avoid blocking the send operation+ // Store message asynchronously with timeout.+ // Preserve device context (for device_id scoping) but detach from request cancellation.go func() {- storeCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)+ storeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)defer cancel()if err := service.chatStorageRepo.StoreSentMessageWithContext(storeCtx, ts.ID, senderJID, recipient.String(), content, ts.Timestamp, msg); err != nil {diff --git a/src/usecase/send_test.go b/src/usecase/send_test.goindex 1419bf26..10e42656 100644--- a/src/usecase/send_test.go+++ b/src/usecase/send_test.go@@ -1,6 +1,28 @@package usecase-import "testing"+import (+ "context"+ "testing"++ "github.com/aldinokemal/go-whatsapp-web-multidevice/infrastructure/whatsapp"+)++func TestWithoutCancelPreservesDeviceContext(t *testing.T) {+ deviceID := "[email protected]"+ ctx := whatsapp.ContextWithDevice(context.Background(), whatsapp.NewDeviceInstance(deviceID, nil, nil))++ cancelledCtx, cancel := context.WithCancel(ctx)+ cancel()++ storeCtx := context.WithoutCancel(cancelledCtx)+ inst, ok := whatsapp.DeviceFromContext(storeCtx)+ if !ok || inst == nil {+ t.Fatal("expected device instance to remain in detached context")+ }+ if got := inst.ID(); got != deviceID {+ t.Fatalf("expected device id %q, got %q", deviceID, got)+ }+}func TestResolveDocumentMIME(t *testing.T) {tests := []struct {