Fix last message deletion

This commit is contained in:
Andrea Maria Piana 2021-08-17 14:40:51 +02:00
parent b8959e3f66
commit 482518d179
3 changed files with 68 additions and 14 deletions

View File

@ -97,7 +97,8 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
s.Require().Len(sendResponse.RemovedMessages(), 1)
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0])
s.Require().Len(sendResponse.Chats(), 1)
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
// LastMessage is removed
s.Require().Nil(sendResponse.Chats()[0].LastMessage)
// Main instance user attempts to delete the message it received from theirMessenger
_, err = s.m.DeleteMessageAndSend(context.Background(), ogMessage.ID)
@ -105,6 +106,55 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
s.Require().Equal(ErrInvalidEditOrDeleteAuthor, err)
}
func (s *MessengerDeleteMessageSuite) TestDeleteMessagePreviousLastMessage() {
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()
s.Require().NoError(err)
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)
inputMessage1 := buildTestMessage(*theirChat)
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage1)
s.NoError(err)
s.Require().Len(sendResponse.Messages(), 1)
inputMessage2 := buildTestMessage(*theirChat)
sendResponse, err = theirMessenger.SendChatMessage(context.Background(), inputMessage2)
s.NoError(err)
s.Require().Len(sendResponse.Messages(), 1)
messageID := sendResponse.Messages()[0].ID
response, err := WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Messages(), 2)
ogMessage := sendResponse.Messages()[0]
sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
s.Require().NoError(err)
s.Require().Len(sendResponse.Messages(), 0)
s.Require().Len(sendResponse.RemovedMessages(), 1)
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0])
s.Require().Len(sendResponse.Chats(), 1)
// LastMessage is updated to previous message
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
s.Require().Equal(inputMessage1.ID, sendResponse.Chats()[0].LastMessage.ID)
}
func (s *MessengerDeleteMessageSuite) TestDeleteWrongMessageType() {
theirMessenger := s.newMessenger()
_, err := theirMessenger.Start()

View File

@ -599,18 +599,9 @@ func (m *Messenger) HandleDeleteMessage(response *MessengerResponse, deleteMessa
}
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
// Get last message that is not hidden
messages, _, err := m.persistence.MessageByChatID(originalMessage.LocalChatID, "", 1)
if err != nil {
if err := m.updateLastMessage(chat); err != nil {
return err
}
if messages != nil {
chat.LastMessage = messages[0]
err := m.saveChat(chat)
if err != nil {
return err
}
}
}
response.AddRemovedMessage(messageID)
response.AddChat(chat)
@ -618,6 +609,21 @@ func (m *Messenger) HandleDeleteMessage(response *MessengerResponse, deleteMessa
return nil
}
func (m *Messenger) updateLastMessage(chat *Chat) error {
// Get last message that is not hidden
messages, _, err := m.persistence.MessageByChatID(chat.ID, "", 1)
if err != nil {
return err
}
if len(messages) > 0 {
chat.LastMessage = messages[0]
} else {
chat.LastMessage = nil
}
return m.saveChat(chat)
}
func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
logger := m.logger.With(zap.String("site", "handleChatMessage"))
if err := ValidateReceivedChatMessage(&state.CurrentMessageState.Message, state.CurrentMessageState.WhisperTimestamp); err != nil {

View File

@ -146,9 +146,7 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string)
}
if chat.LastMessage != nil && chat.LastMessage.ID == message.ID {
chat.LastMessage = message
err := m.saveChat(chat)
if err != nil {
if err := m.updateLastMessage(chat); err != nil {
return nil, err
}
}