[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
This commit is contained in:
tobi 2023-08-09 19:14:33 +02:00 committed by GitHub
commit 9770d54237
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 4110 additions and 2660 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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,27 +139,44 @@ 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)
}
// Set the block target account
block.TargetAccount, err = r.state.DB.GetAccountByID(
gtscontext.SetBarebones(ctx),
block.TargetAccountID,
)
if err != nil {
return nil, fmt.Errorf("error getting block target account: %w", err)
if err := r.state.DB.PopulateBlock(ctx, block); err != nil {
return nil, err
}
return block, nil
}
func (r *relationshipDB) PopulateBlock(ctx context.Context, block *gtsmodel.Block) error {
var (
err error
errs = gtserror.NewMultiError(2)
)
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)
}
}
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 {
return r.state.Caches.GTS.Block().Store(block, func() error {
_, err := r.db.NewInsert().Model(block).Exec(ctx)

View file

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

View file

@ -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,27 +127,44 @@ 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)
}
// Set the follow request target account
followReq.TargetAccount, err = r.state.DB.GetAccountByID(
gtscontext.SetBarebones(ctx),
followReq.TargetAccountID,
)
if err != nil {
return nil, fmt.Errorf("error getting follow request target account: %w", err)
if err := r.state.DB.PopulateFollowRequest(ctx, followReq); err != nil {
return nil, err
}
return followReq, nil
}
func (r *relationshipDB) PopulateFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error {
var (
err error
errs = gtserror.NewMultiError(2)
)
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)
}
}
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 {
return r.state.Caches.GTS.FollowRequest().Store(follow, func() error {
_, err := r.db.NewInsert().Model(follow).Exec(ctx)

View file

@ -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,37 +135,72 @@ 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 len(report.StatusIDs) > 0 {
// Fetch reported statuses
report.Statuses, err = r.state.DB.GetStatusesByIDs(ctx, report.StatusIDs)
if err != nil {
return nil, fmt.Errorf("error getting status mentions: %w", err)
}
}
if report.ActionTakenByAccountID != "" {
// Set the report action taken by account
report.ActionTakenByAccount, err = r.state.DB.GetAccountByID(ctx, report.ActionTakenByAccountID)
if err != nil {
return nil, fmt.Errorf("error getting report action taken by account: %w", err)
}
if err := r.state.DB.PopulateReport(ctx, report); err != nil {
return nil, err
}
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 {
errs.Appendf("error populating report account: %w", err)
}
}
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 {
errs.Appendf("error populating report target account: %w", err)
}
}
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 {
return r.state.Caches.GTS.Report().Store(report, func() error {
_, err := r.db.NewInsert().Model(report).Exec(ctx)

View file

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

View file

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

View file

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

View file

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