From 92ba63b2825a1dc255af471fd9e8e75d85d4b308 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 3 Dec 2024 10:04:21 -0500 Subject: [PATCH] fix(edit)_: make sure the contentType stays the same after an edit (#6133) Fixes https://github.com/status-im/status-desktop/issues/16741 The issue was that in image messages, you can update the text, but then the ContentType would become Text and lose the image. The solution is to ignore ContentType changes, since there is no way to change the type of message. --- protocol/messenger_edit_message_test.go | 87 ++++++++++++++++++++++++- protocol/messenger_messages.go | 2 +- protocol/requests/edit_message.go | 10 ++- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/protocol/messenger_edit_message_test.go b/protocol/messenger_edit_message_test.go index e670329a7..40778482b 100644 --- a/protocol/messenger_edit_message_test.go +++ b/protocol/messenger_edit_message_test.go @@ -90,6 +90,88 @@ func (s *MessengerEditMessageSuite) TestEditMessage() { s.Require().Equal(ErrInvalidEditOrDeleteAuthor, err) } +func (s *MessengerEditMessageSuite) TestEditImageMessage() { + theirMessenger := s.newMessenger() + defer TearDownMessenger(&s.Suite, theirMessenger) + + theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport) + err := theirMessenger.SaveChat(theirChat) + s.Require().NoError(err) + + ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport) + err = s.m.SaveChat(ourChat) + s.Require().NoError(err) + + const messageCount = 1 + var album []*common.Message + + for i := 0; i < messageCount; i++ { + image, err := buildImageWithoutAlbumIDMessage(*ourChat) + s.NoError(err) + image.Text = "my message" + album = append(album, image) + } + + sendResponse, err := s.m.SendChatMessages(context.Background(), album) + s.NoError(err) + s.Require().Len(sendResponse.Messages(), messageCount) + + ogMessage := sendResponse.Messages()[0] + + response, err := WaitOnMessengerResponse( + theirMessenger, + func(r *MessengerResponse) bool { + if len(r.messages) == 0 { + return false + } + _, ok := r.messages[ogMessage.ID] + return ok + }, + "no messages", + ) + s.Require().NoError(err) + s.Require().Len(response.Chats(), 1) + s.Require().Len(response.Messages(), 1) + + messageID, err := types.DecodeHex(ogMessage.ID) + s.Require().NoError(err) + + editedText := "edited text" + editedMessage := &requests.EditMessage{ + ID: messageID, + Text: editedText, + } + + sendResponse, err = s.m.EditMessage(context.Background(), editedMessage) + + s.Require().NoError(err) + s.Require().Len(sendResponse.Messages(), 1) + s.Require().Equal(editedText, sendResponse.Messages()[0].Text) + s.Require().Equal(protobuf.ChatMessage_IMAGE, sendResponse.Messages()[0].ContentType) + + response, err = WaitOnMessengerResponse( + theirMessenger, + func(r *MessengerResponse) bool { + if len(r.messages) == 0 { + return false + } + _, ok := r.messages[sendResponse.Messages()[0].ID] + return ok + }, + "no messages", + ) + s.Require().NoError(err) + s.Require().Len(response.Messages(), 1) + s.Require().NotEmpty(response.Messages()[0].EditedAt) + s.Require().False(response.Messages()[0].New) + s.Require().Equal(protobuf.ChatMessage_IMAGE, response.Messages()[0].ContentType) + + // Check DB to make sure the message is still an image + dbMessage, err := s.m.persistence.MessageByID(ogMessage.ID) + s.Require().NoError(err) + s.Require().Equal(protobuf.ChatMessage_IMAGE, dbMessage.ContentType) +} + func (s *MessengerEditMessageSuite) TestEditBridgeMessage() { theirMessenger := s.newMessenger() defer TearDownMessenger(&s.Suite, theirMessenger) @@ -136,9 +218,8 @@ func (s *MessengerEditMessageSuite) TestEditBridgeMessage() { editedText := "edited text" editedMessage := &requests.EditMessage{ - ID: messageID, - Text: editedText, - ContentType: protobuf.ChatMessage_BRIDGE_MESSAGE, + ID: messageID, + Text: editedText, } sendResponse, err = theirMessenger.EditMessage(context.Background(), editedMessage) diff --git a/protocol/messenger_messages.go b/protocol/messenger_messages.go index e9ff7590a..b723218a7 100644 --- a/protocol/messenger_messages.go +++ b/protocol/messenger_messages.go @@ -70,7 +70,7 @@ func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessa replacedText = request.Text } editMessage.Text = replacedText - editMessage.ContentType = request.ContentType + editMessage.ContentType = message.ContentType // The contentType cannot change editMessage.ChatId = message.ChatId editMessage.MessageId = message.ID editMessage.Clock = clock diff --git a/protocol/requests/edit_message.go b/protocol/requests/edit_message.go index 60c88f21a..76ac140b3 100644 --- a/protocol/requests/edit_message.go +++ b/protocol/requests/edit_message.go @@ -5,18 +5,16 @@ 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/protobuf" ) var ErrEditMessageInvalidID = errors.New("edit-message: invalid id") var ErrEditMessageInvalidText = errors.New("edit-message: invalid text") type EditMessage struct { - ID types.HexBytes `json:"id"` - Text string `json:"text"` - ContentType protobuf.ChatMessage_ContentType `json:"content-type"` - LinkPreviews []common.LinkPreview `json:"linkPreviews"` - StatusLinkPreviews []common.StatusLinkPreview `json:"statusLinkPreviews"` + ID types.HexBytes `json:"id"` + Text string `json:"text"` + LinkPreviews []common.LinkPreview `json:"linkPreviews"` + StatusLinkPreviews []common.StatusLinkPreview `json:"statusLinkPreviews"` } func (e *EditMessage) Validate() error {