Update AC notification on EditMessage

This commit is contained in:
Roman Volosovskyi 2023-02-06 11:38:37 +01:00
parent 2bcd7e97a6
commit 7360e07224
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
4 changed files with 61 additions and 25 deletions

View File

@ -1 +1 @@
0.129.2
0.129.3

View File

@ -2940,6 +2940,30 @@ func (r *ReceivedMessageState) addNewMessageNotification(publicKey ecdsa.PublicK
return nil
}
// updateExistingActivityCenterNotification updates AC notification if it exists and hasn't been read yet
func (r *ReceivedMessageState) updateExistingActivityCenterNotification(publicKey ecdsa.PublicKey, m *Messenger, message *common.Message, responseTo *common.Message) error {
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(message.ID))
if err != nil {
return err
}
if notification == nil || notification.Read {
return nil
}
notification.Message = message
notification.ReplyMessage = responseTo
err = m.persistence.SaveActivityCenterNotification(notification)
if err != nil {
m.logger.Error("failed to save notification", zap.Error(err))
return err
}
r.Response.AddActivityCenterNotification(notification)
return nil
}
// addNewActivityCenterNotification takes a common.Message and generates a new ActivityCenterNotification and appends it to the
// []Response.ActivityCenterNotifications if the message is m.New
func (r *ReceivedMessageState) addNewActivityCenterNotification(publicKey ecdsa.PublicKey, m *Messenger, message *common.Message, responseTo *common.Message) error {
@ -3194,7 +3218,6 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
defer m.handleMessagesMutex.Unlock()
messageState := m.buildMessageState()
response := messageState.Response
logger := m.logger.With(zap.String("site", "RetrieveAll"))
@ -3320,7 +3343,7 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte
ID: messageID,
SigPubKey: publicKey,
}
err = m.HandleEditMessage(response, editMessage)
err = m.HandleEditMessage(messageState, editMessage)
if err != nil {
logger.Warn("failed to handle EditMessage", zap.Error(err))
allMessagesProcessed = false

View File

@ -252,16 +252,18 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
From: wrongContact.ID,
}
response = &MessengerResponse{}
state := &ReceivedMessageState{
Response: &MessengerResponse{},
AllChats: &chatMap{},
}
state.AllChats.Store(ourChat.ID, ourChat)
err = s.m.HandleEditMessage(response, editMessage)
err = s.m.HandleEditMessage(state, editMessage)
// It should error as the user can't edit this message
s.Require().Error(err)
// Edit with a newer clock value
response = &MessengerResponse{}
contact, err := BuildContactFromPublicKey(&theirMessenger.identity.PublicKey)
s.Require().NoError(err)
@ -276,15 +278,15 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
From: contact.ID,
}
err = s.m.HandleEditMessage(response, editMessage)
err = s.m.HandleEditMessage(state, editMessage)
s.Require().NoError(err)
// It save the edit
s.Require().Len(response.Messages(), 1)
s.Require().Len(response.Chats(), 1)
s.Require().NotNil(response.Chats()[0].LastMessage)
s.Require().NotEmpty(response.Chats()[0].LastMessage.EditedAt)
s.Require().Len(state.Response.Messages(), 1)
s.Require().Len(state.Response.Chats(), 1)
s.Require().NotNil(state.Response.Chats()[0].LastMessage)
s.Require().NotEmpty(state.Response.Chats()[0].LastMessage.EditedAt)
editedMessage = response.Messages()[0]
editedMessage = state.Response.Messages()[0]
// In-between edit
editMessage = EditMessage{
@ -298,13 +300,13 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
From: contact.ID,
}
response = &MessengerResponse{}
state.Response = &MessengerResponse{}
err = s.m.HandleEditMessage(response, editMessage)
err = s.m.HandleEditMessage(state, editMessage)
// It should error as the user can't edit this message
s.Require().NoError(err)
// It discards the edit
s.Require().Len(response.Messages(), 0)
s.Require().Len(state.Response.Messages(), 0)
}
func (s *MessengerEditMessageSuite) TestEditMessageFirstEditsThenMessage() {
@ -336,16 +338,17 @@ func (s *MessengerEditMessageSuite) TestEditMessageFirstEditsThenMessage() {
},
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
}
response := &MessengerResponse{}
state := &ReceivedMessageState{
Response: &MessengerResponse{},
}
// Handle edit first
err = s.m.HandleEditMessage(response, editMessage)
err = s.m.HandleEditMessage(state, editMessage)
s.Require().NoError(err)
// Handle chat message
response = &MessengerResponse{}
state := &ReceivedMessageState{
response := &MessengerResponse{}
state = &ReceivedMessageState{
Response: response,
CurrentMessageState: &CurrentMessageState{
Message: inputMessage.ChatMessage,

View File

@ -1341,13 +1341,13 @@ func (m *Messenger) handleWrappedCommunityDescriptionMessage(payload []byte) (*c
return m.communitiesManager.HandleWrappedCommunityDescriptionMessage(payload)
}
func (m *Messenger) HandleEditMessage(response *MessengerResponse, editMessage EditMessage) error {
func (m *Messenger) HandleEditMessage(state *ReceivedMessageState, editMessage EditMessage) error {
if err := ValidateEditMessage(editMessage.EditMessage); err != nil {
return err
}
messageID := editMessage.MessageId
// Check if it's already in the response
originalMessage := response.GetMessage(messageID)
originalMessage := state.Response.GetMessage(messageID)
// otherwise pull from database
if originalMessage == nil {
var err error
@ -1391,8 +1391,18 @@ func (m *Messenger) HandleEditMessage(response *MessengerResponse, editMessage E
return err
}
}
response.AddMessage(originalMessage)
response.AddChat(chat)
responseTo, err := m.persistence.MessageByID(originalMessage.ResponseTo)
if err != nil && err != common.ErrRecordNotFound {
return err
}
err = state.updateExistingActivityCenterNotification(m.identity.PublicKey, m, originalMessage, responseTo)
if err != nil {
return err
}
state.Response.AddMessage(originalMessage)
state.Response.AddChat(chat)
return nil
}