2021-07-26 21:06:32 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2023-06-08 12:49:08 +00:00
|
|
|
"github.com/status-im/status-go/server"
|
2021-07-26 21:06:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMessengerDeleteMessageSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerDeleteMessageSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerDeleteMessageSuite struct {
|
2023-06-08 12:49:08 +00:00
|
|
|
MessengerBaseTestSuite
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
2021-08-13 17:10:32 +00:00
|
|
|
messageID := sendResponse.Messages()[0].ID
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
|
|
|
sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2021-08-13 17:10:32 +00:00
|
|
|
s.Require().Len(sendResponse.Messages(), 0)
|
|
|
|
s.Require().Len(sendResponse.RemovedMessages(), 1)
|
2021-08-20 14:26:13 +00:00
|
|
|
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
2023-02-01 00:57:35 +00:00
|
|
|
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
2021-07-26 21:06:32 +00:00
|
|
|
s.Require().Len(sendResponse.Chats(), 1)
|
2023-05-08 17:02:54 +00:00
|
|
|
// LastMessage is marked as deleted
|
|
|
|
lastRemovedMessage := sendResponse.Chats()[0].LastMessage
|
|
|
|
s.Require().Equal(lastRemovedMessage.ID, messageID)
|
|
|
|
s.Require().Equal(lastRemovedMessage.Deleted, true)
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
// Main instance user attempts to delete the message it received from theirMessenger
|
|
|
|
_, err = s.m.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
s.Require().ErrorContains(err, "can't find chat")
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 12:40:51 +00:00
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessagePreviousLastMessage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2021-08-17 12:40:51 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage1 := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage1)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
inputMessage2 := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err = theirMessenger.SendChatMessage(context.Background(), inputMessage2)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
messageID := sendResponse.Messages()[0].ID
|
|
|
|
|
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) > 0 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 2)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
|
|
|
sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 0)
|
|
|
|
s.Require().Len(sendResponse.RemovedMessages(), 1)
|
2021-08-20 14:26:13 +00:00
|
|
|
s.Require().Equal(messageID, sendResponse.RemovedMessages()[0].MessageID)
|
2021-08-17 12:40:51 +00:00
|
|
|
s.Require().Len(sendResponse.Chats(), 1)
|
2023-05-08 17:02:54 +00:00
|
|
|
// LastMessage is updated and marked as deleted
|
2021-08-17 12:40:51 +00:00
|
|
|
s.Require().NotNil(sendResponse.Chats()[0].LastMessage)
|
2023-05-08 17:02:54 +00:00
|
|
|
s.Require().Equal(inputMessage2.ID, sendResponse.Chats()[0].LastMessage.ID)
|
|
|
|
s.Require().Equal(sendResponse.Chats()[0].LastMessage.Deleted, true)
|
2021-08-17 12:40:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteWrongMessageType() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestGapMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
|
|
|
// Delete should not work
|
|
|
|
_, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
|
|
|
|
|
|
|
s.Require().Equal(ErrInvalidDeleteTypeAuthor, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO fix activity center notifications not being deleted when a message is deleted
|
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessageFirstThenMessage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contact, err := BuildContactFromPublicKey(&theirMessenger.identity.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
messageID := "message-id"
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
inputMessage.Clock = 1
|
2023-08-18 11:39:59 +00:00
|
|
|
deleteMessage := &DeleteMessage{
|
|
|
|
DeleteMessage: &protobuf.DeleteMessage{
|
2021-07-26 21:06:32 +00:00
|
|
|
Clock: 2,
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
MessageId: messageID,
|
|
|
|
ChatId: theirChat.ID,
|
|
|
|
},
|
|
|
|
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:16:45 +00:00
|
|
|
state := &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
}
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
// Handle Delete first
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.handleDeleteMessage(state, deleteMessage)
|
2021-07-26 21:06:32 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// // Handle chat message
|
2021-08-19 13:16:45 +00:00
|
|
|
state = &ReceivedMessageState{
|
2021-08-13 17:10:32 +00:00
|
|
|
Response: &MessengerResponse{},
|
2021-07-26 21:06:32 +00:00
|
|
|
CurrentMessageState: &CurrentMessageState{
|
|
|
|
MessageID: messageID,
|
|
|
|
WhisperTimestamp: s.m.getTimesource().GetCurrentTime(),
|
|
|
|
Contact: contact,
|
|
|
|
PublicKey: &theirMessenger.identity.PublicKey,
|
|
|
|
},
|
|
|
|
}
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.HandleChatMessage(state, inputMessage.ChatMessage, nil)
|
2021-07-26 21:06:32 +00:00
|
|
|
s.Require().NoError(err)
|
2021-08-13 17:10:32 +00:00
|
|
|
s.Require().Len(state.Response.Messages(), 0) // Message should not be added to response
|
|
|
|
s.Require().Len(state.Response.RemovedMessages(), 0)
|
|
|
|
s.Require().Nil(state.Response.Chats()[0].LastMessage)
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
2023-04-14 17:17:56 +00:00
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteImageMessage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2023-04-14 17:17:56 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
messageCount := 3
|
|
|
|
var album []*common.Message
|
|
|
|
for i := 0; i < messageCount; i++ {
|
|
|
|
image, err := buildImageWithoutAlbumIDMessage(*ourChat)
|
|
|
|
s.NoError(err)
|
|
|
|
album = append(album, image)
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s.m.SendChatMessages(context.Background(), album)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// Check that album count was the number of the images sent
|
|
|
|
imagesCount := uint32(0)
|
|
|
|
for _, message := range response.Messages() {
|
|
|
|
if message.ContentType == protobuf.ChatMessage_IMAGE {
|
|
|
|
imagesCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, message := range response.Messages() {
|
|
|
|
s.Require().NotNil(message.GetImage())
|
|
|
|
s.Require().Equal(message.GetImage().AlbumImagesCount, imagesCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().Equal(messageCount, len(response.Messages()), "it returns the messages")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Messages(), messageCount)
|
|
|
|
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
theirMessenger,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) == messageCount },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), messageCount)
|
|
|
|
for _, message := range response.Messages() {
|
|
|
|
image := message.GetImage()
|
|
|
|
s.Require().NotNil(image, "Message.ID=%s", message.ID)
|
|
|
|
s.Require().Equal(image.AlbumImagesCount, imagesCount)
|
|
|
|
s.Require().NotEmpty(image.AlbumId, "Message.ID=%s", message.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstMessageID := response.Messages()[0].ID
|
|
|
|
sendResponse, err := s.m.DeleteMessageAndSend(context.Background(), firstMessageID)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 0)
|
|
|
|
s.Require().Len(sendResponse.RemovedMessages(), 3)
|
|
|
|
s.Require().Equal(sendResponse.RemovedMessages()[0].DeletedBy, "")
|
|
|
|
s.Require().Len(sendResponse.Chats(), 1)
|
2023-05-08 17:02:54 +00:00
|
|
|
|
|
|
|
// LastMessage marked as deleted
|
|
|
|
s.Require().Equal(sendResponse.Chats()[0].LastMessage.ID, album[2].ID)
|
|
|
|
s.Require().Equal(sendResponse.Chats()[0].LastMessage.Deleted, true)
|
2023-04-14 17:17:56 +00:00
|
|
|
|
|
|
|
// Main instance user attempts to delete the message it received from theirMessenger
|
|
|
|
_, err = theirMessenger.DeleteMessageAndSend(context.Background(), firstMessageID)
|
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
s.Require().ErrorContains(err, "can't find chat")
|
2023-04-14 17:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteImageMessageFirstThenMessage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2023-04-14 17:17:56 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contact, err := BuildContactFromPublicKey(&theirMessenger.identity.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
messageID1 := "message-id1"
|
|
|
|
messageID2 := "message-id2"
|
2023-05-16 15:00:49 +00:00
|
|
|
albumID := "album-id1"
|
2023-04-14 17:17:56 +00:00
|
|
|
|
|
|
|
messageCount := 2
|
|
|
|
var album []*common.Message
|
|
|
|
for i := 0; i < messageCount; i++ {
|
2023-05-16 15:00:49 +00:00
|
|
|
image, err := buildImageWithAlbumIDMessage(*ourChat, albumID)
|
2023-04-14 17:17:56 +00:00
|
|
|
image.Clock = 1
|
|
|
|
s.NoError(err)
|
|
|
|
album = append(album, image)
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
deleteMessage := &DeleteMessage{
|
|
|
|
DeleteMessage: &protobuf.DeleteMessage{
|
2023-04-14 17:17:56 +00:00
|
|
|
Clock: 2,
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
MessageId: messageID1,
|
|
|
|
ChatId: theirChat.ID,
|
|
|
|
},
|
|
|
|
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Delete first
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.handleDeleteMessage(state, deleteMessage)
|
2023-04-14 17:17:56 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Handle first image message
|
|
|
|
state = &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
CurrentMessageState: &CurrentMessageState{
|
|
|
|
MessageID: messageID1,
|
|
|
|
WhisperTimestamp: s.m.getTimesource().GetCurrentTime(),
|
|
|
|
Contact: contact,
|
|
|
|
PublicKey: &theirMessenger.identity.PublicKey,
|
|
|
|
},
|
|
|
|
}
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.HandleChatMessage(state, album[0].ChatMessage, nil)
|
2023-04-14 17:17:56 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(state.Response.Messages(), 0) // Message should not be added to response
|
|
|
|
s.Require().Len(state.Response.RemovedMessages(), 0)
|
|
|
|
s.Require().Nil(state.Response.Chats()[0].LastMessage)
|
|
|
|
|
|
|
|
// Handle second image message
|
|
|
|
state = &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
CurrentMessageState: &CurrentMessageState{
|
|
|
|
MessageID: messageID2,
|
|
|
|
WhisperTimestamp: s.m.getTimesource().GetCurrentTime(),
|
|
|
|
Contact: contact,
|
|
|
|
PublicKey: &theirMessenger.identity.PublicKey,
|
|
|
|
},
|
|
|
|
}
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.HandleChatMessage(state, album[1].ChatMessage, nil)
|
2023-04-14 17:17:56 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(state.Response.Messages(), 0) // Message should not be added to response even if we didn't delete that ID
|
|
|
|
s.Require().Len(state.Response.RemovedMessages(), 0)
|
|
|
|
s.Require().Nil(state.Response.Chats()[0].LastMessage)
|
|
|
|
}
|
2023-04-27 14:22:26 +00:00
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessageWithAMention() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2023-04-27 14:22:26 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
inputMessage.Text = "text with a mention @" + common.PubkeyToHex(&s.privateKey.PublicKey)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
messageID := sendResponse.Messages()[0].ID
|
|
|
|
|
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) == 1 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
// Receiver (us) is mentioned
|
|
|
|
s.Require().Equal(int(response.Chats()[0].UnviewedMessagesCount), 1)
|
|
|
|
s.Require().Equal(int(response.Chats()[0].UnviewedMentionsCount), 1)
|
|
|
|
s.Require().True(response.Messages()[0].Mentioned)
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
deleteMessage := &DeleteMessage{
|
|
|
|
DeleteMessage: &protobuf.DeleteMessage{
|
2023-04-27 14:22:26 +00:00
|
|
|
Clock: 2,
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
MessageId: messageID,
|
|
|
|
ChatId: theirChat.ID,
|
|
|
|
},
|
|
|
|
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Delete first
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.handleDeleteMessage(state, deleteMessage)
|
2023-04-27 14:22:26 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
// Receiver (us) is no longer mentioned
|
2023-05-16 16:11:52 +00:00
|
|
|
s.Require().Equal(int(state.Response.Chats()[0].UnviewedMessagesCount), 0)
|
|
|
|
s.Require().Equal(int(state.Response.Chats()[0].UnviewedMentionsCount), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test makes sure the UnviewMessageCount doesn't go below 0 in a very rare case where the Chat could be marked
|
|
|
|
// as read but the message still unseen (Seen == false)
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessageAndChatIsAlreadyRead() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2023-05-16 16:11:52 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) == 1 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Equal(response.Chats()[0].UnviewedMessagesCount, uint(1))
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
|
|
|
|
// Force UnviewedMessagesCount to 0 to test if the uint validation is done correctly
|
|
|
|
ourChat.UnviewedMessagesCount = 0
|
|
|
|
err = s.m.saveChat(ourChat)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
deleteMessage := &DeleteMessage{
|
|
|
|
DeleteMessage: &protobuf.DeleteMessage{
|
2023-05-16 16:11:52 +00:00
|
|
|
Clock: 2,
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
MessageId: ogMessage.ID,
|
|
|
|
ChatId: theirChat.ID,
|
|
|
|
},
|
|
|
|
From: common.PubkeyToHex(&theirMessenger.identity.PublicKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &ReceivedMessageState{
|
|
|
|
Response: &MessengerResponse{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle Delete first
|
2023-08-18 11:39:59 +00:00
|
|
|
err = s.m.handleDeleteMessage(state, deleteMessage)
|
2023-05-16 16:11:52 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
// Receiver (us) no longer has unread messages and it's not negative
|
|
|
|
s.Require().Equal(0, int(state.Response.Chats()[0].UnviewedMessagesCount))
|
2023-04-27 14:22:26 +00:00
|
|
|
}
|
2023-06-08 12:49:08 +00:00
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessageReplyToImage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2023-06-22 12:16:21 +00:00
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
2023-06-08 12:49:08 +00:00
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) == 1 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
|
|
|
// create an http server
|
|
|
|
mediaServer, err := server.NewMediaServer(nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(mediaServer)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
|
|
|
|
|
|
|
theirMessenger.httpServer = mediaServer
|
|
|
|
|
|
|
|
// We reply to our own message with an image
|
|
|
|
imageMessage, err := buildImageWithoutAlbumIDMessage(*theirChat)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
imageMessage.ResponseTo = ogMessage.ID
|
|
|
|
|
|
|
|
_, err = theirMessenger.SendChatMessages(context.Background(), []*common.Message{imageMessage})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// We check that the URL is correctly returned
|
|
|
|
sendResponse, err = theirMessenger.DeleteMessageAndSend(context.Background(), ogMessage.ID)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
s.Require().NotEmpty(sendResponse.Messages()[0].ImageLocalURL)
|
|
|
|
}
|
2023-06-28 14:57:50 +00:00
|
|
|
|
|
|
|
func (s *MessengerDeleteMessageSuite) TestDeleteMessageForMeReplyToImage() {
|
|
|
|
theirMessenger := s.newMessenger()
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
defer theirMessenger.Shutdown() // nolint: errcheck
|
|
|
|
|
|
|
|
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, s.m.transport)
|
|
|
|
err = theirMessenger.SaveChat(theirChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
ourChat := CreateOneToOneChat("Our 1TO1", &theirMessenger.identity.PublicKey, s.m.transport)
|
|
|
|
err = s.m.SaveChat(ourChat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(*theirChat)
|
|
|
|
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(sendResponse.Messages(), 1)
|
|
|
|
|
|
|
|
response, err := WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.messages) == 1 },
|
|
|
|
"no messages",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().Len(response.Messages(), 1)
|
|
|
|
|
|
|
|
ogMessage := sendResponse.Messages()[0]
|
|
|
|
|
|
|
|
// create an http server
|
|
|
|
mediaServer, err := server.NewMediaServer(nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(mediaServer)
|
|
|
|
s.Require().NoError(mediaServer.Start())
|
|
|
|
|
|
|
|
theirMessenger.httpServer = mediaServer
|
|
|
|
|
|
|
|
// We reply to our own message with an image
|
|
|
|
imageMessage, err := buildImageWithoutAlbumIDMessage(*theirChat)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
imageMessage.ResponseTo = ogMessage.ID
|
|
|
|
|
|
|
|
_, err = theirMessenger.SendChatMessages(context.Background(), []*common.Message{imageMessage})
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// We check that the URL is correctly returned
|
|
|
|
sendResponse, err = theirMessenger.DeleteMessageForMeAndSync(context.Background(), theirChat.ID, ogMessage.ID)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
messages := sendResponse.Messages()
|
|
|
|
s.Require().Len(messages, 2)
|
|
|
|
|
|
|
|
var deletedMessage, replyMessage *common.Message
|
|
|
|
if messages[0].ID == ogMessage.ID {
|
|
|
|
deletedMessage = messages[0]
|
|
|
|
replyMessage = messages[1]
|
|
|
|
} else {
|
|
|
|
deletedMessage = messages[1]
|
|
|
|
replyMessage = messages[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().True(deletedMessage.DeletedForMe)
|
|
|
|
s.Require().NotEmpty(replyMessage.ImageLocalURL)
|
|
|
|
}
|