pass context to ParseNotificationTypes

This commit is contained in:
tobi 2024-11-27 13:20:52 +01:00
commit ece83aadcb
2 changed files with 32 additions and 27 deletions

View file

@ -18,11 +18,14 @@
package notifications package notifications
import ( import (
"context"
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util" apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/superseriousbusiness/gotosocial/internal/paging"
) )
@ -161,12 +164,13 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) {
return return
} }
ctx := c.Request.Context()
resp, errWithCode := m.processor.Timeline().NotificationsGet( resp, errWithCode := m.processor.Timeline().NotificationsGet(
c.Request.Context(), ctx,
authed, authed,
page, page,
apiutil.ParseNotificationTypes(c.QueryArray(TypesKey)), // Include types. ParseNotificationTypes(ctx, c.QueryArray(TypesKey)), // Include types.
apiutil.ParseNotificationTypes(c.QueryArray(ExcludeTypesKey)), // Exclude types. ParseNotificationTypes(ctx, c.QueryArray(ExcludeTypesKey)), // Exclude types.
) )
if errWithCode != nil { if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
@ -179,3 +183,28 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) {
apiutil.JSON(c, http.StatusOK, resp.Items) apiutil.JSON(c, http.StatusOK, resp.Items)
} }
// ParseNotificationTypes converts the given slice of string values
// to gtsmodel notification types, logging + skipping unknown types.
func ParseNotificationTypes(
ctx context.Context,
values []string,
) []gtsmodel.NotificationType {
if len(values) == 0 {
return nil
}
ntypes := make([]gtsmodel.NotificationType, 0, len(values))
for _, value := range values {
ntype := gtsmodel.NewNotificationType(value)
if ntype == gtsmodel.NotificationUnknown {
// Type we don't know about (yet), log and ignore it.
log.Debugf(ctx, "ignoring unknown type %s", value)
continue
}
ntypes = append(ntypes, ntype)
}
return ntypes
}

View file

@ -23,8 +23,6 @@ import (
"strings" "strings"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
) )
const ( const (
@ -218,28 +216,6 @@ func ParseInteractionReblogs(value string, defaultValue bool) (bool, gtserror.Wi
return parseBool(value, defaultValue, InteractionReblogsKey) return parseBool(value, defaultValue, InteractionReblogsKey)
} }
// ParseNotificationTypes converts the given slice of string values
// to gtsmodel notification types, logging + skipping unknown types.
func ParseNotificationTypes(values []string) []gtsmodel.NotificationType {
if len(values) == 0 {
return nil
}
ntypes := make([]gtsmodel.NotificationType, 0, len(values))
for _, value := range values {
ntype := gtsmodel.NewNotificationType(value)
if ntype == gtsmodel.NotificationUnknown {
// Type we don't know about (yet), log and ignore it.
log.Debugf(nil, "ignoring unknown notification type %s", value)
continue
}
ntypes = append(ntypes, ntype)
}
return ntypes
}
/* /*
Parse functions for *REQUIRED* parameters. Parse functions for *REQUIRED* parameters.
*/ */