From 0c57890a84968a23b0272c96f98e7eb5d57beb47 Mon Sep 17 00:00:00 2001 From: Ibrahem Khalil Date: Mon, 10 Jul 2023 22:26:32 +0300 Subject: [PATCH] [status-mobile-16467] Fix delete for me on receiver side using wrong chatID (#3732) --- VERSION | 2 +- .../messenger_delete_message_for_me_test.go | 77 +++++++++++++++++++ protocol/messenger_messages.go | 6 +- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 32a29f946..2e038f7d5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.161.2 +0.161.3 diff --git a/protocol/messenger_delete_message_for_me_test.go b/protocol/messenger_delete_message_for_me_test.go index f5ced5808..92f5cc6fd 100644 --- a/protocol/messenger_delete_message_for_me_test.go +++ b/protocol/messenger_delete_message_for_me_test.go @@ -14,6 +14,7 @@ import ( "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/encryption/multidevice" + "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/tt" "github.com/status-im/status-go/waku" ) @@ -210,3 +211,79 @@ func (s *MessengerDeleteMessageForMeSuite) TestDeleteMessageForMe() { s.Require().NoError(err) s.Require().False(otherMessage.DeletedForMe) } + +func (s *MessengerDeleteMessageForMeSuite) TestDeleteImageMessageFromReceiverSide() { + + alice := s.otherNewMessenger() + _, err := alice.Start() + s.Require().NoError(err) + defer alice.Shutdown() // nolint: errcheck + + bob := s.otherNewMessenger() + _, err = bob.Start() + s.Require().NoError(err) + defer bob.Shutdown() // nolint: errcheck + + theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, alice.transport) + err = alice.SaveChat(theirChat) + s.Require().NoError(err) + + ourChat := CreateOneToOneChat("Our 1TO1", &alice.identity.PublicKey, alice.transport) + err = bob.SaveChat(ourChat) + s.Require().NoError(err) + + messageCount := 3 + var album []*common.Message + for i := 0; i < messageCount; i++ { + image, err := buildImageWithoutAlbumIDMessage(*ourChat) + s.NoError(err) + album = append(album, image) + } + + response, err := bob.SendChatMessages(context.Background(), album) + s.NoError(err) + + // Check that album count was the number of the images sent + imagesCount := uint32(0) + for _, message := range response.Messages() { + if message.ContentType == protobuf.ChatMessage_IMAGE { + imagesCount++ + } + } + for _, message := range response.Messages() { + s.Require().NotNil(message.GetImage()) + s.Require().Equal(message.GetImage().AlbumImagesCount, imagesCount) + } + + s.Require().Equal(messageCount, len(response.Messages()), "it returns the messages") + s.Require().NoError(err) + s.Require().Len(response.Messages(), messageCount) + + response, err = WaitOnMessengerResponse( + alice, + func(r *MessengerResponse) bool { return len(r.messages) == messageCount }, + "no messages", + ) + + s.Require().NoError(err) + s.Require().Len(response.Chats(), 1) + s.Require().Len(response.Messages(), messageCount) + for _, message := range response.Messages() { + image := message.GetImage() + s.Require().NotNil(image, "Message.ID=%s", message.ID) + s.Require().Equal(image.AlbumImagesCount, imagesCount) + s.Require().NotEmpty(image.AlbumId, "Message.ID=%s", message.ID) + } + + messages := response.Messages() + firstMessageID := messages[0].ID + localChatID := messages[0].LocalChatID + sendResponse, err := alice.DeleteMessageForMeAndSync(context.Background(), localChatID, firstMessageID) + s.Require().NoError(err) + s.Require().Len(sendResponse.Messages(), 3) + s.Require().Len(sendResponse.Chats(), 1) + + // LastMessage marked as deleted + s.Require().Equal(sendResponse.Chats()[0].LastMessage.ID, album[2].ID) + s.Require().Equal(sendResponse.Chats()[0].LastMessage.DeletedForMe, true) +} diff --git a/protocol/messenger_messages.go b/protocol/messenger_messages.go index 06af827c9..f2baf7168 100644 --- a/protocol/messenger_messages.go +++ b/protocol/messenger_messages.go @@ -239,14 +239,14 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string) return response, nil } -func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, chatID string, messageID string) (*MessengerResponse, error) { +func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, localChatID string, messageID string) (*MessengerResponse, error) { message, err := m.persistence.MessageByID(messageID) if err != nil { return nil, err } // A valid added chat is required. - chat, ok := m.allChats.Load(chatID) + chat, ok := m.allChats.Load(localChatID) if !ok { return nil, ErrChatNotFound } @@ -260,7 +260,7 @@ func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, chatID string return nil, ErrInvalidDeleteTypeAuthor } - messagesToDelete, err := m.getConnectedMessages(message, message.ChatId) + messagesToDelete, err := m.getConnectedMessages(message, localChatID) if err != nil { return nil, err }