diff options
| author | 2025-10-17 19:41:06 +0200 | |
|---|---|---|
| committer | 2025-11-17 14:11:23 +0100 | |
| commit | 14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5 (patch) | |
| tree | 56b675dadea6b525336ec9c109d932c224df9df5 | |
| parent | [chore] update dependencies (#4507) (diff) | |
| download | gotosocial-14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5.tar.xz | |
[performance] reduce account stats database calls (#4496)
Reduces both code complexity and the number of separate database transactions we need to make by moving account statistics operations into the database as side-effects of the operations that effect them. In contrast to currently, where we manually update account statistics at the application layer.
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4496
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
| -rw-r--r-- | internal/cache/invalidate.go | 17 | ||||
| -rw-r--r-- | internal/db/bundb/relationship_follow.go | 284 | ||||
| -rw-r--r-- | internal/db/bundb/relationship_follow_req.go | 269 | ||||
| -rw-r--r-- | internal/db/bundb/relationship_test.go | 4 | ||||
| -rw-r--r-- | internal/db/bundb/status.go | 110 | ||||
| -rw-r--r-- | internal/db/bundb/util.go | 27 | ||||
| -rw-r--r-- | internal/processing/workers/fromclientapi.go | 77 | ||||
| -rw-r--r-- | internal/processing/workers/fromfediapi.go | 74 | ||||
| -rw-r--r-- | internal/processing/workers/util.go | 242 |
9 files changed, 450 insertions, 654 deletions
diff --git a/internal/cache/invalidate.go b/internal/cache/invalidate.go index 58c427050..acb602736 100644 --- a/internal/cache/invalidate.go +++ b/internal/cache/invalidate.go @@ -138,15 +138,20 @@ func (c *Caches) OnInvalidateFilterStatus(filterStatus *gtsmodel.FilterStatus) { } func (c *Caches) OnInvalidateFollow(follow *gtsmodel.Follow) { + bothAccountIDs := []string{ + follow.TargetAccountID, + follow.AccountID, + } + // Invalidate follow request with this same ID. c.DB.FollowRequest.Invalidate("ID", follow.ID) + // Invalidate both follow origin and target account stats. + c.DB.AccountStats.InvalidateIDs("AccountID", bothAccountIDs) + // Invalidate both follow origin and target as // possible lookup targets for visibility results. - c.Visibility.InvalidateIDs("ItemID", []string{ - follow.TargetAccountID, - follow.AccountID, - }) + c.Visibility.InvalidateIDs("ItemID", bothAccountIDs) // Track which of follow / target are local. localAccountIDs := make([]string, 0, 2) @@ -217,6 +222,10 @@ func (c *Caches) OnInvalidateFollowRequest(followReq *gtsmodel.FollowRequest) { // Invalidate follow with this same ID. c.DB.Follow.Invalidate("ID", followReq.ID) + // Invalidate follow target account stats. + c.DB.AccountStats.Invalidate("AccountID", + followReq.TargetAccountID) + // Invalidate ID slice cache. c.DB.FollowRequestIDs.Invalidate( diff --git a/internal/db/bundb/relationship_follow.go b/internal/db/bundb/relationship_follow.go index f37efa94b..ca0ac2bf8 100644 --- a/internal/db/bundb/relationship_follow.go +++ b/internal/db/bundb/relationship_follow.go @@ -189,7 +189,7 @@ func (r *relationshipDB) getFollow(ctx context.Context, lookup string, dbQuery f func (r *relationshipDB) PopulateFollow(ctx context.Context, follow *gtsmodel.Follow) error { var ( err error - errs = gtserror.NewMultiError(2) + errs gtserror.MultiError ) if follow.Account == nil { @@ -218,8 +218,10 @@ func (r *relationshipDB) PopulateFollow(ctx context.Context, follow *gtsmodel.Fo } func (r *relationshipDB) PutFollow(ctx context.Context, follow *gtsmodel.Follow) error { - return r.state.Caches.DB.Follow.Store(follow, func() error { - _, err := r.db.NewInsert().Model(follow).Exec(ctx) + return r.insertFollow(ctx, follow, func(tx bun.Tx) error { + _, err := tx.NewInsert(). + Model(follow). + Exec(ctx) return err }) } @@ -239,7 +241,6 @@ func (r *relationshipDB) UpdateFollow(ctx context.Context, follow *gtsmodel.Foll Exec(ctx); err != nil { return err } - return nil }) } @@ -249,104 +250,63 @@ func (r *relationshipDB) DeleteFollow( sourceAccountID string, targetAccountID string, ) error { + return r.deleteFollow(ctx, func(tx bun.Tx) (*gtsmodel.Follow, error) { + var deleted gtsmodel.Follow + deleted.AccountID = sourceAccountID + deleted.TargetAccountID = targetAccountID + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("account_id"), sourceAccountID). + Where("? = ?", bun.Ident("target_account_id"), targetAccountID). + Returning("?", bun.Ident("id")). + Exec(ctx); err != nil { + return nil, err + } - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.Follow - deleted.AccountID = sourceAccountID - deleted.TargetAccountID = targetAccountID - - // Delete follow from origin - // account, to targeting account, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("account_id"), sourceAccountID). - Where("? = ?", bun.Ident("target_account_id"), targetAccountID). - Returning("?", bun.Ident("id")). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with source / target account IDs, - // manually calling invalidate hook in case it isn't cached. - r.state.Caches.DB.Follow.Invalidate("AccountID,TargetAccountID", - sourceAccountID, targetAccountID) - r.state.Caches.OnInvalidateFollow(&deleted) - - // Delete every list entry that was created targetting this follow ID. - if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil { - return gtserror.Newf("error deleting list entries: %w", err) - } - - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteFollowByID(ctx context.Context, id string) error { - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.Follow - deleted.ID = id - - // Delete follow with given ID, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("id"), id). - Returning("?, ?", - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with ID, manually - // call invalidate hook in case not cached. - r.state.Caches.DB.Follow.Invalidate("ID", id) - r.state.Caches.OnInvalidateFollow(&deleted) - - // Delete every list entry that was created targetting this follow ID. - if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, id); err != nil { - return gtserror.Newf("error deleting list entries: %w", err) - } + return r.deleteFollow(ctx, func(tx bun.Tx) (*gtsmodel.Follow, error) { + var deleted gtsmodel.Follow + deleted.ID = id + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("id"), id). + Returning("?, ?", + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + return nil, err + } - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteFollowByURI(ctx context.Context, uri string) error { - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.Follow - - // Delete follow with given URI, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("uri"), uri). - Returning("?, ?, ?", - bun.Ident("id"), - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with URI, manually - // call invalidate hook in case not cached. - r.state.Caches.DB.Follow.Invalidate("URI", uri) - r.state.Caches.OnInvalidateFollow(&deleted) - - // Delete every list entry that was created targetting this follow ID. - if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, deleted.ID); err != nil { - return gtserror.Newf("error deleting list entries: %w", err) - } + return r.deleteFollow(ctx, func(tx bun.Tx) (*gtsmodel.Follow, error) { + var deleted gtsmodel.Follow + deleted.URI = uri + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("uri"), uri). + Returning("?, ?, ?", + bun.Ident("id"), + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + return nil, err + } - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID string) error { @@ -354,24 +314,52 @@ func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID str // deleted for cache invaliation. var deleted []*gtsmodel.Follow - // Delete all follows either from - // account, or targeting account, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - WhereOr("? = ? OR ? = ?", - bun.Ident("account_id"), - accountID, - bun.Ident("target_account_id"), - accountID, - ). - Returning("?, ?, ?", - bun.Ident("id"), - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { + if err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Delete all follows either from + // account, or targeting account, + // returning the deleted models. + if _, err := tx.NewDelete(). + Model(&deleted). + WhereOr("? = ? OR ? = ?", + bun.Ident("account_id"), + accountID, + bun.Ident("target_account_id"), + accountID, + ). + Returning("?, ?, ?", + bun.Ident("id"), + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + + // the RETURNING here will cause an ErrNoRows + // to be returned on DELETE, which is caught + // outside this RunInTx() func, and ensures we + // return early here to *not* update statistics. + return err + } + + for _, follow := range deleted { + // Decrement origin following statistic. + if err := decrementAccountStats(ctx, tx, + "following_count", + follow.AccountID, + ); err != nil { + return err + } + + // Decrement target followers statistic. + if err := decrementAccountStats(ctx, tx, + "followers_count", + follow.TargetAccountID, + ); err != nil { + return err + } + } + + return nil + }); err != nil && !errors.Is(err, db.ErrNoEntries) { return err } @@ -397,3 +385,77 @@ func (r *relationshipDB) DeleteAccountFollows(ctx context.Context, accountID str return nil } + +func (r *relationshipDB) insertFollow(ctx context.Context, follow *gtsmodel.Follow, insert func(bun.Tx) error) error { + return r.state.Caches.DB.Follow.Store(follow, func() error { + return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Perform the insert operation. + if err := insert(tx); err != nil { + return gtserror.Newf("error inserting follow: %w", err) + } + + // Increment origin following statistic. + if err := incrementAccountStats(ctx, tx, + "following_count", + follow.AccountID, + ); err != nil { + return err + } + + // Increment target followers statistic. + return incrementAccountStats(ctx, tx, + "followers_count", + follow.TargetAccountID, + ) + }) + }) +} + +func (r *relationshipDB) deleteFollow(ctx context.Context, delete func(bun.Tx) (*gtsmodel.Follow, error)) error { + var follow *gtsmodel.Follow + + if err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) (err error) { + // Perform delete operation. + follow, err = delete(tx) + if err != nil { + + // the RETURNING here will cause an ErrNoRows + // to be returned on DELETE, which is caught + // outside this RunInTx() func, and ensures we + // return early here to *not* update statistics. + return err + } + + // Decrement origin following statistic. + if err := decrementAccountStats(ctx, tx, + "following_count", + follow.AccountID, + ); err != nil { + return err + } + + // Decrement target followers statistic. + return decrementAccountStats(ctx, tx, + "followers_count", + follow.TargetAccountID, + ) + }); err != nil && !errors.Is(err, db.ErrNoEntries) { + return err + } + + if follow == nil { + return nil + } + + // Invalidate cached follow with ID, manually + // call invalidate hook in case not cached. + r.state.Caches.DB.Follow.Invalidate("ID", follow.ID) + r.state.Caches.OnInvalidateFollow(follow) + + // Delete every list entry that was created targetting this follow ID. + if err := r.state.DB.DeleteAllListEntriesByFollows(ctx, follow.ID); err != nil { + return gtserror.Newf("error deleting list entries: %w", err) + } + + return nil +} diff --git a/internal/db/bundb/relationship_follow_req.go b/internal/db/bundb/relationship_follow_req.go index 513f5abb0..6a9fbea09 100644 --- a/internal/db/bundb/relationship_follow_req.go +++ b/internal/db/bundb/relationship_follow_req.go @@ -167,7 +167,7 @@ func (r *relationshipDB) getFollowRequest(ctx context.Context, lookup string, db func (r *relationshipDB) PopulateFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error { var ( err error - errs = gtserror.NewMultiError(2) + errs gtserror.MultiError ) if follow.Account == nil { @@ -196,8 +196,10 @@ func (r *relationshipDB) PopulateFollowRequest(ctx context.Context, follow *gtsm } func (r *relationshipDB) PutFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest) error { - return r.state.Caches.DB.FollowRequest.Store(follow, func() error { - _, err := r.db.NewInsert().Model(follow).Exec(ctx) + return r.insertFollowRequest(ctx, follow, func(tx bun.Tx) error { + _, err := tx.NewInsert(). + Model(follow). + Exec(ctx) return err }) } @@ -208,22 +210,17 @@ func (r *relationshipDB) UpdateFollowRequest(ctx context.Context, followRequest // If we're updating by column, ensure "updated_at" is included. columns = append(columns, "updated_at") } - return r.state.Caches.DB.FollowRequest.Store(followRequest, func() error { - if _, err := r.db.NewUpdate(). + _, err := r.db.NewUpdate(). Model(followRequest). Where("? = ?", bun.Ident("follow_request.id"), followRequest.ID). Column(columns...). - Exec(ctx); err != nil { - return err - } - - return nil + Exec(ctx) + return err }) } func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) (*gtsmodel.Follow, error) { - // Get original follow request. followReq, err := r.GetFollowRequest(ctx, sourceAccountID, targetAccountID) if err != nil { return nil, err @@ -242,11 +239,9 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI Notify: followReq.Notify, } - if err := r.state.Caches.DB.Follow.Store(follow, func() error { - // If the follow already exists, just - // replace the URI with the new one. - _, err := r.db. - NewInsert(). + // Insert the new follow modelled after request into database. + if err := r.insertFollow(ctx, follow, func(tx bun.Tx) error { + _, err := tx.NewInsert(). Model(follow). On("CONFLICT (?,?) DO UPDATE set ? = ?", bun.Ident("account_id"), bun.Ident("target_account_id"), bun.Ident("uri"), follow.URI). Exec(ctx) @@ -255,12 +250,8 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI return nil, err } - // Delete original follow request. - if _, err := r.db. - NewDelete(). - Table("follow_requests"). - Where("? = ?", bun.Ident("id"), followReq.ID). - Exec(ctx); err != nil { + // Delete the follow request now that it's accepted and not needed. + if err := r.DeleteFollowRequestByID(ctx, followReq.ID); err != nil { return nil, err } @@ -275,12 +266,9 @@ func (r *relationshipDB) AcceptFollowRequest(ctx context.Context, sourceAccountI } func (r *relationshipDB) RejectFollowRequest(ctx context.Context, sourceAccountID string, targetAccountID string) error { - // Delete follow request first. if err := r.DeleteFollowRequest(ctx, sourceAccountID, targetAccountID); err != nil { return err } - - // Delete follow request notification return r.state.DB.DeleteNotifications(ctx, []gtsmodel.NotificationType{ gtsmodel.NotificationFollowRequest, }, targetAccountID, sourceAccountID) @@ -291,89 +279,63 @@ func (r *relationshipDB) DeleteFollowRequest( sourceAccountID string, targetAccountID string, ) error { + return r.deleteFollowRequest(ctx, func(tx bun.Tx) (*gtsmodel.FollowRequest, error) { + var deleted gtsmodel.FollowRequest + deleted.AccountID = sourceAccountID + deleted.TargetAccountID = targetAccountID + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("account_id"), sourceAccountID). + Where("? = ?", bun.Ident("target_account_id"), targetAccountID). + Returning("?", bun.Ident("id")). + Exec(ctx); err != nil { + return nil, err + } - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.FollowRequest - deleted.AccountID = sourceAccountID - deleted.TargetAccountID = targetAccountID - - // Delete all follow reqs either - // from account, or targeting account, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("account_id"), sourceAccountID). - Where("? = ?", bun.Ident("target_account_id"), targetAccountID). - Returning("?", bun.Ident("id")). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with source / target account IDs, - // manually calling invalidate hook in case it isn't cached. - r.state.Caches.DB.FollowRequest.Invalidate("AccountID,TargetAccountID", - sourceAccountID, targetAccountID) - r.state.Caches.OnInvalidateFollowRequest(&deleted) - - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteFollowRequestByID(ctx context.Context, id string) error { - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.FollowRequest - deleted.ID = id - - // Delete follow with given URI, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("id"), id). - Returning("?, ?", - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with URI, manually - // call invalidate hook in case not cached. - r.state.Caches.DB.FollowRequest.Invalidate("ID", id) - r.state.Caches.OnInvalidateFollowRequest(&deleted) + return r.deleteFollowRequest(ctx, func(tx bun.Tx) (*gtsmodel.FollowRequest, error) { + var deleted gtsmodel.FollowRequest + deleted.ID = id + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("id"), id). + Returning("?, ?", + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + return nil, err + } - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteFollowRequestByURI(ctx context.Context, uri string) error { - // Gather necessary fields from - // deleted for cache invaliation. - var deleted gtsmodel.FollowRequest - - // Delete follow with given URI, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("uri"), uri). - Returning("?, ?, ?", - bun.Ident("id"), - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { - return err - } - - // Invalidate cached follow with URI, manually - // call invalidate hook in case not cached. - r.state.Caches.DB.FollowRequest.Invalidate("URI", uri) - r.state.Caches.OnInvalidateFollowRequest(&deleted) + return r.deleteFollowRequest(ctx, func(tx bun.Tx) (*gtsmodel.FollowRequest, error) { + var deleted gtsmodel.FollowRequest + deleted.URI = uri + + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("uri"), uri). + Returning("?, ?, ?", + bun.Ident("id"), + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + return nil, err + } - return nil + return &deleted, nil + }) } func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accountID string) error { @@ -381,24 +343,44 @@ func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accoun // deleted for cache invaliation. var deleted []*gtsmodel.FollowRequest - // Delete all follows either from - // account, or targeting account, - // returning the deleted models. - if _, err := r.db.NewDelete(). - Model(&deleted). - WhereOr("? = ? OR ? = ?", - bun.Ident("account_id"), - accountID, - bun.Ident("target_account_id"), - accountID, - ). - Returning("?, ?, ?", - bun.Ident("id"), - bun.Ident("account_id"), - bun.Ident("target_account_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { + if err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Delete all follows either from + // account, or targeting account, + // returning the deleted models. + if _, err := tx.NewDelete(). + Model(&deleted). + WhereOr("? = ? OR ? = ?", + bun.Ident("account_id"), + accountID, + bun.Ident("target_account_id"), + accountID, + ). + Returning("?, ?, ?", + bun.Ident("id"), + bun.Ident("account_id"), + bun.Ident("target_account_id"), + ). + Exec(ctx); err != nil { + + // the RETURNING here will cause an ErrNoRows + // to be returned on DELETE, which is caught + // outside this RunInTx() func, and ensures we + // return early here to *not* update statistics. + return err + } + + for _, follow := range deleted { + // Decrement target follow requests count. + if err := decrementAccountStats(ctx, tx, + "follow_requests_count", + follow.TargetAccountID, + ); err != nil { + return err + } + } + + return nil + }); err != nil && !errors.Is(err, db.ErrNoEntries) { return err } @@ -414,3 +396,56 @@ func (r *relationshipDB) DeleteAccountFollowRequests(ctx context.Context, accoun return nil } + +func (r *relationshipDB) insertFollowRequest(ctx context.Context, follow *gtsmodel.FollowRequest, insert func(bun.Tx) error) error { + return r.state.Caches.DB.FollowRequest.Store(follow, func() error { + return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Perform the insert operation. + if err := insert(tx); err != nil { + return gtserror.Newf("error inserting follow request: %w", err) + } + + // Increment target follow requests count. + return incrementAccountStats(ctx, tx, + "follow_requests_count", + follow.TargetAccountID, + ) + }) + }) +} + +func (r *relationshipDB) deleteFollowRequest(ctx context.Context, delete func(bun.Tx) (*gtsmodel.FollowRequest, error)) error { + var follow *gtsmodel.FollowRequest + + if err := r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) (err error) { + // Perform delete operation. + follow, err = delete(tx) + if err != nil { + + // the RETURNING here will cause an ErrNoRows + // to be returned on DELETE, which is caught + // outside this RunInTx() func, and ensures we + // return early here to *not* update statistics. + return err + } + + // Decrement target follow requests count. + return decrementAccountStats(ctx, tx, + "follow_requests_count", + follow.TargetAccountID, + ) + }); err != nil && !errors.Is(err, db.ErrNoEntries) { + return err + } + + if follow == nil { + return nil + } + + // Invalidate cached follow with ID, manually + // call invalidate hook in case not cached. + r.state.Caches.DB.FollowRequest.Invalidate("ID", follow.ID) + r.state.Caches.OnInvalidateFollowRequest(follow) + + return nil +} diff --git a/internal/db/bundb/relationship_test.go b/internal/db/bundb/relationship_test.go index a8b456286..c1336aa50 100644 --- a/internal/db/bundb/relationship_test.go +++ b/internal/db/bundb/relationship_test.go @@ -194,7 +194,7 @@ func (suite *RelationshipTestSuite) TestGetFollowBy() { // Attempt to place the follow in database (if not already). if err := suite.db.PutFollow(ctx, follow); err != nil { - if err != db.ErrAlreadyExists { + if !errors.Is(err, db.ErrAlreadyExists) { // Unrecoverable database error. t.Fatalf("error creating follow: %v", err) } @@ -306,7 +306,7 @@ func (suite *RelationshipTestSuite) TestGetFollowRequestBy() { // Attempt to place the follow in database (if not already). if err := suite.db.PutFollowRequest(ctx, followReq); err != nil { - if err != db.ErrAlreadyExists { + if !errors.Is(err, db.ErrAlreadyExists) { // Unrecoverable database error. t.Fatalf("error creating follow request: %v", err) } diff --git a/internal/db/bundb/status.go b/internal/db/bundb/status.go index 5b72f5fbe..3dbd5f671 100644 --- a/internal/db/bundb/status.go +++ b/internal/db/bundb/status.go @@ -579,10 +579,21 @@ func insertStatus(ctx context.Context, tx bun.Tx, status *gtsmodel.Status) error return gtserror.Newf("error inserting status: %w", err) } - return nil + // Increment status author statistics. + return incrementAccountStats(ctx, tx, + "statuses_count", + status.AccountID, + ) } func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, columns ...string) error { + var isPinning bool + for _, col := range columns { + if col == "pinned_at" { + isPinning = true + break + } + } return s.state.Caches.DB.Status.Store(status, func() error { // It is safe to run this database transaction within cache.Store // as the cache does not attempt a mutex lock until AFTER hook. @@ -634,12 +645,33 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co } // Finally, update the status - _, err := tx.NewUpdate(). + if _, err := tx.NewUpdate(). Model(status). Column(columns...). Where("? = ?", bun.Ident("status.id"), status.ID). - Exec(ctx) - return err + Exec(ctx); err != nil { + return err + } + + switch { + case !isPinning: + // nothing + return nil + + case !status.PinnedAt.IsZero(): + // Increment author pinned statistics. + return decrementAccountStats(ctx, tx, + "statuses_pinned_count", + status.AccountID, + ) + + default: + // Decrement author pinned statistics. + return decrementAccountStats(ctx, tx, + "statuses_pinned_count", + status.AccountID, + ) + } }) }) } @@ -653,10 +685,30 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error { // Delete status from database and any related links in a transaction. if err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // delete the status itself + if _, err := tx.NewDelete(). + Model(&deleted). + Where("? = ?", bun.Ident("id"), id). + Returning("?, ?, ?, ?, ?, ?", + bun.Ident("account_id"), + bun.Ident("boost_of_id"), + bun.Ident("in_reply_to_id"), + bun.Ident("attachments"), + bun.Ident("poll_id"), + bun.Ident("pinned_at"), + ). + Exec(ctx); err != nil { + + // the RETURNING here will cause an ErrNoRows + // to be returned on DELETE, which is caught + // outside this RunInTx() func, and ensures we + // return early here to *not* update statistics. + return err + } + // delete links between this // status and any emojis it uses - if _, err := tx. - NewDelete(). + if _, err := tx.NewDelete(). TableExpr("? AS ?", bun.Ident("status_to_emojis"), bun.Ident("status_to_emoji")). Where("? = ?", bun.Ident("status_to_emoji.status_id"), id). Exec(ctx); err != nil { @@ -665,33 +717,33 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error { // delete links between this // status and any tags it uses - if _, err := tx. - NewDelete(). + if _, err := tx.NewDelete(). TableExpr("? AS ?", bun.Ident("status_to_tags"), bun.Ident("status_to_tag")). Where("? = ?", bun.Ident("status_to_tag.status_id"), id). Exec(ctx); err != nil { return err } - // delete the status itself - if _, err := tx. - NewDelete(). - Model(&deleted). - Where("? = ?", bun.Ident("id"), id). - Returning("?, ?, ?, ?, ?", - bun.Ident("account_id"), - bun.Ident("boost_of_id"), - bun.Ident("in_reply_to_id"), - bun.Ident("attachments"), - bun.Ident("poll_id"), - ). - Exec(ctx); err != nil && - !errors.Is(err, db.ErrNoEntries) { + // decrement status author statistics. + if err := decrementAccountStats(ctx, tx, + "statuses_count", + deleted.AccountID, + ); err != nil { return err } + if !deleted.PinnedAt.IsZero() { + // decrement author pinned statistics. + if err := decrementAccountStats(ctx, tx, + "statuses_pinned_count", + deleted.AccountID, + ); err != nil { + return err + } + } + return nil - }); err != nil { + }); err != nil && !errors.Is(err, db.ErrNoEntries) { return err } @@ -788,7 +840,8 @@ func (s *statusDB) getStatusReplyIDs(ctx context.Context, statusID string) ([]st return s.state.Caches.DB.InReplyToIDs.Load(statusID, func() ([]string, error) { var statusIDs []string - // Status reply IDs not in cache, perform DB query! + // Status reply IDs not in + // cache, perform DB query! if err := s.db. NewSelect(). Table("statuses"). @@ -832,7 +885,8 @@ func (s *statusDB) getStatusBoostIDs(ctx context.Context, statusID string) ([]st return s.state.Caches.DB.BoostOfIDs.Load(statusID, func() ([]string, error) { var statusIDs []string - // Status boost IDs not in cache, perform DB query! + // Status boost IDs not in + // cache, perform DB query! if err := s.db. NewSelect(). Table("statuses"). @@ -966,10 +1020,7 @@ func (s *statusDB) GetStatusInteractions( return interactions, nil } -func (s *statusDB) GetStatusByEditID( - ctx context.Context, - editID string, -) (*gtsmodel.Status, error) { +func (s *statusDB) GetStatusByEditID(ctx context.Context, editID string) (*gtsmodel.Status, error) { edit, err := s.state.DB.GetStatusEditByID( gtscontext.SetBarebones(ctx), editID, @@ -977,6 +1028,5 @@ func (s *statusDB) GetStatusByEditID( if err != nil { return nil, err } - return s.GetStatusByID(ctx, edit.StatusID) } diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index 39849ba73..ac58dd6f4 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -25,6 +25,8 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/cache" "code.superseriousbusiness.org/gotosocial/internal/db" + "code.superseriousbusiness.org/gotosocial/internal/gtserror" + "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" "code.superseriousbusiness.org/gotosocial/internal/log" "code.superseriousbusiness.org/gotosocial/internal/paging" "github.com/uptrace/bun" @@ -151,6 +153,7 @@ func notExists(ctx context.Context, query *bun.SelectQuery) (bool, error) { // loadPagedIDs loads a page of IDs from given SliceCache by `key`, resorting to `loadDESC` if required. Uses `page` to sort + page resulting IDs. // NOTE: IDs returned from `cache` / `loadDESC` MUST be in descending order, otherwise paging will not work correctly / return things out of order. func loadPagedIDs(cache *cache.SliceCache[string], key string, page *paging.Page, loadDESC func() ([]string, error)) ([]string, error) { + // Check cache for IDs, else load. ids, err := cache.Load(key, loadDESC) if err != nil { @@ -171,6 +174,30 @@ func loadPagedIDs(cache *cache.SliceCache[string], key string, page *paging.Page return ids, nil } +// incrementAccountStats will increment the given column in the `account_stats` table matching `account_id`. +func incrementAccountStats(ctx context.Context, tx bun.Tx, col bun.Ident, accountID string) error { + if _, err := tx.NewUpdate(). + Model((*gtsmodel.AccountStats)(nil)). + Where("? = ?", bun.Ident("account_id"), accountID). + Set("? = (? + 1)", col, col). + Exec(ctx); err != nil { + return gtserror.Newf("error updating %s: %w", col, err) + } + return nil +} + +// decrementAccountStats will decrement the given column in the `account_stats` table matching `account_id`. +func decrementAccountStats(ctx context.Context, tx bun.Tx, col bun.Ident, accountID string) error { + if _, err := tx.NewUpdate(). + Model((*gtsmodel.AccountStats)(nil)). + Where("? = ?", bun.Ident("account_id"), accountID). + Set("? = (? - 1)", col, col). + Exec(ctx); err != nil { + return gtserror.Newf("error updating %s: %w", col, err) + } + return nil +} + // updateWhere parses []db.Where and adds it to the given update query. func updateWhere(q *bun.UpdateQuery, where []db.Where) { for _, w := range where { diff --git a/internal/processing/workers/fromclientapi.go b/internal/processing/workers/fromclientapi.go index 9cdbcc548..992f6d9e8 100644 --- a/internal/processing/workers/fromclientapi.go +++ b/internal/processing/workers/fromclientapi.go @@ -351,11 +351,6 @@ func (p *clientAPI) CreateStatus(ctx context.Context, cMsg *messages.FromClientA // Don't return, just continue as normal. } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // We specifically do not timeline // or notify for backfilled statuses, // as these are more for archival than @@ -451,11 +446,6 @@ func (p *clientAPI) CreateFollowReq(ctx context.Context, cMsg *messages.FromClie }) } - // Update stats for the target account. - if err := p.utils.incrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.surface.notifyFollowRequest(ctx, followRequest); err != nil { log.Errorf(ctx, "error notifying follow request: %v", err) } @@ -659,11 +649,6 @@ func (p *clientAPI) CreateAnnounce(ctx context.Context, cMsg *messages.FromClien // Don't return, just continue as normal. } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, cMsg.Origin, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the boost wrapper status. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -840,20 +825,6 @@ func (p *clientAPI) AcceptFollow(ctx context.Context, cMsg *messages.FromClientA return gtserror.Newf("%T not parseable as *gtsmodel.Follow", cMsg.GTSModel) } - // Update stats for the target account. - if err := p.utils.decrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - if err := p.utils.incrementFollowersCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the origin account. - if err := p.utils.incrementFollowingCount(ctx, cMsg.Origin); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.surface.notifyFollow(ctx, follow); err != nil { log.Errorf(ctx, "error notifying follow: %v", err) } @@ -871,13 +842,7 @@ func (p *clientAPI) RejectFollowRequest(ctx context.Context, cMsg *messages.From return gtserror.Newf("%T not parseable as *gtsmodel.FollowRequest", cMsg.GTSModel) } - // Update stats for the target account. - if err := p.utils.decrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - if err := p.federate.RejectFollow( - ctx, + if err := p.federate.RejectFollow(ctx, p.converter.FollowRequestToFollow(ctx, followReq), ); err != nil { log.Errorf(ctx, "error federating follow reject: %v", err) @@ -892,16 +857,6 @@ func (p *clientAPI) UndoFollow(ctx context.Context, cMsg *messages.FromClientAPI return gtserror.Newf("%T not parseable as *gtsmodel.Follow", cMsg.GTSModel) } - // Update stats for the origin account. - if err := p.utils.decrementFollowingCount(ctx, cMsg.Origin); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the target account. - if err := p.utils.decrementFollowersCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if follow.Account.IsLocal() { // Remove posts by target from origin's timelines. p.surface.removeRelationshipFromTimelines(ctx, @@ -965,11 +920,6 @@ func (p *clientAPI) UndoAnnounce(ctx context.Context, cMsg *messages.FromClientA return gtserror.Newf("db error deleting status: %w", err) } - // Update stats for the origin account. - if err := p.utils.decrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Delete the boost wrapper status from timelines. p.surface.deleteStatusFromTimelines(ctx, status.ID) @@ -1030,11 +980,6 @@ func (p *clientAPI) DeleteStatus(ctx context.Context, cMsg *messages.FromClientA log.Errorf(ctx, "error wiping status: %v", err) } - // Update stats for the origin account. - if err := p.utils.decrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.federate.DeleteStatus(ctx, status); err != nil { log.Errorf(ctx, "error federating status delete: %v", err) } @@ -1258,15 +1203,7 @@ func (p *clientAPI) AcceptReply(ctx context.Context, cMsg *messages.FromClientAP return gtserror.Newf("%T not parseable as *gtsmodel.InteractionRequest", cMsg.GTSModel) } - var ( - interactingAcct = req.InteractingAccount - reply = req.Reply - ) - - // Update stats for the reply author account. - if err := p.utils.incrementStatusesCount(ctx, interactingAcct, reply); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } + reply := req.Reply // Timeline the reply + notify relevant accounts. if err := p.surface.timelineAndNotifyStatus(ctx, reply); err != nil { @@ -1291,15 +1228,7 @@ func (p *clientAPI) AcceptAnnounce(ctx context.Context, cMsg *messages.FromClien return gtserror.Newf("%T not parseable as *gtsmodel.InteractionRequest", cMsg.GTSModel) } - var ( - interactingAcct = req.InteractingAccount - boost = req.Announce - ) - - // Update stats for the boost author account. - if err := p.utils.incrementStatusesCount(ctx, interactingAcct, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } + boost := req.Announce // Timeline and notify the announce. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { diff --git a/internal/processing/workers/fromfediapi.go b/internal/processing/workers/fromfediapi.go index 797a2d9c6..b64b8bbec 100644 --- a/internal/processing/workers/fromfediapi.go +++ b/internal/processing/workers/fromfediapi.go @@ -369,11 +369,6 @@ func (p *fediAPI) CreateStatus(ctx context.Context, fMsg *messages.FromFediAPI) // Don't return, just continue as normal. } - // Update stats for the remote account. - if err := p.utils.incrementStatusesCount(ctx, fMsg.Requesting, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.surface.timelineAndNotifyStatus(ctx, status); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) } @@ -483,11 +478,6 @@ func (p *fediAPI) CreateReplyRequest(ctx context.Context, fMsg *messages.FromFed log.Errorf(ctx, "error federating accept: %v", err) } - // Update stats for the replying account. - if err := p.utils.incrementStatusesCount(ctx, fMsg.Requesting, reply); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline the reply + notify recipient(s). if err := p.surface.timelineAndNotifyStatus(ctx, reply); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -597,11 +587,6 @@ func (p *fediAPI) CreateFollowReq(ctx context.Context, fMsg *messages.FromFediAP log.Errorf(ctx, "error notifying follow request: %v", err) } - // And update stats for the local account. - if err := p.utils.incrementFollowRequestsCount(ctx, fMsg.Receiving); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - return nil } @@ -617,16 +602,6 @@ func (p *fediAPI) CreateFollowReq(ctx context.Context, fMsg *messages.FromFediAP return gtserror.Newf("error accepting follow request: %w", err) } - // Update stats for the local account. - if err := p.utils.incrementFollowersCount(ctx, fMsg.Receiving); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the remote account. - if err := p.utils.incrementFollowingCount(ctx, fMsg.Requesting); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.federate.AcceptFollow(ctx, follow); err != nil { log.Errorf(ctx, "error federating follow request accept: %v", err) } @@ -913,11 +888,6 @@ func (p *fediAPI) CreateAnnounce(ctx context.Context, fMsg *messages.FromFediAPI // Don't return, just continue as normal. } - // Update stats for the remote account. - if err := p.utils.incrementStatusesCount(ctx, fMsg.Requesting, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the announce. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -1014,11 +984,6 @@ func (p *fediAPI) CreateAnnounceRequest(ctx context.Context, fMsg *messages.From log.Errorf(ctx, "error federating accept: %v", err) } - // Update stats for the boosting account. - if err := p.utils.incrementStatusesCount(ctx, fMsg.Requesting, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline the boost + notify recipient(s). if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -1141,20 +1106,6 @@ func (p *fediAPI) UpdateAccount(ctx context.Context, fMsg *messages.FromFediAPI) } func (p *fediAPI) AcceptFollow(ctx context.Context, fMsg *messages.FromFediAPI) error { - // Update stats for the remote account. - if err := p.utils.decrementFollowRequestsCount(ctx, fMsg.Requesting); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - if err := p.utils.incrementFollowersCount(ctx, fMsg.Requesting); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the local account. - if err := p.utils.incrementFollowingCount(ctx, fMsg.Receiving); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - return nil } @@ -1170,11 +1121,6 @@ func (p *fediAPI) AcceptReply(ctx context.Context, fMsg *messages.FromFediAPI) e return gtserror.Newf("%T not parseable as *gtsmodel.Status", fMsg.GTSModel) } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, reply.Account, reply); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the status. if err := p.surface.timelineAndNotifyStatus(ctx, reply); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -1274,11 +1220,6 @@ func (p *fediAPI) AcceptPoliteReplyRequest(ctx context.Context, fMsg *messages.F return gtserror.Newf("error populating status: %w", err) } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, reply.Account, reply); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the status. if err := p.surface.timelineAndNotifyStatus(ctx, reply); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -1302,11 +1243,6 @@ func (p *fediAPI) AcceptAnnounce(ctx context.Context, fMsg *messages.FromFediAPI return gtserror.Newf("%T not parseable as *gtsmodel.Status", fMsg.GTSModel) } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, boost.Account, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the boost wrapper status. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -1455,11 +1391,6 @@ func (p *fediAPI) DeleteStatus(ctx context.Context, fMsg *messages.FromFediAPI) log.Errorf(ctx, "error wiping status: %v", err) } - // Update stats for the remote account. - if err := p.utils.decrementStatusesCount(ctx, fMsg.Requesting, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if status.InReplyToID != "" { // Interaction counts changed on the replied status; // uncache the prepared version from all timelines. @@ -1661,11 +1592,6 @@ func (p *fediAPI) UndoAnnounce( return gtserror.Newf("db error deleting boost: %w", err) } - // Update statuses count for the requesting account. - if err := p.utils.decrementStatusesCount(ctx, fMsg.Requesting, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Remove the boost wrapper from all timelines. p.surface.deleteStatusFromTimelines(ctx, boost.ID) diff --git a/internal/processing/workers/util.go b/internal/processing/workers/util.go index 6382887eb..0aa0febf0 100644 --- a/internal/processing/workers/util.go +++ b/internal/processing/workers/util.go @@ -31,7 +31,6 @@ import ( "code.superseriousbusiness.org/gotosocial/internal/processing/media" "code.superseriousbusiness.org/gotosocial/internal/state" "code.superseriousbusiness.org/gotosocial/internal/typeutils" - "code.superseriousbusiness.org/gotosocial/internal/util" ) // util provides util functions used by both @@ -285,247 +284,6 @@ func (u *utils) redirectFollowers( return true } -func (u *utils) incrementStatusesCount( - ctx context.Context, - account *gtsmodel.Account, - status *gtsmodel.Status, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update status meta for account. - *account.Stats.StatusesCount++ - account.Stats.LastStatusAt = status.CreatedAt - - // Update details in the database for stats. - if err := u.state.DB.UpdateAccountStats(ctx, - account.Stats, - "statuses_count", - "last_status_at", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) decrementStatusesCount( - ctx context.Context, - account *gtsmodel.Account, - status *gtsmodel.Status, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update status meta for account (safely checking for zero value). - *account.Stats.StatusesCount = util.Decr(*account.Stats.StatusesCount) - - if !status.PinnedAt.IsZero() { - // Update status pinned count for account (safely checking for zero value). - *account.Stats.StatusesPinnedCount = util.Decr(*account.Stats.StatusesPinnedCount) - } - - // Update details in the database for stats. - if err := u.state.DB.UpdateAccountStats(ctx, - account.Stats, - "statuses_count", - "statuses_pinned_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) incrementFollowersCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by incrementing followers - // count by one and setting last posted. - *account.Stats.FollowersCount++ - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "followers_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) decrementFollowersCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by decrementing - // followers count by one. - // - // Clamp to 0 to avoid funny business. - *account.Stats.FollowersCount-- - if *account.Stats.FollowersCount < 0 { - *account.Stats.FollowersCount = 0 - } - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "followers_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) incrementFollowingCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by incrementing - // followers count by one. - *account.Stats.FollowingCount++ - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "following_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) decrementFollowingCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by decrementing - // following count by one. - // - // Clamp to 0 to avoid funny business. - *account.Stats.FollowingCount-- - if *account.Stats.FollowingCount < 0 { - *account.Stats.FollowingCount = 0 - } - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "following_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) incrementFollowRequestsCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by incrementing - // follow requests count by one. - *account.Stats.FollowRequestsCount++ - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "follow_requests_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - -func (u *utils) decrementFollowRequestsCount( - ctx context.Context, - account *gtsmodel.Account, -) error { - // Lock on this account since we're changing stats. - unlock := u.state.ProcessingLocks.Lock(account.URI) - defer unlock() - - // Ensure account stats are populated. - if err := u.state.DB.PopulateAccountStats(ctx, account); err != nil { - return gtserror.Newf("db error getting account stats: %w", err) - } - - // Update stats by decrementing - // follow requests count by one. - // - // Clamp to 0 to avoid funny business. - *account.Stats.FollowRequestsCount-- - if *account.Stats.FollowRequestsCount < 0 { - *account.Stats.FollowRequestsCount = 0 - } - if err := u.state.DB.UpdateAccountStats( - ctx, - account.Stats, - "follow_requests_count", - ); err != nil { - return gtserror.Newf("db error updating account stats: %w", err) - } - - return nil -} - // impoliteFaveRequest stores an interaction request // for the given fave, and notifies the interactee. // |
