From c90b83e49efe5dd1300a954ef3495c4589038fc8 Mon Sep 17 00:00:00 2001 From: tobi Date: Fri, 22 Mar 2024 13:53:27 +0100 Subject: [PATCH] don't use separate settings_id --- .../wellknown/webfinger/webfingerget_test.go | 4 ++ internal/cache/db.go | 2 +- internal/cache/size.go | 2 +- internal/db/bundb/account.go | 16 ++++---- internal/db/bundb/admin.go | 13 ++----- .../20240318115336_account_settings.go | 38 +------------------ internal/gtsmodel/account.go | 1 - internal/gtsmodel/accountsettings.go | 3 +- internal/trans/import.go | 7 +++- internal/trans/model/account.go | 3 +- testrig/testmodels.go | 12 ++---- 11 files changed, 30 insertions(+), 71 deletions(-) diff --git a/internal/api/wellknown/webfinger/webfingerget_test.go b/internal/api/wellknown/webfinger/webfingerget_test.go index 258e073a5..84562187d 100644 --- a/internal/api/wellknown/webfinger/webfingerget_test.go +++ b/internal/api/wellknown/webfinger/webfingerget_test.go @@ -117,6 +117,10 @@ func (suite *WebfingerGetTestSuite) funkifyAccountDomain(host string, accountDom suite.FailNow(err.Error()) } + if err := suite.db.PutAccountSettings(context.Background(), >smodel.AccountSettings{AccountID: targetAccount.ID}); err != nil { + suite.FailNow(err.Error()) + } + return targetAccount } diff --git a/internal/cache/db.go b/internal/cache/db.go index eea09e10f..ff38c1d93 100644 --- a/internal/cache/db.go +++ b/internal/cache/db.go @@ -277,7 +277,7 @@ func (c *Caches) initAccountSettings() { c.GTS.AccountSettings.Init(structr.Config[*gtsmodel.AccountSettings]{ Indices: []structr.IndexConfig{ - {Fields: "ID"}, + {Fields: "AccountID"}, }, MaxSize: cap, IgnoreErr: ignoreErrors, diff --git a/internal/cache/size.go b/internal/cache/size.go index 07ef0cba6..080fefea3 100644 --- a/internal/cache/size.go +++ b/internal/cache/size.go @@ -249,7 +249,7 @@ func sizeofAccountNote() uintptr { func sizeofAccountSettings() uintptr { return uintptr(size.Of(>smodel.AccountSettings{ - ID: exampleID, + AccountID: exampleID, CreatedAt: exampleTime, UpdatedAt: exampleTime, Reason: exampleText, diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index fb1d5b327..870c2ff55 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -338,11 +338,11 @@ func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Accou } } - if account.Settings == nil && account.SettingsID != "" { + if account.IsLocal() && account.Settings == nil && !account.IsInstance() { // Account settings not set, fetch from db. account.Settings, err = a.state.DB.GetAccountSettings( ctx, // these are already barebones - account.SettingsID, + account.ID, ) if err != nil { errs.Appendf("error populating account settings: %w", err) @@ -524,7 +524,7 @@ func (a *accountDB) GetAccountCustomCSSByUsername(ctx context.Context, username // Ensure settings populated, in case // barebones context was passed. if account.Settings == nil { - account.Settings, err = a.GetAccountSettings(ctx, account.SettingsID) + account.Settings, err = a.GetAccountSettings(ctx, account.ID) if err != nil { return "", err } @@ -804,24 +804,24 @@ func (a *accountDB) GetAccountWebStatuses(ctx context.Context, accountID string, func (a *accountDB) GetAccountSettings( ctx context.Context, - settingsID string, + accountID string, ) (*gtsmodel.AccountSettings, error) { // Fetch settings from db cache with loader callback. return a.state.Caches.GTS.AccountSettings.LoadOne( - "ID", + "AccountID", func() (*gtsmodel.AccountSettings, error) { // Not cached! Perform database query. var settings gtsmodel.AccountSettings if err := a.db. NewSelect(). Model(&settings). - Where("? = ?", bun.Ident("account_settings.id"), settingsID). + Where("? = ?", bun.Ident("account_settings.account_id"), accountID). Scan(ctx); err != nil { return nil, err } return &settings, nil }, - settingsID, + accountID, ) } @@ -858,7 +858,7 @@ func (a *accountDB) UpdateAccountSettings( NewUpdate(). Model(settings). Column(columns...). - Where("? = ?", bun.Ident("account_settings.id"), settings.ID). + Where("? = ?", bun.Ident("account_settings.account_id"), settings.AccountID). Exec(ctx); err != nil { return err } diff --git a/internal/db/bundb/admin.go b/internal/db/bundb/admin.go index 89ee9d42c..832db1d8f 100644 --- a/internal/db/bundb/admin.go +++ b/internal/db/bundb/admin.go @@ -113,12 +113,6 @@ func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) ( return nil, err } - settingsID, err := id.NewRandomULID() - if err != nil { - err := gtserror.Newf("error creating new account settings id: %w", err) - return nil, err - } - privKey, err := rsa.GenerateKey(rand.Reader, rsaKeyBits) if err != nil { err := gtserror.Newf("error creating new rsa private key: %w", err) @@ -126,9 +120,9 @@ func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) ( } settings := >smodel.AccountSettings{ - ID: settingsID, - Reason: newSignup.Reason, - Privacy: gtsmodel.VisibilityDefault, + AccountID: accountID, + Reason: newSignup.Reason, + Privacy: gtsmodel.VisibilityDefault, } // Insert the settings! @@ -151,7 +145,6 @@ func (a *adminDB) NewSignup(ctx context.Context, newSignup gtsmodel.NewSignup) ( PrivateKey: privKey, PublicKey: &privKey.PublicKey, PublicKeyURI: uris.PublicKeyURI, - SettingsID: settingsID, Settings: settings, } diff --git a/internal/db/bundb/migrations/20240318115336_account_settings.go b/internal/db/bundb/migrations/20240318115336_account_settings.go index 9f00f2b08..90d3ff420 100644 --- a/internal/db/bundb/migrations/20240318115336_account_settings.go +++ b/internal/db/bundb/migrations/20240318115336_account_settings.go @@ -19,12 +19,9 @@ package migrations import ( "context" - "fmt" - "strings" oldgtsmodel "github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations/20230328203024_migration_fix" newgtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/util" @@ -34,24 +31,6 @@ import ( func init() { up := func(ctx context.Context, db *bun.DB) error { log.Info(ctx, "migrating account settings to new table, please wait...") - - // Add settings_id to accounts table. - // - // Do this outside of the transaction so it doesn't mess - // up the transaction if the column exists already. - _, err := db.ExecContext(ctx, - "ALTER TABLE ? ADD COLUMN ? CHAR(26)", - bun.Ident("accounts"), bun.Ident("settings_id"), - ) - if err != nil { - e := err.Error() - if !(strings.Contains(e, "already exists") || - strings.Contains(e, "duplicate column name") || - strings.Contains(e, "SQLSTATE 42701")) { - return err - } - } - return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { // Columns we'll be moving // to AccountSettings. @@ -94,13 +73,8 @@ func init() { // Create a settings entry for each existing account, taking // values from the old account model (with sensible defaults). for _, account := range accounts { - settingsID, err := id.NewRandomULID() - if err != nil { - return fmt.Errorf("error creating settingsID: %w", err) - } - settings := &newgtsmodel.AccountSettings{ - ID: settingsID, + AccountID: account.ID, CreatedAt: account.CreatedAt, Reason: account.Reason, Privacy: newgtsmodel.Visibility(account.Privacy), @@ -119,16 +93,6 @@ func init() { Exec(ctx); err != nil { return err } - - // Update account with new settings ID. - if _, err := tx. - NewUpdate(). - Table("accounts"). - Set("? = ?", bun.Ident("settings_id"), settings.ID). - Where("? = ?", bun.Ident("id"), account.ID). - Exec(ctx); err != nil { - return err - } } // Drop now unused columns from accounts table. diff --git a/internal/gtsmodel/account.go b/internal/gtsmodel/account.go index 037bc4d73..2ac107e56 100644 --- a/internal/gtsmodel/account.go +++ b/internal/gtsmodel/account.go @@ -79,7 +79,6 @@ type Account struct { SilencedAt time.Time `bun:"type:timestamptz,nullzero"` // When was this account silenced (eg., statuses only visible to followers, not public)? SuspendedAt time.Time `bun:"type:timestamptz,nullzero"` // When was this account suspended (eg., don't allow it to log in/post, don't accept media/posts from this account) SuspensionOrigin string `bun:"type:CHAR(26),nullzero"` // id of the database entry that caused this account to become suspended -- can be an account ID or a domain block ID - SettingsID string `bun:"type:CHAR(26),nullzero"` // If account is local, ID of the settings model for this account. Settings *AccountSettings `bun:"-"` // gtsmodel.AccountSettings for this account. } diff --git a/internal/gtsmodel/accountsettings.go b/internal/gtsmodel/accountsettings.go index 7f557c0be..cb5411050 100644 --- a/internal/gtsmodel/accountsettings.go +++ b/internal/gtsmodel/accountsettings.go @@ -19,8 +19,9 @@ package gtsmodel import "time" +// AccountSettings models settings / preferences for a local, non-instance account. type AccountSettings struct { - ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database + AccountID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // AccountID that owns this settings. 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 was last updated. Reason string `bun:",nullzero"` // What reason was given for signing up when this account was created? diff --git a/internal/trans/import.go b/internal/trans/import.go index d412efdda..c77b439f5 100644 --- a/internal/trans/import.go +++ b/internal/trans/import.go @@ -25,6 +25,7 @@ import ( "io" "os" + "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/log" transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" ) @@ -73,9 +74,11 @@ func (i *importer) inputEntry(ctx context.Context, entry transmodel.Entry) error if err := i.putInDB(ctx, account); err != nil { return fmt.Errorf("inputEntry: error adding account to database: %s", err) } - if account.SettingsID != "" { + if account.Domain == "" && account.Username != config.GetHost() { + // Local, non-instance account. // Insert barebones settings model. - if err := i.putInDB(ctx, &transmodel.AccountSettings{ID: account.SettingsID}); err != nil { + settings := &transmodel.AccountSettings{AccountID: account.ID} + if err := i.putInDB(ctx, settings); err != nil { return fmt.Errorf("inputEntry: error adding account settings to database: %s", err) } } diff --git a/internal/trans/model/account.go b/internal/trans/model/account.go index a568de826..097dea3a3 100644 --- a/internal/trans/model/account.go +++ b/internal/trans/model/account.go @@ -55,9 +55,8 @@ type Account struct { SilencedAt *time.Time `json:"silencedAt,omitempty" bun:",nullzero"` SuspendedAt *time.Time `json:"suspendedAt,omitempty" bun:",nullzero"` SuspensionOrigin string `json:"suspensionOrigin,omitempty" bun:",nullzero"` - SettingsID string `json:"settingsID,omitempty" bun:",nullzero"` } type AccountSettings struct { - ID string + AccountID string } diff --git a/testrig/testmodels.go b/testrig/testmodels.go index a76c62346..9d014bbca 100644 --- a/testrig/testmodels.go +++ b/testrig/testmodels.go @@ -353,7 +353,6 @@ func NewTestAccounts() map[string]*gtsmodel.Account { SilencedAt: time.Time{}, SuspendedAt: time.Time{}, SuspensionOrigin: "", - SettingsID: "01HS90H04H2MSARC9QQE5RR8VX", Settings: settings["unconfirmed_account"], }, "admin_account": { @@ -388,7 +387,6 @@ func NewTestAccounts() map[string]*gtsmodel.Account { SilencedAt: time.Time{}, SuspendedAt: time.Time{}, SuspensionOrigin: "", - SettingsID: "01HS90JWVBQK1BPMMQGVAM1MND", Settings: settings["admin_account"], }, "local_account_1": { @@ -423,7 +421,6 @@ func NewTestAccounts() map[string]*gtsmodel.Account { SilencedAt: time.Time{}, SuspendedAt: time.Time{}, SuspensionOrigin: "", - SettingsID: "01HS90M11AWFA7DB00XQ3BB1D1", Settings: settings["local_account_1"], }, "local_account_2": { @@ -477,7 +474,6 @@ func NewTestAccounts() map[string]*gtsmodel.Account { SilencedAt: time.Time{}, SuspendedAt: time.Time{}, SuspensionOrigin: "", - SettingsID: "01HS90N2SDC5JHA0F8TMAM7TBE", Settings: settings["local_account_2"], }, "remote_account_1": { @@ -665,7 +661,7 @@ func NewTestAccounts() map[string]*gtsmodel.Account { func NewTestAccountSettings() map[string]*gtsmodel.AccountSettings { return map[string]*gtsmodel.AccountSettings{ "unconfirmed_account": { - ID: "01HS90H04H2MSARC9QQE5RR8VX", + AccountID: "01F8MH0BBE4FHXPH513MBVFHB0", CreatedAt: TimeMustParse("2022-06-04T13:12:00Z"), UpdatedAt: TimeMustParse("2022-06-04T13:12:00Z"), Reason: "hi, please let me in! I'm looking for somewhere neato bombeato to hang out.", @@ -676,7 +672,7 @@ func NewTestAccountSettings() map[string]*gtsmodel.AccountSettings { HideCollections: util.Ptr(false), }, "admin_account": { - ID: "01HS90JWVBQK1BPMMQGVAM1MND", + AccountID: "01F8MH17FWEB39HZJ76B6VXSKF", CreatedAt: TimeMustParse("2022-05-17T13:10:59Z"), UpdatedAt: TimeMustParse("2022-05-17T13:10:59Z"), Reason: "", @@ -687,7 +683,7 @@ func NewTestAccountSettings() map[string]*gtsmodel.AccountSettings { HideCollections: util.Ptr(false), }, "local_account_1": { - ID: "01HS90M11AWFA7DB00XQ3BB1D1", + AccountID: "01F8MH1H7YV1Z7D2C8K2730QBF", CreatedAt: TimeMustParse("2022-05-20T11:09:18Z"), UpdatedAt: TimeMustParse("2022-05-20T11:09:18Z"), Reason: "I wanna be on this damned webbed site so bad! Please! Wow", @@ -698,7 +694,7 @@ func NewTestAccountSettings() map[string]*gtsmodel.AccountSettings { HideCollections: util.Ptr(false), }, "local_account_2": { - ID: "01HS90N2SDC5JHA0F8TMAM7TBE", + AccountID: "01F8MH5NBDF2MV7CTC4Q5128HF", CreatedAt: TimeMustParse("2022-06-04T13:12:00Z"), UpdatedAt: TimeMustParse("2022-06-04T13:12:00Z"), Reason: "",