From 0e29f1f5bb68a48d9b837d7f4e0a16370734955b Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Tue, 9 May 2023 12:16:10 +0200 Subject: [feature] Enable federation in/out of profile PropertyValue fields (#1722) Co-authored-by: kim Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> --- internal/validate/formvalidation.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'internal/validate/formvalidation.go') 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 } -- cgit v1.2.3