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(message): scope reaction lookup to device and chat (#903)

Aldino Kemal4456510daf74
diff --git a/src/usecase/message.go b/src/usecase/message.go
index 393c79cb..5bfa09f4 100644
--- a/src/usecase/message.go
+++ b/src/usecase/message.go
@@ -261,7 +261,7 @@ func (service serviceMessage) ReactMessage(ctx context.Context, request domainMe
return response, pkgError.ErrWaCLI
}
- dataWaRecipient, err := utils.ValidateJidWithLogin(client, request.Phone)
+ dataWaRecipient, err := service.validateJID(client, request.Phone)
if err != nil {
return response, err
}
@@ -271,7 +271,13 @@ func (service serviceMessage) ReactMessage(ctx context.Context, request domainMe
// Participant field for group chats — required by the WhatsApp protocol.
// An empty JID means "message was from me".
senderJID := types.EmptyJID
- message, err := service.chatStorageRepo.GetMessageByID(request.MessageID)
+ // IsFromMe is relative to the receiving device, so another device's copy
+ // of the same message cannot determine the reaction's sender.
+ message, err := service.chatStorageRepo.GetMessageByIDChatAndDevice(
+ deviceIDFromContext(ctx),
+ dataWaRecipient.ToNonAD().String(),
+ request.MessageID,
+ )
if err != nil {
logrus.Warnf("Failed to lookup message %s for reaction: %v, using heuristic", request.MessageID, err)
if len(request.MessageID) > 22 {
@@ -295,7 +301,7 @@ func (service serviceMessage) ReactMessage(ctx context.Context, request domainMe
// BuildReaction correctly constructs the MessageKey with Participant field
// for group chats, which is required for the reaction to be delivered.
msg := client.BuildReaction(dataWaRecipient, senderJID, request.MessageID, request.Emoji)
- ts, err := client.SendMessage(ctx, dataWaRecipient, msg)
+ ts, err := service.sendMessage(ctx, client, dataWaRecipient, msg)
if err != nil {
return response, err
}
diff --git a/src/usecase/message_reaction_test.go b/src/usecase/message_reaction_test.go
new file mode 100644
index 00000000..d143f65f
--- /dev/null
+++ b/src/usecase/message_reaction_test.go
@@ -0,0 +1,105 @@
+package usecase
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
+ domainMessage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/message"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "go.mau.fi/whatsmeow"
+ "go.mau.fi/whatsmeow/proto/waE2E"
+ "go.mau.fi/whatsmeow/types"
+)
+
+func TestReactMessageUsesCurrentDeviceAndChat(t *testing.T) {
+ groupJID := types.NewJID("120363000000000002", types.GroupServer)
+ otherGroupJID := types.NewJID("120363000000000001", types.GroupServer)
+ senderJID := types.NewJID("628987654321", types.DefaultUserServer)
+ currentDevice := "[email protected]"
+
+ tests := []struct {
+ name string
+ messages []domainChatStorage.Message
+ wantFromMe bool
+ wantParticipant string
+ }{
+ {
+ name: "incoming message also stored as outgoing on another device",
+ messages: []domainChatStorage.Message{
+ {DeviceID: senderJID.String(), ChatJID: groupJID.String(), Sender: senderJID.String(), IsFromMe: true},
+ {DeviceID: currentDevice, ChatJID: groupJID.String(), Sender: senderJID.String()},
+ },
+ wantParticipant: senderJID.String(),
+ },
+ {
+ name: "outgoing message also stored as incoming on another device",
+ messages: []domainChatStorage.Message{
+ {DeviceID: senderJID.String(), ChatJID: groupJID.String(), Sender: currentDevice},
+ {DeviceID: currentDevice, ChatJID: groupJID.String(), Sender: currentDevice, IsFromMe: true},
+ },
+ wantFromMe: true,
+ },
+ {
+ name: "same message ID in another chat",
+ messages: []domainChatStorage.Message{
+ {DeviceID: currentDevice, ChatJID: otherGroupJID.String(), Sender: currentDevice, IsFromMe: true},
+ {DeviceID: currentDevice, ChatJID: groupJID.String(), Sender: senderJID.String()},
+ },
+ wantParticipant: senderJID.String(),
+ },
+ {
+ name: "message only stored on another device keeps missing-message fallback",
+ messages: []domainChatStorage.Message{
+ {DeviceID: senderJID.String(), ChatJID: groupJID.String(), Sender: senderJID.String()},
+ },
+ wantFromMe: true,
+ },
+ {
+ name: "message only stored in another chat keeps missing-message fallback",
+ messages: []domainChatStorage.Message{
+ {DeviceID: currentDevice, ChatJID: otherGroupJID.String(), Sender: senderJID.String()},
+ },
+ wantFromMe: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ service, repo, ctx := newMessageActionTestService(t, nil)
+ for _, message := range tt.messages {
+ message.ID = "reaction-target"
+ message.Content = "message to react to"
+ message.Timestamp = time.Now()
+ require.NoError(t, repo.StoreMessage(&message))
+ }
+ service.validateJIDFn = func(_ *whatsmeow.Client, _ string) (types.JID, error) {
+ return groupJID, nil
+ }
+ sent := false
+ service.sendMessageFn = func(_ context.Context, _ *whatsmeow.Client, recipient types.JID, message *waE2E.Message) (whatsmeow.SendResponse, error) {
+ sent = true
+ assert.Equal(t, groupJID, recipient)
+ reaction := message.GetReactionMessage()
+ require.NotNil(t, reaction)
+ assert.Equal(t, "👍", reaction.GetText())
+ assert.Equal(t, "reaction-target", reaction.GetKey().GetID())
+ assert.Equal(t, groupJID.String(), reaction.GetKey().GetRemoteJID())
+ assert.Equal(t, tt.wantFromMe, reaction.GetKey().GetFromMe())
+ assert.Equal(t, tt.wantParticipant, reaction.GetKey().GetParticipant())
+ return whatsmeow.SendResponse{ID: "reaction-1"}, nil
+ }
+
+ response, err := service.ReactMessage(ctx, domainMessage.ReactionRequest{
+ MessageID: "reaction-target",
+ Phone: groupJID.String(),
+ Emoji: "👍",
+ })
+ require.NoError(t, err)
+ assert.True(t, sent)
+ assert.Equal(t, "reaction-1", response.MessageID)
+ })
+ }
+}