diff options
author | 2024-03-22 14:03:46 +0100 | |
---|---|---|
committer | 2024-03-22 14:03:46 +0100 | |
commit | 7f4a0a1aeb8a294ee967c63d7a48446df013ec44 (patch) | |
tree | b9b3836fa0abe1d7a5758d07d6ebb6486a353d56 /internal/db/bundb/account.go | |
parent | [bugfix] add all possible busy result codes to the sqlite errBusy catching ch... (diff) | |
download | gotosocial-7f4a0a1aeb8a294ee967c63d7a48446df013ec44.tar.xz |
[chore] Move local account settings to separate db table (#2770)
* [chore] Move local account settings to separate database model
* don't use separate settings_id
Diffstat (limited to 'internal/db/bundb/account.go')
-rw-r--r-- | internal/db/bundb/account.go | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 5b1dab143..870c2ff55 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -338,6 +338,17 @@ func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Accou } } + 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.ID, + ) + if err != nil { + errs.Appendf("error populating account settings: %w", err) + } + } + return errs.Combine() } @@ -504,12 +515,22 @@ func (a *accountDB) SetAccountHeaderOrAvatar(ctx context.Context, mediaAttachmen } func (a *accountDB) GetAccountCustomCSSByUsername(ctx context.Context, username string) (string, error) { + // Get local account. account, err := a.GetAccountByUsernameDomain(ctx, username, "") if err != nil { return "", err } - return account.CustomCSS, nil + // Ensure settings populated, in case + // barebones context was passed. + if account.Settings == nil { + account.Settings, err = a.GetAccountSettings(ctx, account.ID) + if err != nil { + return "", err + } + } + + return account.Settings.CustomCSS, nil } func (a *accountDB) GetAccountsUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Account, error) { @@ -780,3 +801,68 @@ func (a *accountDB) GetAccountWebStatuses(ctx context.Context, accountID string, return a.state.DB.GetStatusesByIDs(ctx, statusIDs) } + +func (a *accountDB) GetAccountSettings( + ctx context.Context, + accountID string, +) (*gtsmodel.AccountSettings, error) { + // Fetch settings from db cache with loader callback. + return a.state.Caches.GTS.AccountSettings.LoadOne( + "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.account_id"), accountID). + Scan(ctx); err != nil { + return nil, err + } + return &settings, nil + }, + accountID, + ) +} + +func (a *accountDB) PutAccountSettings( + ctx context.Context, + settings *gtsmodel.AccountSettings, +) error { + return a.state.Caches.GTS.AccountSettings.Store(settings, func() error { + if _, err := a.db. + NewInsert(). + Model(settings). + Exec(ctx); err != nil { + return err + } + + return nil + }) +} + +func (a *accountDB) UpdateAccountSettings( + ctx context.Context, + settings *gtsmodel.AccountSettings, + columns ...string, +) error { + return a.state.Caches.GTS.AccountSettings.Store(settings, func() error { + settings.UpdatedAt = time.Now() + if len(columns) > 0 { + // If we're updating by column, + // ensure "updated_at" is included. + columns = append(columns, "updated_at") + } + + if _, err := a.db. + NewUpdate(). + Model(settings). + Column(columns...). + Where("? = ?", bun.Ident("account_settings.account_id"), settings.AccountID). + Exec(ctx); err != nil { + return err + } + + return nil + }) +} |