Fix duplicated notifications on receiving contact update

This commit fixes an issue where when accepting a contact request
the other end would display an extra notification.

It also changes WaitOnResponse to collect results. This should make
tests less flaky, since sometimes messages are processed in different
batches.
Now we need to be though exact on what we expect from the response (i.e
use == instead of >, otherwise the same behavior applies)
This uncovered a couple of issues with messenger.Merge, so I have moved
the struct to use a map based collection instead of an array.
This commit is contained in:
Andrea Maria Piana 2023-02-13 20:09:20 +00:00
parent f6b4721c4a
commit b69042e7d7
14 changed files with 308 additions and 188 deletions

View File

@ -1 +1 @@
0.131.1 0.131.2

View File

@ -4261,7 +4261,7 @@ func (m *Messenger) saveDataAndPrepareResponse(messageState *ReceivedMessageStat
} }
for _, emojiReaction := range messageState.EmojiReactions { for _, emojiReaction := range messageState.EmojiReactions {
messageState.Response.EmojiReactions = append(messageState.Response.EmojiReactions, emojiReaction) messageState.Response.AddEmojiReaction(emojiReaction)
} }
for _, groupChatInvitation := range messageState.GroupChatInvitations { for _, groupChatInvitation := range messageState.GroupChatInvitations {
@ -5684,7 +5684,7 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str
return nil, err return nil, err
} }
response.EmojiReactions = []*EmojiReaction{emojiR} response.AddEmojiReaction(emojiR)
response.AddChat(chat) response.AddChat(chat)
err = m.persistence.SaveEmojiReaction(emojiR) err = m.persistence.SaveEmojiReaction(emojiR)
@ -5772,7 +5772,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti
// Update MessengerResponse // Update MessengerResponse
response := MessengerResponse{} response := MessengerResponse{}
emojiR.Retracted = true emojiR.Retracted = true
response.EmojiReactions = []*EmojiReaction{emojiR} response.AddEmojiReaction(emojiR)
response.AddChat(chat) response.AddChat(chat)
// Persist retraction state for emoji reaction // Persist retraction state for emoji reaction

View File

