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 revoke lookup to device and chat (#905)

Aldino Kemal1a7f9f7d7b39
diff --git a/src/usecase/message.go b/src/usecase/message.go
index 5bfa09f4..53593ed2 100644
--- a/src/usecase/message.go
+++ b/src/usecase/message.go
@@ -329,11 +329,14 @@ func (service serviceMessage) RevokeMessage(ctx context.Context, request domainM
// Resolve the original sender so group admins can revoke other members'
// messages. BuildRevoke treats types.EmptyJID as "message was from me";
// any other JID is admin-revoke and requires the bot to be group admin.
- // WhatsApp message IDs are globally unique, so a cross-device lookup
- // via GetMessageByID yields the same sender regardless of which device
- // owns the row.
+ // IsFromMe belongs to a device's copy of the message, so resolve it
+ // within the current device and chat before choosing self- or admin-revoke.
senderJID := types.EmptyJID
- message, lookupErr := service.chatStorageRepo.GetMessageByID(request.MessageID)
+ message, lookupErr := service.chatStorageRepo.GetMessageByIDChatAndDevice(
+ deviceIDFromContext(ctx),
+ dataWaRecipient.ToNonAD().String(),
+ request.MessageID,
+ )
if lookupErr != nil {
logrus.Warnf("Failed to lookup message %s for revoke: %v, assuming self-revoke", request.MessageID, lookupErr)
} else if message != nil && !message.IsFromMe && message.Sender != "" {
diff --git a/src/usecase/message_revoke_test.go b/src/usecase/message_revoke_test.go
new file mode 100644
index 00000000..6bf7177a
--- /dev/null
+++ b/src/usecase/message_revoke_test.go
@@ -0,0 +1,84 @@
+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 TestRevokeMessageUsesCurrentDeviceAndChat(t *testing.T) {
+ groupJID := types.NewJID("120363000000000002", types.GroupServer)
+ senderJID := types.NewJID("628987654321", types.DefaultUserServer)
+ currentDevice := "[email protected]"
+
+ tests := []struct {
+ name string
+ otherDevice string
+ otherChat string
+ }{
+ {
+ name: "message is outgoing on another connected device",
+ otherDevice: senderJID.String(),
+ otherChat: groupJID.String(),
+ },
+ {
+ name: "message ID also exists in another chat",
+ otherDevice: currentDevice,
+ otherChat: "[email protected]",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ service, repo, ctx := newMessageActionTestService(t, nil)
+ for _, message := range []domainChatStorage.Message{
+ {DeviceID: tt.otherDevice, ChatJID: tt.otherChat, Sender: senderJID.String(), IsFromMe: true},
+ {DeviceID: currentDevice, ChatJID: groupJID.String(), Sender: senderJID.String()},
+ } {
+ message.ID = "revoke-target"
+ message.Content = "message to revoke"
+ 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)
+ revoke := message.GetProtocolMessage()
+ require.NotNil(t, revoke)
+ assert.Equal(t, waE2E.ProtocolMessage_REVOKE, revoke.GetType())
+ assert.Equal(t, "revoke-target", revoke.GetKey().GetID())
+ assert.Equal(t, groupJID.String(), revoke.GetKey().GetRemoteJID())
+ assert.False(t, revoke.GetKey().GetFromMe(), "the current device is revoking another member's message")
+ assert.Equal(t, senderJID.String(), revoke.GetKey().GetParticipant())
+ return whatsmeow.SendResponse{ID: "revoke-1"}, nil
+ }
+
+ response, err := service.RevokeMessage(ctx, domainMessage.RevokeRequest{
+ MessageID: "revoke-target",
+ Phone: groupJID.String(),
+ })
+ require.NoError(t, err)
+ assert.True(t, sent)
+ assert.Equal(t, "revoke-1", response.MessageID)
+
+ message, err := repo.GetMessageByIDChatAndDevice(currentDevice, groupJID.String(), "revoke-target")
+ require.NoError(t, err)
+ assert.Nil(t, message)
+ otherMessage, err := repo.GetMessageByIDChatAndDevice(tt.otherDevice, tt.otherChat, "revoke-target")
+ require.NoError(t, err)
+ require.NotNil(t, otherMessage, "local cleanup must preserve the other device/chat's copy")
+ })
+ }
+}