From b69042e7d7612d68996db9498cede8bb5d1599a8 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 13 Feb 2023 20:09:20 +0000 Subject: [PATCH] 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. --- VERSION | 2 +- protocol/messenger.go | 6 +- protocol/messenger_contact_requests_test.go | 111 ++++++++------ protocol/messenger_contact_verification.go | 12 +- .../messenger_contact_verification_test.go | 142 +++++++++--------- protocol/messenger_emoji_test.go | 28 ++-- protocol/messenger_handler.go | 27 ++-- protocol/messenger_response.go | 106 +++++++++++-- protocol/messenger_response_test.go | 11 -- protocol/messenger_saved_address.go | 4 +- .../messenger_sync_saved_addresses_test.go | 27 ++-- protocol/messenger_sync_verification_test.go | 2 +- protocol/messenger_test.go | 14 +- services/wallet/saved_addresses.go | 4 + 14 files changed, 308 insertions(+), 188 deletions(-) diff --git a/VERSION b/VERSION index bfc1b49dd..e113be9c0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.131.1 +0.131.2 diff --git a/protocol/messenger.go b/protocol/messenger.go index 14ff659f5..40198f661 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -4261,7 +4261,7 @@ func (m *Messenger) saveDataAndPrepareResponse(messageState *ReceivedMessageStat } for _, emojiReaction := range messageState.EmojiReactions { - messageState.Response.EmojiReactions = append(messageState.Response.EmojiReactions, emojiReaction) + messageState.Response.AddEmojiReaction(emojiReaction) } for _, groupChatInvitation := range messageState.GroupChatInvitations { @@ -5684,7 +5684,7 @@ func (m *Messenger) SendEmojiReaction(ctx context.Context, chatID, messageID str return nil, err } - response.EmojiReactions = []*EmojiReaction{emojiR} + response.AddEmojiReaction(emojiR) response.AddChat(chat) err = m.persistence.SaveEmojiReaction(emojiR) @@ -5772,7 +5772,7 @@ func (m *Messenger) SendEmojiReactionRetraction(ctx context.Context, emojiReacti // Update MessengerResponse response := MessengerResponse{} emojiR.Retracted = true - response.EmojiReactions = []*EmojiReaction{emojiR} + response.AddEmojiReaction(emojiR) response.AddChat(chat) // Persist retraction state for emoji reaction diff --git a/protocol/messenger_contact_requests_test.go b/protocol/messenger_contact_requests_test.go index 8ea59c92f..27d060bd5 100644 --- a/protocol/messenger_contact_requests_test.go +++ b/protocol/messenger_contact_requests_test.go @@ -61,8 +61,7 @@ func (s *MessengerContactRequestSuite) newMessenger(shh types.Waku) *Messenger { return messenger } -// NOTE(cammellos): Disabling for hotfix -func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { //nolint: unused +func (s *MessengerContactRequestSuite) TestReceiveAndAcceptContactRequest() { //nolint: unused messageText := "hello!" @@ -156,7 +155,7 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { // resp, err = WaitOnMessengerResponse( s.m, 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", ) @@ -172,9 +171,18 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequest() { // // Make sure the message is updated, sender s2de s.Require().NotNil(resp) - s.Require().Len(resp.Messages(), 1) - 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 mutualContacts = s.m.MutualContacts() @@ -278,8 +286,7 @@ func (s *MessengerContactRequestSuite) TestReceiveAndDismissContactRequest() { s.Require().Len(contacts, 0) } -// NOTE(cammellos): Disabling for hotfix -func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest() { //nolint: unused +func (s *MessengerContactRequestSuite) TestReceiveAcceptAndRetractContactRequest() { //nolint: unused messageText := "hello!" @@ -376,7 +383,7 @@ func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest resp, err = WaitOnMessengerResponse( s.m, 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", ) @@ -393,9 +400,18 @@ func (s *MessengerContactRequestSuite) testReceiveAcceptAndRetractContactRequest // Make sure the message is updated, sender side s.Require().NotNil(resp) - s.Require().Len(resp.Messages(), 1) - 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 mutualContacts = s.m.MutualContacts() @@ -483,8 +499,7 @@ func (s *MessengerContactRequestSuite) TestReceiveAcceptAndRetractContactRequest 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!" @@ -575,7 +590,7 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequestTwice() resp, err = WaitOnMessengerResponse( s.m, 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", ) @@ -588,9 +603,19 @@ func (s *MessengerContactRequestSuite) testReceiveAndAcceptContactRequestTwice() // Make sure the message is updated, sender s2de s.Require().NotNil(resp) - s.Require().Len(resp.Messages(), 1) - s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID) - s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState) + s.Require().Len(resp.Messages(), 2) + + 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 mutualContacts = s.m.MutualContacts() @@ -660,11 +685,7 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact( resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - contactRequests, _, err := theirMessenger.PendingContactRequests("", 10) - if err != nil { - return false - } - return len(contactRequests) == 1 + return len(r.Contacts) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) @@ -719,7 +740,7 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact( resp, err = WaitOnMessengerResponse( s.m, 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", ) @@ -727,26 +748,33 @@ func (s *MessengerContactRequestSuite) TestAcceptLatestContactRequestForContact( // Make sure the message is updated, sender side 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) - // TODO(cammellos): This code duplicates contact requests - // 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) + var message *common.Message - // Check activity center notification is of the right type - s.Require().Equal(ActivityCenterNotificationTypeContactRequest, resp.ActivityCenterNotifications()[0].Type) - s.Require().NotNil(resp.ActivityCenterNotifications()[0].Message) - s.Require().Equal(common.ContactRequestStateAccepted, resp.ActivityCenterNotifications()[0].Message.ContactRequestState) + for _, m := range resp.Messages() { + if m.ID == contactRequests[0].ID { + message = m + } + } + s.Require().NotNil(message) + s.Require().Equal(common.ContactRequestStateAccepted, message.ContactRequestState) - // Make sure we consider them a mutual contact, sender side - mutualContacts = s.m.MutualContacts() - s.Require().Len(mutualContacts, 1) + // Check activity center notification is of the right type + s.Require().Equal(ActivityCenterNotificationTypeContactRequest, resp.ActivityCenterNotifications()[0].Type) + s.Require().NotNil(resp.ActivityCenterNotifications()[0].Message) + s.Require().Equal(common.ContactRequestStateAccepted, resp.ActivityCenterNotifications()[0].Message.ContactRequestState) - // Check the contact state is correctly set - s.Require().Len(resp.Contacts, 1) - s.Require().True(resp.Contacts[0].mutual()) */ + // Make sure we consider them a mutual contact, sender side + mutualContacts = s.m.MutualContacts() + 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() { @@ -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) _, err := theirMessenger.Start() diff --git a/protocol/messenger_contact_verification.go b/protocol/messenger_contact_verification.go index b7b586e9b..a1207246e 100644 --- a/protocol/messenger_contact_verification.go +++ b/protocol/messenger_contact_verification.go @@ -115,9 +115,9 @@ func (m *Messenger) SendContactVerificationRequest(ctx context.Context, contactI return nil, err } - response := &MessengerResponse{ - VerificationRequests: []*verification.Request{verifRequest}, - } + response := &MessengerResponse{} + + response.AddVerificationRequest(verifRequest) err = m.createOrUpdateOutgoingContactVerificationNotification(contact, response, verifRequest, chatMessage, nil) if err != nil { @@ -332,9 +332,9 @@ func (m *Messenger) AcceptContactVerificationRequest(ctx context.Context, id str if err != nil { return nil, err } - resp := &MessengerResponse{ - VerificationRequests: []*verification.Request{verifRequest}, - } + resp := &MessengerResponse{} + + resp.AddVerificationRequest(verifRequest) replyMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted) if err != nil { diff --git a/protocol/messenger_contact_verification_test.go b/protocol/messenger_contact_verification_test.go index 9629c2112..82dbde858 100644 --- a/protocol/messenger_contact_verification_test.go +++ b/protocol/messenger_contact_verification_test.go @@ -19,8 +19,7 @@ import ( "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)) } @@ -136,7 +135,7 @@ func (s *MessengerVerificationRequests) mutualContact(theirMessenger *Messenger) resp, err = WaitOnMessengerResponse( s.m, 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", ) @@ -149,9 +148,18 @@ func (s *MessengerVerificationRequests) mutualContact(theirMessenger *Messenger) // Make sure the message is updated, sender s2de s.Require().NotNil(resp) - s.Require().Len(resp.Messages(), 1) - s.Require().Equal(resp.Messages()[0].ID, contactRequests[0].ID) - s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState) + s.Require().Len(resp.Messages(), 2) + 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 mutualContacts = s.m.MutualContacts() @@ -175,8 +183,8 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() { resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - verificationRequestID := resp.VerificationRequests[0].ID + s.Require().Len(resp.VerificationRequests(), 1) + verificationRequestID := resp.VerificationRequests()[0].ID s.Require().Len(resp.Messages(), 1) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) @@ -187,13 +195,13 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() { resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) 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().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) - s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) - s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) + s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED) + s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt) s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) @@ -237,20 +245,20 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() { resp, err = WaitOnMessengerResponse( s.m, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 + return len(r.VerificationRequests()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) messages := resp.Messages() s.Require().Len(messages, 2) var originalMessage *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] replyMessage = messages[1] } else { @@ -300,8 +308,8 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() { resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - verificationRequestID := resp.VerificationRequests[0].ID + s.Require().Len(resp.VerificationRequests(), 1) + verificationRequestID := resp.VerificationRequests()[0].ID s.Require().Len(resp.Messages(), 1) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) @@ -312,13 +320,13 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() { resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) 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().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) - s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) - s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) + s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED) + s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt) s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) @@ -362,13 +370,13 @@ func (s *MessengerVerificationRequests) TestTrustedVerificationRequests() { resp, err = WaitOnMessengerResponse( s.m, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 + return len(r.VerificationRequests()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) messages := resp.Messages() s.Require().Len(messages, 2) @@ -413,8 +421,8 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests() resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - verificationRequestID := resp.VerificationRequests[0].ID + s.Require().Len(resp.VerificationRequests(), 1) + verificationRequestID := resp.VerificationRequests()[0].ID s.Require().Len(resp.Messages(), 1) s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus) @@ -425,13 +433,13 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests() resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) 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().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) - s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusACCEPTED) - s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) + s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusACCEPTED) + s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt) s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) @@ -475,20 +483,20 @@ func (s *MessengerVerificationRequests) TestUnthrustworthyVerificationRequests() resp, err = WaitOnMessengerResponse( s.m, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 + return len(r.VerificationRequests()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) messages := resp.Messages() s.Require().Len(messages, 2) var originalMessage *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] replyMessage = messages[1] } else { @@ -540,8 +548,8 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() { resp, err := s.m.SendContactVerificationRequest(context.Background(), theirPk, challenge) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - verificationRequestID := resp.VerificationRequests[0].ID + s.Require().Len(resp.VerificationRequests(), 1) + verificationRequestID := resp.VerificationRequests()[0].ID s.Require().Len(resp.Messages(), 1) s.Require().Equal(challenge, resp.Messages()[0].Text) @@ -551,13 +559,13 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() { resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) @@ -588,10 +596,10 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() { s.Require().NotNil(resp) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) - s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusDECLINED) - s.Require().NotEmpty(resp.VerificationRequests[0].RepliedAt) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) + s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusDECLINED) + s.Require().NotEmpty(resp.VerificationRequests()[0].RepliedAt) s.Require().Len(resp.ActivityCenterNotifications(), 1) s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID) @@ -619,13 +627,13 @@ func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() { resp, err = WaitOnMessengerResponse( s.m, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 + return len(r.VerificationRequests()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Len(resp.Messages(), 1) 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) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - verificationRequestID := resp.VerificationRequests[0].ID + s.Require().Len(resp.VerificationRequests(), 1) + verificationRequestID := resp.VerificationRequests()[0].ID s.Require().Len(resp.Messages(), 1) s.Require().Equal(challenge, resp.Messages()[0].Text) @@ -662,13 +670,13 @@ func (s *MessengerVerificationRequests) TestCancelVerificationRequest() { resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusPENDING) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, false) @@ -699,21 +707,21 @@ func (s *MessengerVerificationRequests) TestCancelVerificationRequest() { s.Require().NotNil(resp) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) - s.Require().Equal(resp.VerificationRequests[0].RequestStatus, verification.RequestStatusCANCELED) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) + s.Require().Equal(resp.VerificationRequests()[0].RequestStatus, verification.RequestStatusCANCELED) // Check canceled state on the receiver's side resp, err = WaitOnMessengerResponse( theirMessenger, func(r *MessengerResponse) bool { - return len(r.VerificationRequests) > 0 && len(r.ActivityCenterNotifications()) > 0 + return len(r.VerificationRequests()) == 1 && len(r.ActivityCenterNotifications()) == 1 }, "no messages", ) s.Require().NoError(err) - s.Require().Len(resp.VerificationRequests, 1) - s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID) + s.Require().Len(resp.VerificationRequests(), 1) + s.Require().Equal(resp.VerificationRequests()[0].ID, verificationRequestID) s.Require().Equal(resp.ActivityCenterNotifications()[0].Type, ActivityCenterNotificationTypeContactVerification) s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusCANCELED) s.Require().Equal(resp.ActivityCenterNotifications()[0].Read, true) diff --git a/protocol/messenger_emoji_test.go b/protocol/messenger_emoji_test.go index 38ab81872..5bd1b5a8f 100644 --- a/protocol/messenger_emoji_test.go +++ b/protocol/messenger_emoji_test.go @@ -108,40 +108,40 @@ func (s *MessengerEmojiSuite) TestSendEmoji() { response, err = bob.SendEmojiReaction(context.Background(), chat.ID, messageID, protobuf.EmojiReaction_SAD) 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 response, err = WaitOnMessengerResponse( alice, - func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, + func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 }, "no emoji", ) s.Require().NoError(err) - s.Require().Len(response.EmojiReactions, 1) - s.Require().Equal(response.EmojiReactions[0].ID(), emojiID) - s.Require().Equal(response.EmojiReactions[0].Type, protobuf.EmojiReaction_SAD) + s.Require().Len(response.EmojiReactions(), 1) + s.Require().Equal(response.EmojiReactions()[0].ID(), emojiID) + s.Require().Equal(response.EmojiReactions()[0].Type, protobuf.EmojiReaction_SAD) // Retract the emoji response, err = bob.SendEmojiReactionRetraction(context.Background(), emojiID) s.Require().NoError(err) - s.Require().Len(response.EmojiReactions, 1) - s.Require().True(response.EmojiReactions[0].Retracted) + s.Require().Len(response.EmojiReactions(), 1) + s.Require().True(response.EmojiReactions()[0].Retracted) // Wait for the emoji to arrive to alice response, err = WaitOnMessengerResponse( alice, - func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, + func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 }, "no emoji", ) s.Require().NoError(err) - s.Require().Len(response.EmojiReactions, 1) - s.Require().Equal(response.EmojiReactions[0].ID(), emojiID) - s.Require().Equal(response.EmojiReactions[0].Type, protobuf.EmojiReaction_SAD) - s.Require().True(response.EmojiReactions[0].Retracted) + s.Require().Len(response.EmojiReactions(), 1) + s.Require().Equal(response.EmojiReactions()[0].ID(), emojiID) + s.Require().Equal(response.EmojiReactions()[0].Type, protobuf.EmojiReaction_SAD) + s.Require().True(response.EmojiReactions()[0].Retracted) s.Require().NoError(bob.Shutdown()) } @@ -198,7 +198,7 @@ func (s *MessengerEmojiSuite) TestEmojiPrivateGroup() { // Wait for the message to reach its destination _, err = WaitOnMessengerResponse( alice, - func(r *MessengerResponse) bool { return len(r.EmojiReactions) > 0 }, + func(r *MessengerResponse) bool { return len(r.EmojiReactions()) == 1 }, "no emoji reaction received", ) s.Require().NoError(err) diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index b9bb1cf0a..783352e1e 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -262,7 +262,7 @@ func (m *Messenger) PendingNotificationContactRequest(contactID string) (*Activi 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 { notification, err := m.PendingNotificationContactRequest(contact.ID) @@ -330,7 +330,12 @@ func (m *Messenger) createContactRequestNotification(contact *Contact, messageSt contactRequest.ContentType = protobuf.ChatMessage_CONTACT_REQUEST contactRequest.Clock = messageState.CurrentMessageState.Message.Clock 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)) if err != nil { return err @@ -344,6 +349,10 @@ func (m *Messenger) createContactRequestNotification(contact *Contact, messageSt } } + if !createNewNotification { + return nil + } + notification := &ActivityCenterNotification{ ID: types.FromHex(contactRequest.ID), Name: contact.CanonicalName(), @@ -801,11 +810,9 @@ func (m *Messenger) HandleAcceptContactRequest(state *ReceivedMessageState, mess 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 { // 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 { 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 { - logger.Debug("creating contact request notification") - err = m.createContactRequestNotification(contact, state, nil) + err = m.createContactRequestNotification(contact, state, nil, true) if err != nil { return err } @@ -906,7 +912,6 @@ func (m *Messenger) HandleContactUpdate(state *ReceivedMessageState, message pro } if contact.LastUpdated < message.Clock { - logger.Debug("Updating contact") if contact.EnsName != message.EnsName { contact.EnsName = message.EnsName contact.ENSVerified = false @@ -918,7 +923,7 @@ func (m *Messenger) HandleContactUpdate(state *ReceivedMessageState, message pro r := contact.ContactRequestReceived(message.ContactRequestClock) if r.newContactRequestReceived { - err = m.createContactRequestNotification(contact, state, nil) + err = m.createContactRequestNotification(contact, state, nil, true) if err != nil { 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 { - err = m.createContactRequestNotification(contact, state, receivedMessage) + err = m.createContactRequestNotification(contact, state, receivedMessage, true) if err != nil { return err } @@ -1751,7 +1756,7 @@ func (m *Messenger) HandleChatMessage(state *ReceivedMessageState) error { state.AllContacts.Store(chatContact.ID, chatContact) if sendNotification { - err = m.createContactRequestNotification(chatContact, state, receivedMessage) + err = m.createContactRequestNotification(chatContact, state, receivedMessage, true) if err != nil { return err } diff --git a/protocol/messenger_response.go b/protocol/messenger_response.go index 87385ee6d..e25a0142c 100644 --- a/protocol/messenger_response.go +++ b/protocol/messenger_response.go @@ -34,7 +34,6 @@ type ClearedHistory struct { type MessengerResponse struct { Contacts []*Contact Installations []*multidevice.Installation - EmojiReactions []*EmojiReaction Invitations []*GroupChatInvitation CommunityChanges []*communities.CommunityChanges RequestsToJoinCommunity []*communities.RequestToJoin @@ -44,11 +43,9 @@ type MessengerResponse struct { Settings []*settings.SyncSettingField IdentityImages []images.IdentityImage Accounts []*accounts.Account - VerificationRequests []*verification.Request DiscordCategories []*discord.Category DiscordChannels []*discord.Channel DiscordOldestMessageTimestamp int - SavedAddresses []*wallet.SavedAddress // notifications a list of notifications derived from messenger events // that are useful to notify the user about @@ -67,7 +64,10 @@ type MessengerResponse struct { currentStatus *UserStatus statusUpdates map[string]UserStatus clearedHistories map[string]*ClearedHistory + verificationRequests map[string]*verification.Request trustStatus map[string]verification.TrustStatus + emojiReactions map[string]*EmojiReaction + savedAddresses map[string]*wallet.SavedAddress } func (r *MessengerResponse) MarshalJSON() ([]byte, error) { @@ -108,7 +108,6 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) { }{ Contacts: r.Contacts, Installations: r.Installations, - EmojiReactions: r.EmojiReactions, Invitations: r.Invitations, CommunityChanges: r.CommunityChanges, RequestsToJoinCommunity: r.RequestsToJoinCommunity, @@ -118,10 +117,10 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) { Settings: r.Settings, IdentityImages: r.IdentityImages, Accounts: r.Accounts, - VerificationRequests: r.VerificationRequests, - SavedAddresses: r.SavedAddresses, Messages: r.Messages(), + VerificationRequests: r.VerificationRequests(), + SavedAddresses: r.SavedAddresses(), Notifications: r.Notifications(), Chats: r.Chats(), Communities: r.Communities(), @@ -131,6 +130,7 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) { ClearedHistories: r.ClearedHistories(), ActivityCenterNotifications: r.ActivityCenterNotifications(), PinMessages: r.PinMessages(), + EmojiReactions: r.EmojiReactions(), StatusUpdates: r.StatusUpdates(), DiscordCategories: r.DiscordCategories, DiscordChannels: r.DiscordChannels, @@ -197,6 +197,14 @@ func (r *MessengerResponse) Notifications() []*localnotifications.Notification { 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 { var pinMessages []*common.PinMessage for _, pm := range r.pinMessages { @@ -236,7 +244,7 @@ func (r *MessengerResponse) IsEmpty() bool { len(r.Settings)+ len(r.Installations)+ len(r.Invitations)+ - len(r.EmojiReactions)+ + len(r.emojiReactions)+ len(r.communities)+ len(r.CommunityChanges)+ len(r.removedChats)+ @@ -248,24 +256,20 @@ func (r *MessengerResponse) IsEmpty() bool { len(r.statusUpdates)+ len(r.activityCenterNotifications)+ len(r.trustStatus)+ - len(r.VerificationRequests)+ + len(r.verificationRequests)+ len(r.RequestsToJoinCommunity) == 0 && - len(r.SavedAddresses) == 0 && + len(r.savedAddresses) == 0 && r.currentStatus == nil } // Merge takes another response and appends the new Chats & new Messages and replaces // the existing Messages & Chats if they have the same ID func (r *MessengerResponse) Merge(response *MessengerResponse) error { - if len(response.Installations)+ - len(response.EmojiReactions)+ - len(response.Invitations)+ + if len(response.Invitations)+ len(response.RequestsToJoinCommunity)+ len(response.Mailservers)+ - len(response.EmojiReactions)+ len(response.Bookmarks)+ len(response.clearedHistories)+ - len(response.VerificationRequests)+ len(response.DiscordChannels)+ len(response.DiscordCategories) != 0 { return ErrNotImplemented @@ -276,11 +280,15 @@ func (r *MessengerResponse) Merge(response *MessengerResponse) error { r.AddRemovedMessages(response.RemovedMessages()) r.AddNotifications(response.Notifications()) r.AddMessages(response.Messages()) + r.AddContacts(response.Contacts) r.AddCommunities(response.Communities()) r.AddPinMessages(response.PinMessages()) - r.AddVerificationRequests(response.VerificationRequests) + r.AddVerificationRequests(response.VerificationRequests()) r.AddTrustStatuses(response.trustStatus) r.AddActivityCenterNotifications(response.ActivityCenterNotifications()) + r.AddEmojiReactions(response.EmojiReactions()) + r.AddInstallations(response.Installations) + r.AddSavedAddresses(response.SavedAddresses()) r.CommunityChanges = append(r.CommunityChanges, response.CommunityChanges...) return nil @@ -323,7 +331,11 @@ func (r *MessengerResponse) AddBookmarks(bookmarks []*browsers.Bookmark) { } 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) { @@ -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) { if r.notifications == nil { 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) { r.messages = make(map[string]*common.Message) r.AddMessages(messages) diff --git a/protocol/messenger_response_test.go b/protocol/messenger_response_test.go index 3e81f7db2..96562fe14 100644 --- a/protocol/messenger_response_test.go +++ b/protocol/messenger_response_test.go @@ -6,7 +6,6 @@ import ( "github.com/stretchr/testify/require" "github.com/status-im/status-go/protocol/common" - "github.com/status-im/status-go/protocol/encryption/multidevice" ) func TestMessengerResponseMergeChats(t *testing.T) { @@ -55,16 +54,6 @@ func TestMessengerResponseMergeNotImplemented(t *testing.T) { response1 := &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{{}}, } require.Error(t, response1.Merge(response2)) diff --git a/protocol/messenger_saved_address.go b/protocol/messenger_saved_address.go index ba1f78377..d55e8ceb9 100644 --- a/protocol/messenger_saved_address.go +++ b/protocol/messenger_saved_address.go @@ -100,7 +100,7 @@ func (m *Messenger) handleSyncSavedAddress(state *ReceivedMessageState, syncMess if err != nil { 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 { sa := wallet.SavedAddress{ Address: address, @@ -113,7 +113,7 @@ func (m *Messenger) handleSyncSavedAddress(state *ReceivedMessageState, syncMess if err != nil { return err } - state.Response.SavedAddresses = append(state.Response.SavedAddresses, &sa) + state.Response.AddSavedAddress(&sa) } return } diff --git a/protocol/messenger_sync_saved_addresses_test.go b/protocol/messenger_sync_saved_addresses_test.go index 46e64c475..9dbe55c80 100644 --- a/protocol/messenger_sync_saved_addresses_test.go +++ b/protocol/messenger_sync_saved_addresses_test.go @@ -3,6 +3,7 @@ package protocol import ( "context" "crypto/ecdsa" + "fmt" "testing" "github.com/stretchr/testify/suite" @@ -149,8 +150,10 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncExistingSavedAddresses() { _, err = WaitOnMessengerResponse( s.other, func(r *MessengerResponse) bool { - if len(r.SavedAddresses) == 2 { - s.Require().True(haveSameElements([]wallet.SavedAddress{sa1, sa2}, []wallet.SavedAddress{*r.SavedAddresses[0], *r.SavedAddresses[1]}, savedAddressDataIsEqual)) + fmt.Println("LENG", len(r.SavedAddresses())) + 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 false @@ -193,8 +196,9 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncSavedAddresses() { _, err = WaitOnMessengerResponse( s.other, func(r *MessengerResponse) bool { - if len(r.SavedAddresses) == 2 { - 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 false @@ -237,8 +241,9 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncDeletesOfSavedAddresses() { _, err = WaitOnMessengerResponse( s.other, func(r *MessengerResponse) bool { - if len(r.SavedAddresses) == 2 { - 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 false @@ -259,12 +264,12 @@ func (s *MessengerSyncSavedAddressesSuite) TestSyncDeletesOfSavedAddresses() { _, err = WaitOnMessengerResponse( s.other, 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 - s.Require().Equal(sa1.Address, r.SavedAddresses[0].Address) - s.Require().Equal(sa1.ChainID, r.SavedAddresses[0].ChainID) - s.Require().Equal("", r.SavedAddresses[0].Name) - s.Require().Equal(false, r.SavedAddresses[0].Favourite) + s.Require().Equal(sa1.Address, r.SavedAddresses()[0].Address) + s.Require().Equal(sa1.ChainID, r.SavedAddresses()[0].ChainID) + s.Require().Equal("", r.SavedAddresses()[0].Name) + s.Require().Equal(false, r.SavedAddresses()[0].Favourite) return true } return false diff --git a/protocol/messenger_sync_verification_test.go b/protocol/messenger_sync_verification_test.go index 79a69ed1d..771cdc8c8 100644 --- a/protocol/messenger_sync_verification_test.go +++ b/protocol/messenger_sync_verification_test.go @@ -106,7 +106,7 @@ func (s *MessengerSyncVerificationRequests) TestSyncVerificationRequests() { return err } - if response.VerificationRequests != nil { + if len(response.VerificationRequests()) == 1 { return nil } return errors.New("Not received all verification requests") diff --git a/protocol/messenger_test.go b/protocol/messenger_test.go index 9932bb360..5ff49e611 100644 --- a/protocol/messenger_test.go +++ b/protocol/messenger_test.go @@ -2431,13 +2431,21 @@ func (t *testTimeSource) GetCurrentTime() uint64 { // WaitOnMessengerResponse Wait until the condition is true or the timeout is reached. func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) { - var response *MessengerResponse - return response, tt.RetryWithBackOff(func() error { + response := &MessengerResponse{} + err := tt.RetryWithBackOff(func() 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) { err = errors.New(errorMessage) } return err }) + if err != nil { + return nil, err + } + return response, nil } diff --git a/services/wallet/saved_addresses.go b/services/wallet/saved_addresses.go index 9eb4e629f..3b8e3ed6b 100644 --- a/services/wallet/saved_addresses.go +++ b/services/wallet/saved_addresses.go @@ -23,6 +23,10 @@ type SavedAddress struct { savedAddressMeta } +func (s *SavedAddress) ID() string { + return fmt.Sprintf("%s-%d", s.Address.Hex(), s.ChainID) +} + type SavedAddressesManager struct { db *sql.DB }