@ -61,8 +61,7 @@ func (s *MessengerContactRequestSuite) newMessenger(shh types.Waku) *Messenger {
return messenger return messenger
} }
// NOTE(cammellos): Disabling for hotfix func (s *MessengerContactRequestSuite) TestReceiveAndAcceptContactRequest() { //nolint: unused
func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { //nolint: unused
messageText := "hello!" messageText := "hello!"
@ -156,7 +155,7 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { //
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.Contacts) == 1 && len(r.Messages()) == 2 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
@ -172,9 +171,18 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { //
// Make sure the message is updated, sender s2de // Make sure the message is updated, sender s2de
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID) var message *common.Message
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState)
for _, m := range resp.Messages() {
if m.ID == contactRequests[0].ID {
message = m
}
}
s.Require().NotNil(message)
s.Require().Equal(message.ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState)
// Make sure we consider them a mutual contact, sender side // Make sure we consider them a mutual contact, sender side
mutualContacts = s.m.MutualContacts() mutualContacts = s.m.MutualContacts()
@ -278,8 +286,7 @@ func (s *MessengerContactRequestSuite) TestReceiveAndDismissContactRequest() {
s.Require().Len(contacts, 0) s.Require().Len(contacts, 0)
} }
// NOTE(cammellos): Disabling for hotfix func (s *MessengerContactRequestSuite) TestReceiveAcceptAndRetractContactRequest() { //nolint: unused
func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest() { //nolint: unused
messageText := "hello!" messageText := "hello!"
@ -376,7 +383,7 @@ func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0 && len(r.Contacts) > 0 return len(r.Messages()) == 2 && len(r.ActivityCenterNotifications()) == 1 && len(r.Contacts) == 1
}, },
"no messages", "no messages",
) )
@ -393,9 +400,18 @@ func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest
// Make sure the message is updated, sender side // Make sure the message is updated, sender side
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID) var message *common.Message
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState)
for _, m := range resp.Messages() {
if m.ID == contactRequests[0].ID {
message = m
}
}
s.Require().NotNil(message)
s.Require().Equal(message.ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState)
// Make sure we consider them a mutual contact, sender side // Make sure we consider them a mutual contact, sender side
mutualContacts = s.m.MutualContacts() mutualContacts = s.m.MutualContacts()
@ -483,8 +499,7 @@ func (s *MessengerContactRequestSuite) TestReceiveAcceptAndRetractContactRequest
s.Require().Equal(ContactRequestStateReceived, contacts[0].ContactRequestRemoteState) s.Require().Equal(ContactRequestStateReceived, contacts[0].ContactRequestRemoteState)
} }
// NOTE(cammellos): disabling for hotfix func (s *MessengerContactRequestSuite) TestReceiveAndAcceptContactRequestTwice() { //nolint: unused
func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequestTwice() { //nolint: unused
messageText := "hello!" messageText := "hello!"
@ -575,7 +590,7 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequestTwice()
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.Contacts) == 1 && len(r.Messages()) == 2 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
@ -588,9 +603,19 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequestTwice()
// Make sure the message is updated, sender s2de // Make sure the message is updated, sender s2de
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 2)
s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState) var message *common.Message
for _, m := range resp.Messages() {
if m.ID == contactRequests[0].ID {
message = m
}
}
s.Require().NotNil(message)
s.Require().Equal(message.ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState)
// Make sure we consider them a mutual contact, sender side // Make sure we consider them a mutual contact, sender side
mutualContacts = s.m.MutualContacts() mutualContacts = s.m.MutualContacts()
@ -660,11 +685,7 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact(
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
contactRequests, _, err := theirMessenger.PendingContactRequests("", 10) return len(r.Contacts) == 1 && len(r.ActivityCenterNotifications()) == 1
if err != nil {
return false
}
return len(contactRequests) == 1
}, },
"no messages", "no messages",
) )
@ -719,7 +740,7 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact(
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.Messages()) == 2 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
@ -727,26 +748,33 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact(
// Make sure the message is updated, sender side // Make sure the message is updated, sender side
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.ActivityCenterNotifications(), 1)
// We receive two messages, one for the default contact request, that
// is dispatched for backward compatibility, and one for the updated
// contact request that we sent
s.Require().Len(resp.Messages(), 2) s.Require().Len(resp.Messages(), 2)
// TODO(cammellos): This code duplicates contact requests var message *common.Message
// this is a known issue, we want to merge this quickly
// for RC(1.21), but will be addresse immediately after
/*
s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState)
// Check activity center notification is of the right type for _, m := range resp.Messages() {
s.Require().Equal(ActivityCenterNotificationTypeContactRequest, resp.ActivityCenterNotifications()[0].Type) if m.ID == contactRequests[0].ID {
s.Require().NotNil(resp.ActivityCenterNotifications()[0].Message) message = m
s.Require().Equal(common.ContactRequestStateAccepted, resp.ActivityCenterNotifications()[0].Message.ContactRequestState) }
}
s.Require().NotNil(message)
s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState)
// Make sure we consider them a mutual contact, sender side // Check activity center notification is of the right type
mutualContacts = s.m.MutualContacts() s.Require().Equal(ActivityCenterNotificationTypeContactRequest, resp.ActivityCenterNotifications()[0].Type)
s.Require().Len(mutualContacts, 1) s.Require().NotNil(resp.ActivityCenterNotifications()[0].Message)
s.Require().Equal(common.ContactRequestStateAccepted, resp.ActivityCenterNotifications()[0].Message.ContactRequestState)
// Check the contact state is correctly set // Make sure we consider them a mutual contact, sender side
s.Require().Len(resp.Contacts, 1) mutualContacts = s.m.MutualContacts()
s.Require().True(resp.Contacts[0].mutual()) */ s.Require().Len(mutualContacts, 1)
// Check the contact state is correctly set
s.Require().Len(resp.Contacts, 1)
s.Require().True(resp.Contacts[0].mutual())
} }
func (s *MessengerContactRequestSuite) TestDismissLatestContactRequestForContact() { func (s *MessengerContactRequestSuite) TestDismissLatestContactRequestForContact() {
@ -1049,8 +1077,7 @@ func (s *MessengerContactRequestSuite) TestReceiveMultipleLegacy() {
} }
// NOTE(cammellos): Disabling for hotfix func (s *MessengerContactRequestSuite) TestAcceptLatestLegacyContactRequestForContact() { // nolint: unused
func (s *MessengerContactRequestSuite) testAcceptLatestLegacyContactRequestForContact() { // nolint: unused
theirMessenger := s.newMessenger(s.shh) theirMessenger := s.newMessenger(s.shh)
_, err := theirMessenger.Start() _, err := theirMessenger.Start()

View File

@ -115,9 +115,9 @@ func (m *Messenger) SendContactVerificationRequest(ctx context.Context, contactI
return nil, err return nil, err
} }
response := &MessengerResponse{ response := &MessengerResponse{}
VerificationRequests: []*verification.Request{verifRequest},
} response.AddVerificationRequest(verifRequest)
err = m.createOrUpdateOutgoingContactVerificationNotification(contact, response, verifRequest, chatMessage, nil) err = m.createOrUpdateOutgoingContactVerificationNotification(contact, response, verifRequest, chatMessage, nil)
if err != nil { if err != nil {
@ -332,9 +332,9 @@ func (m *Messenger) AcceptContactVerificationRequest(ctx context.Context, id str
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp := &MessengerResponse{ resp := &MessengerResponse{}
VerificationRequests: []*verification.Request{verifRequest},
} resp.AddVerificationRequest(verifRequest)
replyMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted) replyMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted)
if err != nil { if err != nil {

View File

@ -19,8 +19,7 @@ import (
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
) )
// NOTE(cammellos): Disabling for hotfix func TestMessengerVerificationRequests(t *testing.T) { // nolint: deadcode,unused
func testMessengerVerificationRequests(t *testing.T) { // nolint: deadcode,unused
suite.Run(t, new(MessengerVerificationRequests)) suite.Run(t, new(MessengerVerificationRequests))
} }
@ -136,7 +135,7 @@ func (s *MessengerVerificationRequests) mutualContact(theirMessenger *Messenger)
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && len(r.Messages()) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.Contacts) == 1 && len(r.Messages()) == 2 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
@ -149,9 +148,18 @@ func (s *MessengerVerificationRequests) mutualContact(theirMessenger *Messenger)
// Make sure the message is updated, sender s2de // Make sure the message is updated, sender s2de
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 2)
s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID) var message *common.Message
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState)
for _, m := range resp.Messages() {
if m.ID == contactRequests[0].ID {
message = m
}
}
s.Require().NotNil(message)
s.Require().Equal(message.ID, contactRequests[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState)
// Make sure we consider them a mutual contact, sender side // Make sure we consider them a mutual contact, sender side
mutualContacts = s.m.MutualContacts() mutualContacts = s.m.MutualContacts()
@ -175,8 +183,8 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
verificationRequestID := resp.VerificationRequests[0].ID verificationRequestID := resp.VerificationRequests()[0].ID
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus)
@ -187,13 +195,13 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false)
@ -213,10 +221,10 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED)
s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt)
s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Len(resp.ActivityCenterNotifications(), 1)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID)
@ -237,20 +245,20 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 return len(r.VerificationRequests()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
messages := resp.Messages() messages := resp.Messages()
s.Require().Len(messages, 2) s.Require().Len(messages, 2)
var originalMessage *common.Message var originalMessage *common.Message
var replyMessage *common.Message var replyMessage *common.Message
if messages[0].ID == resp.VerificationRequests[0].ID { if messages[0].ID == resp.VerificationRequests()[0].ID {
originalMessage = messages[0] originalMessage = messages[0]
replyMessage = messages[1] replyMessage = messages[1]
} else { } else {
@ -300,8 +308,8 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() {
resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
verificationRequestID := resp.VerificationRequests[0].ID verificationRequestID := resp.VerificationRequests()[0].ID
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus)
@ -312,13 +320,13 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false)
@ -338,10 +346,10 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() {
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED)
s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt)
s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Len(resp.ActivityCenterNotifications(), 1)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID)
@ -362,13 +370,13 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 return len(r.VerificationRequests()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
messages := resp.Messages() messages := resp.Messages()
s.Require().Len(messages, 2) s.Require().Len(messages, 2)
@ -413,8 +421,8 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests()
resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
verificationRequestID := resp.VerificationRequests[0].ID verificationRequestID := resp.VerificationRequests()[0].ID
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus)
@ -425,13 +433,13 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests()
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false)
@ -451,10 +459,10 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests()
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED)
s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt)
s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Len(resp.ActivityCenterNotifications(), 1)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID)
@ -475,20 +483,20 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests()
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 return len(r.VerificationRequests()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
messages := resp.Messages() messages := resp.Messages()
s.Require().Len(messages, 2) s.Require().Len(messages, 2)
var originalMessage *common.Message var originalMessage *common.Message
var replyMessage *common.Message var replyMessage *common.Message
if messages[0].ID == resp.VerificationRequests[0].ID { if messages[0].ID == resp.VerificationRequests()[0].ID {
originalMessage = messages[0] originalMessage = messages[0]
replyMessage = messages[1] replyMessage = messages[1]
} else { } else {
@ -540,8 +548,8 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() {
resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
verificationRequestID := resp.VerificationRequests[0].ID verificationRequestID := resp.VerificationRequests()[0].ID
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().Equal(challenge, resp.Messages()[0].Text) s.Require().Equal(challenge, resp.Messages()[0].Text)
@ -551,13 +559,13 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false)
@ -588,10 +596,10 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() {
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusDECLINED) s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusDECLINED)
s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt)
s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Len(resp.ActivityCenterNotifications(), 1)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID)
@ -619,13 +627,13 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
s.m, s.m,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 return len(r.VerificationRequests()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().Equal(resp.Messages()[0].ContactVerificationState, common.ContactVerificationStateDeclined) s.Require().Equal(resp.Messages()[0].ContactVerificationState, common.ContactVerificationStateDeclined)
@ -651,8 +659,8 @@ func (s *MessengerVerificationRequests) TestCancelVerificationRequest() {
resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
verificationRequestID := resp.VerificationRequests[0].ID verificationRequestID := resp.VerificationRequests()[0].ID
s.Require().Len(resp.Messages(), 1) s.Require().Len(resp.Messages(), 1)
s.Require().Equal(challenge, resp.Messages()[0].Text) s.Require().Equal(challenge, resp.Messages()[0].Text)
@ -662,13 +670,13 @@ func (s *MessengerVerificationRequests) TestCancelVerificationRequest() {
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false)
@ -699,21 +707,21 @@ func (s *MessengerVerificationRequests) TestCancelVerificationRequest() {
s.Require().NotNil(resp) s.Require().NotNil(resp)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusCANCELED) s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusCANCELED)
// Check canceled state on the receiver's side // Check canceled state on the receiver's side
resp, err = WaitOnMessengerResponse( resp, err = WaitOnMessengerResponse(
theirMessenger, theirMessenger,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1
}, },
"no messages", "no messages",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(resp.VerificationRequests, 1) s.Require().Len(resp.VerificationRequests(), 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusCANCELED) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusCANCELED)
s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, true) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, true)

