mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2026-01-03 21:03:16 -06:00
use Nullable type for form.expires_in
This commit is contained in:
parent
7670a7d964
commit
1fa7958ae3
10 changed files with 152 additions and 74 deletions
|
|
@ -46,12 +46,11 @@ func validateNormalizeCreateUpdateFilter(form *apimodel.FilterCreateUpdateReques
|
|||
return errors.New("irreversible aka server-side drop filters are not supported yet")
|
||||
}
|
||||
|
||||
// Normalize filter expiry if necessary.
|
||||
if form.ExpiresInI != nil {
|
||||
// If we parsed this as JSON, expires_in
|
||||
// may be either a float64 or a string.
|
||||
// If `expires_in` was provided
|
||||
// as JSON, then normalize it.
|
||||
if form.ExpiresInI.IsSpecified() {
|
||||
var err error
|
||||
form.ExpiresIn, err = apiutil.ParseDuration(
|
||||
form.ExpiresIn, err = apiutil.ParseNullableDuration(
|
||||
form.ExpiresInI,
|
||||
"expires_in",
|
||||
)
|
||||
|
|
@ -60,10 +59,5 @@ func validateNormalizeCreateUpdateFilter(form *apimodel.FilterCreateUpdateReques
|
|||
}
|
||||
}
|
||||
|
||||
// Interpret zero as indefinite duration.
|
||||
if form.ExpiresIn != nil && *form.ExpiresIn == 0 {
|
||||
form.ExpiresIn = nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,12 +225,11 @@ func validateNormalizeCreateFilter(form *apimodel.FilterCreateRequestV2) error {
|
|||
// Apply defaults for missing fields.
|
||||
form.FilterAction = util.Ptr(action)
|
||||
|
||||
// Normalize filter expiry if necessary.
|
||||
if form.ExpiresInI != nil {
|
||||
// If we parsed this as JSON, expires_in
|
||||
// may be either a float64 or a string.
|
||||
// If `expires_in` was provided
|
||||
// as JSON, then normalize it.
|
||||
if form.ExpiresInI.IsSpecified() {
|
||||
var err error
|
||||
form.ExpiresIn, err = apiutil.ParseDuration(
|
||||
form.ExpiresIn, err = apiutil.ParseNullableDuration(
|
||||
form.ExpiresInI,
|
||||
"expires_in",
|
||||
)
|
||||
|
|
@ -239,11 +238,6 @@ func validateNormalizeCreateFilter(form *apimodel.FilterCreateRequestV2) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Interpret zero as indefinite duration.
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -18,10 +18,7 @@
|
|||
package v2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
|
@ -183,43 +180,12 @@ func (m *Module) FilterPUTHandler(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// If the request is JSON:
|
||||
// Explicitly check whether `expires_in` is a null literal (vs. not being set at all).
|
||||
hasNullExpiresIn := false
|
||||
if c.ContentType() == gin.MIMEJSON {
|
||||
// To do this, we need to read the request body twice, once here and once below for the form, so we buffer it.
|
||||
// If a filter update request is bigger than a megabyte, somebody's messing with us.
|
||||
bodyBytes, err := io.ReadAll(io.LimitReader(c.Request.Body, 1024*1024))
|
||||
if err != nil {
|
||||
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
||||
|
||||
// Partially parse the body as a JSON object.
|
||||
requestJSONObject := map[string]json.RawMessage{}
|
||||
if err := json.Unmarshal(bodyBytes, &requestJSONObject); err != nil {
|
||||
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to parse the `expires_in` field as a literal null.
|
||||
if raw, found := requestJSONObject["expires_in"]; found {
|
||||
hasNullExpiresIn = string(raw) == "null"
|
||||
}
|
||||
}
|
||||
|
||||
form := &apimodel.FilterUpdateRequestV2{}
|
||||
if err := c.ShouldBind(form); err != nil {
|
||||
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
|
||||
return
|
||||
}
|
||||
|
||||
// Interpret a literal null `expires_in` as unsetting the expiration date.
|
||||
if hasNullExpiresIn {
|
||||
form.ExpiresIn = util.Ptr(0)
|
||||
}
|
||||
|
||||
if err := validateNormalizeUpdateFilter(form); err != nil {
|
||||
apiutil.ErrorHandler(c, gtserror.NewErrorUnprocessableEntity(err, err.Error()), m.processor.InstanceGetV1)
|
||||
return
|
||||
|
|
@ -303,12 +269,11 @@ func validateNormalizeUpdateFilter(form *apimodel.FilterUpdateRequestV2) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Normalize filter expiry if necessary.
|
||||
if form.ExpiresInI != nil {
|
||||
// If we parsed this as JSON, expires_in
|
||||
// may be either a float64 or a string.
|
||||
// If `expires_in` was provided
|
||||
// as JSON, then normalize it.
|
||||
if form.ExpiresInI.IsSpecified() {
|
||||
var err error
|
||||
form.ExpiresIn, err = apiutil.ParseDuration(
|
||||
form.ExpiresIn, err = apiutil.ParseNullableDuration(
|
||||
form.ExpiresInI,
|
||||
"expires_in",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue