[bugfix] only attempt to populate account/statuses from DB if already exist (#1839)

* only attempt to populate account/statuses from DB if already up-to-date

Signed-off-by: kim <grufwub@gmail.com>

* add missing status is-up-to-date check :grimace: + ensure populated if so

Signed-off-by: kim <grufwub@gmail.com>

---------

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2023-05-31 09:39:54 +01:00 committed by GitHub
parent 36fcd2e604
commit 9da20eeecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 9 deletions

View file

@ -30,6 +30,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/id"
@ -90,15 +91,23 @@ func (d *deref) getAccountByURI(ctx context.Context, requestUser string, uri *ur
err error err error
) )
// Search the database for existing account with ID URI. // Search the database for existing account with URI.
account, err = d.state.DB.GetAccountByURI(ctx, uriStr) account, err = d.state.DB.GetAccountByURI(
// request a barebones object, it may be in the
// db but with related models not yet dereferenced.
gtscontext.SetBarebones(ctx),
uriStr,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, nil, gtserror.Newf("error checking database for account %s by uri: %w", uriStr, err) return nil, nil, gtserror.Newf("error checking database for account %s by uri: %w", uriStr, err)
} }
if account == nil { if account == nil {
// Else, search the database for existing by ID URL. // Else, search the database for existing by URL.
account, err = d.state.DB.GetAccountByURL(ctx, uriStr) account, err = d.state.DB.GetAccountByURL(
gtscontext.SetBarebones(ctx),
uriStr,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, nil, gtserror.Newf("error checking database for account %s by url: %w", uriStr, err) return nil, nil, gtserror.Newf("error checking database for account %s by url: %w", uriStr, err)
} }
@ -120,6 +129,10 @@ func (d *deref) getAccountByURI(ctx context.Context, requestUser string, uri *ur
// Check whether needs update. // Check whether needs update.
if accountUpToDate(account) { if accountUpToDate(account) {
// This is existing up-to-date account, ensure it is populated.
if err := d.state.DB.PopulateAccount(ctx, account); err != nil {
log.Errorf(ctx, "error populating existing account: %v", err)
}
return account, nil, nil return account, nil, nil
} }
@ -153,7 +166,12 @@ func (d *deref) GetAccountByUsernameDomain(ctx context.Context, requestUser stri
} }
// Search the database for existing account with USERNAME@DOMAIN. // Search the database for existing account with USERNAME@DOMAIN.
account, err := d.state.DB.GetAccountByUsernameDomain(ctx, username, domain) account, err := d.state.DB.GetAccountByUsernameDomain(
// request a barebones object, it may be in the
// db but with related models not yet dereferenced.
gtscontext.SetBarebones(ctx),
username, domain,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, nil, gtserror.Newf("error checking database for account %s@%s: %w", username, domain, err) return nil, nil, gtserror.Newf("error checking database for account %s@%s: %w", username, domain, err)
} }
@ -196,6 +214,13 @@ func (d *deref) GetAccountByUsernameDomain(ctx context.Context, requestUser stri
return account, nil, nil //nolint return account, nil, nil //nolint
} }
if apubAcc == nil {
// This is existing up-to-date account, ensure it is populated.
if err := d.state.DB.PopulateAccount(ctx, account); err != nil {
log.Errorf(ctx, "error populating existing account: %v", err)
}
}
return latest, apubAcc, nil return latest, apubAcc, nil
} }

View file

@ -27,6 +27,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/id"
@ -80,15 +81,23 @@ func (d *deref) getStatusByURI(ctx context.Context, requestUser string, uri *url
err error err error
) )
// Search the database for existing status with ID URI. // Search the database for existing status with URI.
status, err = d.state.DB.GetStatusByURI(ctx, uriStr) status, err = d.state.DB.GetStatusByURI(
// request a barebones object, it may be in the
// db but with related models not yet dereferenced.
gtscontext.SetBarebones(ctx),
uriStr,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, nil, gtserror.Newf("error checking database for status %s by uri: %w", uriStr, err) return nil, nil, gtserror.Newf("error checking database for status %s by uri: %w", uriStr, err)
} }
if status == nil { if status == nil {
// Else, search the database for existing by ID URL. // Else, search the database for existing by URL.
status, err = d.state.DB.GetStatusByURL(ctx, uriStr) status, err = d.state.DB.GetStatusByURL(
gtscontext.SetBarebones(ctx),
uriStr,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) { if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, nil, gtserror.Newf("error checking database for status %s by url: %w", uriStr, err) return nil, nil, gtserror.Newf("error checking database for status %s by url: %w", uriStr, err)
} }
@ -107,6 +116,15 @@ func (d *deref) getStatusByURI(ctx context.Context, requestUser string, uri *url
}, nil) }, nil)
} }
// Check whether needs update.
if statusUpToDate(status) {
// This is existing up-to-date status, ensure it is populated.
if err := d.state.DB.PopulateStatus(ctx, status); err != nil {
log.Errorf(ctx, "error populating existing status: %v", err)
}
return status, nil, nil
}
// Try to update + deref existing status model. // Try to update + deref existing status model.
latest, apubStatus, err := d.enrichStatus(ctx, latest, apubStatus, err := d.enrichStatus(ctx,
requestUser, requestUser,