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: store media metadata for sent messages in chat storage

Aldino Kemal8eb40a51ef05
diff --git a/src/domains/chatstorage/interfaces.go b/src/domains/chatstorage/interfaces.go
index 8eda0963..e891edf7 100644
--- a/src/domains/chatstorage/interfaces.go
+++ b/src/domains/chatstorage/interfaces.go
@@ -4,6 +4,7 @@ import (
"context"
"time"
+ "go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
@@ -26,7 +27,7 @@ type IChatStorageRepository interface {
SearchMessages(deviceID, chatJID, searchText string, limit int) ([]*Message, error) // Database-level search with device isolation
DeleteMessage(id, chatJID string) error
DeleteMessageByDevice(deviceID, id, chatJID string) error
- StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error
+ StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time, msg *waE2E.Message) error
// Statistics
GetChatMessageCount(chatJID string) (int64, error)
diff --git a/src/infrastructure/chatstorage/device_repository.go b/src/infrastructure/chatstorage/device_repository.go
index 733fe08e..1a58dd05 100644
--- a/src/infrastructure/chatstorage/device_repository.go
+++ b/src/infrastructure/chatstorage/device_repository.go
@@ -5,6 +5,7 @@ import (
"time"
domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
+ "go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
@@ -100,8 +101,8 @@ func (r *DeviceRepository) DeleteMessageByDevice(deviceID, id, chatJID string) e
return r.base.DeleteMessageByDevice(deviceID, id, chatJID)
}
-func (r *DeviceRepository) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error {
- return r.base.StoreSentMessageWithContext(ctx, messageID, senderJID, recipientJID, content, timestamp)
+func (r *DeviceRepository) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time, msg *waE2E.Message) error {
+ return r.base.StoreSentMessageWithContext(ctx, messageID, senderJID, recipientJID, content, timestamp, msg)
}
func (r *DeviceRepository) GetChatMessageCount(chatJID string) (int64, error) {
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index 8722eac0..1d925550 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -11,6 +11,7 @@ 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/proto/waE2E"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
@@ -915,7 +916,7 @@ func (r *SQLiteRepository) TruncateAllDataWithLogging(logPrefix string) error {
}
// StoreSentMessageWithContext stores a message that was sent by the user with context cancellation support
-func (r *SQLiteRepository) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error {
+func (r *SQLiteRepository) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time, msg *waE2E.Message) error {
// Check if context is already cancelled before starting
select {
case <-ctx.Done():
@@ -986,15 +987,30 @@ func (r *SQLiteRepository) StoreSentMessageWithContext(ctx context.Context, mess
default:
}
+ // Extract media info from the protobuf message if available
+ var mediaType, filename, mediaURL string
+ var mediaKey, fileSHA256, fileEncSHA256 []byte
+ var fileLength uint64
+ if msg != nil {
+ mediaType, filename, mediaURL, mediaKey, fileSHA256, fileEncSHA256, fileLength = utils.ExtractMediaInfo(msg)
+ }
+
// Store the sent message
message := &domainChatStorage.Message{
- ID: messageID,
- ChatJID: chatJID,
- DeviceID: deviceID,
- Sender: senderJID,
- Content: content,
- Timestamp: timestamp,
- IsFromMe: true,
+ ID: messageID,
+ ChatJID: chatJID,
+ DeviceID: deviceID,
+ Sender: senderJID,
+ Content: content,
+ Timestamp: timestamp,
+ IsFromMe: true,
+ MediaType: mediaType,
+ Filename: filename,
+ URL: mediaURL,
+ MediaKey: mediaKey,
+ FileSHA256: fileSHA256,
+ FileEncSHA256: fileEncSHA256,
+ FileLength: fileLength,
}
return r.StoreMessage(message)
diff --git a/src/infrastructure/whatsapp/auto_reply.go b/src/infrastructure/whatsapp/auto_reply.go
index e7edc6ab..9f8da08e 100644
--- a/src/infrastructure/whatsapp/auto_reply.go
+++ b/src/infrastructure/whatsapp/auto_reply.go
@@ -95,6 +95,7 @@ func handleAutoReply(ctx context.Context, evt *events.Message, chatStorageRepo d
recipientJID.String(), // Recipient JID
config.WhatsappAutoReplyMessage, // Auto-reply content
response.Timestamp, // Timestamp from response
+ nil, // text-only message, no media
); err != nil {
// Log storage error but don't fail the auto-reply
log.Errorf("Failed to store auto-reply message in chat storage: %v", err)
diff --git a/src/infrastructure/whatsapp/chatstorage_wrapper.go b/src/infrastructure/whatsapp/chatstorage_wrapper.go
index a09662cd..bf169fc2 100644
--- a/src/infrastructure/whatsapp/chatstorage_wrapper.go
+++ b/src/infrastructure/whatsapp/chatstorage_wrapper.go
@@ -5,6 +5,7 @@ import (
"time"
domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
+ "go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
@@ -102,8 +103,8 @@ func (r *deviceChatStorage) DeleteMessageByDevice(deviceID, id, chatJID string)
return r.base.DeleteMessageByDevice(deviceID, id, chatJID)
}
-func (r *deviceChatStorage) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error {
- return r.base.StoreSentMessageWithContext(ctx, messageID, senderJID, recipientJID, content, timestamp)
+func (r *deviceChatStorage) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time, msg *waE2E.Message) error {
+ return r.base.StoreSentMessageWithContext(ctx, messageID, senderJID, recipientJID, content, timestamp, msg)
}
func (r *deviceChatStorage) GetChatMessageCount(chatJID string) (int64, error) {
diff --git a/src/usecase/send.go b/src/usecase/send.go
index cb0d4e56..8dfc058b 100644
--- a/src/usecase/send.go
+++ b/src/usecase/send.go
@@ -69,7 +69,7 @@ func (service serviceSend) wrapSendMessage(ctx context.Context, client *whatsmeo
storeCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
- if err := service.chatStorageRepo.StoreSentMessageWithContext(storeCtx, ts.ID, senderJID, recipient.String(), content, ts.Timestamp); err != nil {
+ if err := service.chatStorageRepo.StoreSentMessageWithContext(storeCtx, ts.ID, senderJID, recipient.String(), content, ts.Timestamp, msg); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
logrus.Warn("Timeout storing sent message")
} else {