diff --git a/VERSION b/VERSION index 1d5e6c02b..88bbfa0da 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.91.5 +0.91.6 diff --git a/protocol/messenger_contact_update_test.go b/protocol/messenger_contact_update_test.go index f8e1e8172..debb66580 100644 --- a/protocol/messenger_contact_update_test.go +++ b/protocol/messenger_contact_update_test.go @@ -197,3 +197,38 @@ func (s *MessengerContactUpdateSuite) TestAddContactWithENS() { receivedContact := response.Contacts[0] s.Require().NotEmpty(receivedContact.LastUpdated) } + +func (s *MessengerContactUpdateSuite) TestRejectContactRequest() { + contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey)) + + theirMessenger := s.newMessenger(s.shh) + _, err := theirMessenger.Start() + s.Require().NoError(err) + + response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)}) + s.Require().NoError(err) + s.Require().NotNil(response) + + s.Require().Len(response.Contacts, 1) + contact := response.Contacts[0] + // It should add the contact + s.Require().True(contact.Added) + + // Wait for the message to reach its destination + response, err = WaitOnMessengerResponse( + s.m, + func(r *MessengerResponse) bool { return len(r.Contacts) > 0 }, + "contact request not received", + ) + s.Require().NoError(err) + + // Make sure HasAddedUs is set + receivedContact := response.Contacts[0] + s.Require().True(receivedContact.HasAddedUs) + + response, err = s.m.RejectContactRequest(context.Background(), &requests.RejectContactRequest{ID: types.Hex2Bytes(contactID)}) + s.Require().NoError(err) + s.Require().Len(response.Contacts, 1) + s.Require().Equal(response.Contacts[0].ID, contactID) + s.Require().False(response.Contacts[0].HasAddedUs) +} diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index ccabfb295..653f5234f 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -12,6 +12,38 @@ import ( "github.com/status-im/status-go/protocol/transport" ) +// NOTE: This sets HasAddedUs to false, so next time we receive a contact request it will be reset to true +func (m *Messenger) RejectContactRequest(ctx context.Context, request *requests.RejectContactRequest) (*MessengerResponse, error) { + err := request.Validate() + if err != nil { + return nil, err + } + + pubKey := request.ID.String() + contact, ok := m.allContacts.Load(pubKey) + if !ok { + var err error + contact, err = buildContactFromPkString(pubKey) + if err != nil { + return nil, err + } + } + + contact.HasAddedUs = false + + err = m.persistence.SaveContact(contact, nil) + if err != nil { + return nil, err + } + + m.allContacts.Store(contact.ID, contact) + + response := &MessengerResponse{} + response.Contacts = []*Contact{contact} + + return response, nil +} + func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact) (*MessengerResponse, error) { err := request.Validate() if err != nil { diff --git a/protocol/requests/reject_contact_request.go b/protocol/requests/reject_contact_request.go new file mode 100644 index 000000000..048887b4d --- /dev/null +++ b/protocol/requests/reject_contact_request.go @@ -0,0 +1,21 @@ +package requests + +import ( + "errors" + + "github.com/status-im/status-go/eth-node/types" +) + +var ErrRejectContactRequestInvalidID = errors.New("reject-contact-request: invalid id") + +type RejectContactRequest struct { + ID types.HexBytes `json:"id"` +} + +func (a *RejectContactRequest) Validate() error { + if len(a.ID) == 0 { + return ErrRejectContactRequestInvalidID + } + + return nil +} diff --git a/services/ext/api.go b/services/ext/api.go index 1f783a142..ef825df7e 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -592,6 +592,10 @@ func (api *PublicAPI) AddContact(ctx context.Context, request *requests.AddConta return api.service.messenger.AddContact(ctx, request) } +func (api *PublicAPI) RejectContactRequest(ctx context.Context, request *requests.RejectContactRequest) (*protocol.MessengerResponse, error) { + return api.service.messenger.RejectContactRequest(ctx, request) +} + func (api *PublicAPI) RemoveContact(ctx context.Context, pubKey string) (*protocol.MessengerResponse, error) { return api.service.messenger.RemoveContact(ctx, pubKey) }