mirror of
https://github.com/status-im/status-go.git
synced 2025-02-19 18:28:18 +00:00
chore: add bio and social links validation
This commit is contained in:
parent
b711b61fb9
commit
78d4d86f68
@ -55,21 +55,6 @@ func (s SocialLinks) Equals(rhs SocialLinks) bool {
|
|||||||
return true
|
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) {
|
func (s *SocialLinks) Serialize() ([]byte, error) {
|
||||||
return json.Marshal(s)
|
return json.Marshal(s)
|
||||||
}
|
}
|
||||||
|
@ -20,16 +20,19 @@ func TestEquals(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
other := []*protobuf.SocialLink{}
|
protobufLinks := []*protobuf.SocialLink{}
|
||||||
require.False(t, socialLinks.EqualsProtobuf(other))
|
transformedLinks := NewSocialLinks(protobufLinks)
|
||||||
|
require.False(t, socialLinks.Equals(*transformedLinks))
|
||||||
|
|
||||||
other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"})
|
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"})
|
||||||
other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"})
|
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"})
|
||||||
require.True(t, socialLinks.EqualsProtobuf(other))
|
transformedLinks = NewSocialLinks(protobufLinks)
|
||||||
|
require.True(t, socialLinks.Equals(*transformedLinks))
|
||||||
|
|
||||||
// order does not matter
|
// order does not matter
|
||||||
other = []*protobuf.SocialLink{}
|
protobufLinks = []*protobuf.SocialLink{}
|
||||||
other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"})
|
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"})
|
||||||
other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"})
|
protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"})
|
||||||
require.True(t, socialLinks.EqualsProtobuf(other))
|
transformedLinks = NewSocialLinks(protobufLinks)
|
||||||
|
require.True(t, socialLinks.Equals(*transformedLinks))
|
||||||
}
|
}
|
||||||
|
@ -1887,13 +1887,22 @@ func (m *Messenger) HandleChatIdentity(state *ReceivedMessageState, ci protobuf.
|
|||||||
contactModified = true
|
contactModified = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = ValidateBio(&ci.Description); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if contact.Bio != ci.Description {
|
if contact.Bio != ci.Description {
|
||||||
contact.Bio = ci.Description
|
contact.Bio = ci.Description
|
||||||
contactModified = true
|
contactModified = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if !contact.SocialLinks.EqualsProtobuf(ci.SocialLinks) {
|
socialLinks := identity.NewSocialLinks(ci.SocialLinks)
|
||||||
contact.SocialLinks = *identity.NewSocialLinks(ci.SocialLinks)
|
if err = ValidateSocialLinks(socialLinks); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !contact.SocialLinks.Equals(*socialLinks) {
|
||||||
|
contact.SocialLinks = *socialLinks
|
||||||
contactModified = true
|
contactModified = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,16 @@ import (
|
|||||||
"github.com/status-im/status-go/protocol/identity/alias"
|
"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 ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores and hyphens allowed")
|
||||||
var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
|
var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
|
||||||
var ErrInvalidDisplayNameNotAllowed = errors.New("name is 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 {
|
func ValidateDisplayName(displayName *string) error {
|
||||||
name := strings.TrimSpace(*displayName)
|
name := strings.TrimSpace(*displayName)
|
||||||
@ -72,6 +79,13 @@ func (m *Messenger) SetDisplayName(displayName string) error {
|
|||||||
return m.publishContactCode()
|
return m.publishContactCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ValidateBio(bio *string) error {
|
||||||
|
if len(*bio) > maxBioLength {
|
||||||
|
return ErrInvalidBioLength
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Messenger) SetBio(bio string) error {
|
func (m *Messenger) SetBio(bio string) error {
|
||||||
currentBio, err := m.settings.Bio()
|
currentBio, err := m.settings.Bio()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,21 +96,30 @@ func (m *Messenger) SetBio(bio string) error {
|
|||||||
return nil // Do nothing
|
return nil // Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add validation
|
if err = ValidateBio(&bio); err != nil {
|
||||||
|
|
||||||
err = m.settings.SaveSettingField(settings.Bio, bio)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.resetLastPublishedTimeForChatIdentity()
|
if err = m.settings.SaveSettingField(settings.Bio, bio); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = m.resetLastPublishedTimeForChatIdentity(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.publishContactCode()
|
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 {
|
func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error {
|
||||||
currentSocialLinks, err := m.settings.GetSocialLinks()
|
currentSocialLinks, err := m.settings.GetSocialLinks()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -107,15 +130,15 @@ func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error {
|
|||||||
return nil // Do nothing
|
return nil // Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add validation
|
if err = ValidateSocialLinks(socialLinks); err != nil {
|
||||||
|
|
||||||
err = m.settings.SetSocialLinks(socialLinks)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.resetLastPublishedTimeForChatIdentity()
|
if err = m.settings.SetSocialLinks(socialLinks); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = m.resetLastPublishedTimeForChatIdentity(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user