summaryrefslogtreecommitdiff
path: root/internal/validate
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-05-09 12:16:10 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-09 11:16:10 +0100
commit0e29f1f5bb68a48d9b837d7f4e0a16370734955b (patch)
treef08d203ec8ca8aeea728e5251b1dc3956524b4f4 /internal/validate
parent[chore/performance] Make sender multiplier configurable (#1750) (diff)
downloadgotosocial-0e29f1f5bb68a48d9b837d7f4e0a16370734955b.tar.xz
[feature] Enable federation in/out of profile PropertyValue fields (#1722)
Co-authored-by: kim <grufwub@gmail.com> Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>
Diffstat (limited to 'internal/validate')
-rw-r--r--internal/validate/account_test.go2
-rw-r--r--internal/validate/formvalidation.go30
-rw-r--r--internal/validate/formvalidation_test.go57
3 files changed, 47 insertions, 42 deletions
diff --git a/internal/validate/account_test.go b/internal/validate/account_test.go
index 5a7855492..57f0f4900 100644
--- a/internal/validate/account_test.go
+++ b/internal/validate/account_test.go
@@ -50,7 +50,7 @@ func happyAccount() *gtsmodel.Account {
HeaderMediaAttachment: nil,
HeaderRemoteURL: "",
DisplayName: "original zork (he/they)",
- Fields: []gtsmodel.Field{},
+ Fields: []*gtsmodel.Field{},
Note: "hey yo this is my profile!",
Memorial: testrig.FalseBool(),
AlsoKnownAs: "",
diff --git a/internal/validate/formvalidation.go b/internal/validate/formvalidation.go
index dd3925572..20d4aa782 100644
--- a/internal/validate/formvalidation.go
+++ b/internal/validate/formvalidation.go
@@ -25,6 +25,7 @@ import (
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/regexes"
pwv "github.com/wagslane/go-password-validator"
"golang.org/x/text/language"
@@ -43,7 +44,7 @@ const (
maximumCustomCSSLength = 5000
maximumEmojiCategoryLength = 64
maximumProfileFieldLength = 255
- maximumProfileFields = 4
+ maximumProfileFields = 6
)
// NewPassword returns an error if the given password is not sufficiently strong, or nil if it's ok.
@@ -233,19 +234,26 @@ func ULID(i string) bool {
return regexes.ULID.MatchString(i)
}
-func ProfileFieldsCount(fields []apimodel.UpdateField) error {
- if length := len(fields); length > maximumProfileFields {
+// ProfileFields validates the length of provided fields slice,
+// and also iterates through the fields and trims each name + value
+// to maximumProfileFieldLength, if they were above.
+func ProfileFields(fields []*gtsmodel.Field) error {
+ if len(fields) > maximumProfileFields {
return fmt.Errorf("cannot have more than %d profile fields", maximumProfileFields)
}
- return nil
-}
-
-func ProfileField(f *string) string {
- s := []rune(*f)
- if len(s) > maximumProfileFieldLength {
- return string(s[:maximumProfileFieldLength]) // trim profile field to maximum allowed length
+ // Trim each field name + value to maximum allowed length.
+ for _, field := range fields {
+ n := []rune(field.Name)
+ if len(n) > maximumProfileFieldLength {
+ field.Name = string(n[:maximumProfileFieldLength])
+ }
+
+ v := []rune(field.Value)
+ if len(v) > maximumProfileFieldLength {
+ field.Value = string(v[:maximumProfileFieldLength])
+ }
}
- return string(*f)
+ return nil
}
diff --git a/internal/validate/formvalidation_test.go b/internal/validate/formvalidation_test.go
index fa59977b9..9cfa07700 100644
--- a/internal/validate/formvalidation_test.go
+++ b/internal/validate/formvalidation_test.go
@@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
- "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/validate"
)
@@ -268,37 +268,34 @@ func (suite *ValidationTestSuite) TestValidateReason() {
}
}
-func (suite *ValidationTestSuite) TestValidateProfileFieldsCount() {
- noFields := []model.UpdateField{}
- fewFields := []model.UpdateField{{}, {}}
- tooManyFields := []model.UpdateField{{}, {}, {}, {}, {}}
- err := validate.ProfileFieldsCount(tooManyFields)
- if assert.Error(suite.T(), err) {
- assert.Equal(suite.T(), errors.New("cannot have more than 4 profile fields"), err)
- }
-
- err = validate.ProfileFieldsCount(noFields)
- assert.NoError(suite.T(), err)
-
- err = validate.ProfileFieldsCount(fewFields)
- assert.NoError(suite.T(), err)
-}
-
func (suite *ValidationTestSuite) TestValidateProfileField() {
- shortProfileField := "pronouns"
- tooLongProfileField := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui eleifend massa nunc."
- trimmedProfileField := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui "
-
- validated := validate.ProfileField(&shortProfileField)
- assert.Equal(suite.T(), shortProfileField, validated)
-
- validated = validate.ProfileField(&tooLongProfileField)
- assert.Len(suite.T(), validated, 255)
- assert.Equal(suite.T(), trimmedProfileField, validated)
+ var (
+ shortProfileField = "pronouns"
+ tooLongProfileField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui eleifend massa nunc."
+ trimmedProfileField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui "
+ err error
+ )
+
+ okFields := []*gtsmodel.Field{
+ {
+ Name: "example",
+ Value: shortProfileField,
+ },
+ }
+ err = validate.ProfileFields(okFields)
+ suite.NoError(err)
+ suite.Equal(shortProfileField, okFields[0].Value)
- validated = validate.ProfileField(&trimmedProfileField)
- assert.Len(suite.T(), validated, 255)
- assert.Equal(suite.T(), trimmedProfileField, validated)
+ dodgyFields := []*gtsmodel.Field{
+ {
+ Name: "example",
+ Value: tooLongProfileField,
+ },
+ }
+ err = validate.ProfileFields(dodgyFields)
+ suite.NoError(err)
+ suite.Equal(trimmedProfileField, dodgyFields[0].Value)
+ suite.Len(dodgyFields[0].Value, 255)
}
func TestValidationTestSuite(t *testing.T) {