chore: add bio and social links validation

This commit is contained in:
Patryk Osmaczko 2022-08-10 15:09:15 +02:00 committed by osmaczko
parent b711b61fb9
commit 78d4d86f68
4 changed files with 58 additions and 38 deletions

View file

@ -55,21 +55,6 @@ func (s SocialLinks) Equals(rhs SocialLinks) bool {
return true
}
func (s SocialLinks) EqualsProtobuf(rhs []*protobuf.SocialLink) bool {
if len(s) != len(rhs) {
return false
}
sort.Slice(s, func(i, j int) bool { return s[i].Text < s[j].Text })
sort.Slice(rhs, func(i, j int) bool { return rhs[i].Text < rhs[j].Text })
for i := range s {
if s[i].Text != rhs[i].Text || s[i].URL != rhs[i].Url {
return false
}
}
return true
}
func (s *SocialLinks) Serialize() ([]byte, error) {
return json.Marshal(s)
}

View file

@ -20,16 +20,19 @@ func TestEquals(t *testing.T) {
},
}
other := []*protobuf.SocialLink{}
require.False(t, socialLinks.EqualsProtobuf(other))
protobufLinks := []*protobuf.SocialLink{}
transformedLinks := NewSocialLinks(protobufLinks)
require.False(t, socialLinks.Equals(*transformedLinks))
other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"})
other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"})
require.True(t, socialLinks.EqualsProtobuf(other))
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"})
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"})
transformedLinks = NewSocialLinks(protobufLinks)
require.True(t, socialLinks.Equals(*transformedLinks))
// order does not matter
other = []*protobuf.SocialLink{}
other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"})
other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"})
require.True(t, socialLinks.EqualsProtobuf(other))
protobufLinks = []*protobuf.SocialLink{}
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"})
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"})
transformedLinks = NewSocialLinks(protobufLinks)
require.True(t, socialLinks.Equals(*transformedLinks))
}

View file

@ -1887,13 +1887,22 @@ func (m *Messenger) HandleChatIdentity(state *ReceivedMessageState, ci protobuf.
contactModified = true
}
if err = ValidateBio(&ci.Description); err != nil {
return err
}
if contact.Bio != ci.Description {
contact.Bio = ci.Description
contactModified = true
}
if !contact.SocialLinks.EqualsProtobuf(ci.SocialLinks) {
contact.SocialLinks = *identity.NewSocialLinks(ci.SocialLinks)
socialLinks := identity.NewSocialLinks(ci.SocialLinks)
if err = ValidateSocialLinks(socialLinks); err != nil {
return err
}
if !contact.SocialLinks.Equals(*socialLinks) {
contact.SocialLinks = *socialLinks
contactModified = true
}
}

View file

@ -10,9 +10,16 @@ import (
"github.com/status-im/status-go/protocol/identity/alias"
)
const (
maxBioLength = 240
maxSocialLinkTextLength = 24
)
var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores and hyphens allowed")
var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed")
var ErrInvalidBioLength = errors.New("invalid bio length")
var ErrInvalidSocialLinkTextLength = errors.New("invalid social link text length")
func ValidateDisplayName(displayName *string) error {
name := strings.TrimSpace(*displayName)
@ -72,6 +79,13 @@ func (m *Messenger) SetDisplayName(displayName string) error {
return m.publishContactCode()
}
func ValidateBio(bio *string) error {
if len(*bio) > maxBioLength {
return ErrInvalidBioLength
}
return nil
}
func (m *Messenger) SetBio(bio string) error {
currentBio, err := m.settings.Bio()
if err != nil {
@ -82,21 +96,30 @@ func (m *Messenger) SetBio(bio string) error {
return nil // Do nothing
}
// TODO: add validation
err = m.settings.SaveSettingField(settings.Bio, bio)
if err != nil {
if err = ValidateBio(&bio); err != nil {
return err
}
err = m.resetLastPublishedTimeForChatIdentity()
if err != nil {
if err = m.settings.SaveSettingField(settings.Bio, bio); err != nil {
return err
}
if err = m.resetLastPublishedTimeForChatIdentity(); err != nil {
return err
}
return m.publishContactCode()
}
func ValidateSocialLinks(socialLinks *identity.SocialLinks) error {
for _, link := range *socialLinks {
if len(link.Text) > maxSocialLinkTextLength {
return ErrInvalidSocialLinkTextLength
}
}
return nil
}
func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error {
currentSocialLinks, err := m.settings.GetSocialLinks()
if err != nil {
@ -107,15 +130,15 @@ func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error {
return nil // Do nothing
}
// TODO: add validation
err = m.settings.SetSocialLinks(socialLinks)
if err != nil {
if err = ValidateSocialLinks(socialLinks); err != nil {
return err
}
err = m.resetLastPublishedTimeForChatIdentity()
if err != nil {
if err = m.settings.SetSocialLinks(socialLinks); err != nil {
return err
}
if err = m.resetLastPublishedTimeForChatIdentity(); err != nil {
return err
}