don't use separate settings_id

This commit is contained in:
tobi 2024-03-22 13:53:27 +01:00
parent acd1e1b283
commit c90b83e49e
11 changed files with 30 additions and 71 deletions

View File

@ -117,6 +117,10 @@ func (suite *WebfingerGetTestSuite) funkifyAccountDomain(host string, accountDom
suite.FailNow(err.Error())
}
if err := suite.db.PutAccountSettings(context.Background(), &gtsmodel.AccountSettings{AccountID: targetAccount.ID}); err != nil {
suite.FailNow(err.Error())
}
return targetAccount
}

View File

@ -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,

View File

@ -249,7 +249,7 @@ func sizeofAccountNote() uintptr {
func sizeofAccountSettings() uintptr {
return uintptr(size.Of(&gtsmodel.AccountSettings{
ID: exampleID,
AccountID: exampleID,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
Reason: exampleText,

View File

@ -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
}

View File

@ -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 := &gtsmodel.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,
}

View File

@ -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.

View File

@ -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.
}

View File

@ -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?

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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: "",