mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 06:22:25 -05:00
[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:
parent
a82d574acc
commit
faed35c938
65 changed files with 1645 additions and 766 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import (
|
|||
"code.superseriousbusiness.org/gotosocial/internal/email"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/federation"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/interaction"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/media"
|
||||
|
|
@ -123,6 +124,7 @@ func (suite *RealSenderStandardTestSuite) SetupTest() {
|
|||
suite.emailSender,
|
||||
suite.webPushSender,
|
||||
visibility.NewFilter(&suite.state),
|
||||
mutes.NewFilter(&suite.state),
|
||||
interaction.NewFilter(&suite.state),
|
||||
)
|
||||
testrig.StartWorkers(&suite.state, suite.processor.Workers())
|
||||
|
|
@ -188,8 +190,14 @@ func (suite *RealSenderStandardTestSuite) simulatePushNotification(
|
|||
}, nil
|
||||
}
|
||||
|
||||
apiNotif, err := suite.typeconverter.NotificationToAPINotification(ctx, notification, nil)
|
||||
suite.NoError(err)
|
||||
|
||||
// Send the push notification.
|
||||
sendError := suite.webPushSender.Send(ctx, notification, nil, nil)
|
||||
sendError := suite.webPushSender.Send(ctx,
|
||||
notification,
|
||||
apiNotif,
|
||||
)
|
||||
|
||||
// Wait for it to be sent or for the context to time out.
|
||||
bodyClosed := false
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ import (
|
|||
"context"
|
||||
"net/http"
|
||||
|
||||
"code.superseriousbusiness.org/gotosocial/internal/filter/usermute"
|
||||
apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/httpclient"
|
||||
"code.superseriousbusiness.org/gotosocial/internal/state"
|
||||
|
|
@ -31,14 +31,8 @@ import (
|
|||
// Sender can send Web Push notifications.
|
||||
type Sender interface {
|
||||
|
||||
// Send queues up a notification for delivery to
|
||||
// all of an account's Web Push subscriptions.
|
||||
Send(
|
||||
ctx context.Context,
|
||||
notification *gtsmodel.Notification,
|
||||
filters []*gtsmodel.Filter,
|
||||
mutes *usermute.CompiledUserMuteList,
|
||||
) error
|
||||
// Send queues up a notification for delivery to all of an account's Web Push subscriptions.
|
||||
Send(ctx context.Context, notif *gtsmodel.Notification, apiNotif *apimodel.Notification) error
|
||||
}
|
||||
|
||||
// NewSender creates a new sender from an HTTP client, DB, and worker pool.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue