add last message

This commit is contained in:
Andrea Maria Piana 2021-06-23 12:07:26 +02:00
parent b4f8ae7602
commit 9807c285e5
3 changed files with 36 additions and 4 deletions

View File

@ -528,6 +528,13 @@ func (m *Messenger) HandleEditMessage(response *MessengerResponse, editMessage E
return err return err
} }
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
chat.LastMessage = originalMessage
err := m.saveChat(chat)
if err != nil {
return err
}
}
response.AddMessage(originalMessage) response.AddMessage(originalMessage)
response.AddChat(chat) response.AddChat(chat)

View File

@ -104,6 +104,9 @@ func (s *MessengerEditMessageSuite) TestEditMessage() {
s.Require().Len(sendResponse.Messages(), 1) s.Require().Len(sendResponse.Messages(), 1)
s.Require().NotEmpty(sendResponse.Messages()[0].EditedAt) s.Require().NotEmpty(sendResponse.Messages()[0].EditedAt)
s.Require().Equal(sendResponse.Messages()[0].Text, editedText) s.Require().Equal(sendResponse.Messages()[0].Text, editedText)
s.Require().Len(sendResponse.Chats(), 1)
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
s.Require().NotEmpty(sendResponse.Chats()[0].LastMessage.EditedAt)
response, err = WaitOnMessengerResponse( response, err = WaitOnMessengerResponse(
s.m, s.m,
@ -195,10 +198,12 @@ func (s *MessengerEditMessageSuite) TestEditMessageEdgeCases() {
} }
err = s.m.HandleEditMessage(response, editMessage) err = s.m.HandleEditMessage(response, editMessage)
// It should error as the user can't edit this message
s.Require().NoError(err) s.Require().NoError(err)
// It save the edit // It save the edit
s.Require().Len(response.Messages(), 1) 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)
editedMessage = response.Messages()[0] editedMessage = response.Messages()[0]

View File

@ -3,7 +3,6 @@ package protocol
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
@ -18,7 +17,6 @@ func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessa
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println("IDDD", request.ID.String())
message, err := m.persistence.MessageByID(request.ID.String()) message, err := m.persistence.MessageByID(request.ID.String())
if err != nil { if err != nil {
return nil, err return nil, err
@ -68,17 +66,39 @@ func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessa
return nil, err return nil, err
} }
if chat.LastMessage != nil && chat.LastMessage.ID == message.ID {
chat.LastMessage = message
err := m.saveChat(chat)
if err != nil {
return nil, err
}
}
response := &MessengerResponse{} response := &MessengerResponse{}
response.AddMessage(message) response.AddMessage(message)
response.AddChat(chat)
return response, nil return response, nil
} }
func (m *Messenger) applyEditMessage(editMessage *protobuf.EditMessage, message *common.Message) error { func (m *Messenger) applyEditMessage(editMessage *protobuf.EditMessage, message *common.Message) error {
// TODO: should save an edit if it's the first time it's being edited
message.Text = editMessage.Text message.Text = editMessage.Text
message.EditedAt = editMessage.Clock message.EditedAt = editMessage.Clock
// Save original message as edit so we can retrieve history
if message.EditedAt == 0 {
originalEdit := EditMessage{}
originalEdit.Clock = message.Clock
originalEdit.LocalChatID = message.LocalChatID
originalEdit.MessageId = message.ID
originalEdit.Text = message.Text
originalEdit.From = message.From
err := m.persistence.SaveEdit(originalEdit)
if err != nil {
return err
}
}
err := message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey)) err := message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
if err != nil { if err != nil {
return err return err