diff options
author | 2023-08-09 19:14:33 +0200 | |
---|---|---|
committer | 2023-08-09 19:14:33 +0200 | |
commit | 9770d54237bea828cab7e50aec7dff452c203138 (patch) | |
tree | 59c444a02e81925bab47d3656a489a8c7087d530 /internal/db | |
parent | [bugfix] Fix incorrect per-loop variable capture (#2092) (diff) | |
download | gotosocial-9770d54237bea828cab7e50aec7dff452c203138.tar.xz |
[feature] List replies policy, refactor async workers (#2087)
* Add/update some DB functions.
* move async workers into subprocessor
* rename FromFederator -> FromFediAPI
* update home timeline check to include check for current status first before moving to parent status
* change streamMap to pointer to mollify linter
* update followtoas func signature
* fix merge
* remove errant debug log
* don't use separate errs.Combine() check to wrap errs
* wrap parts of workers functionality in sub-structs
* populate report using new db funcs
* embed federator (tiny bit tidier)
* flesh out error msg, add continue(!)
* fix other error messages to be more specific
* better, nicer
* give parseURI util function a bit more util
* missing headers
* use pointers for subprocessors
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/bundb/account.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/instance.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/list.go | 22 | ||||
-rw-r--r-- | internal/db/bundb/list_test.go | 21 | ||||
-rw-r--r-- | internal/db/bundb/relationship_block.go | 47 | ||||
-rw-r--r-- | internal/db/bundb/relationship_follow.go | 6 | ||||
-rw-r--r-- | internal/db/bundb/relationship_follow_req.go | 47 | ||||
-rw-r--r-- | internal/db/bundb/report.go | 71 | ||||
-rw-r--r-- | internal/db/bundb/statusfave.go | 6 | ||||
-rw-r--r-- | internal/db/list.go | 3 | ||||
-rw-r--r-- | internal/db/relationship.go | 6 | ||||
-rw-r--r-- | internal/db/report.go | 7 |
12 files changed, 175 insertions, 73 deletions
diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 83b3c13f5..2d9a64454 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -290,11 +290,7 @@ func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Accou } } - if err := errs.Combine(); err != nil { - return gtserror.Newf("%w", err) - } - - return nil + return errs.Combine() } func (a *accountDB) PutAccount(ctx context.Context, account *gtsmodel.Account) error { diff --git a/internal/db/bundb/instance.go b/internal/db/bundb/instance.go index 6657072fd..09084642f 100644 --- a/internal/db/bundb/instance.go +++ b/internal/db/bundb/instance.go @@ -198,11 +198,7 @@ func (i *instanceDB) populateInstance(ctx context.Context, instance *gtsmodel.In } } - if err := errs.Combine(); err != nil { - return gtserror.Newf("%w", err) - } - - return nil + return errs.Combine() } func (i *instanceDB) PutInstance(ctx context.Context, instance *gtsmodel.Instance) error { diff --git a/internal/db/bundb/list.go b/internal/db/bundb/list.go index 5cf10ce3c..ec96f1dfc 100644 --- a/internal/db/bundb/list.go +++ b/internal/db/bundb/list.go @@ -143,11 +143,7 @@ func (l *listDB) PopulateList(ctx context.Context, list *gtsmodel.List) error { } } - if err := errs.Combine(); err != nil { - return gtserror.Newf("%w", err) - } - - return nil + return errs.Combine() } func (l *listDB) PutList(ctx context.Context, list *gtsmodel.List) error { @@ -503,6 +499,22 @@ func (l *listDB) DeleteListEntriesForFollowID(ctx context.Context, followID stri return nil } +func (l *listDB) ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) { + exists, err := l.db. + NewSelect(). + TableExpr("? AS ?", bun.Ident("list_entries"), bun.Ident("list_entry")). + Join( + "JOIN ? AS ? ON ? = ?", + bun.Ident("follows"), bun.Ident("follow"), + bun.Ident("list_entry.follow_id"), bun.Ident("follow.id"), + ). + Where("? = ?", bun.Ident("list_entry.list_id"), listID). + Where("? = ?", bun.Ident("follow.target_account_id"), accountID). + Exists(ctx) + + return exists, l.db.ProcessError(err) +} + // collate will collect the values of type T from an expected slice of length 'len', // passing the expected index to each call of 'get' and deduplicating the end result. func collate[T comparable](get func(int) T, len int) []T { diff --git a/internal/db/bundb/list_test.go b/internal/db/bundb/list_test.go index 296ab7c1a..ca078d086 100644 --- a/internal/db/bundb/list_test.go +++ b/internal/db/bundb/list_test.go @@ -310,6 +310,27 @@ func (suite *ListTestSuite) TestDeleteListEntriesForFollowID() { suite.checkList(testList, dbList) } +func (suite *ListTestSuite) TestListIncludesAccount() { + ctx := context.Background() + testList, _ := suite.testStructs() + + for accountID, expected := range map[string]bool{ + suite.testAccounts["admin_account"].ID: true, + suite.testAccounts["local_account_1"].ID: false, + suite.testAccounts["local_account_2"].ID: true, + "01H7074GEZJ56J5C86PFB0V2CT": false, + } { + includes, err := suite.db.ListIncludesAccount(ctx, testList.ID, accountID) + if err != nil { + suite.FailNow(err.Error()) + } + + if includes != expected { + suite.FailNow("", "expected %t for accountID %s got %t", expected, accountID, includes) + } + } +} + func TestListTestSuite(t *testing.T) { suite.Run(t, new(ListTestSuite)) } diff --git a/internal/db/bundb/relationship_block.go b/internal/db/bundb/relationship_block.go index 2a042bed4..33a3b85fa 100644 --- a/internal/db/bundb/relationship_block.go +++ b/internal/db/bundb/relationship_block.go @@ -20,10 +20,10 @@ package bundb import ( "context" "errors" - "fmt" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtscontext" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/uptrace/bun" @@ -139,25 +139,42 @@ func (r *relationshipDB) getBlock(ctx context.Context, lookup string, dbQuery fu return block, nil } - // Set the block source account - block.Account, err = r.state.DB.GetAccountByID( - gtscontext.SetBarebones(ctx), - block.AccountID, - ) - if err != nil { - return nil, fmt.Errorf("error getting block source account: %w", err) + if err := r.state.DB.PopulateBlock(ctx, block); err != nil { + return nil, err } - // Set the block target account - block.TargetAccount, err = r.state.DB.GetAccountByID( - gtscontext.SetBarebones(ctx), - block.TargetAccountID, + return block, nil +} + +func (r *relationshipDB) PopulateBlock(ctx context.Context, block *gtsmodel.Block) error { + var ( + err error + errs = gtserror.NewMultiError(2) ) - if err != nil { - return nil, fmt.Errorf("error getting block target account: %w", err) + + if block.Account == nil { + // Block origin account is not set, fetch from database. + block.Account, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + block.AccountID, + ) + if err != nil { + errs.Appendf("error populating block account: %w", err) + } } - return block, nil + if block.TargetAccount == nil { + // Block target account is not set, fetch from database. + block.TargetAccount, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + block.TargetAccountID, + ) + if err != nil { + errs.Appendf("error populating block target account: %w", err) + } + } + + return errs.Combine() } func (r *relationshipDB) PutBlock(ctx context.Context, block *gtsmodel.Block) error { diff --git a/internal/db/bundb/relationship_follow.go b/internal/db/bundb/relationship_follow.go index e22ed30de..b693269df 100644 --- a/internal/db/bundb/relationship_follow.go +++ b/internal/db/bundb/relationship_follow.go @@ -185,11 +185,7 @@ func (r *relationshipDB) PopulateFollow(ctx context.Context, follow *gtsmodel.Fo } } - if err := errs.Combine(); err != nil { - return gtserror.Newf("%w", err) - } - - return nil + return errs.Combine() } func (r *relationshipDB) PutFollow(ctx context.Context, follow *gtsmodel.Follow) error { diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index dc5e760e6..cde9dc187 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -20,11 +20,11 @@ package bundb import ( "context" "errors" - "fmt" "time" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtscontext" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/uptrace/bun" @@ -127,25 +127,42 @@ func (r *relationshipDB) getFollowRequest(ctx context.Context, lookup string, db return followReq, nil } - // Set the follow request source account - followReq.Account, err = r.state.DB.GetAccountByID( - gtscontext.SetBarebones(ctx), - followReq.AccountID, - ) - if err != nil { - return nil, fmt.Errorf("error getting follow request source account: %w", err) + if err := r.state.DB.PopulateFollowRequest(ctx, followReq); err != nil { + return nil, err } - // Set the follow request target account - followReq.TargetAccount, err = r.state.DB.GetAccountByID( - gtscontext.SetBarebones(ctx), - followReq.TargetAccountID, + return followReq, nil +} + +func (r *relationshipDB) PopulateFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error { + var ( + err error + errs = gtserror.NewMultiError(2) ) - if err != nil { - return nil, fmt.Errorf("error getting follow request target account: %w", err) + + if follow.Account == nil { + // Follow account is not set, fetch from the database. + follow.Account, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + follow.AccountID, + ) + if err != nil { + errs.Appendf("error populating follow request account: %w", err) + } } - return followReq, nil + if follow.TargetAccount == nil { + // Follow target account is not set, fetch from the database. + follow.TargetAccount, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + follow.TargetAccountID, + ) + if err != nil { + errs.Appendf("error populating follow target request account: %w", err) + } + } + + return errs.Combine() } func (r *relationshipDB) PutFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error { diff --git a/internal/db/bundb/report.go b/internal/db/bundb/report.go index 3a1e18789..eaeac4860 100644 --- a/internal/db/bundb/report.go +++ b/internal/db/bundb/report.go @@ -20,11 +20,11 @@ package bundb import ( "context" "errors" - "fmt" "time" "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtscontext" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/state" @@ -135,35 +135,70 @@ func (r *reportDB) getReport(ctx context.Context, lookup string, dbQuery func(*g return nil, err } - // Set the report author account - report.Account, err = r.state.DB.GetAccountByID(ctx, report.AccountID) - if err != nil { - return nil, fmt.Errorf("error getting report account: %w", err) + if gtscontext.Barebones(ctx) { + // Only a barebones model was requested. + return report, nil } - // Set the report target account - report.TargetAccount, err = r.state.DB.GetAccountByID(ctx, report.TargetAccountID) - if err != nil { - return nil, fmt.Errorf("error getting report target account: %w", err) + if err := r.state.DB.PopulateReport(ctx, report); err != nil { + return nil, err } - if len(report.StatusIDs) > 0 { - // Fetch reported statuses - report.Statuses, err = r.state.DB.GetStatusesByIDs(ctx, report.StatusIDs) + return report, nil +} + +func (r *reportDB) PopulateReport(ctx context.Context, report *gtsmodel.Report) error { + var ( + err error + errs = gtserror.NewMultiError(4) + ) + + if report.Account == nil { + // Report account is not set, fetch from the database. + report.Account, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + report.AccountID, + ) if err != nil { - return nil, fmt.Errorf("error getting status mentions: %w", err) + errs.Appendf("error populating report account: %w", err) } } - if report.ActionTakenByAccountID != "" { - // Set the report action taken by account - report.ActionTakenByAccount, err = r.state.DB.GetAccountByID(ctx, report.ActionTakenByAccountID) + if report.TargetAccount == nil { + // Report target account is not set, fetch from the database. + report.TargetAccount, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + report.TargetAccountID, + ) if err != nil { - return nil, fmt.Errorf("error getting report action taken by account: %w", err) + errs.Appendf("error populating report target account: %w", err) } } - return report, nil + if l := len(report.StatusIDs); l > 0 && l != len(report.Statuses) { + // Report target statuses not set, fetch from the database. + report.Statuses, err = r.state.DB.GetStatusesByIDs( + gtscontext.SetBarebones(ctx), + report.StatusIDs, + ) + if err != nil { + errs.Appendf("error populating report statuses: %w", err) + } + } + + if report.ActionTakenByAccountID != "" && + report.ActionTakenByAccount == nil { + // Report action account is not set, fetch from the database. + report.ActionTakenByAccount, err = r.state.DB.GetAccountByID( + gtscontext.SetBarebones(ctx), + report.ActionTakenByAccountID, + ) + if err != nil { + errs.Appendf("error populating report action taken by account: %w", err) + } + } + + return errs.Combine() } func (r *reportDB) PutReport(ctx context.Context, report *gtsmodel.Report) error { diff --git a/internal/db/bundb/statusfave.go b/internal/db/bundb/statusfave.go index ab09fb1ba..37b88326b 100644 --- a/internal/db/bundb/statusfave.go +++ b/internal/db/bundb/statusfave.go @@ -197,11 +197,7 @@ func (s *statusFaveDB) PopulateStatusFave(ctx context.Context, statusFave *gtsmo } } - if err := errs.Combine(); err != nil { - return gtserror.Newf("%w", err) - } - - return nil + return errs.Combine() } func (s *statusFaveDB) PutStatusFave(ctx context.Context, fave *gtsmodel.StatusFave) error { diff --git a/internal/db/list.go b/internal/db/list.go index 4472589dc..91a540486 100644 --- a/internal/db/list.go +++ b/internal/db/list.go @@ -64,4 +64,7 @@ type List interface { // DeleteListEntryForFollowID deletes all list entries with the given followID. DeleteListEntriesForFollowID(ctx context.Context, followID string) error + + // ListIncludesAccount returns true if the given listID includes the given accountID. + ListIncludesAccount(ctx context.Context, listID string, accountID string) (bool, error) } diff --git a/internal/db/relationship.go b/internal/db/relationship.go index 6ba9fdf8c..50f615ef3 100644 --- a/internal/db/relationship.go +++ b/internal/db/relationship.go @@ -41,6 +41,9 @@ type Relationship interface { // GetBlock returns the block from account1 targeting account2, if it exists, or an error if it doesn't. GetBlock(ctx context.Context, account1 string, account2 string) (*gtsmodel.Block, error) + // PopulateBlock populates the struct pointers on the given block. + PopulateBlock(ctx context.Context, block *gtsmodel.Block) error + // PutBlock attempts to place the given account block in the database. PutBlock(ctx context.Context, block *gtsmodel.Block) error @@ -77,6 +80,9 @@ type Relationship interface { // GetFollowRequest retrieves a follow request if it exists between source and target accounts. GetFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.FollowRequest, error) + // PopulateFollowRequest populates the struct pointers on the given follow request. + PopulateFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error + // IsFollowing returns true if sourceAccount follows target account, or an error if something goes wrong while finding out. IsFollowing(ctx context.Context, sourceAccountID string, targetAccountID string) (bool, error) diff --git a/internal/db/report.go b/internal/db/report.go index f39e53140..a04b4d3fa 100644 --- a/internal/db/report.go +++ b/internal/db/report.go @@ -27,17 +27,24 @@ import ( type Report interface { // GetReportByID gets one report by its db id GetReportByID(ctx context.Context, id string) (*gtsmodel.Report, error) + // GetReports gets limit n reports using the given parameters. // Parameters that are empty / zero are ignored. GetReports(ctx context.Context, resolved *bool, accountID string, targetAccountID string, maxID string, sinceID string, minID string, limit int) ([]*gtsmodel.Report, error) + + // PopulateReport populates the struct pointers on the given report. + PopulateReport(ctx context.Context, report *gtsmodel.Report) error + // PutReport puts the given report in the database. PutReport(ctx context.Context, report *gtsmodel.Report) error + // UpdateReport updates one report by its db id. // The given columns will be updated; if no columns are // provided, then all columns will be updated. // updated_at will also be updated, no need to pass this // as a specific column. UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) + // DeleteReportByID deletes report with the given id. DeleteReportByID(ctx context.Context, id string) error } |