From 5d374113dfa5dbca2606a405c80637dbeeddcaa3 Mon Sep 17 00:00:00 2001 From: tobi Date: Tue, 5 Nov 2024 12:57:18 +0100 Subject: [PATCH 1/4] [bugfix] Fix setting immediate `expires_at` value on filter endpoints --- internal/api/client/accounts/mute.go | 30 +++------ internal/api/client/filters/v1/validate.go | 39 +++++------ internal/api/client/filters/v2/filterpost.go | 34 +++++----- internal/api/client/filters/v2/filterput.go | 34 +++++----- internal/api/client/statuses/statuscreate.go | 33 ++++----- internal/api/util/parseform.go | 70 ++++++++++++++++++++ 6 files changed, 140 insertions(+), 100 deletions(-) create mode 100644 internal/api/util/parseform.go diff --git a/internal/api/client/accounts/mute.go b/internal/api/client/accounts/mute.go index affb0f0557..04030586e7 100644 --- a/internal/api/client/accounts/mute.go +++ b/internal/api/client/accounts/mute.go @@ -19,9 +19,7 @@ package accounts import ( "errors" - "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -140,28 +138,18 @@ func normalizeCreateUpdateMute(form *apimodel.UserMuteCreateUpdateRequest) error // Apply defaults for missing fields. form.Notifications = util.Ptr(util.PtrOrValue(form.Notifications, false)) - // Normalize mute duration if necessary. - // If we parsed this as JSON, expires_in - // may be either a float64 or a string. - if ei := form.DurationI; ei != nil { - switch e := ei.(type) { - case float64: - form.Duration = util.Ptr(int(e)) - - case string: - duration, err := strconv.Atoi(e) - if err != nil { - return fmt.Errorf("could not parse duration value %s as integer: %w", e, err) - } - - form.Duration = &duration - - default: - return fmt.Errorf("could not parse expires_in type %T as integer", ei) + // Normalize duration if necessary. + if form.DurationI != nil { + // If we parsed this as JSON, duration + // may be either a float64 or a string. + duration, err := apiutil.ParseDuration(form.DurationI, "duration") + if err != nil { + return err } + form.Duration = duration } - // Interpret zero as indefinite duration. + // Ensure no zero duration is set. if form.Duration != nil && *form.Duration == 0 { form.Duration = nil } diff --git a/internal/api/client/filters/v1/validate.go b/internal/api/client/filters/v1/validate.go index cce00fdc4e..9d72e3ffa7 100644 --- a/internal/api/client/filters/v1/validate.go +++ b/internal/api/client/filters/v1/validate.go @@ -19,15 +19,14 @@ package v1 import ( "errors" - "fmt" - "strconv" - "github.com/superseriousbusiness/gotosocial/internal/api/model" + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" "github.com/superseriousbusiness/gotosocial/internal/util" "github.com/superseriousbusiness/gotosocial/internal/validate" ) -func validateNormalizeCreateUpdateFilter(form *model.FilterCreateUpdateRequestV1) error { +func validateNormalizeCreateUpdateFilter(form *apimodel.FilterCreateUpdateRequestV1) error { if err := validate.FilterKeyword(form.Phrase); err != nil { return err } @@ -48,25 +47,23 @@ func validateNormalizeCreateUpdateFilter(form *model.FilterCreateUpdateRequestV1 } // Normalize filter expiry if necessary. - // If we parsed this as JSON, expires_in - // may be either a float64 or a string. - if ei := form.ExpiresInI; ei != nil { - switch e := ei.(type) { - case float64: - form.ExpiresIn = util.Ptr(int(e)) - - case string: - expiresIn, err := strconv.Atoi(e) - if err != nil { - return fmt.Errorf("could not parse expires_in value %s as integer: %w", e, err) - } - - form.ExpiresIn = &expiresIn - - default: - return fmt.Errorf("could not parse expires_in type %T as integer", ei) + if form.ExpiresInI != nil { + // If we parsed this as JSON, expires_in + // may be either a float64 or a string. + var err error + form.ExpiresIn, err = apiutil.ParseDuration( + form.ExpiresInI, + "expires_in", + ) + if err != nil { + return err } } + // Ensure no zero duration is set. + if form.ExpiresIn != nil && *form.ExpiresIn == 0 { + form.ExpiresIn = nil + } + return nil } diff --git a/internal/api/client/filters/v2/filterpost.go b/internal/api/client/filters/v2/filterpost.go index 13270b1e58..27ac93ab7a 100644 --- a/internal/api/client/filters/v2/filterpost.go +++ b/internal/api/client/filters/v2/filterpost.go @@ -18,9 +18,7 @@ package v2 import ( - "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -228,26 +226,24 @@ func validateNormalizeCreateFilter(form *apimodel.FilterCreateRequestV2) error { form.FilterAction = util.Ptr(action) // Normalize filter expiry if necessary. - // If we parsed this as JSON, expires_in - // may be either a float64 or a string. - if ei := form.ExpiresInI; ei != nil { - switch e := ei.(type) { - case float64: - form.ExpiresIn = util.Ptr(int(e)) - - case string: - expiresIn, err := strconv.Atoi(e) - if err != nil { - return fmt.Errorf("could not parse expires_in value %s as integer: %w", e, err) - } - - form.ExpiresIn = &expiresIn - - default: - return fmt.Errorf("could not parse expires_in type %T as integer", ei) + if form.ExpiresInI != nil { + // If we parsed this as JSON, expires_in + // may be either a float64 or a string. + var err error + form.ExpiresIn, err = apiutil.ParseDuration( + form.ExpiresInI, + "expires_in", + ) + if err != nil { + return err } } + // Ensure no zero duration is set. + if form.ExpiresIn != nil && *form.ExpiresIn == 0 { + form.ExpiresIn = nil + } + // Normalize and validate new keywords and statuses. for i, formKeyword := range form.Keywords { if err := validate.FilterKeyword(formKeyword.Keyword); err != nil { diff --git a/internal/api/client/filters/v2/filterput.go b/internal/api/client/filters/v2/filterput.go index 24f7e75673..86e026e155 100644 --- a/internal/api/client/filters/v2/filterput.go +++ b/internal/api/client/filters/v2/filterput.go @@ -19,9 +19,7 @@ package v2 import ( "errors" - "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" @@ -272,26 +270,24 @@ func validateNormalizeUpdateFilter(form *apimodel.FilterUpdateRequestV2) error { } // Normalize filter expiry if necessary. - // If we parsed this as JSON, expires_in - // may be either a float64 or a string. - if ei := form.ExpiresInI; ei != nil { - switch e := ei.(type) { - case float64: - form.ExpiresIn = util.Ptr(int(e)) - - case string: - expiresIn, err := strconv.Atoi(e) - if err != nil { - return fmt.Errorf("could not parse expires_in value %s as integer: %w", e, err) - } - - form.ExpiresIn = &expiresIn - - default: - return fmt.Errorf("could not parse expires_in type %T as integer", ei) + if form.ExpiresInI != nil { + // If we parsed this as JSON, expires_in + // may be either a float64 or a string. + var err error + form.ExpiresIn, err = apiutil.ParseDuration( + form.ExpiresInI, + "expires_in", + ) + if err != nil { + return err } } + // Ensure no zero duration is set. + if form.ExpiresIn != nil && *form.ExpiresIn == 0 { + form.ExpiresIn = nil + } + // Normalize and validate updates. for i, formKeyword := range form.Keywords { if formKeyword.Keyword != nil { diff --git a/internal/api/client/statuses/statuscreate.go b/internal/api/client/statuses/statuscreate.go index 48d11f363e..8198d53587 100644 --- a/internal/api/client/statuses/statuscreate.go +++ b/internal/api/client/statuses/statuscreate.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "net/http" - "strconv" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" @@ -474,25 +473,19 @@ func validateStatusPoll(form *apimodel.StatusCreateRequest) gtserror.WithCode { } // Normalize poll expiry if necessary. - // If we parsed this as JSON, expires_in - // may be either a float64 or a string. - if ei := form.Poll.ExpiresInI; ei != nil { - switch e := ei.(type) { - case float64: - form.Poll.ExpiresIn = int(e) - - case string: - expiresIn, err := strconv.Atoi(e) - if err != nil { - text := fmt.Sprintf("could not parse expires_in value %s as integer: %v", e, err) - return gtserror.NewErrorBadRequest(errors.New(text), text) - } - - form.Poll.ExpiresIn = expiresIn - - default: - text := fmt.Sprintf("could not parse expires_in type %T as integer", ei) - return gtserror.NewErrorBadRequest(errors.New(text), text) + if form.Poll.ExpiresInI != nil { + // If we parsed this as JSON, expires_in + // may be either a float64 or a string. + expiresIn, err := apiutil.ParseDuration( + form.Poll.ExpiresInI, + "expires_in", + ) + if err != nil { + return gtserror.NewErrorBadRequest(err, err.Error()) + } + + if expiresIn != nil { + form.Poll.ExpiresIn = *expiresIn } } diff --git a/internal/api/util/parseform.go b/internal/api/util/parseform.go new file mode 100644 index 0000000000..19e24189f3 --- /dev/null +++ b/internal/api/util/parseform.go @@ -0,0 +1,70 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package util + +import ( + "fmt" + "strconv" +) + +// ParseDuration parses the given raw interface belonging to +// the given fieldName as an integer duration. +// +// Will return nil, nil if rawI is the zero value of its type. +func ParseDuration(rawI any, fieldName string) (*int, error) { + var ( + asInteger int + err error + ) + + switch raw := rawI.(type) { + case float64: + // Submitted as JSON number + // (casts to float64 by default). + asInteger = int(raw) + + case string: + // Submitted as JSON string or form field. + asInteger, err = strconv.Atoi(raw) + if err != nil { + err = fmt.Errorf( + "could not parse %s value %s as integer: %w", + fieldName, raw, err, + ) + } + + default: + // Submitted as god-knows-what. + err = fmt.Errorf( + "could not parse %s type %T as integer", + fieldName, rawI, + ) + } + + if err != nil { + return nil, err + } + + // Someone submitted 0, + // don't point to this. + if asInteger == 0 { + return nil, nil + } + + return &asInteger, nil +} From 7159182be3d393adabe6a947b757bb5793438f87 Mon Sep 17 00:00:00 2001 From: tobi Date: Tue, 5 Nov 2024 12:58:43 +0100 Subject: [PATCH 2/4] update wording --- internal/api/client/accounts/mute.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/api/client/accounts/mute.go b/internal/api/client/accounts/mute.go index 04030586e7..c9a57a3486 100644 --- a/internal/api/client/accounts/mute.go +++ b/internal/api/client/accounts/mute.go @@ -149,7 +149,7 @@ func normalizeCreateUpdateMute(form *apimodel.UserMuteCreateUpdateRequest) error form.Duration = duration } - // Ensure no zero duration is set. + // Interpret zero as indefinite duration. if form.Duration != nil && *form.Duration == 0 { form.Duration = nil } From bac1a08802a95138a23ee85fe026e6bec27d85ae Mon Sep 17 00:00:00 2001 From: tobi Date: Tue, 5 Nov 2024 13:00:33 +0100 Subject: [PATCH 3/4] update wording --- internal/api/client/filters/v1/validate.go | 2 +- internal/api/client/filters/v2/filterpost.go | 2 +- internal/api/client/filters/v2/filterput.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/api/client/filters/v1/validate.go b/internal/api/client/filters/v1/validate.go index 9d72e3ffa7..60823462b7 100644 --- a/internal/api/client/filters/v1/validate.go +++ b/internal/api/client/filters/v1/validate.go @@ -60,7 +60,7 @@ func validateNormalizeCreateUpdateFilter(form *apimodel.FilterCreateUpdateReques } } - // Ensure no zero duration is set. + // Interpret zero as indefinite duration.. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil } diff --git a/internal/api/client/filters/v2/filterpost.go b/internal/api/client/filters/v2/filterpost.go index 27ac93ab7a..9660f2f18d 100644 --- a/internal/api/client/filters/v2/filterpost.go +++ b/internal/api/client/filters/v2/filterpost.go @@ -239,7 +239,7 @@ func validateNormalizeCreateFilter(form *apimodel.FilterCreateRequestV2) error { } } - // Ensure no zero duration is set. + // Interpret zero as indefinite duration.. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil } diff --git a/internal/api/client/filters/v2/filterput.go b/internal/api/client/filters/v2/filterput.go index 86e026e155..3805d4f802 100644 --- a/internal/api/client/filters/v2/filterput.go +++ b/internal/api/client/filters/v2/filterput.go @@ -283,7 +283,7 @@ func validateNormalizeUpdateFilter(form *apimodel.FilterUpdateRequestV2) error { } } - // Ensure no zero duration is set. + // Interpret zero as indefinite duration.. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil } From 7641a2c94d38c70ea228560ef1199da7f67a9e09 Mon Sep 17 00:00:00 2001 From: tobi Date: Tue, 5 Nov 2024 13:01:12 +0100 Subject: [PATCH 4/4] oh my --- internal/api/client/filters/v1/validate.go | 2 +- internal/api/client/filters/v2/filterpost.go | 2 +- internal/api/client/filters/v2/filterput.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/api/client/filters/v1/validate.go b/internal/api/client/filters/v1/validate.go index 60823462b7..9e876c8cfc 100644 --- a/internal/api/client/filters/v1/validate.go +++ b/internal/api/client/filters/v1/validate.go @@ -60,7 +60,7 @@ func validateNormalizeCreateUpdateFilter(form *apimodel.FilterCreateUpdateReques } } - // Interpret zero as indefinite duration.. + // Interpret zero as indefinite duration. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil } diff --git a/internal/api/client/filters/v2/filterpost.go b/internal/api/client/filters/v2/filterpost.go index 9660f2f18d..632c4402f3 100644 --- a/internal/api/client/filters/v2/filterpost.go +++ b/internal/api/client/filters/v2/filterpost.go @@ -239,7 +239,7 @@ func validateNormalizeCreateFilter(form *apimodel.FilterCreateRequestV2) error { } } - // Interpret zero as indefinite duration.. + // Interpret zero as indefinite duration. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil } diff --git a/internal/api/client/filters/v2/filterput.go b/internal/api/client/filters/v2/filterput.go index 3805d4f802..cde03360d3 100644 --- a/internal/api/client/filters/v2/filterput.go +++ b/internal/api/client/filters/v2/filterput.go @@ -283,7 +283,7 @@ func validateNormalizeUpdateFilter(form *apimodel.FilterUpdateRequestV2) error { } } - // Interpret zero as indefinite duration.. + // Interpret zero as indefinite duration. if form.ExpiresIn != nil && *form.ExpiresIn == 0 { form.ExpiresIn = nil }