mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-26 02:13:33 -06:00
[performance] cache follow, follow request and block ID lists (#2027)
This commit is contained in:
parent
de148e9f9f
commit
ed2477ebea
29 changed files with 1283 additions and 335 deletions
|
|
@ -20,7 +20,6 @@ package account
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
|
|
@ -114,38 +113,38 @@ func (p *Processor) DeleteSelf(ctx context.Context, account *gtsmodel.Account) g
|
|||
func (p *Processor) deleteUserAndTokensForAccount(ctx context.Context, account *gtsmodel.Account) error {
|
||||
user, err := p.state.DB.GetUserByAccountID(ctx, account.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error getting user: %w", err)
|
||||
return gtserror.Newf("db error getting user: %w", err)
|
||||
}
|
||||
|
||||
tokens := []*gtsmodel.Token{}
|
||||
if err := p.state.DB.GetWhere(ctx, []db.Where{{Key: "user_id", Value: user.ID}}, &tokens); err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error getting tokens: %w", err)
|
||||
return gtserror.Newf("db error getting tokens: %w", err)
|
||||
}
|
||||
|
||||
for _, t := range tokens {
|
||||
// Delete any OAuth clients associated with this token.
|
||||
if err := p.state.DB.DeleteByID(ctx, t.ClientID, &[]*gtsmodel.Client{}); err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error deleting client: %w", err)
|
||||
return gtserror.Newf("db error deleting client: %w", err)
|
||||
}
|
||||
|
||||
// Delete any OAuth applications associated with this token.
|
||||
if err := p.state.DB.DeleteWhere(ctx, []db.Where{{Key: "client_id", Value: t.ClientID}}, &[]*gtsmodel.Application{}); err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error deleting application: %w", err)
|
||||
return gtserror.Newf("db error deleting application: %w", err)
|
||||
}
|
||||
|
||||
// Delete the token itself.
|
||||
if err := p.state.DB.DeleteByID(ctx, t.ID, t); err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error deleting token: %w", err)
|
||||
return gtserror.Newf("db error deleting token: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
columns, err := stubbifyUser(user)
|
||||
if err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: error stubbifying user: %w", err)
|
||||
return gtserror.Newf("error stubbifying user: %w", err)
|
||||
}
|
||||
|
||||
if err := p.state.DB.UpdateUser(ctx, user, columns...); err != nil {
|
||||
return fmt.Errorf("deleteUserAndTokensForAccount: db error updating user: %w", err)
|
||||
return gtserror.Newf("db error updating user: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -160,24 +159,24 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel.
|
|||
// Delete follows targeting this account.
|
||||
followedBy, err := p.state.DB.GetAccountFollowers(ctx, account.ID)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return fmt.Errorf("deleteAccountFollows: db error getting follows targeting account %s: %w", account.ID, err)
|
||||
return gtserror.Newf("db error getting follows targeting account %s: %w", account.ID, err)
|
||||
}
|
||||
|
||||
for _, follow := range followedBy {
|
||||
if err := p.state.DB.DeleteFollowByID(ctx, follow.ID); err != nil {
|
||||
return fmt.Errorf("deleteAccountFollows: db error unfollowing account followedBy: %w", err)
|
||||
return gtserror.Newf("db error unfollowing account followedBy: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete follow requests targeting this account.
|
||||
followRequestedBy, err := p.state.DB.GetAccountFollowRequests(ctx, account.ID)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return fmt.Errorf("deleteAccountFollows: db error getting follow requests targeting account %s: %w", account.ID, err)
|
||||
return gtserror.Newf("db error getting follow requests targeting account %s: %w", account.ID, err)
|
||||
}
|
||||
|
||||
for _, followRequest := range followRequestedBy {
|
||||
if err := p.state.DB.DeleteFollowRequestByID(ctx, followRequest.ID); err != nil {
|
||||
return fmt.Errorf("deleteAccountFollows: db error unfollowing account followRequestedBy: %w", err)
|
||||
return gtserror.Newf("db error unfollowing account followRequestedBy: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -193,14 +192,14 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel.
|
|||
// Delete follows originating from this account.
|
||||
following, err := p.state.DB.GetAccountFollows(ctx, account.ID)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return fmt.Errorf("deleteAccountFollows: db error getting follows owned by account %s: %w", account.ID, err)
|
||||
return gtserror.Newf("db error getting follows owned by account %s: %w", account.ID, err)
|
||||
}
|
||||
|
||||
// For each follow owned by this account, unfollow
|
||||
// and process side effects (noop if remote account).
|
||||
for _, follow := range following {
|
||||
if err := p.state.DB.DeleteFollowByID(ctx, follow.ID); err != nil {
|
||||
return fmt.Errorf("deleteAccountFollows: db error unfollowing account: %w", err)
|
||||
return gtserror.Newf("db error unfollowing account: %w", err)
|
||||
}
|
||||
if msg := unfollowSideEffects(ctx, account, follow); msg != nil {
|
||||
// There was a side effect to process.
|
||||
|
|
@ -211,14 +210,14 @@ func (p *Processor) deleteAccountFollows(ctx context.Context, account *gtsmodel.
|
|||
// Delete follow requests originating from this account.
|
||||
followRequesting, err := p.state.DB.GetAccountFollowRequesting(ctx, account.ID)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return fmt.Errorf("deleteAccountFollows: db error getting follow requests owned by account %s: %w", account.ID, err)
|
||||
return gtserror.Newf("db error getting follow requests owned by account %s: %w", account.ID, err)
|
||||
}
|
||||
|
||||
// For each follow owned by this account, unfollow
|
||||
// and process side effects (noop if remote account).
|
||||
for _, followRequest := range followRequesting {
|
||||
if err := p.state.DB.DeleteFollowRequestByID(ctx, followRequest.ID); err != nil {
|
||||
return fmt.Errorf("deleteAccountFollows: db error unfollowingRequesting account: %w", err)
|
||||
return gtserror.Newf("db error unfollowingRequesting account: %w", err)
|
||||
}
|
||||
|
||||
// Dummy out a follow so our side effects func
|
||||
|
|
@ -279,7 +278,7 @@ func (p *Processor) unfollowSideEffectsFunc(deletedAccount *gtsmodel.Account) fu
|
|||
|
||||
func (p *Processor) deleteAccountBlocks(ctx context.Context, account *gtsmodel.Account) error {
|
||||
if err := p.state.DB.DeleteAccountBlocks(ctx, account.ID); err != nil {
|
||||
return fmt.Errorf("deleteAccountBlocks: db error deleting account blocks for %s: %w", account.ID, err)
|
||||
return gtserror.Newf("db error deleting account blocks for %s: %w", account.ID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -333,7 +332,7 @@ statusLoop:
|
|||
// Look for any boosts of this status in DB.
|
||||
boosts, err := p.state.DB.GetStatusReblogs(ctx, status)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return fmt.Errorf("deleteAccountStatuses: error fetching status reblogs for %s: %w", status.ID, err)
|
||||
return gtserror.Newf("error fetching status reblogs for %s: %w", status.ID, err)
|
||||
}
|
||||
|
||||
for _, boost := range boosts {
|
||||
|
|
@ -347,7 +346,7 @@ statusLoop:
|
|||
log.WithContext(ctx).WithField("boost", boost).Warnf("no account found with id %s for boost %s", boost.AccountID, boost.ID)
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("deleteAccountStatuses: error fetching boosted status account for %s: %w", boost.AccountID, err)
|
||||
return gtserror.Newf("error fetching boosted status account for %s: %w", boost.AccountID, err)
|
||||
}
|
||||
|
||||
// Set account model
|
||||
|
|
@ -505,7 +504,7 @@ func stubbifyUser(user *gtsmodel.User) ([]string, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var never = time.Time{}
|
||||
never := time.Time{}
|
||||
|
||||
user.EncryptedPassword = string(dummyPassword)
|
||||
user.SignUpIP = net.IPv4zero
|
||||
|
|
|
|||
|
|
@ -19,69 +19,71 @@ package processing
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"errors"
|
||||
|
||||
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/paging"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
func (p *Processor) BlocksGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, limit int) (*apimodel.BlocksResponse, gtserror.WithCode) {
|
||||
accounts, nextMaxID, prevMinID, err := p.state.DB.GetAccountBlocks(ctx, authed.Account.ID, maxID, sinceID, limit)
|
||||
if err != nil {
|
||||
if err == db.ErrNoEntries {
|
||||
// there are just no entries
|
||||
return &apimodel.BlocksResponse{
|
||||
Accounts: []*apimodel.Account{},
|
||||
}, nil
|
||||
}
|
||||
// there's an actual error
|
||||
// BlocksGet ...
|
||||
func (p *Processor) BlocksGet(
|
||||
ctx context.Context,
|
||||
requestingAccount *gtsmodel.Account,
|
||||
page paging.Pager,
|
||||
) (*apimodel.PageableResponse, gtserror.WithCode) {
|
||||
blocks, err := p.state.DB.GetAccountBlocks(ctx,
|
||||
requestingAccount.ID,
|
||||
&page,
|
||||
)
|
||||
if err != nil && !errors.Is(err, db.ErrNoEntries) {
|
||||
return nil, gtserror.NewErrorInternalError(err)
|
||||
}
|
||||
|
||||
apiAccounts := []*apimodel.Account{}
|
||||
for _, a := range accounts {
|
||||
apiAccount, err := p.tc.AccountToAPIAccountBlocked(ctx, a)
|
||||
if err != nil {
|
||||
// Check for zero length.
|
||||
count := len(blocks)
|
||||
if len(blocks) == 0 {
|
||||
return util.EmptyPageableResponse(), nil
|
||||
}
|
||||
|
||||
var (
|
||||
items = make([]interface{}, 0, count)
|
||||
|
||||
// Set next + prev values before API converting
|
||||
// so the caller can still page even on error.
|
||||
nextMaxIDValue = blocks[count-1].ID
|
||||
prevMinIDValue = blocks[0].ID
|
||||
)
|
||||
|
||||
for _, block := range blocks {
|
||||
if block.TargetAccount == nil {
|
||||
// All models should be populated at this point.
|
||||
log.Warnf(ctx, "block target account was nil: %v", err)
|
||||
continue
|
||||
}
|
||||
apiAccounts = append(apiAccounts, apiAccount)
|
||||
}
|
||||
|
||||
return p.packageBlocksResponse(apiAccounts, "/api/v1/blocks", nextMaxID, prevMinID, limit)
|
||||
}
|
||||
|
||||
func (p *Processor) packageBlocksResponse(accounts []*apimodel.Account, path string, nextMaxID string, prevMinID string, limit int) (*apimodel.BlocksResponse, gtserror.WithCode) {
|
||||
resp := &apimodel.BlocksResponse{
|
||||
Accounts: []*apimodel.Account{},
|
||||
}
|
||||
resp.Accounts = accounts
|
||||
|
||||
// prepare the next and previous links
|
||||
if len(accounts) != 0 {
|
||||
protocol := config.GetProtocol()
|
||||
host := config.GetHost()
|
||||
|
||||
nextLink := &url.URL{
|
||||
Scheme: protocol,
|
||||
Host: host,
|
||||
Path: path,
|
||||
RawQuery: fmt.Sprintf("limit=%d&max_id=%s", limit, nextMaxID),
|
||||
// Convert target account to frontend API model.
|
||||
account, err := p.tc.AccountToAPIAccountBlocked(ctx, block.TargetAccount)
|
||||
if err != nil {
|
||||
log.Errorf(ctx, "error converting account to public api account: %v", err)
|
||||
continue
|
||||
}
|
||||
next := fmt.Sprintf("<%s>; rel=\"next\"", nextLink.String())
|
||||
|
||||
prevLink := &url.URL{
|
||||
Scheme: protocol,
|
||||
Host: host,
|
||||
Path: path,
|
||||
RawQuery: fmt.Sprintf("limit=%d&min_id=%s", limit, prevMinID),
|
||||
}
|
||||
prev := fmt.Sprintf("<%s>; rel=\"prev\"", prevLink.String())
|
||||
resp.LinkHeader = fmt.Sprintf("%s, %s", next, prev)
|
||||
// Append target to return items.
|
||||
items = append(items, account)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
return util.PackagePageableResponse(util.PageableResponseParams{
|
||||
Items: items,
|
||||
Path: "/api/v1/blocks",
|
||||
NextMaxIDKey: "max_id",
|
||||
PrevMinIDKey: "since_id",
|
||||
NextMaxIDValue: nextMaxIDValue,
|
||||
PrevMinIDValue: prevMinIDValue,
|
||||
Limit: page.Limit,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue