Reject contact request endpoint

This commit is contained in:
Andrea Maria Piana 2021-11-22 17:21:27 +00:00 committed by r4bbit.eth
parent d32f0467ae
commit ded81b7a51
5 changed files with 93 additions and 1 deletions

View File

@ -1 +1 @@
0.91.5 0.91.6

View File

@ -197,3 +197,38 @@ func (s *MessengerContactUpdateSuite) TestAddContactWithENS() {
receivedContact := response.Contacts[0] receivedContact := response.Contacts[0]
s.Require().NotEmpty(receivedContact.LastUpdated) 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)
}

View File

@ -12,6 +12,38 @@ import (
"github.com/status-im/status-go/protocol/transport" "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) { func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact) (*MessengerResponse, error) {
err := request.Validate() err := request.Validate()
if err != nil { if err != nil {

View File

@ -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
}

View File

@ -592,6 +592,10 @@ func (api *PublicAPI) AddContact(ctx context.Context, request *requests.AddConta
return api.service.messenger.AddContact(ctx, request) 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) { func (api *PublicAPI) RemoveContact(ctx context.Context, pubKey string) (*protocol.MessengerResponse, error) {
return api.service.messenger.RemoveContact(ctx, pubKey) return api.service.messenger.RemoveContact(ctx, pubKey)
} }