[performance] cache mute check results (#4202)

This separates our the user mute handling from the typeconverter code, and creates a new "mutes" filter type (in a similar vein to the visibility filter) subpkg with its own result cache. This is a heavy mix of both chore given that mute calculation shouldn't have been handled in the conversion to frontend API types, and a performance bonus since we don't need to load and calculate so many things each time, just the single result each time with all necessary invalidation handled by database cache hooks.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4202
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2025-05-31 17:30:57 +02:00 committed by kim
commit faed35c938
65 changed files with 1645 additions and 766 deletions

View file

@ -29,7 +29,6 @@ import (
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
"code.superseriousbusiness.org/gotosocial/internal/config"
"code.superseriousbusiness.org/gotosocial/internal/filter/usermute"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
"code.superseriousbusiness.org/gotosocial/internal/log"
@ -49,25 +48,23 @@ type realSender struct {
func (r *realSender) Send(
ctx context.Context,
notification *gtsmodel.Notification,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
notif *gtsmodel.Notification,
apiNotif *apimodel.Notification,
) error {
// Get notification target.
target := notif.TargetAccount
// Load subscriptions.
subscriptions, err := r.state.DB.GetWebPushSubscriptionsByAccountID(ctx, notification.TargetAccountID)
subscriptions, err := r.state.DB.GetWebPushSubscriptionsByAccountID(ctx, target.ID)
if err != nil {
return gtserror.Newf(
"error getting Web Push subscriptions for account %s: %w",
notification.TargetAccountID,
err,
)
return gtserror.Newf("error getting Web Push subscriptions for account %s: %w", target.URI, err)
}
// Subscriptions we're actually going to send to.
relevantSubscriptions := slices.DeleteFunc(
subscriptions,
func(subscription *gtsmodel.WebPushSubscription) bool {
return r.shouldSkipSubscription(ctx, notification, subscription)
return r.shouldSkipSubscription(ctx, notif, subscription)
},
)
if len(relevantSubscriptions) == 0 {
@ -80,31 +77,28 @@ func (r *realSender) Send(
return gtserror.Newf("error getting VAPID key pair: %w", err)
}
// Get target account settings.
targetAccountSettings, err := r.state.DB.GetAccountSettings(ctx, notification.TargetAccountID)
if err != nil {
return gtserror.Newf("error getting settings for account %s: %w", notification.TargetAccountID, err)
}
if target.Settings == nil {
// Ensure the target account's settings are populated.
settings, err := r.state.DB.GetAccountSettings(ctx, target.ID)
if err != nil {
return gtserror.Newf("error getting settings for account %s: %w", target.URI, err)
}
// Get API representations of notification and accounts involved.
apiNotification, err := r.converter.NotificationToAPINotification(ctx, notification, filters, mutes)
if err != nil {
return gtserror.Newf("error converting notification %s to API representation: %w", notification.ID, err)
// Set target's settings.
target.Settings = settings
}
// Queue up a .Send() call for each relevant subscription.
for _, subscription := range relevantSubscriptions {
r.state.Workers.WebPush.Queue.Push(func(ctx context.Context) {
if err := r.sendToSubscription(
ctx,
if err := r.sendToSubscription(ctx,
vapidKeyPair,
targetAccountSettings,
target.Settings,
subscription,
notification,
apiNotification,
notif,
apiNotif,
); err != nil {
log.Errorf(
ctx,
log.Errorf(ctx,
"error sending Web Push notification for subscription with token ID %s: %v",
subscription.TokenID,
err,
@ -137,8 +131,7 @@ func (r *realSender) shouldSkipSubscription(
// Allow if the subscription account follows the notifying account.
isFollowing, err := r.state.DB.IsFollowing(ctx, subscription.AccountID, notification.OriginAccountID)
if err != nil {
log.Errorf(
ctx,
log.Errorf(ctx,
"error checking whether account %s follows account %s: %v",
subscription.AccountID,
notification.OriginAccountID,
@ -152,8 +145,7 @@ func (r *realSender) shouldSkipSubscription(
// Allow if the notifying account follows the subscription account.
isFollowing, err := r.state.DB.IsFollowing(ctx, notification.OriginAccountID, subscription.AccountID)
if err != nil {
log.Errorf(
ctx,
log.Errorf(ctx,
"error checking whether account %s follows account %s: %v",
notification.OriginAccountID,
subscription.AccountID,
@ -168,8 +160,7 @@ func (r *realSender) shouldSkipSubscription(
return true
default:
log.Errorf(
ctx,
log.Errorf(ctx,
"unknown Web Push notification policy for subscription with token ID %s: %d",
subscription.TokenID,
subscription.Policy,