View File

@ -108,40 +108,40 @@ func (s *MessengerEmojiSuite) TestSendEmoji() {
response, err = bob.SendEmojiReaction(context.Background(), chat.ID, messageID, protobuf.EmojiReaction_SAD) response, err = bob.SendEmojiReaction(context.Background(), chat.ID, messageID, protobuf.EmojiReaction_SAD)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.EmojiReactions, 1) s.Require().Len(response.EmojiReactions(), 1)
emojiID := response.EmojiReactions[0].ID() emojiID := response.EmojiReactions()[0].ID()
// Wait for the emoji to arrive to alice // Wait for the emoji to arrive to alice
response, err = WaitOnMessengerResponse( response, err = WaitOnMessengerResponse(
alice, alice,
func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 },
"no emoji", "no emoji",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.EmojiReactions, 1) s.Require().Len(response.EmojiReactions(), 1)
s.Require().Equal(response.EmojiReactions[0].ID(), emojiID) s.Require().Equal(response.EmojiReactions()[0].ID(), emojiID)
s.Require().Equal(response.EmojiReactions[0].Type, protobuf.EmojiReaction_SAD) s.Require().Equal(response.EmojiReactions()[0].Type, protobuf.EmojiReaction_SAD)
// Retract the emoji // Retract the emoji
response, err = bob.SendEmojiReactionRetraction(context.Background(), emojiID) response, err = bob.SendEmojiReactionRetraction(context.Background(), emojiID)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.EmojiReactions, 1) s.Require().Len(response.EmojiReactions(), 1)
s.Require().True(response.EmojiReactions[0].Retracted) s.Require().True(response.EmojiReactions()[0].Retracted)
// Wait for the emoji to arrive to alice // Wait for the emoji to arrive to alice
response, err = WaitOnMessengerResponse( response, err = WaitOnMessengerResponse(
alice, alice,
func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 },
"no emoji", "no emoji",
) )
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(response.EmojiReactions, 1) s.Require().Len(response.EmojiReactions(), 1)
s.Require().Equal(response.EmojiReactions[0].ID(), emojiID) s.Require().Equal(response.EmojiReactions()[0].ID(), emojiID)
s.Require().Equal(response.EmojiReactions[0].Type, protobuf.EmojiReaction_SAD) s.Require().Equal(response.EmojiReactions()[0].Type, protobuf.EmojiReaction_SAD)
s.Require().True(response.EmojiReactions[0].Retracted) s.Require().True(response.EmojiReactions()[0].Retracted)
s.Require().NoError(bob.Shutdown()) s.Require().NoError(bob.Shutdown())
} }
@ -198,7 +198,7 @@ func (s *MessengerEmojiSuite) TestEmojiPrivateGroup() {
// Wait for the message to reach its destination // Wait for the message to reach its destination
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
alice, alice,
func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 },
"no emoji reaction received", "no emoji reaction received",
) )
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -262,7 +262,7 @@ func (m *Messenger) PendingNotificationContactRequest(contactID string) (*Activi
return m.persistence.ActiveContactRequestNotification(contactID) return m.persistence.ActiveContactRequestNotification(contactID)
} }
func (m *Messenger) createContactRequestNotification(contact *Contact, messageState *ReceivedMessageState, contactRequest *common.Message) error { func (m *Messenger) createContactRequestNotification(contact *Contact, messageState *ReceivedMessageState, contactRequest *common.Message, createNewNotification bool) error {
if contactRequest == nil || contactRequest.ContactRequestState == common.ContactRequestStatePending { if contactRequest == nil || contactRequest.ContactRequestState == common.ContactRequestStatePending {
notification, err := m.PendingNotificationContactRequest(contact.ID) notification, err := m.PendingNotificationContactRequest(contact.ID)
@ -330,7 +330,12 @@ func (m *Messenger) createContactRequestNotification(contact *Contact, messageSt
contactRequest.ContentType = protobuf.ChatMessage_CONTACT_REQUEST contactRequest.ContentType = protobuf.ChatMessage_CONTACT_REQUEST
contactRequest.Clock = messageState.CurrentMessageState.Message.Clock contactRequest.Clock = messageState.CurrentMessageState.Message.Clock
contactRequest.ID = defaultID contactRequest.ID = defaultID
contactRequest.ContactRequestState = common.ContactRequestStatePending
if contact.mutual() {
contactRequest.ContactRequestState = common.ContactRequestStateAccepted
} else {
contactRequest.ContactRequestState = common.ContactRequestStatePending
}
err = contactRequest.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey)) err = contactRequest.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
if err != nil { if err != nil {
return err return err
@ -344,6 +349,10 @@ func (m *Messenger) createContactRequestNotification(contact *Contact, messageSt
} }
} }
if !createNewNotification {
return nil
}
notification := &ActivityCenterNotification{ notification := &ActivityCenterNotification{
ID: types.FromHex(contactRequest.ID), ID: types.FromHex(contactRequest.ID),
Name: contact.CanonicalName(), Name: contact.CanonicalName(),
@ -801,11 +810,9 @@ func (m *Messenger) HandleAcceptContactRequest(state *ReceivedMessageState, mess
state.AllChats.Store(chat.ID, chat) state.AllChats.Store(chat.ID, chat)
} }
// We only want to update a notification here, we don't want (yet) to
// create a new one
if originalRequest != nil { if originalRequest != nil {
// Update contact requests if existing, or create a new one // Update contact requests if existing, or create a new one
err = m.createContactRequestNotification(contact, state, originalRequest) err = m.createContactRequestNotification(contact, state, originalRequest, processingResponse.newContactRequestReceived)
if err != nil { if err != nil {
m.logger.Warn("could not create contact request notification", zap.Error(err)) m.logger.Warn("could not create contact request notification", zap.Error(err))
} }
@ -892,8 +899,7 @@ func (m *Messenger) HandleContactUpdate(state *ReceivedMessageState, message pro
} }
if result.newContactRequestReceived { if result.newContactRequestReceived {
logger.Debug("creating contact request notification") err = m.createContactRequestNotification(contact, state, nil, true)
err = m.createContactRequestNotification(contact, state, nil)
if err != nil { if err != nil {
return err return err
} }
@ -906,7 +912,6 @@ func (m *Messenger) HandleContactUpdate(state *ReceivedMessageState, message pro
} }
if contact.LastUpdated < message.Clock { if contact.LastUpdated < message.Clock {
logger.Debug("Updating contact")
if contact.EnsName != message.EnsName { if contact.EnsName != message.EnsName {
contact.EnsName = message.EnsName contact.EnsName = message.EnsName
contact.ENSVerified = false contact.ENSVerified = false
@ -918,7 +923,7 @@ func (m *Messenger) HandleContactUpdate(state *ReceivedMessageState, message pro
r := contact.ContactRequestReceived(message.ContactRequestClock) r := contact.ContactRequestReceived(message.ContactRequestClock)
if r.newContactRequestReceived { if r.newContactRequestReceived {
err = m.createContactRequestNotification(contact, state, nil) err = m.createContactRequestNotification(contact, state, nil, true)
if err != nil { if err != nil {
m.logger.Warn("could not create contact request notification", zap.Error(err)) m.logger.Warn("could not create contact request notification", zap.Error(err))
} }
@ -1722,7 +1727,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
} }
} }
if result.newContactRequestReceived { if result.newContactRequestReceived {
err = m.createContactRequestNotification(contact, state, receivedMessage) err = m.createContactRequestNotification(contact, state, receivedMessage, true)
if err != nil { if err != nil {
return err return err
} }
@ -1751,7 +1756,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error {
state.AllContacts.Store(chatContact.ID, chatContact) state.AllContacts.Store(chatContact.ID, chatContact)
if sendNotification { if sendNotification {
err = m.createContactRequestNotification(chatContact, state, receivedMessage) err = m.createContactRequestNotification(chatContact, state, receivedMessage, true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -34,7 +34,6 @@ type ClearedHistory struct {
type MessengerResponse struct { type MessengerResponse struct {
Contacts []*Contact Contacts []*Contact
Installations []*multidevice.Installation Installations []*multidevice.Installation
EmojiReactions []*EmojiReaction
Invitations []*GroupChatInvitation Invitations []*GroupChatInvitation
CommunityChanges []*communities.CommunityChanges CommunityChanges []*communities.CommunityChanges
RequestsToJoinCommunity []*communities.RequestToJoin RequestsToJoinCommunity []*communities.RequestToJoin
@ -44,11 +43,9 @@ type MessengerResponse struct {
Settings []*settings.SyncSettingField Settings []*settings.SyncSettingField
IdentityImages []images.IdentityImage IdentityImages []images.IdentityImage
Accounts []*accounts.Account Accounts []*accounts.Account
VerificationRequests []*verification.Request
DiscordCategories []*discord.Category DiscordCategories []*discord.Category
DiscordChannels []*discord.Channel DiscordChannels []*discord.Channel
DiscordOldestMessageTimestamp int DiscordOldestMessageTimestamp int
SavedAddresses []*wallet.SavedAddress
// notifications a list of notifications derived from messenger events // notifications a list of notifications derived from messenger events
// that are useful to notify the user about // that are useful to notify the user about
@ -67,7 +64,10 @@ type MessengerResponse struct {
currentStatus *UserStatus currentStatus *UserStatus
statusUpdates map[string]UserStatus statusUpdates map[string]UserStatus
clearedHistories map[string]*ClearedHistory clearedHistories map[string]*ClearedHistory
verificationRequests map[string]*verification.Request
trustStatus map[string]verification.TrustStatus trustStatus map[string]verification.TrustStatus
emojiReactions map[string]*EmojiReaction
savedAddresses map[string]*wallet.SavedAddress
} }
func (r *MessengerResponse) MarshalJSON() ([]byte, error) { func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
@ -108,7 +108,6 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
}{ }{
Contacts: r.Contacts, Contacts: r.Contacts,
Installations: r.Installations, Installations: r.Installations,
EmojiReactions: r.EmojiReactions,
Invitations: r.Invitations, Invitations: r.Invitations,
CommunityChanges: r.CommunityChanges, CommunityChanges: r.CommunityChanges,
RequestsToJoinCommunity: r.RequestsToJoinCommunity, RequestsToJoinCommunity: r.RequestsToJoinCommunity,
@ -118,10 +117,10 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
Settings: r.Settings, Settings: r.Settings,
IdentityImages: r.IdentityImages, IdentityImages: r.IdentityImages,
Accounts: r.Accounts, Accounts: r.Accounts,
VerificationRequests: r.VerificationRequests,
SavedAddresses: r.SavedAddresses,
Messages: r.Messages(), Messages: r.Messages(),
VerificationRequests: r.VerificationRequests(),
SavedAddresses: r.SavedAddresses(),
Notifications: r.Notifications(), Notifications: r.Notifications(),
Chats: r.Chats(), Chats: r.Chats(),
Communities: r.Communities(), Communities: r.Communities(),
@ -131,6 +130,7 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
ClearedHistories: r.ClearedHistories(), ClearedHistories: r.ClearedHistories(),
ActivityCenterNotifications: r.ActivityCenterNotifications(), ActivityCenterNotifications: r.ActivityCenterNotifications(),
PinMessages: r.PinMessages(), PinMessages: r.PinMessages(),
EmojiReactions: r.EmojiReactions(),
StatusUpdates: r.StatusUpdates(), StatusUpdates: r.StatusUpdates(),
DiscordCategories: r.DiscordCategories, DiscordCategories: r.DiscordCategories,
DiscordChannels: r.DiscordChannels, DiscordChannels: r.DiscordChannels,
@ -197,6 +197,14 @@ func (r *MessengerResponse) Notifications() []*localnotifications.Notification {
return notifications return notifications
} }
func (r *MessengerResponse) VerificationRequests() []*verification.Request {
var verifications []*verification.Request
for _, v := range r.verificationRequests {
verifications = append(verifications, v)
}
return verifications
}
func (r *MessengerResponse) PinMessages() []*common.PinMessage { func (r *MessengerResponse) PinMessages() []*common.PinMessage {
var pinMessages []*common.PinMessage var pinMessages []*common.PinMessage
for _, pm := range r.pinMessages { for _, pm := range r.pinMessages {
@ -236,7 +244,7 @@ func (r *MessengerResponse) IsEmpty() bool {
len(r.Settings)+ len(r.Settings)+
len(r.Installations)+ len(r.Installations)+
len(r.Invitations)+ len(r.Invitations)+
len(r.EmojiReactions)+ len(r.emojiReactions)+
len(r.communities)+ len(r.communities)+
len(r.CommunityChanges)+ len(r.CommunityChanges)+
len(r.removedChats)+ len(r.removedChats)+
@ -248,24 +256,20 @@ func (r *MessengerResponse) IsEmpty() bool {
len(r.statusUpdates)+ len(r.statusUpdates)+
len(r.activityCenterNotifications)+ len(r.activityCenterNotifications)+
len(r.trustStatus)+ len(r.trustStatus)+
len(r.VerificationRequests)+ len(r.verificationRequests)+
len(r.RequestsToJoinCommunity) == 0 && len(r.RequestsToJoinCommunity) == 0 &&
len(r.SavedAddresses) == 0 && len(r.savedAddresses) == 0 &&
r.currentStatus == nil r.currentStatus == nil
} }
// Merge takes another response and appends the new Chats & new Messages and replaces // Merge takes another response and appends the new Chats & new Messages and replaces
// the existing Messages & Chats if they have the same ID // the existing Messages & Chats if they have the same ID
func (r *MessengerResponse) Merge(response *MessengerResponse) error { func (r *MessengerResponse) Merge(response *MessengerResponse) error {
if len(response.Installations)+ if len(response.Invitations)+
len(response.EmojiReactions)+
len(response.Invitations)+
len(response.RequestsToJoinCommunity)+ len(response.RequestsToJoinCommunity)+
len(response.Mailservers)+ len(response.Mailservers)+
len(response.EmojiReactions)+
len(response.Bookmarks)+ len(response.Bookmarks)+
len(response.clearedHistories)+ len(response.clearedHistories)+
len(response.VerificationRequests)+
len(response.DiscordChannels)+ len(response.DiscordChannels)+
len(response.DiscordCategories) != 0 { len(response.DiscordCategories) != 0 {
return ErrNotImplemented return ErrNotImplemented
@ -276,11 +280,15 @@ func (r *MessengerResponse) Merge(response *MessengerResponse) error {
r.AddRemovedMessages(response.RemovedMessages()) r.AddRemovedMessages(response.RemovedMessages())
r.AddNotifications(response.Notifications()) r.AddNotifications(response.Notifications())
r.AddMessages(response.Messages()) r.AddMessages(response.Messages())
r.AddContacts(response.Contacts)
r.AddCommunities(response.Communities()) r.AddCommunities(response.Communities())
r.AddPinMessages(response.PinMessages()) r.AddPinMessages(response.PinMessages())
r.AddVerificationRequests(response.VerificationRequests) r.AddVerificationRequests(response.VerificationRequests())
r.AddTrustStatuses(response.trustStatus) r.AddTrustStatuses(response.trustStatus)
r.AddActivityCenterNotifications(response.ActivityCenterNotifications()) r.AddActivityCenterNotifications(response.ActivityCenterNotifications())
r.AddEmojiReactions(response.EmojiReactions())
r.AddInstallations(response.Installations)
r.AddSavedAddresses(response.SavedAddresses())
r.CommunityChanges = append(r.CommunityChanges, response.CommunityChanges...) r.CommunityChanges = append(r.CommunityChanges, response.CommunityChanges...)
return nil return nil
@ -323,7 +331,11 @@ func (r *MessengerResponse) AddBookmarks(bookmarks []*browsers.Bookmark) {
} }
func (r *MessengerResponse) AddVerificationRequest(vr *verification.Request) { func (r *MessengerResponse) AddVerificationRequest(vr *verification.Request) {
r.VerificationRequests = append(r.VerificationRequests, vr) if r.verificationRequests == nil {
r.verificationRequests = make(map[string]*verification.Request)
}
r.verificationRequests[vr.ID] = vr
} }
func (r *MessengerResponse) AddVerificationRequests(vrs []*verification.Request) { func (r *MessengerResponse) AddVerificationRequests(vrs []*verification.Request) {
@ -364,6 +376,50 @@ func (r *MessengerResponse) AddChats(chats []*Chat) {
} }
} }
func (r *MessengerResponse) AddEmojiReactions(ers []*EmojiReaction) {
for _, e := range ers {
r.AddEmojiReaction(e)
}
}
func (r *MessengerResponse) AddEmojiReaction(er *EmojiReaction) {
if r.emojiReactions == nil {
r.emojiReactions = make(map[string]*EmojiReaction)
}
r.emojiReactions[er.ID()] = er
}
func (r *MessengerResponse) EmojiReactions() []*EmojiReaction {
var ers []*EmojiReaction
for _, er := range r.emojiReactions {
ers = append(ers, er)
}
return ers
}
func (r *MessengerResponse) AddSavedAddresses(ers []*wallet.SavedAddress) {
for _, e := range ers {
r.AddSavedAddress(e)
}
}
func (r *MessengerResponse) AddSavedAddress(er *wallet.SavedAddress) {
if r.savedAddresses == nil {
r.savedAddresses = make(map[string]*wallet.SavedAddress)
}
r.savedAddresses[er.ID()] = er
}
func (r *MessengerResponse) SavedAddresses() []*wallet.SavedAddress {
var ers []*wallet.SavedAddress
for _, er := range r.savedAddresses {
ers = append(ers, er)
}
return ers
}
func (r *MessengerResponse) AddNotification(n *localnotifications.Notification) { func (r *MessengerResponse) AddNotification(n *localnotifications.Notification) {
if r.notifications == nil { if r.notifications == nil {
r.notifications = make(map[string]*localnotifications.Notification) r.notifications = make(map[string]*localnotifications.Notification)
@ -580,6 +636,24 @@ func (r *MessengerResponse) AddContacts(contacts []*Contact) {
} }
} }
func (r *MessengerResponse) AddInstallation(i *multidevice.Installation) {
for idx, i1 := range r.Installations {
if i1.ID == i.ID {
r.Installations[idx] = i
return
}
}
r.Installations = append(r.Installations, i)
}
func (r *MessengerResponse) AddInstallations(installations []*multidevice.Installation) {
for idx := range installations {
r.AddInstallation(installations[idx])
}
}
func (r *MessengerResponse) SetMessages(messages []*common.Message) { func (r *MessengerResponse) SetMessages(messages []*common.Message) {
r.messages = make(map[string]*common.Message) r.messages = make(map[string]*common.Message)
r.AddMessages(messages) r.AddMessages(messages)

View File

@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/encryption/multidevice"
) )
func TestMessengerResponseMergeChats(t *testing.T) { func TestMessengerResponseMergeChats(t *testing.T) {
@ -55,16 +54,6 @@ func TestMessengerResponseMergeNotImplemented(t *testing.T) {
response1 := &MessengerResponse{} response1 := &MessengerResponse{}
response2 := &MessengerResponse{ response2 := &MessengerResponse{
Installations: []*multidevice.Installation{{}},
}
require.Error(t, response1.Merge(response2))
response2 = &MessengerResponse{
EmojiReactions: []*EmojiReaction{{}},
}
require.Error(t, response1.Merge(response2))
response2 = &MessengerResponse{
Invitations: []*GroupChatInvitation{{}}, Invitations: []*GroupChatInvitation{{}},
} }
require.Error(t, response1.Merge(response2)) require.Error(t, response1.Merge(response2))

View File

@ -100,7 +100,7 @@ func (m *Messenger) handleSyncSavedAddress(state *ReceivedMessageState, syncMess
if err != nil { if err != nil {
return err return err
} }
state.Response.SavedAddresses = append(state.Response.SavedAddresses, &wallet.SavedAddress{ChainID: syncMessage.ChainId, Address: address}) state.Response.AddSavedAddress(&wallet.SavedAddress{ChainID: syncMessage.ChainId, Address: address})
} else { } else {
sa := wallet.SavedAddress{ sa := wallet.SavedAddress{
Address: address, Address: address,
@ -113,7 +113,7 @@ func (m *Messenger) handleSyncSavedAddress(state *ReceivedMessageState, syncMess
if err != nil { if err != nil {
return err return err
} }
state.Response.SavedAddresses = append(state.Response.SavedAddresses, &sa) state.Response.AddSavedAddress(&sa)
} }
return return
} }

View File

@ -3,6 +3,7 @@ package protocol
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
@ -149,8 +150,10 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncExistingSavedAddresses() {
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
s.other, s.other,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
if len(r.SavedAddresses) == 2 { fmt.Println("LENG", len(r.SavedAddresses()))
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*r.SavedAddresses[0], *r.SavedAddresses[1]}, savedAddressDataIsEqual)) if len(r.SavedAddresses()) == 2 {
sas := r.SavedAddresses()
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*sas[0], *sas[1]}, savedAddressDataIsEqual))
return true return true
} }
return false return false
@ -193,8 +196,9 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncSavedAddresses() {
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
s.other, s.other,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
if len(r.SavedAddresses) == 2 { if len(r.SavedAddresses()) == 2 {
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*r.SavedAddresses[0], *r.SavedAddresses[1]}, savedAddressDataIsEqual)) sas := r.SavedAddresses()
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*sas[0], *sas[1]}, savedAddressDataIsEqual))
return true return true
} }
return false return false
@ -237,8 +241,9 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncDeletesOfSavedAddresses() {
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
s.other, s.other,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
if len(r.SavedAddresses) == 2 { if len(r.SavedAddresses()) == 2 {
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*r.SavedAddresses[0], *r.SavedAddresses[1]}, savedAddressDataIsEqual)) sas := r.SavedAddresses()
s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*sas[0], *sas[1]}, savedAddressDataIsEqual))
return true return true
} }
return false return false
@ -259,12 +264,12 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncDeletesOfSavedAddresses() {
_, err = WaitOnMessengerResponse( _, err = WaitOnMessengerResponse(
s.other, s.other,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {
if len(r.SavedAddresses) == 1 { if len(r.SavedAddresses()) == 1 {
// We expect the deleted event to report only address and chain ID // We expect the deleted event to report only address and chain ID
s.Require().Equal(sa1.Address, r.SavedAddresses[0].Address) s.Require().Equal(sa1.Address, r.SavedAddresses()[0].Address)
s.Require().Equal(sa1.ChainID, r.SavedAddresses[0].ChainID) s.Require().Equal(sa1.ChainID, r.SavedAddresses()[0].ChainID)
s.Require().Equal("", r.SavedAddresses[0].Name) s.Require().Equal("", r.SavedAddresses()[0].Name)
s.Require().Equal(false, r.SavedAddresses[0].Favourite) s.Require().Equal(false, r.SavedAddresses()[0].Favourite)
return true return true
} }
return false return false

View File

@ -106,7 +106,7 @@ func (s *MessengerSyncVerificationRequests) TestSyncVerificationRequests() {
return err return err
} }
if response.VerificationRequests != nil { if len(response.VerificationRequests()) == 1 {
return nil return nil
} }
return errors.New("Not received all verification requests") return errors.New("Not received all verification requests")

View File

@ -2431,13 +2431,21 @@ func (t *testTimeSource) GetCurrentTime() uint64 {
// WaitOnMessengerResponse Wait until the condition is true or the timeout is reached. // WaitOnMessengerResponse Wait until the condition is true or the timeout is reached.
func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) { func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) {
var response *MessengerResponse response := &MessengerResponse{}
return response, tt.RetryWithBackOff(func() error { err := tt.RetryWithBackOff(func() error {
var err error var err error
response, err = m.RetrieveAll() r, err := m.RetrieveAll()
if err := response.Merge(r); err != nil {
panic(err)
}
if err == nil && !condition(response) { if err == nil && !condition(response) {
err = errors.New(errorMessage) err = errors.New(errorMessage)
} }
return err return err
}) })
if err != nil {
return nil, err
}
return response, nil
} }

View File

@ -23,6 +23,10 @@ type SavedAddress struct {
savedAddressMeta savedAddressMeta
} }
func (s *SavedAddress) ID() string {
return fmt.Sprintf("%s-%d", s.Address.Hex(), s.ChainID)
}
type SavedAddressesManager struct { type SavedAddressesManager struct {
db *sql.DB db *sql.DB
} }