More fields added to ChatPreview. LastMessage now can be a deleted message (#3397)

This commit is contained in:
Volodymyr Kozieiev 2023-05-08 18:02:54 +01:00 committed by GitHub
parent c5a8b40d9c
commit a669e7d038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 64 deletions

View File

@ -219,6 +219,13 @@ type ChatPreview struct {
// Members array to represent how many there are for chats preview of group chats
Members []ChatMember `json:"members"`
OutgoingStatus string `json:"outgoingStatus,omitempty"`
ResponseTo string `json:"responseTo"`
AlbumImagesCount uint32 `json:"albumImagesCount,omitempty"`
From string `json:"from"`
Deleted bool `json:"deleted"`
DeletedForMe bool `json:"deletedForMe"`
}
func (c *Chat) PublicKey() (*ecdsa.PublicKey, error) {

View File

@ -351,17 +351,21 @@ func (m *Message) UnmarshalJSON(data []byte) error {
type Alias Message
aux := struct {
*Alias
ResponseTo string `json:"responseTo"`
EnsName string `json:"ensName"`
DisplayName string `json:"displayName"`
ChatID string `json:"chatId"`
Sticker *protobuf.StickerMessage `json:"sticker"`
AudioDurationMs uint64 `json:"audioDurationMs"`
ParsedText json.RawMessage `json:"parsedText"`
ContentType protobuf.ChatMessage_ContentType `json:"contentType"`
AlbumID string `json:"albumId"`
ImageWidth uint32 `json:"imageWidth"`
ImageHeight uint32 `json:"imageHeight"`
ResponseTo string `json:"responseTo"`
EnsName string `json:"ensName"`
DisplayName string `json:"displayName"`
ChatID string `json:"chatId"`
Sticker *protobuf.StickerMessage `json:"sticker"`
AudioDurationMs uint64 `json:"audioDurationMs"`
ParsedText json.RawMessage `json:"parsedText"`
ContentType protobuf.ChatMessage_ContentType `json:"contentType"`
AlbumID string `json:"albumId"`
ImageWidth uint32 `json:"imageWidth"`
ImageHeight uint32 `json:"imageHeight"`
AlbumImagesCount uint32 `json:"albumImagesCount"`
From string `json:"from"`
Deleted bool `json:"deleted,omitempty"`
DeletedForMe bool `json:"deletedForMe,omitempty"`
}{
Alias: (*Alias)(m),
}
@ -380,9 +384,10 @@ func (m *Message) UnmarshalJSON(data []byte) error {
if aux.ContentType == protobuf.ChatMessage_IMAGE {
m.Payload = &protobuf.ChatMessage_Image{
Image: &protobuf.ImageMessage{
AlbumId: aux.AlbumID,
Width: aux.ImageWidth,
Height: aux.ImageHeight},
AlbumId: aux.AlbumID,
Width: aux.ImageWidth,
Height: aux.ImageHeight,
AlbumImagesCount: aux.AlbumImagesCount},
}
}
@ -392,6 +397,9 @@ func (m *Message) UnmarshalJSON(data []byte) error {
m.ChatId = aux.ChatID
m.ContentType = aux.ContentType
m.ParsedText = aux.ParsedText
m.From = aux.From
m.Deleted = aux.Deleted
m.DeletedForMe = aux.DeletedForMe
return nil
}

View File

@ -3,13 +3,12 @@ package protocol
import (
"context"
"errors"
"strings"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/transport"
"strings"
)
func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) {
@ -76,8 +75,26 @@ func (m *Messenger) ChatsPreview() []*ChatPreview {
Highlight: chat.Highlight,
Members: chat.Members,
}
if chat.LastMessage != nil {
chatPreview.OutgoingStatus = chat.LastMessage.OutgoingStatus
chatPreview.ResponseTo = chat.LastMessage.ResponseTo
chatPreview.ContentType = chat.LastMessage.ContentType
chatPreview.From = chat.LastMessage.From
chatPreview.Deleted = chat.LastMessage.Deleted
chatPreview.DeletedForMe = chat.LastMessage.DeletedForMe
if chat.LastMessage.ContentType == protobuf.ChatMessage_IMAGE {
chatPreview.ParsedText = chat.LastMessage.ParsedText
image := chat.LastMessage.GetImage()
if image != nil {
chatPreview.AlbumImagesCount = image.AlbumImagesCount
chatPreview.ParsedText = chat.LastMessage.ParsedText
}
}
if chat.LastMessage.ContentType == protobuf.ChatMessage_TEXT_PLAIN {
simplifiedText, err := chat.LastMessage.GetSimplifiedText("", nil)

View File

@ -162,7 +162,8 @@ func (s *MessengerDeleteMessageForMeSuite) TestDeleteMessageForMe() {
response, err = s.alice1.DeleteMessageForMeAndSync(context.Background(), chatID, messageID)
s.Require().NoError(err)
s.Require().True(response.Messages()[0].DeletedForMe)
s.Require().Nil(response.Chats()[0].LastMessage)
s.Require().Equal(response.Chats()[0].LastMessage.ID, messageID)
s.Require().Equal(response.Chats()[0].LastMessage.DeletedForMe, true)
err = tt.RetryWithBackOff(func() error {
var err error

View File

@ -98,8 +98,10 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
s.Require().Len(sendResponse.Chats(), 1)
// LastMessage is removed
s.Require().Nil(sendResponse.Chats()[0].LastMessage)
// LastMessage is marked as deleted
lastRemovedMessage := sendResponse.Chats()[0].LastMessage
s.Require().Equal(lastRemovedMessage.ID, messageID)
s.Require().Equal(lastRemovedMessage.Deleted, true)
// Main instance user attempts to delete the message it received from theirMessenger
_, err = s.m.DeleteMessageAndSend(context.Background(), ogMessage.ID)
@ -150,9 +152,10 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessagePreviousLastMessage() {
s.Require().Len(sendResponse.RemovedMessages(), 1)
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
s.Require().Len(sendResponse.Chats(), 1)
// LastMessage is updated to previous message
// LastMessage is updated and marked as deleted
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
s.Require().Equal(inputMessage1.ID, sendResponse.Chats()[0].LastMessage.ID)
s.Require().Equal(inputMessage2.ID, sendResponse.Chats()[0].LastMessage.ID)
s.Require().Equal(sendResponse.Chats()[0].LastMessage.Deleted, true)
}
@ -303,8 +306,10 @@ func (s *MessengerDeleteMessageSuite) TestDeleteImageMessage() {
s.Require().Len(sendResponse.RemovedMessages(), 3)
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
s.Require().Len(sendResponse.Chats(), 1)
// LastMessage is removed
s.Require().Nil(sendResponse.Chats()[0].LastMessage)
// LastMessage marked as deleted
s.Require().Equal(sendResponse.Chats()[0].LastMessage.ID, album[2].ID)
s.Require().Equal(sendResponse.Chats()[0].LastMessage.Deleted, true)
// Main instance user attempts to delete the message it received from theirMessenger
_, err = theirMessenger.DeleteMessageAndSend(context.Background(), firstMessageID)

View File

@ -179,7 +179,7 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c
// Only create a message notification when the user is added, not when removed
if !chat.Active && wasUserAdded {
chat.Highlight = true
m.createMessageNotification(chat, messageState)
m.createMessageNotification(chat, messageState, chat.LastMessage)
}
profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility()
@ -241,7 +241,7 @@ func (m *Messenger) checkIfCreatorIsOurContact(group *v1protocol.Group) bool {
return false
}
func (m *Messenger) createMessageNotification(chat *Chat, messageState *ReceivedMessageState) {
func (m *Messenger) createMessageNotification(chat *Chat, messageState *ReceivedMessageState, message *common.Message) {
var notificationType ActivityCenterType
if chat.OneToOne() {
@ -252,7 +252,7 @@ func (m *Messenger) createMessageNotification(chat *Chat, messageState *Received
notification := &ActivityCenterNotification{
ID: types.FromHex(chat.ID),
Name: chat.Name,
LastMessage: chat.LastMessage,
LastMessage: message,
Type: notificationType,
Author: messageState.CurrentMessageState.Contact.ID,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
@ -394,7 +394,7 @@ func (m *Messenger) handleCommandMessage(state *ReceivedMessageState, message *c
}
if !chat.Active {
m.createMessageNotification(chat, state)
m.createMessageNotification(chat, state, chat.LastMessage)
}
// Add to response
@ -1607,7 +1607,7 @@ func (m *Messenger) HandleDeleteMessage(state *ReceivedMessageState, deleteMessa
}
}
messagesToDelete, err := m.getConnectedMessages(originalMessage, deleteMessage.ChatId)
messagesToDelete, err := m.getConnectedMessages(originalMessage, originalMessage.LocalChatID)
if err != nil {
return err
}
@ -1649,13 +1649,22 @@ func (m *Messenger) HandleDeleteMessage(state *ReceivedMessageState, deleteMessa
Deleted: true,
})
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
if err := m.updateLastMessage(chat); err != nil {
return err
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
chat.LastMessage = messageToDelete
err = m.saveChat(chat)
if err != nil {
return nil
}
}
if chat.LastMessage != nil && !chat.LastMessage.Seen && chat.OneToOne() && !chat.Active {
m.createMessageNotification(chat, state)
messages, err := m.persistence.LatestMessageByChatID(chat.ID)
if err != nil {
return err
}
if len(messages) > 0 {
previousNotDeletedMessage := messages[0]
if previousNotDeletedMessage != nil && !previousNotDeletedMessage.Seen && chat.OneToOne() && !chat.Active {
m.createMessageNotification(chat, state, previousNotDeletedMessage)
}
}
@ -1725,8 +1734,10 @@ func (m *Messenger) HandleDeleteForMeMessage(state *ReceivedMessageState, delete
}
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
if err := m.updateLastMessage(chat); err != nil {
return err
chat.LastMessage = messageToDelete
err = m.saveChat(chat)
if err != nil {
return nil
}
}
@ -1737,21 +1748,6 @@ func (m *Messenger) HandleDeleteForMeMessage(state *ReceivedMessageState, delete
return nil
}
func (m *Messenger) updateLastMessage(chat *Chat) error {
// Get last message that is not hidden
messages, err := m.persistence.LatestMessageByChatID(chat.ID)
if err != nil {
return err
}
if len(messages) > 0 {
chat.LastMessage = messages[0]
} else {
chat.LastMessage = nil
}
return m.saveChat(chat)
}
func handleContactRequestChatMessage(receivedMessage *common.Message, contact *Contact, outgoing bool, logger *zap.Logger) (bool, error) {
receivedMessage.ContactRequestState = common.ContactRequestStatePending
@ -1981,18 +1977,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
return err
}
if (receivedMessage.Deleted || receivedMessage.DeletedForMe) && (chat.LastMessage == nil || chat.LastMessage.ID == receivedMessage.ID) {
// Get last message that is not hidden
messages, err := m.persistence.LatestMessageByChatID(receivedMessage.LocalChatID)
if err != nil {
return err
}
if len(messages) != 0 {
chat.LastMessage = messages[0]
} else {
chat.LastMessage = nil
}
} else {
if !receivedMessage.Deleted && !receivedMessage.DeletedForMe {
err = chat.UpdateFromMessage(receivedMessage, m.getTimesource())
if err != nil {
return err

View File

@ -217,7 +217,10 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string)
response.AddRemovedMessage(&RemovedMessage{MessageID: messageToDelete.ID, ChatID: chat.ID, DeletedBy: deletedBy})
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
if err := m.updateLastMessage(chat); err != nil {
chat.LastMessage = messageToDelete
err = m.saveChat(chat)
if err != nil {
return nil, err
}
}
@ -269,7 +272,9 @@ func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, chatID string
}
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
if err := m.updateLastMessage(chat); err != nil {
chat.LastMessage = messageToDelete
err = m.saveChat(chat)
if err != nil {
return nil, err
}
}