Create contact if not in contacts when setting nickname

This commit is contained in:
Andrea Maria Piana 2021-10-25 11:25:37 +01:00
parent f04e5c741c
commit 1cc7546e67
4 changed files with 81 additions and 10 deletions

View File

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

View File

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

View File

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

View File

@ -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) {