More fields added to ChatPreview. LastMessage now can be a deleted message (#3397)
This commit is contained in:
parent
c5a8b40d9c
commit
a669e7d038
|
@ -219,6 +219,13 @@ type ChatPreview struct {
|
||||||
|
|
||||||
// Members array to represent how many there are for chats preview of group chats
|
// Members array to represent how many there are for chats preview of group chats
|
||||||
Members []ChatMember `json:"members"`
|
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) {
|
func (c *Chat) PublicKey() (*ecdsa.PublicKey, error) {
|
||||||
|
|
|
@ -362,6 +362,10 @@ func (m *Message) UnmarshalJSON(data []byte) error {
|
||||||
AlbumID string `json:"albumId"`
|
AlbumID string `json:"albumId"`
|
||||||
ImageWidth uint32 `json:"imageWidth"`
|
ImageWidth uint32 `json:"imageWidth"`
|
||||||
ImageHeight uint32 `json:"imageHeight"`
|
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),
|
Alias: (*Alias)(m),
|
||||||
}
|
}
|
||||||
|
@ -382,7 +386,8 @@ func (m *Message) UnmarshalJSON(data []byte) error {
|
||||||
Image: &protobuf.ImageMessage{
|
Image: &protobuf.ImageMessage{
|
||||||
AlbumId: aux.AlbumID,
|
AlbumId: aux.AlbumID,
|
||||||
Width: aux.ImageWidth,
|
Width: aux.ImageWidth,
|
||||||
Height: aux.ImageHeight},
|
Height: aux.ImageHeight,
|
||||||
|
AlbumImagesCount: aux.AlbumImagesCount},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,6 +397,9 @@ func (m *Message) UnmarshalJSON(data []byte) error {
|
||||||
m.ChatId = aux.ChatID
|
m.ChatId = aux.ChatID
|
||||||
m.ContentType = aux.ContentType
|
m.ContentType = aux.ContentType
|
||||||
m.ParsedText = aux.ParsedText
|
m.ParsedText = aux.ParsedText
|
||||||
|
m.From = aux.From
|
||||||
|
m.Deleted = aux.Deleted
|
||||||
|
m.DeletedForMe = aux.DeletedForMe
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,12 @@ package protocol
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"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"
|
||||||
"github.com/status-im/status-go/protocol/requests"
|
"github.com/status-im/status-go/protocol/requests"
|
||||||
"github.com/status-im/status-go/protocol/transport"
|
"github.com/status-im/status-go/protocol/transport"
|
||||||
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) {
|
func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) {
|
||||||
|
@ -76,8 +75,26 @@ func (m *Messenger) ChatsPreview() []*ChatPreview {
|
||||||
Highlight: chat.Highlight,
|
Highlight: chat.Highlight,
|
||||||
Members: chat.Members,
|
Members: chat.Members,
|
||||||
}
|
}
|
||||||
|
|
||||||
if chat.LastMessage != nil {
|
if chat.LastMessage != nil {
|
||||||
|
|
||||||
|
chatPreview.OutgoingStatus = chat.LastMessage.OutgoingStatus
|
||||||
|
chatPreview.ResponseTo = chat.LastMessage.ResponseTo
|
||||||
chatPreview.ContentType = chat.LastMessage.ContentType
|
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 {
|
if chat.LastMessage.ContentType == protobuf.ChatMessage_TEXT_PLAIN {
|
||||||
|
|
||||||
simplifiedText, err := chat.LastMessage.GetSimplifiedText("", nil)
|
simplifiedText, err := chat.LastMessage.GetSimplifiedText("", nil)
|
||||||
|
|
|
@ -162,7 +162,8 @@ func (s *MessengerDeleteMessageForMeSuite) TestDeleteMessageForMe() {
|
||||||
response, err = s.alice1.DeleteMessageForMeAndSync(context.Background(), chatID, messageID)
|
response, err = s.alice1.DeleteMessageForMeAndSync(context.Background(), chatID, messageID)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
s.Require().True(response.Messages()[0].DeletedForMe)
|
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 {
|
err = tt.RetryWithBackOff(func() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -98,8 +98,10 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
|
||||||
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
||||||
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
||||||
s.Require().Len(sendResponse.Chats(), 1)
|
s.Require().Len(sendResponse.Chats(), 1)
|
||||||
// LastMessage is removed
|
// LastMessage is marked as deleted
|
||||||
s.Require().Nil(sendResponse.Chats()[0].LastMessage)
|
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
|
// Main instance user attempts to delete the message it received from theirMessenger
|
||||||
_, err = s.m.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
_, err = s.m.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
||||||
|
@ -150,9 +152,10 @@ func (s *MessengerDeleteMessageSuite) TestDeleteMessagePreviousLastMessage() {
|
||||||
s.Require().Len(sendResponse.RemovedMessages(), 1)
|
s.Require().Len(sendResponse.RemovedMessages(), 1)
|
||||||
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
||||||
s.Require().Len(sendResponse.Chats(), 1)
|
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().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().Len(sendResponse.RemovedMessages(), 3)
|
||||||
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
||||||
s.Require().Len(sendResponse.Chats(), 1)
|
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
|
// Main instance user attempts to delete the message it received from theirMessenger
|
||||||
_, err = theirMessenger.DeleteMessageAndSend(context.Background(), firstMessageID)
|
_, err = theirMessenger.DeleteMessageAndSend(context.Background(), firstMessageID)
|
||||||
|
|
|
@ -179,7 +179,7 @@ func (m *Messenger) HandleMembershipUpdate(messageState *ReceivedMessageState, c
|
||||||
// Only create a message notification when the user is added, not when removed
|
// Only create a message notification when the user is added, not when removed
|
||||||
if !chat.Active && wasUserAdded {
|
if !chat.Active && wasUserAdded {
|
||||||
chat.Highlight = true
|
chat.Highlight = true
|
||||||
m.createMessageNotification(chat, messageState)
|
m.createMessageNotification(chat, messageState, chat.LastMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility()
|
profilePicturesVisibility, err := m.settings.GetProfilePicturesVisibility()
|
||||||
|
@ -241,7 +241,7 @@ func (m *Messenger) checkIfCreatorIsOurContact(group *v1protocol.Group) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Messenger) createMessageNotification(chat *Chat, messageState *ReceivedMessageState) {
|
func (m *Messenger) createMessageNotification(chat *Chat, messageState *ReceivedMessageState, message *common.Message) {
|
||||||
|
|
||||||
var notificationType ActivityCenterType
|
var notificationType ActivityCenterType
|
||||||
if chat.OneToOne() {
|
if chat.OneToOne() {
|
||||||
|
@ -252,7 +252,7 @@ func (m *Messenger) createMessageNotification(chat *Chat, messageState *Received
|
||||||
notification := &ActivityCenterNotification{
|
notification := &ActivityCenterNotification{
|
||||||
ID: types.FromHex(chat.ID),
|
ID: types.FromHex(chat.ID),
|
||||||
Name: chat.Name,
|
Name: chat.Name,
|
||||||
LastMessage: chat.LastMessage,
|
LastMessage: message,
|
||||||
Type: notificationType,
|
Type: notificationType,
|
||||||
Author: messageState.CurrentMessageState.Contact.ID,
|
Author: messageState.CurrentMessageState.Contact.ID,
|
||||||
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
||||||
|
@ -394,7 +394,7 @@ func (m *Messenger) handleCommandMessage(state *ReceivedMessageState, message *c
|
||||||
}
|
}
|
||||||
|
|
||||||
if !chat.Active {
|
if !chat.Active {
|
||||||
m.createMessageNotification(chat, state)
|
m.createMessageNotification(chat, state, chat.LastMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to response
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1649,13 +1649,22 @@ func (m *Messenger) HandleDeleteMessage(state *ReceivedMessageState, deleteMessa
|
||||||
Deleted: true,
|
Deleted: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
if chat.LastMessage != nil && chat.LastMessage.ID == originalMessage.ID {
|
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
|
||||||
if err := m.updateLastMessage(chat); err != nil {
|
chat.LastMessage = messageToDelete
|
||||||
return err
|
err = m.saveChat(chat)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if chat.LastMessage != nil && !chat.LastMessage.Seen && chat.OneToOne() && !chat.Active {
|
messages, err := m.persistence.LatestMessageByChatID(chat.ID)
|
||||||
m.createMessageNotification(chat, state)
|
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 chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
|
||||||
if err := m.updateLastMessage(chat); err != nil {
|
chat.LastMessage = messageToDelete
|
||||||
return err
|
err = m.saveChat(chat)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1737,21 +1748,6 @@ func (m *Messenger) HandleDeleteForMeMessage(state *ReceivedMessageState, delete
|
||||||
return nil
|
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) {
|
func handleContactRequestChatMessage(receivedMessage *common.Message, contact *Contact, outgoing bool, logger *zap.Logger) (bool, error) {
|
||||||
receivedMessage.ContactRequestState = common.ContactRequestStatePending
|
receivedMessage.ContactRequestState = common.ContactRequestStatePending
|
||||||
|
|
||||||
|
@ -1981,18 +1977,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if (receivedMessage.Deleted || receivedMessage.DeletedForMe) && (chat.LastMessage == nil || chat.LastMessage.ID == receivedMessage.ID) {
|
if !receivedMessage.Deleted && !receivedMessage.DeletedForMe {
|
||||||
// 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 {
|
|
||||||
err = chat.UpdateFromMessage(receivedMessage, m.getTimesource())
|
err = chat.UpdateFromMessage(receivedMessage, m.getTimesource())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -217,7 +217,10 @@ func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string)
|
||||||
response.AddRemovedMessage(&RemovedMessage{MessageID: messageToDelete.ID, ChatID: chat.ID, DeletedBy: deletedBy})
|
response.AddRemovedMessage(&RemovedMessage{MessageID: messageToDelete.ID, ChatID: chat.ID, DeletedBy: deletedBy})
|
||||||
|
|
||||||
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
|
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
|
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 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
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue