From 1cc7546e6709b1e5400872be47041ca94f3b3080 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 25 Oct 2021 11:25:37 +0100 Subject: [PATCH] Create contact if not in contacts when setting nickname --- protocol/messenger_contacts.go | 39 ++++++++++++++++--- protocol/requests/add_contact.go | 22 +++++++++++ .../requests/set_contact_local_nickname.go | 22 +++++++++++ services/ext/api.go | 8 ++-- 4 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 protocol/requests/add_contact.go create mode 100644 protocol/requests/set_contact_local_nickname.go diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index b46a36baa..6877025be 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -3,16 +3,23 @@ package protocol import ( "context" "crypto/ecdsa" - "errors" "github.com/golang/protobuf/proto" "github.com/status-im/status-go/protocol/common" "github.com/status-im/status-go/protocol/protobuf" + "github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/protocol/transport" ) -func (m *Messenger) AddContact(ctx context.Context, pubKey string) (*MessengerResponse, error) { +func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact) (*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 @@ -22,13 +29,17 @@ func (m *Messenger) AddContact(ctx context.Context, pubKey string) (*MessengerRe } } + if len(request.Nickname) != 0 { + contact.LocalNickname = request.Nickname + } + if !contact.Added { contact.Added = true } contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime() // We sync the contact with the other devices - err := m.syncContact(context.Background(), contact) + err = m.syncContact(context.Background(), contact) if err != nil { return nil, err } @@ -204,21 +215,37 @@ func (m *Messenger) GetContactByID(pubKey string) *Contact { return contact } -func (m *Messenger) SetContactLocalNickname(pubKey string, nickname string) (*MessengerResponse, error) { +func (m *Messenger) SetContactLocalNickname(request *requests.SetContactLocalNickname) (*MessengerResponse, error) { + + if err := request.Validate(); err != nil { + return nil, err + } + + pubKey := request.ID.String() + nickname := request.Nickname contact, ok := m.allContacts.Load(pubKey) if !ok { - return nil, errors.New("not existing contact") + var err error + contact, err = buildContactFromPkString(pubKey) + if err != nil { + return nil, err + } } clock := m.getTimesource().GetCurrentTime() contact.LocalNickname = nickname contact.LastUpdatedLocally = clock + err := m.persistence.SaveContact(contact, nil) + if err != nil { + return nil, err + } + response := &MessengerResponse{} response.Contacts = []*Contact{contact} - err := m.syncContact(context.Background(), contact) + err = m.syncContact(context.Background(), contact) if err != nil { return nil, err } diff --git a/protocol/requests/add_contact.go b/protocol/requests/add_contact.go new file mode 100644 index 000000000..ff221151c --- /dev/null +++ b/protocol/requests/add_contact.go @@ -0,0 +1,22 @@ +package requests + +import ( + "errors" + + "github.com/status-im/status-go/eth-node/types" +) + +var ErrAddContactInvalidID = errors.New("add-contact: invalid id") + +type AddContact struct { + ID types.HexBytes `json:"id"` + Nickname string `json:"nickname"` +} + +func (a *AddContact) Validate() error { + if len(a.ID) == 0 { + return ErrAddContactInvalidID + } + + return nil +} diff --git a/protocol/requests/set_contact_local_nickname.go b/protocol/requests/set_contact_local_nickname.go new file mode 100644 index 000000000..81aaf42fa --- /dev/null +++ b/protocol/requests/set_contact_local_nickname.go @@ -0,0 +1,22 @@ +package requests + +import ( + "errors" + + "github.com/status-im/status-go/eth-node/types" +) + +var ErrSetContactLocalNicknameInvalidID = errors.New("add-contact: invalid id") + +type SetContactLocalNickname struct { + ID types.HexBytes `json:"id"` + Nickname string `json:"nickname"` +} + +func (a *SetContactLocalNickname) Validate() error { + if len(a.ID) == 0 { + return ErrSetContactLocalNicknameInvalidID + } + + return nil +} diff --git a/services/ext/api.go b/services/ext/api.go index ed4319686..8dd769e53 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -588,16 +588,16 @@ func (api *PublicAPI) MarkAllReadInCommunity(communityID string) ([]string, erro return api.service.messenger.MarkAllReadInCommunity(communityID) } -func (api *PublicAPI) AddContact(ctx context.Context, pubKey string) (*protocol.MessengerResponse, error) { - return api.service.messenger.AddContact(ctx, pubKey) +func (api *PublicAPI) AddContact(ctx context.Context, request *requests.AddContact) (*protocol.MessengerResponse, error) { + return api.service.messenger.AddContact(ctx, request) } func (api *PublicAPI) RemoveContact(ctx context.Context, pubKey string) (*protocol.MessengerResponse, error) { return api.service.messenger.RemoveContact(ctx, pubKey) } -func (api *PublicAPI) SetContactLocalNickname(ctx context.Context, pubKey, nickname string) (*protocol.MessengerResponse, error) { - return api.service.messenger.SetContactLocalNickname(pubKey, nickname) +func (api *PublicAPI) SetContactLocalNickname(ctx context.Context, request *requests.SetContactLocalNickname) (*protocol.MessengerResponse, error) { + return api.service.messenger.SetContactLocalNickname(request) } func (api *PublicAPI) ClearHistory(request *requests.ClearHistory) (*protocol.MessengerResponse, error) {