From 759df1240adf39348eaa836ae3c04bdfd02977b3 Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 21 Nov 2024 17:03:53 +0000 Subject: [PATCH] convert statuses.visibility and notifications.notification_type columns from type string -> int for performance / space savings --- internal/ap/extract.go | 2 +- .../client/notifications/notificationsget.go | 16 +- internal/api/util/parsequery.go | 47 +++++ internal/db/bundb/account_test.go | 2 +- .../20240620074530_interaction_policy.go | 12 +- .../status.go | 93 ++++---- .../20241121121623_enum_strings_to_ints.go | 198 ++++++++++++++++++ .../notification.go | 57 +++++ .../status.go | 95 +++++++++ internal/db/bundb/notification.go | 6 +- internal/db/bundb/relationship_follow_req.go | 8 +- internal/db/notification.go | 4 +- internal/gtsmodel/interactionpolicy.go | 2 +- internal/gtsmodel/notification.go | 59 ++++-- internal/gtsmodel/status.go | 55 +++-- internal/processing/preferences.go | 2 +- internal/processing/status/create.go | 2 +- internal/processing/timeline/notification.go | 4 +- internal/processing/workers/surfacenotify.go | 2 +- internal/typeutils/frontendtointernal.go | 2 +- internal/typeutils/internaltofrontend.go | 2 +- 21 files changed, 586 insertions(+), 84 deletions(-) create mode 100644 internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go create mode 100644 internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/notification.go create mode 100644 internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/status.go diff --git a/internal/ap/extract.go b/internal/ap/extract.go index f5486a051..543ee8dca 100644 --- a/internal/ap/extract.go +++ b/internal/ap/extract.go @@ -1027,7 +1027,7 @@ func ExtractVisibility(addressable Addressable, actorFollowersURI string) (gtsmo ) if len(to) == 0 && len(cc) == 0 { - return "", gtserror.Newf("message wasn't TO or CC anyone") + return 0, gtserror.Newf("message wasn't TO or CC anyone") } // Assume most restrictive visibility, 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. */ diff --git a/internal/db/bundb/account_test.go b/internal/db/bundb/account_test.go index 2caffdeb1..7dcc0f9e7 100644 --- a/internal/db/bundb/account_test.go +++ b/internal/db/bundb/account_test.go @@ -106,7 +106,7 @@ func (suite *AccountTestSuite) populateTestStatus(testAccountKey string, status status.URI = fmt.Sprintf("http://localhost:8080/users/%s/statuses/%s", testAccount.Username, status.ID) status.Local = util.Ptr(true) - if status.Visibility == "" { + if status.Visibility == 0 { status.Visibility = gtsmodel.VisibilityDefault } if status.ActivityStreamsType == "" { diff --git a/internal/db/bundb/migrations/20240620074530_interaction_policy.go b/internal/db/bundb/migrations/20240620074530_interaction_policy.go index bbc75d9ec..ea4320f01 100644 --- a/internal/db/bundb/migrations/20240620074530_interaction_policy.go +++ b/internal/db/bundb/migrations/20240620074530_interaction_policy.go @@ -20,6 +20,7 @@ import ( "context" + new_gtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" oldmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20240620074530_interaction_policy" @@ -165,7 +166,16 @@ type spec struct { // to new version of interaction policy. for _, oldStatus := range oldStatuses { // Start with default policy for this visibility. - v := gtsmodel.Visibility(oldStatus.Visibility) + v := func() gtsmodel.Visibility { + return map[oldmodel.Visibility]gtsmodel.Visibility{ + oldmodel.VisibilityNone: new_gtsmodel.VisibilityNone, + oldmodel.VisibilityPublic: new_gtsmodel.VisibilityPublic, + oldmodel.VisibilityUnlocked: new_gtsmodel.VisibilityUnlocked, + oldmodel.VisibilityFollowersOnly: new_gtsmodel.VisibilityFollowersOnly, + oldmodel.VisibilityMutualsOnly: new_gtsmodel.VisibilityMutualsOnly, + oldmodel.VisibilityDirect: new_gtsmodel.VisibilityDirect, + }[oldStatus.Visibility] + }() policy := gtsmodel.DefaultInteractionPolicyFor(v) if !*oldStatus.Likeable { diff --git a/internal/db/bundb/migrations/20240620074530_interaction_policy/status.go b/internal/db/bundb/migrations/20240620074530_interaction_policy/status.go index ae96d047d..615c81b66 100644 --- a/internal/db/bundb/migrations/20240620074530_interaction_policy/status.go +++ b/internal/db/bundb/migrations/20240620074530_interaction_policy/status.go @@ -22,40 +22,61 @@ ) type Status struct { - ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` - CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` - UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` - FetchedAt time.Time `bun:"type:timestamptz,nullzero"` - PinnedAt time.Time `bun:"type:timestamptz,nullzero"` - URI string `bun:",unique,nullzero,notnull"` - URL string `bun:",nullzero"` - Content string `bun:""` - AttachmentIDs []string `bun:"attachments,array"` - TagIDs []string `bun:"tags,array"` - MentionIDs []string `bun:"mentions,array"` - EmojiIDs []string `bun:"emojis,array"` - Local *bool `bun:",nullzero,notnull,default:false"` - AccountID string `bun:"type:CHAR(26),nullzero,notnull"` - AccountURI string `bun:",nullzero,notnull"` - InReplyToID string `bun:"type:CHAR(26),nullzero"` - InReplyToURI string `bun:",nullzero"` - InReplyToAccountID string `bun:"type:CHAR(26),nullzero"` - InReplyTo *Status `bun:"-"` - BoostOfID string `bun:"type:CHAR(26),nullzero"` - BoostOfURI string `bun:"-"` - BoostOfAccountID string `bun:"type:CHAR(26),nullzero"` - BoostOf *Status `bun:"-"` - ThreadID string `bun:"type:CHAR(26),nullzero"` - PollID string `bun:"type:CHAR(26),nullzero"` - ContentWarning string `bun:",nullzero"` - Visibility string `bun:",nullzero,notnull"` - Sensitive *bool `bun:",nullzero,notnull,default:false"` - Language string `bun:",nullzero"` - CreatedWithApplicationID string `bun:"type:CHAR(26),nullzero"` - ActivityStreamsType string `bun:",nullzero,notnull"` - Text string `bun:""` - Federated *bool `bun:",notnull"` - Boostable *bool `bun:",notnull"` - Replyable *bool `bun:",notnull"` - Likeable *bool `bun:",notnull"` + ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` + CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` + UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` + FetchedAt time.Time `bun:"type:timestamptz,nullzero"` + PinnedAt time.Time `bun:"type:timestamptz,nullzero"` + URI string `bun:",unique,nullzero,notnull"` + URL string `bun:",nullzero"` + Content string `bun:""` + AttachmentIDs []string `bun:"attachments,array"` + TagIDs []string `bun:"tags,array"` + MentionIDs []string `bun:"mentions,array"` + EmojiIDs []string `bun:"emojis,array"` + Local *bool `bun:",nullzero,notnull,default:false"` + AccountID string `bun:"type:CHAR(26),nullzero,notnull"` + AccountURI string `bun:",nullzero,notnull"` + InReplyToID string `bun:"type:CHAR(26),nullzero"` + InReplyToURI string `bun:",nullzero"` + InReplyToAccountID string `bun:"type:CHAR(26),nullzero"` + InReplyTo *Status `bun:"-"` + BoostOfID string `bun:"type:CHAR(26),nullzero"` + BoostOfURI string `bun:"-"` + BoostOfAccountID string `bun:"type:CHAR(26),nullzero"` + BoostOf *Status `bun:"-"` + ThreadID string `bun:"type:CHAR(26),nullzero"` + PollID string `bun:"type:CHAR(26),nullzero"` + ContentWarning string `bun:",nullzero"` + Visibility Visibility `bun:",nullzero,notnull"` + Sensitive *bool `bun:",nullzero,notnull,default:false"` + Language string `bun:",nullzero"` + CreatedWithApplicationID string `bun:"type:CHAR(26),nullzero"` + ActivityStreamsType string `bun:",nullzero,notnull"` + Text string `bun:""` + Federated *bool `bun:",notnull"` + Boostable *bool `bun:",notnull"` + Replyable *bool `bun:",notnull"` + Likeable *bool `bun:",notnull"` } + +// Visibility represents the visibility granularity of a status. +type Visibility string + +const ( + // VisibilityNone means nobody can see this. + // It's only used for web status visibility. + VisibilityNone Visibility = "none" + // VisibilityPublic means this status will be visible to everyone on all timelines. + VisibilityPublic Visibility = "public" + // VisibilityUnlocked means this status will be visible to everyone, but will only show on home timeline to followers, and in lists. + VisibilityUnlocked Visibility = "unlocked" + // VisibilityFollowersOnly means this status is viewable to followers only. + VisibilityFollowersOnly Visibility = "followers_only" + // VisibilityMutualsOnly means this status is visible to mutual followers only. + VisibilityMutualsOnly Visibility = "mutuals_only" + // VisibilityDirect means this status is visible only to mentioned recipients. + VisibilityDirect Visibility = "direct" + // VisibilityDefault is used when no other setting can be found. + VisibilityDefault Visibility = VisibilityUnlocked +) diff --git a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go new file mode 100644 index 000000000..6fc024eac --- /dev/null +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints.go @@ -0,0 +1,198 @@ +// 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 migrations + +import ( + "context" + "errors" + + old_gtsmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints" + new_gtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/log" + + "github.com/uptrace/bun" +) + +func init() { + up := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + var visIndices = []struct { + name string + cols []string + order string + }{ + { + name: "statuses_visibility_idx", + cols: []string{"visibility"}, + order: "", + }, + { + name: "statuses_profile_web_view_idx", + cols: []string{"account_id", "visibility"}, + order: "id DESC", + }, + { + name: "statuses_public_timeline_idx", + cols: []string{"visibility"}, + order: "id DESC", + }, + } + + // Before making changes to the visibility col + // we must drop all indices that rely on it. + for _, index := range visIndices { + if _, err := tx.NewDropIndex(). + Index(index.name). + Exec(ctx); err != nil { + return err + } + } + + // Now migrate old visibility column type over to new type. + if err := convertEnums(ctx, tx, "statuses", "visibility", + map[old_gtsmodel.Visibility]new_gtsmodel.Visibility{ + old_gtsmodel.VisibilityNone: new_gtsmodel.VisibilityNone, + old_gtsmodel.VisibilityPublic: new_gtsmodel.VisibilityPublic, + old_gtsmodel.VisibilityUnlocked: new_gtsmodel.VisibilityUnlocked, + old_gtsmodel.VisibilityFollowersOnly: new_gtsmodel.VisibilityFollowersOnly, + old_gtsmodel.VisibilityMutualsOnly: new_gtsmodel.VisibilityMutualsOnly, + old_gtsmodel.VisibilityDirect: new_gtsmodel.VisibilityDirect, + }, nil); err != nil { + return err + } + + // Recreate the visibility indices. + for _, index := range visIndices { + q := tx.NewCreateIndex(). + Table("statuses"). + Index(index.name). + Column(index.cols...) + if index.order != "" { + q = q.ColumnExpr(index.order) + } + if _, err := q.Exec(ctx); err != nil { + return err + } + } + + // Migrate over old notifications table column over to new column type. + if err := convertEnums(ctx, tx, "notifications", "notification_type", //nolint:revive + map[old_gtsmodel.NotificationType]new_gtsmodel.NotificationType{ + old_gtsmodel.NotificationFollow: new_gtsmodel.NotificationFollow, + old_gtsmodel.NotificationFollowRequest: new_gtsmodel.NotificationFollowRequest, + old_gtsmodel.NotificationMention: new_gtsmodel.NotificationMention, + old_gtsmodel.NotificationReblog: new_gtsmodel.NotificationReblog, + old_gtsmodel.NotificationFave: new_gtsmodel.NotificationFave, + old_gtsmodel.NotificationPoll: new_gtsmodel.NotificationPoll, + old_gtsmodel.NotificationStatus: new_gtsmodel.NotificationStatus, + old_gtsmodel.NotificationSignup: new_gtsmodel.NotificationSignup, + old_gtsmodel.NotificationPendingFave: new_gtsmodel.NotificationPendingFave, + old_gtsmodel.NotificationPendingReply: new_gtsmodel.NotificationPendingReply, + old_gtsmodel.NotificationPendingReblog: new_gtsmodel.NotificationPendingReblog, + }, nil); err != nil { + return err + } + + return nil + }) + } + + down := func(ctx context.Context, db *bun.DB) error { + return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + return nil + }) + } + + if err := Migrations.Register(up, down); err != nil { + panic(err) + } +} + +// convertEnums performs a transaction that converts +// a table's column of our old-style enums (strings) to +// more performant and space-saving integer types. +func convertEnums[OldType ~string, NewType ~int]( + ctx context.Context, + tx bun.Tx, + table string, + column string, + mapping map[OldType]NewType, + defaultValue *NewType, +) error { + if len(mapping) == 0 { + return errors.New("empty mapping") + } + + // Generate new column name. + newColumn := column + "_new" + + log.Infof(ctx, "converting %s.%s enums; "+ + "this may take a while, please don't interrupt!", + table, column, + ) + + var columnExpr string + var columnArgs []any + + // Build new column expr with args. + columnExpr = "? INTEGER NOT NULL" + columnArgs = []any{bun.Ident(newColumn)} + if defaultValue != nil { + columnExpr += " DEFAULT ?" + columnArgs = append(columnArgs, *defaultValue) + } + + // Add new column to database. + if _, err := tx.NewAddColumn(). + Table(table). + ColumnExpr(columnExpr, columnArgs...). + Exec(ctx); err != nil { + return err + } + + // Update existing values via mapping. + for old, new := range mapping { + if _, err := tx.NewUpdate(). + Table(table). + Where("? = ?", bun.Ident(column), old). + Set("? = ?", bun.Ident(newColumn), new). + Exec(ctx); err != nil { + return err + } + } + + // Drop the old column from table. + if _, err := tx.NewDropColumn(). + Table(table). + ColumnExpr("?", bun.Ident(column)). + Exec(ctx); err != nil { + return err + } + + // Rename new to old name. + if _, err := tx.NewRaw( + "ALTER TABLE ? RENAME COLUMN ? TO ?", + bun.Ident(table), + bun.Ident(newColumn), + bun.Ident(column), + ).Exec(ctx); err != nil { + return err + } + + return nil +} diff --git a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/notification.go b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/notification.go new file mode 100644 index 000000000..77166a35d --- /dev/null +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/notification.go @@ -0,0 +1,57 @@ +// 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 gtsmodel + +import ( + "time" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +// Notification models an alert/notification sent to an account about something like a reblog, like, new follow request, etc. +type Notification struct { + ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database + CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created + UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated + NotificationType NotificationType `bun:",nullzero,notnull"` // Type of this notification + TargetAccountID string `bun:"type:CHAR(26),nullzero,notnull"` // ID of the account targeted by the notification (ie., who will receive the notification?) + TargetAccount *gtsmodel.Account `bun:"-"` // Account corresponding to TargetAccountID. Can be nil, always check first + select using ID if necessary. + OriginAccountID string `bun:"type:CHAR(26),nullzero,notnull"` // ID of the account that performed the action that created the notification. + OriginAccount *gtsmodel.Account `bun:"-"` // Account corresponding to OriginAccountID. Can be nil, always check first + select using ID if necessary. + StatusID string `bun:"type:CHAR(26),nullzero"` // If the notification pertains to a status, what is the database ID of that status? + Status *Status `bun:"-"` // Status corresponding to StatusID. Can be nil, always check first + select using ID if necessary. + Read *bool `bun:",nullzero,notnull,default:false"` // Notification has been seen/read +} + +// NotificationType describes the reason/type of this notification. +type NotificationType string + +// Notification Types +const ( + NotificationFollow NotificationType = "follow" // NotificationFollow -- someone followed you + NotificationFollowRequest NotificationType = "follow_request" // NotificationFollowRequest -- someone requested to follow you + NotificationMention NotificationType = "mention" // NotificationMention -- someone mentioned you in their status + NotificationReblog NotificationType = "reblog" // NotificationReblog -- someone boosted one of your statuses + NotificationFave NotificationType = "favourite" // NotificationFave -- someone faved/liked one of your statuses + NotificationPoll NotificationType = "poll" // NotificationPoll -- a poll you voted in or created has ended + NotificationStatus NotificationType = "status" // NotificationStatus -- someone you enabled notifications for has posted a status. + NotificationSignup NotificationType = "admin.sign_up" // NotificationSignup -- someone has submitted a new account sign-up to the instance. + NotificationPendingFave NotificationType = "pending.favourite" // Someone has faved a status of yours, which requires approval by you. + NotificationPendingReply NotificationType = "pending.reply" // Someone has replied to a status of yours, which requires approval by you. + NotificationPendingReblog NotificationType = "pending.reblog" // Someone has boosted a status of yours, which requires approval by you. +) diff --git a/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/status.go b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/status.go new file mode 100644 index 000000000..38583c7fc --- /dev/null +++ b/internal/db/bundb/migrations/20241121121623_enum_strings_to_ints/status.go @@ -0,0 +1,95 @@ +// 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 gtsmodel + +import ( + "time" + + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +// Status represents a user-created 'post' or 'status' in the database, either remote or local +type Status struct { + ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database + CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created + UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated + FetchedAt time.Time `bun:"type:timestamptz,nullzero"` // when was item (remote) last fetched. + PinnedAt time.Time `bun:"type:timestamptz,nullzero"` // Status was pinned by owning account at this time. + URI string `bun:",unique,nullzero,notnull"` // activitypub URI of this status + URL string `bun:",nullzero"` // web url for viewing this status + Content string `bun:""` // content of this status; likely html-formatted but not guaranteed + AttachmentIDs []string `bun:"attachments,array"` // Database IDs of any media attachments associated with this status + Attachments []*gtsmodel.MediaAttachment `bun:"attached_media,rel:has-many"` // Attachments corresponding to attachmentIDs + TagIDs []string `bun:"tags,array"` // Database IDs of any tags used in this status + Tags []*gtsmodel.Tag `bun:"attached_tags,m2m:status_to_tags"` // Tags corresponding to tagIDs. https://bun.uptrace.dev/guide/relations.html#many-to-many-relation + MentionIDs []string `bun:"mentions,array"` // Database IDs of any mentions in this status + Mentions []*gtsmodel.Mention `bun:"attached_mentions,rel:has-many"` // Mentions corresponding to mentionIDs + EmojiIDs []string `bun:"emojis,array"` // Database IDs of any emojis used in this status + Emojis []*gtsmodel.Emoji `bun:"attached_emojis,m2m:status_to_emojis"` // Emojis corresponding to emojiIDs. https://bun.uptrace.dev/guide/relations.html#many-to-many-relation + Local *bool `bun:",nullzero,notnull,default:false"` // is this status from a local account? + AccountID string `bun:"type:CHAR(26),nullzero,notnull"` // which account posted this status? + Account *gtsmodel.Account `bun:"rel:belongs-to"` // account corresponding to accountID + AccountURI string `bun:",nullzero,notnull"` // activitypub uri of the owner of this status + InReplyToID string `bun:"type:CHAR(26),nullzero"` // id of the status this status replies to + InReplyToURI string `bun:",nullzero"` // activitypub uri of the status this status is a reply to + InReplyToAccountID string `bun:"type:CHAR(26),nullzero"` // id of the account that this status replies to + InReplyTo *Status `bun:"-"` // status corresponding to inReplyToID + InReplyToAccount *gtsmodel.Account `bun:"rel:belongs-to"` // account corresponding to inReplyToAccountID + BoostOfID string `bun:"type:CHAR(26),nullzero"` // id of the status this status is a boost of + BoostOfURI string `bun:"-"` // URI of the status this status is a boost of; field not inserted in the db, just for dereferencing purposes. + BoostOfAccountID string `bun:"type:CHAR(26),nullzero"` // id of the account that owns the boosted status + BoostOf *Status `bun:"-"` // status that corresponds to boostOfID + BoostOfAccount *gtsmodel.Account `bun:"rel:belongs-to"` // account that corresponds to boostOfAccountID + ThreadID string `bun:"type:CHAR(26),nullzero"` // id of the thread to which this status belongs; only set for remote statuses if a local account is involved at some point in the thread, otherwise null + PollID string `bun:"type:CHAR(26),nullzero"` // + Poll *gtsmodel.Poll `bun:"-"` // + ContentWarning string `bun:",nullzero"` // cw string for this status + Visibility Visibility `bun:",nullzero,notnull"` // visibility entry for this status + Sensitive *bool `bun:",nullzero,notnull,default:false"` // mark the status as sensitive? + Language string `bun:",nullzero"` // what language is this status written in? + CreatedWithApplicationID string `bun:"type:CHAR(26),nullzero"` // Which application was used to create this status? + CreatedWithApplication *gtsmodel.Application `bun:"rel:belongs-to"` // application corresponding to createdWithApplicationID + ActivityStreamsType string `bun:",nullzero,notnull"` // What is the activitystreams type of this status? See: https://www.w3.org/TR/activitystreams-vocabulary/#object-types. Will probably almost always be Note but who knows!. + Text string `bun:""` // Original text of the status without formatting + Federated *bool `bun:",notnull"` // This status will be federated beyond the local timeline(s) + InteractionPolicy *gtsmodel.InteractionPolicy `bun:""` // InteractionPolicy for this status. If null then the default InteractionPolicy should be assumed for this status's Visibility. Always null for boost wrappers. + PendingApproval *bool `bun:",nullzero,notnull,default:false"` // If true then status is a reply or boost wrapper that must be Approved by the reply-ee or boost-ee before being fully distributed. + PreApproved bool `bun:"-"` // If true, then status is a reply to or boost wrapper of a status on our instance, has permission to do the interaction, and an Accept should be sent out for it immediately. Field not stored in the DB. + ApprovedByURI string `bun:",nullzero"` // URI of an Accept Activity that approves the Announce or Create Activity that this status was/will be attached to. +} + +// Visibility represents the visibility granularity of a status. +type Visibility string + +const ( + // VisibilityNone means nobody can see this. + // It's only used for web status visibility. + VisibilityNone Visibility = "none" + // VisibilityPublic means this status will be visible to everyone on all timelines. + VisibilityPublic Visibility = "public" + // VisibilityUnlocked means this status will be visible to everyone, but will only show on home timeline to followers, and in lists. + VisibilityUnlocked Visibility = "unlocked" + // VisibilityFollowersOnly means this status is viewable to followers only. + VisibilityFollowersOnly Visibility = "followers_only" + // VisibilityMutualsOnly means this status is visible to mutual followers only. + VisibilityMutualsOnly Visibility = "mutuals_only" + // VisibilityDirect means this status is visible only to mentioned recipients. + VisibilityDirect Visibility = "direct" + // VisibilityDefault is used when no other setting can be found. + VisibilityDefault Visibility = VisibilityUnlocked +) diff --git a/internal/db/bundb/notification.go b/internal/db/bundb/notification.go index ef2527637..a20ab7e3f 100644 --- a/internal/db/bundb/notification.go +++ b/internal/db/bundb/notification.go @@ -196,8 +196,8 @@ func (n *notificationDB) GetAccountNotifications( sinceID string, minID string, limit int, - types []string, - excludeTypes []string, + types []gtsmodel.NotificationType, + excludeTypes []gtsmodel.NotificationType, ) ([]*gtsmodel.Notification, error) { // Ensure reasonable if limit < 0 { @@ -303,7 +303,7 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string) return nil } -func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error { +func (n *notificationDB) DeleteNotifications(ctx context.Context, types []gtsmodel.NotificationType, targetAccountID string, originAccountID string) error { if targetAccountID == "" && originAccountID == "" { return gtserror.New("one of targetAccountID or originAccountID must be set") } diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index 030c99c58..f36d626ca 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -265,8 +265,8 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI } // Delete original follow request notification - if err := r.state.DB.DeleteNotifications(ctx, []string{ - string(gtsmodel.NotificationFollowRequest), + if err := r.state.DB.DeleteNotifications(ctx, []gtsmodel.NotificationType{ + gtsmodel.NotificationFollowRequest, }, targetAccountID, sourceAccountID); err != nil { return nil, err } @@ -281,8 +281,8 @@ func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountI } // Delete follow request notification - return r.state.DB.DeleteNotifications(ctx, []string{ - string(gtsmodel.NotificationFollowRequest), + return r.state.DB.DeleteNotifications(ctx, []gtsmodel.NotificationType{ + gtsmodel.NotificationFollowRequest, }, targetAccountID, sourceAccountID) } diff --git a/internal/db/notification.go b/internal/db/notification.go index deb58835a..c962023be 100644 --- a/internal/db/notification.go +++ b/internal/db/notification.go @@ -29,7 +29,7 @@ type Notification interface { // // Returned notifications will be ordered ID descending (ie., highest/newest to lowest/oldest). // If types is empty, *all* notification types will be included. - GetAccountNotifications(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, types []string, excludeTypes []string) ([]*gtsmodel.Notification, error) + GetAccountNotifications(ctx context.Context, accountID string, maxID string, sinceID string, minID string, limit int, types []gtsmodel.NotificationType, excludeTypes []gtsmodel.NotificationType) ([]*gtsmodel.Notification, error) // GetNotificationByID returns one notification according to its id. GetNotificationByID(ctx context.Context, id string) (*gtsmodel.Notification, error) @@ -64,7 +64,7 @@ type Notification interface { // originate from originAccountID will be deleted. // // At least one parameter must not be an empty string. - DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error + DeleteNotifications(ctx context.Context, types []gtsmodel.NotificationType, targetAccountID string, originAccountID string) error // DeleteNotificationsForStatus deletes all notifications that relate to // the given statusID. This function is useful when a status has been deleted, diff --git a/internal/gtsmodel/interactionpolicy.go b/internal/gtsmodel/interactionpolicy.go index d8d890e69..7fcafc80d 100644 --- a/internal/gtsmodel/interactionpolicy.go +++ b/internal/gtsmodel/interactionpolicy.go @@ -224,7 +224,7 @@ func DefaultInteractionPolicyFor(v Visibility) *InteractionPolicy { case VisibilityDirect: return DefaultInteractionPolicyDirect() default: - panic("visibility " + v + " not recognized") + panic("invalid visibility") } } diff --git a/internal/gtsmodel/notification.go b/internal/gtsmodel/notification.go index 5cf6b061a..88d5247be 100644 --- a/internal/gtsmodel/notification.go +++ b/internal/gtsmodel/notification.go @@ -34,20 +34,51 @@ type Notification struct { Read *bool `bun:",nullzero,notnull,default:false"` // Notification has been seen/read } -// NotificationType describes the reason/type of this notification. -type NotificationType string +// NotificationType describes the +// reason/type of this notification. +type NotificationType int -// Notification Types const ( - NotificationFollow NotificationType = "follow" // NotificationFollow -- someone followed you - NotificationFollowRequest NotificationType = "follow_request" // NotificationFollowRequest -- someone requested to follow you - NotificationMention NotificationType = "mention" // NotificationMention -- someone mentioned you in their status - NotificationReblog NotificationType = "reblog" // NotificationReblog -- someone boosted one of your statuses - NotificationFave NotificationType = "favourite" // NotificationFave -- someone faved/liked one of your statuses - NotificationPoll NotificationType = "poll" // NotificationPoll -- a poll you voted in or created has ended - NotificationStatus NotificationType = "status" // NotificationStatus -- someone you enabled notifications for has posted a status. - NotificationSignup NotificationType = "admin.sign_up" // NotificationSignup -- someone has submitted a new account sign-up to the instance. - NotificationPendingFave NotificationType = "pending.favourite" // Someone has faved a status of yours, which requires approval by you. - NotificationPendingReply NotificationType = "pending.reply" // Someone has replied to a status of yours, which requires approval by you. - NotificationPendingReblog NotificationType = "pending.reblog" // Someone has boosted a status of yours, which requires approval by you. + // Notification Types + NotificationFollow NotificationType = 1 // NotificationFollow -- someone followed you + NotificationFollowRequest NotificationType = 2 // NotificationFollowRequest -- someone requested to follow you + NotificationMention NotificationType = 3 // NotificationMention -- someone mentioned you in their status + NotificationReblog NotificationType = 4 // NotificationReblog -- someone boosted one of your statuses + NotificationFave NotificationType = 5 // NotificationFave -- someone faved/liked one of your statuses + NotificationPoll NotificationType = 6 // NotificationPoll -- a poll you voted in or created has ended + NotificationStatus NotificationType = 7 // NotificationStatus -- someone you enabled notifications for has posted a status. + NotificationSignup NotificationType = 8 // NotificationSignup -- someone has submitted a new account sign-up to the instance. + NotificationPendingFave NotificationType = 9 // Someone has faved a status of yours, which requires approval by you. + NotificationPendingReply NotificationType = 10 // Someone has replied to a status of yours, which requires approval by you. + NotificationPendingReblog NotificationType = 11 // Someone has boosted a status of yours, which requires approval by you. ) + +// String returns a stringified, frontend API compatible form of NotificationType. +func (t NotificationType) String() string { + switch t { + case NotificationFollow: + return "follow" + case NotificationFollowRequest: + return "follow_request" + case NotificationMention: + return "mention" + case NotificationReblog: + return "reblog" + case NotificationFave: + return "favourite" + case NotificationPoll: + return "poll" + case NotificationStatus: + return "status" + case NotificationSignup: + return "admin.sign_up" + case NotificationPendingFave: + return "pending.favourite" + case NotificationPendingReply: + return "pending.reply" + case NotificationPendingReblog: + return "pending.reblog" + default: + panic("invalid notification type") + } +} diff --git a/internal/gtsmodel/status.go b/internal/gtsmodel/status.go index 91c0ada61..245b06fcd 100644 --- a/internal/gtsmodel/status.go +++ b/internal/gtsmodel/status.go @@ -263,27 +263,58 @@ type StatusToEmoji struct { Emoji *Emoji `bun:"rel:belongs-to"` } -// Visibility represents the visibility granularity of a status. -type Visibility string +// Visibility represents the +// visibility granularity of a status. +type Visibility int const ( // VisibilityNone means nobody can see this. // It's only used for web status visibility. - VisibilityNone Visibility = "none" - // VisibilityPublic means this status will be visible to everyone on all timelines. - VisibilityPublic Visibility = "public" - // VisibilityUnlocked means this status will be visible to everyone, but will only show on home timeline to followers, and in lists. - VisibilityUnlocked Visibility = "unlocked" + VisibilityNone Visibility = 1 + + // VisibilityPublic means this status will + // be visible to everyone on all timelines. + VisibilityPublic Visibility = 2 + + // VisibilityUnlocked means this status will be visible to everyone, + // but will only show on home timeline to followers, and in lists. + VisibilityUnlocked Visibility = 3 + // VisibilityFollowersOnly means this status is viewable to followers only. - VisibilityFollowersOnly Visibility = "followers_only" - // VisibilityMutualsOnly means this status is visible to mutual followers only. - VisibilityMutualsOnly Visibility = "mutuals_only" - // VisibilityDirect means this status is visible only to mentioned recipients. - VisibilityDirect Visibility = "direct" + VisibilityFollowersOnly Visibility = 4 + + // VisibilityMutualsOnly means this status + // is visible to mutual followers only. + VisibilityMutualsOnly Visibility = 5 + + // VisibilityDirect means this status is + // visible only to mentioned recipients. + VisibilityDirect Visibility = 6 + // VisibilityDefault is used when no other setting can be found. VisibilityDefault Visibility = VisibilityUnlocked ) +// String returns a stringified, frontend API compatible form of Visibility. +func (v Visibility) String() string { + switch v { + case VisibilityNone: + return "none" + case VisibilityPublic: + return "public" + case VisibilityUnlocked: + return "unlocked" + case VisibilityFollowersOnly: + return "followers_only" + case VisibilityMutualsOnly: + return "mutuals_only" + case VisibilityDirect: + return "direct" + default: + panic("invalid visibility") + } +} + // Content models the simple string content // of a status along with its ContentMap, // which contains content entries keyed by diff --git a/internal/processing/preferences.go b/internal/processing/preferences.go index 0a5f566ae..fb445ec5b 100644 --- a/internal/processing/preferences.go +++ b/internal/processing/preferences.go @@ -46,7 +46,7 @@ func (p *Processor) PreferencesGet(ctx context.Context, accountID string) (*apim func mastoPrefVisibility(vis gtsmodel.Visibility) string { switch vis { case gtsmodel.VisibilityPublic, gtsmodel.VisibilityDirect: - return string(vis) + return vis.String() case gtsmodel.VisibilityUnlocked: return "unlisted" default: diff --git a/internal/processing/status/create.go b/internal/processing/status/create.go index fbc2dadf7..ef8f8aa56 100644 --- a/internal/processing/status/create.go +++ b/internal/processing/status/create.go @@ -372,7 +372,7 @@ func (p *Processor) processVisibility( // Fall back to account default, set // this back on the form for later use. - case accountDefaultVis != "": + case accountDefaultVis != 0: status.Visibility = accountDefaultVis form.Visibility = p.converter.VisToAPIVis(ctx, accountDefaultVis) diff --git a/internal/processing/timeline/notification.go b/internal/processing/timeline/notification.go index 34e6d865d..92dbf851f 100644 --- a/internal/processing/timeline/notification.go +++ b/internal/processing/timeline/notification.go @@ -41,8 +41,8 @@ func (p *Processor) NotificationsGet( sinceID string, minID string, limit int, - types []string, - excludeTypes []string, + types []gtsmodel.NotificationType, + excludeTypes []gtsmodel.NotificationType, ) (*apimodel.PageableResponse, gtserror.WithCode) { notifs, err := p.state.DB.GetAccountNotifications( ctx, diff --git a/internal/processing/workers/surfacenotify.go b/internal/processing/workers/surfacenotify.go index 872ccca65..1520d2ec0 100644 --- a/internal/processing/workers/surfacenotify.go +++ b/internal/processing/workers/surfacenotify.go @@ -542,7 +542,7 @@ func getNotifyLockURI( ) string { builder := strings.Builder{} builder.WriteString("notification:?") - builder.WriteString("type=" + string(notificationType)) + builder.WriteString("type=" + notificationType.String()) builder.WriteString("&target=" + targetAccount.URI) builder.WriteString("&origin=" + originAccount.URI) if statusID != "" { diff --git a/internal/typeutils/frontendtointernal.go b/internal/typeutils/frontendtointernal.go index 1f7d1877e..82957ee05 100644 --- a/internal/typeutils/frontendtointernal.go +++ b/internal/typeutils/frontendtointernal.go @@ -41,7 +41,7 @@ func APIVisToVis(m apimodel.Visibility) gtsmodel.Visibility { case apimodel.VisibilityNone: return gtsmodel.VisibilityNone } - return "" + return 0 } func APIMarkerNameToMarkerName(m apimodel.MarkerName) gtsmodel.MarkerName { diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 5f919f014..750d4eec4 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -1862,7 +1862,7 @@ func (c *Converter) NotificationToAPINotification( return &apimodel.Notification{ ID: n.ID, - Type: string(n.NotificationType), + Type: n.NotificationType.String(), CreatedAt: util.FormatISO8601(n.CreatedAt), Account: apiAccount, Status: apiStatus,