fix: delete messages when the signal arrives before the OG msg (#2310)

* fix: delete messages when the signal arrives before the OG msg
* fix: code review
This commit is contained in:
RichΛrd 2021-08-13 13:10:32 -04:00 committed by GitHub
parent 5ac8b873b8
commit dae447466f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 11 deletions

View File

@ -77,6 +77,8 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
s.NoError(err) s.NoError(err)
s.Require().Len(sendResponse.Messages(), 1) s.Require().Len(sendResponse.Messages(), 1)
messageID := sendResponse.Messages()[0].ID
response, err := WaitOnMessengerResponse( response, err := WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { return len(r.messages) > 0 }, func(r *MessengerResponse) bool { return len(r.messages) > 0 },
@ -91,8 +93,9 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID) sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(sendResponse.Messages(), 1) s.Require().Len(sendResponse.Messages(), 0)
s.Require().True(sendResponse.Messages()[0].Deleted) s.Require().Len(sendResponse.RemovedMessages(), 1)
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0])
s.Require().Len(sendResponse.Chats(), 1) s.Require().Len(sendResponse.Chats(), 1)
s.Require().NotNil(sendResponse.Chats()[0].LastMessage) s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
@ -166,9 +169,8 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessageFirstThenMessage() {
s.Require().NoError(err) s.Require().NoError(err)
// // Handle chat message // // Handle chat message
response = &MessengerResponse{}
state := &ReceivedMessageState{ state := &ReceivedMessageState{
Response: response, Response: &MessengerResponse{},
CurrentMessageState: &CurrentMessageState{ CurrentMessageState: &CurrentMessageState{
Message: inputMessage.ChatMessage, Message: inputMessage.ChatMessage,
MessageID: messageID, MessageID: messageID,
@ -179,7 +181,7 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessageFirstThenMessage() {
} }
err = s.m.HandleChatMessage(state) err = s.m.HandleChatMessage(state)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.Messages(), 1) s.Require().Len(state.Response.Messages(), 0) // Message should not be added to response
s.Require().True(response.Messages()[0].Deleted) s.Require().Len(state.Response.RemovedMessages(), 0)
s.Require().Nil(state.Response.Chats()[0].LastMessage)
} }

View File

@ -701,13 +701,24 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
return err return err
} }
err = chat.UpdateFromMessage(receivedMessage, m.getTimesource()) if receivedMessage.Deleted && (chat.LastMessage == nil || chat.LastMessage.ID == receivedMessage.ID) {
if err != nil { // Get last message that is not hidden
return err messages, _, err := m.persistence.MessageByChatID(receivedMessage.LocalChatID, "", 1)
if err != nil {
return err
}
if messages != nil {
chat.LastMessage = messages[0]
}
} else {
err = chat.UpdateFromMessage(receivedMessage, m.getTimesource())
if err != nil {
return err
}
} }
// If the chat is not active, create a notification in the center // If the chat is not active, create a notification in the center
if chat.OneToOne() && !chat.Active { if !receivedMessage.Deleted && chat.OneToOne() && !chat.Active {
m.createMessageNotification(chat, state) m.createMessageNotification(chat, state)
} }

View File

@ -155,6 +155,7 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string)
response := &MessengerResponse{} response := &MessengerResponse{}
response.AddMessage(message) response.AddMessage(message)
response.AddRemovedMessage(messageID)
response.AddChat(chat) response.AddChat(chat)
return response, nil return response, nil

View File

@ -320,6 +320,9 @@ func (r *MessengerResponse) AddMessage(message *common.Message) {
if r.messages == nil { if r.messages == nil {
r.messages = make(map[string]*common.Message) r.messages = make(map[string]*common.Message)
} }
if message.Deleted {
return
}
r.messages[message.ID] = message r.messages[message.ID] = message
} }