From cac9d65029e972af9440ff79a2617d5c524a9d64 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:48:59 +0000 Subject: [performance] convert enum strings to ints (#3558) * convert statuses.visibility and notifications.notification_type columns from type string -> int for performance / space savings * fix test trying to compare string to int * fix instance count query using string literal instead of gtsmodel const type * ensure a default value is always set * also migrate the account settings and sin bin status tables * initialize maps outside loops and place into singular enum mapping creation func * use int16 for enum types * update sinbinstatus creation to be from a snapshot at initial creation * add snapshot of poll type at creation time --- internal/api/client/accounts/accountverify_test.go | 3 +- .../api/client/notifications/notificationsget.go | 16 +++++++- internal/api/util/parsequery.go | 47 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) (limited to 'internal/api') diff --git a/internal/api/client/accounts/accountverify_test.go b/internal/api/client/accounts/accountverify_test.go index af8c2346c..3f67cdefb 100644 --- a/internal/api/client/accounts/accountverify_test.go +++ b/internal/api/client/accounts/accountverify_test.go @@ -28,7 +28,6 @@ import ( "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/api/client/accounts" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/oauth" ) @@ -99,7 +98,7 @@ func (suite *AccountVerifyTestSuite) TestAccountVerifyGet() { suite.Equal(2, apimodelAccount.FollowersCount) suite.Equal(2, apimodelAccount.FollowingCount) suite.Equal(8, apimodelAccount.StatusesCount) - suite.EqualValues(gtsmodel.VisibilityPublic, apimodelAccount.Source.Privacy) + suite.EqualValues(apimodel.VisibilityPublic, apimodelAccount.Source.Privacy) suite.Equal(testAccount.Settings.Language, apimodelAccount.Source.Language) suite.Equal(testAccount.NoteRaw, apimodelAccount.Source.Note) } diff --git a/internal/api/client/notifications/notificationsget.go b/internal/api/client/notifications/notificationsget.go index f7bcf1994..cc3e5bdb7 100644 --- a/internal/api/client/notifications/notificationsget.go +++ b/internal/api/client/notifications/notificationsget.go @@ -164,6 +164,18 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) { limit = int(i) } + types, errWithCode := apiutil.ParseNotificationTypes(c.QueryArray(TypesKey)) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + + exclTypes, errWithCode := apiutil.ParseNotificationTypes(c.QueryArray(ExcludeTypesKey)) + if errWithCode != nil { + apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) + return + } + resp, errWithCode := m.processor.Timeline().NotificationsGet( c.Request.Context(), authed, @@ -171,8 +183,8 @@ func (m *Module) NotificationsGETHandler(c *gin.Context) { c.Query(SinceIDKey), c.Query(MinIDKey), limit, - c.QueryArray(TypesKey), - c.QueryArray(ExcludeTypesKey), + types, + exclTypes, ) if errWithCode != nil { apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1) diff --git a/internal/api/util/parsequery.go b/internal/api/util/parsequery.go index 9f4c02aed..9929524c5 100644 --- a/internal/api/util/parsequery.go +++ b/internal/api/util/parsequery.go @@ -18,11 +18,13 @@ package util import ( + "errors" "fmt" "strconv" "strings" "github.com/superseriousbusiness/gotosocial/internal/gtserror" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) const ( @@ -216,6 +218,51 @@ func ParseInteractionReblogs(value string, defaultValue bool) (bool, gtserror.Wi return parseBool(value, defaultValue, InteractionReblogsKey) } +func ParseNotificationType(value string) (gtsmodel.NotificationType, gtserror.WithCode) { + switch strings.ToLower(value) { + case "follow": + return gtsmodel.NotificationFollow, nil + case "follow_request": + return gtsmodel.NotificationFollowRequest, nil + case "mention": + return gtsmodel.NotificationMention, nil + case "reblog": + return gtsmodel.NotificationReblog, nil + case "favourite": + return gtsmodel.NotificationFave, nil + case "poll": + return gtsmodel.NotificationPoll, nil + case "status": + return gtsmodel.NotificationStatus, nil + case "admin.sign_up": + return gtsmodel.NotificationSignup, nil + case "pending.favourite": + return gtsmodel.NotificationPendingFave, nil + case "pending.reply": + return gtsmodel.NotificationPendingReply, nil + case "pending.reblog": + return gtsmodel.NotificationPendingReblog, nil + default: + text := fmt.Sprintf("unrecognized notification type %s", value) + return 0, gtserror.NewErrorBadRequest(errors.New(text), text) + } +} + +func ParseNotificationTypes(values []string) ([]gtsmodel.NotificationType, gtserror.WithCode) { + if len(values) == 0 { + return nil, nil + } + ntypes := make([]gtsmodel.NotificationType, len(values)) + for i, value := range values { + ntype, errWithCode := ParseNotificationType(value) + if errWithCode != nil { + return nil, errWithCode + } + ntypes[i] = ntype + } + return ntypes, nil +} + /* Parse functions for *REQUIRED* parameters. */ -- cgit v1.2.3