[bugfix] Allow unsetting filter expiration dates (#3560)

* Regression tests for #3497 (v1 and v2)
* use Nullable type for v2 form.expires_in

---------

Co-authored-by: tobi <tobi.smethurst@protonmail.com>
This commit is contained in:
Vyr Cossont
2024-11-26 08:23:00 -08:00
committed by GitHub
parent 5c818debb2
commit 6a8af42647
15 changed files with 379 additions and 85 deletions

View File

@@ -20,12 +20,13 @@ package util
import (
"fmt"
"strconv"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// ParseDuration parses the given raw interface belonging to
// ParseDuration parses the given raw interface belonging
// 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
@@ -60,11 +61,28 @@ func ParseDuration(rawI any, fieldName string) (*int, error) {
return nil, err
}
// Someone submitted 0,
// don't point to this.
if asInteger == 0 {
return nil, nil
}
return &asInteger, nil
}
// ParseNullableDuration is like ParseDuration, but
// for JSON values that may have been sent as `null`.
//
// IsSpecified should be checked and "true" on the
// given nullable before calling this function.
func ParseNullableDuration(
nullable apimodel.Nullable[any],
fieldName string,
) (*int, error) {
if nullable.IsNull() {
// Was specified as `null`,
// return pointer to zero value.
return util.Ptr(0), nil
}
rawI, err := nullable.Get()
if err != nil {
return nil, err
}
return ParseDuration(rawI, fieldName)
}