[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
parent a82d574acc
commit faed35c938
65 changed files with 1645 additions and 766 deletions

View File

@@ -748,10 +748,17 @@ func (p *clientAPI) UpdateStatus(ctx context.Context, cMsg *messages.FromClientA
}
}
// Notify of the latest edit.
if editsLen := len(status.EditIDs); editsLen != 0 {
editID := status.EditIDs[editsLen-1]
if err := p.surface.notifyStatusEdit(ctx, status, editID); err != nil {
if len(status.EditIDs) > 0 {
// Ensure edits are fully populated for this status before anything.
if err := p.surface.State.DB.PopulateStatusEdits(ctx, status); err != nil {
log.Error(ctx, "error populating updated status edits: %v")
// Then send notifications of a status edit
// to any local interactors of the status.
} else if err := p.surface.notifyStatusEdit(ctx,
status,
status.Edits[len(status.Edits)-1], // latest
); err != nil {
log.Errorf(ctx, "error notifying status edit: %v", err)
}
}

View File

@@ -215,7 +215,6 @@ func (suite *FromClientAPITestSuite) statusJSON(
requestingAccount,
statusfilter.FilterContextNone,
nil,
nil,
)
if err != nil {
suite.FailNow(err.Error())
@@ -240,7 +239,6 @@ func (suite *FromClientAPITestSuite) conversationJSON(
conversation,
requestingAccount,
nil,
nil,
)
if err != nil {
suite.FailNow(err.Error())
@@ -348,7 +346,7 @@ func (suite *FromClientAPITestSuite) TestProcessCreateStatusWithNotification() {
suite.FailNow("timed out waiting for new status notification")
}
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil, nil)
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil)
if err != nil {
suite.FailNow(err.Error())
}
@@ -2035,7 +2033,7 @@ func (suite *FromClientAPITestSuite) TestProcessCreateStatusWithAuthorOnExclusiv
suite.FailNow("timed out waiting for new status notification")
}
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil, nil)
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil)
if err != nil {
suite.FailNow(err.Error())
}
@@ -2220,7 +2218,7 @@ func (suite *FromClientAPITestSuite) TestProcessUpdateStatusInteractedWith() {
suite.FailNow("timed out waiting for edited status notification")
}
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil, nil)
apiNotif, err := testStructs.TypeConverter.NotificationToAPINotification(ctx, notif, nil)
if err != nil {
suite.FailNow(err.Error())
}

View File

@@ -1010,10 +1010,17 @@ func (p *fediAPI) UpdateStatus(ctx context.Context, fMsg *messages.FromFediAPI)
}
}
// Notify of the latest edit.
if editsLen := len(status.EditIDs); editsLen != 0 {
editID := status.EditIDs[editsLen-1]
if err := p.surface.notifyStatusEdit(ctx, status, editID); err != nil {
if len(status.EditIDs) > 0 {
// Ensure edits are fully populated for this status before anything.
if err := p.surface.State.DB.PopulateStatusEdits(ctx, status); err != nil {
log.Error(ctx, "error populating updated status edits: %v")
// Then send notifications of a status edit
// to any local interactors of the status.
} else if err := p.surface.notifyStatusEdit(ctx,
status,
status.Edits[len(status.Edits)-1], // latest
); err != nil {
log.Errorf(ctx, "error notifying status edit: %v", err)
}
}

View File

@@ -19,6 +19,7 @@ package workers
import (
"code.superseriousbusiness.org/gotosocial/internal/email"
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
"code.superseriousbusiness.org/gotosocial/internal/processing/conversations"
"code.superseriousbusiness.org/gotosocial/internal/processing/stream"
@@ -38,6 +39,7 @@ type Surface struct {
Converter *typeutils.Converter
Stream *stream.Processor
VisFilter *visibility.Filter
MuteFilter *mutes.Filter
EmailSender email.Sender
WebPushSender webpush.Sender
Conversations *conversations.Processor

View File

@@ -23,8 +23,7 @@ import (
"strings"
"code.superseriousbusiness.org/gotosocial/internal/db"
"code.superseriousbusiness.org/gotosocial/internal/filter/status"
"code.superseriousbusiness.org/gotosocial/internal/filter/usermute"
statusfilter "code.superseriousbusiness.org/gotosocial/internal/filter/status"
"code.superseriousbusiness.org/gotosocial/internal/gtscontext"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
@@ -59,8 +58,7 @@ func (s *Surface) notifyPendingReply(
// Ensure thread not muted
// by replied-to account.
muted, err := s.State.DB.IsThreadMutedByAccount(
ctx,
muted, err := s.State.DB.IsThreadMutedByAccount(ctx,
status.ThreadID,
status.InReplyToAccountID,
)
@@ -81,7 +79,8 @@ func (s *Surface) notifyPendingReply(
gtsmodel.NotificationPendingReply,
status.InReplyToAccount,
status.Account,
status.ID,
status,
nil,
); err != nil {
return gtserror.Newf("error notifying replied-to account %s: %w", status.InReplyToAccountID, err)
}
@@ -135,8 +134,7 @@ func (s *Surface) notifyMention(
// Ensure thread not muted
// by mentioned account.
muted, err := s.State.DB.IsThreadMutedByAccount(
ctx,
muted, err := s.State.DB.IsThreadMutedByAccount(ctx,
mention.Status.ThreadID,
mention.TargetAccountID,
)
@@ -160,7 +158,8 @@ func (s *Surface) notifyMention(
gtsmodel.NotificationMention,
mention.TargetAccount,
mention.OriginAccount,
mention.StatusID,
mention.Status,
nil,
); err != nil {
return gtserror.Newf(
"error notifying mention target %s: %w",
@@ -193,7 +192,8 @@ func (s *Surface) notifyFollowRequest(
gtsmodel.NotificationFollowRequest,
followReq.TargetAccount,
followReq.Account,
"",
nil,
nil,
); err != nil {
return gtserror.Newf("error notifying follow target %s: %w", followReq.TargetAccountID, err)
}
@@ -245,7 +245,8 @@ func (s *Surface) notifyFollow(
gtsmodel.NotificationFollow,
follow.TargetAccount,
follow.Account,
"",
nil,
nil,
); err != nil {
return gtserror.Newf("error notifying follow target %s: %w", follow.TargetAccountID, err)
}
@@ -275,7 +276,8 @@ func (s *Surface) notifyFave(
gtsmodel.NotificationFavourite,
fave.TargetAccount,
fave.Account,
fave.StatusID,
fave.Status,
nil,
); err != nil {
return gtserror.Newf("error notifying status author %s: %w", fave.TargetAccountID, err)
}
@@ -306,7 +308,8 @@ func (s *Surface) notifyPendingFave(
gtsmodel.NotificationPendingFave,
fave.TargetAccount,
fave.Account,
fave.StatusID,
fave.Status,
nil,
); err != nil {
return gtserror.Newf("error notifying status author %s: %w", fave.TargetAccountID, err)
}
@@ -339,8 +342,7 @@ func (s *Surface) notifyableFave(
// Ensure favee hasn't
// muted the thread.
muted, err := s.State.DB.IsThreadMutedByAccount(
ctx,
muted, err := s.State.DB.IsThreadMutedByAccount(ctx,
fave.Status.ThreadID,
fave.TargetAccountID,
)
@@ -379,7 +381,8 @@ func (s *Surface) notifyAnnounce(
gtsmodel.NotificationReblog,
boost.BoostOfAccount,
boost.Account,
boost.ID,
boost,
nil,
); err != nil {
return gtserror.Newf("error notifying boost target %s: %w", boost.BoostOfAccountID, err)
}
@@ -410,7 +413,8 @@ func (s *Surface) notifyPendingAnnounce(
gtsmodel.NotificationPendingReblog,
boost.BoostOfAccount,
boost.Account,
boost.ID,
boost,
nil,
); err != nil {
return gtserror.Newf("error notifying boost target %s: %w", boost.BoostOfAccountID, err)
}
@@ -448,8 +452,7 @@ func (s *Surface) notifyableAnnounce(
// Ensure boostee hasn't
// muted the thread.
muted, err := s.State.DB.IsThreadMutedByAccount(
ctx,
muted, err := s.State.DB.IsThreadMutedByAccount(ctx,
status.BoostOf.ThreadID,
status.BoostOfAccountID,
)
@@ -488,7 +491,8 @@ func (s *Surface) notifyPollClose(ctx context.Context, status *gtsmodel.Status)
gtsmodel.NotificationPoll,
status.Account,
status.Account,
status.ID,
status,
nil,
); err != nil {
errs.Appendf("error notifying poll author: %w", err)
}
@@ -507,7 +511,8 @@ func (s *Surface) notifyPollClose(ctx context.Context, status *gtsmodel.Status)
gtsmodel.NotificationPoll,
vote.Account,
status.Account,
status.ID,
status,
nil,
); err != nil {
errs.Appendf("error notifying poll voter %s: %w", vote.AccountID, err)
continue
@@ -546,7 +551,8 @@ func (s *Surface) notifySignup(ctx context.Context, newUser *gtsmodel.User) erro
gtsmodel.NotificationAdminSignup,
mod,
newUser.Account,
"",
nil,
nil,
); err != nil {
errs.Appendf("error notifying moderator %s: %w", mod.ID, err)
continue
@@ -559,7 +565,7 @@ func (s *Surface) notifySignup(ctx context.Context, newUser *gtsmodel.User) erro
func (s *Surface) notifyStatusEdit(
ctx context.Context,
status *gtsmodel.Status,
editID string,
edit *gtsmodel.StatusEdit,
) error {
// Get local-only interactions (we can't/don't notify remotes).
interactions, err := s.State.DB.GetStatusInteractions(ctx, status.ID, true)
@@ -594,7 +600,8 @@ func (s *Surface) notifyStatusEdit(
gtsmodel.NotificationUpdate,
targetAcct,
status.Account,
editID,
status,
edit,
); err != nil {
errs.Appendf("error notifying status edit: %w", err)
continue
@@ -637,22 +644,32 @@ func (s *Surface) Notify(
notificationType gtsmodel.NotificationType,
targetAccount *gtsmodel.Account,
originAccount *gtsmodel.Account,
statusOrEditID string,
status *gtsmodel.Status,
edit *gtsmodel.StatusEdit,
) error {
if targetAccount.IsRemote() {
// nothing to do.
return nil
}
// Get status / edit ID
// if either was provided.
// (prefer edit though!)
var statusOrEditID string
if edit != nil {
statusOrEditID = edit.ID
} else if status != nil {
statusOrEditID = status.ID
}
// We're doing state-y stuff so get a
// lock on this combo of notif params.
lockURI := getNotifyLockURI(
unlock := s.State.ProcessingLocks.Lock(getNotifyLockURI(
notificationType,
targetAccount,
originAccount,
statusOrEditID,
)
unlock := s.State.ProcessingLocks.Lock(lockURI)
))
// Wrap the unlock so we
// can do granular unlocking.
@@ -696,29 +713,57 @@ func (s *Surface) Notify(
// with the state-y stuff.
unlock()
// Stream notification to the user.
// Check whether origin account is muted by target account.
muted, err := s.MuteFilter.AccountNotificationsMuted(ctx,
targetAccount,
originAccount,
)
if err != nil {
return gtserror.Newf("error checking account mute: %w", err)
}
if muted {
// Don't notify.
return nil
}
if status != nil {
// Check whether status is muted by the target account.
muted, err := s.MuteFilter.StatusNotificationsMuted(ctx,
targetAccount,
status,
)
if err != nil {
return gtserror.Newf("error checking status mute: %w", err)
}
if muted {
// Don't notify.
return nil
}
}
filters, err := s.State.DB.GetFiltersForAccountID(ctx, targetAccount.ID)
if err != nil {
return gtserror.Newf("couldn't retrieve filters for account %s: %w", targetAccount.ID, err)
}
mutes, err := s.State.DB.GetAccountMutes(gtscontext.SetBarebones(ctx), targetAccount.ID, nil)
if err != nil {
return gtserror.Newf("couldn't retrieve mutes for account %s: %w", targetAccount.ID, err)
}
compiledMutes := usermute.NewCompiledUserMuteList(mutes)
apiNotif, err := s.Converter.NotificationToAPINotification(ctx, notif, filters, compiledMutes)
if err != nil {
if errors.Is(err, status.ErrHideStatus) {
return nil
}
// Convert the notification to frontend API model for streaming / push.
apiNotif, err := s.Converter.NotificationToAPINotification(ctx, notif, filters)
if err != nil && !errors.Is(err, statusfilter.ErrHideStatus) {
return gtserror.Newf("error converting notification to api representation: %w", err)
}
if apiNotif == nil {
// Filtered.
return nil
}
// Stream notification to the user.
s.Stream.Notify(ctx, targetAccount, apiNotif)
// Send Web Push notification to the user.
if err = s.WebPushSender.Send(ctx, notif, filters, compiledMutes); err != nil {
if err = s.WebPushSender.Send(ctx, notif, apiNotif); err != nil {
return gtserror.Newf("error sending Web Push notifications: %w", err)
}

View File

@@ -22,6 +22,7 @@ import (
"testing"
"time"
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
"code.superseriousbusiness.org/gotosocial/internal/gtscontext"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
@@ -43,6 +44,7 @@ func (suite *SurfaceNotifyTestSuite) TestSpamNotifs() {
Converter: testStructs.TypeConverter,
Stream: testStructs.Processor.Stream(),
VisFilter: visibility.NewFilter(testStructs.State),
MuteFilter: mutes.NewFilter(testStructs.State),
EmailSender: testStructs.EmailSender,
WebPushSender: testStructs.WebPushSender,
Conversations: testStructs.Processor.Conversations(),
@@ -74,7 +76,8 @@ func (suite *SurfaceNotifyTestSuite) TestSpamNotifs() {
notificationType,
targetAccount,
originAccount,
"",
nil,
nil,
); err != nil {
suite.FailNow(err.Error())
}

View File

@@ -23,7 +23,6 @@ import (
"code.superseriousbusiness.org/gotosocial/internal/cache/timeline"
statusfilter "code.superseriousbusiness.org/gotosocial/internal/filter/status"
"code.superseriousbusiness.org/gotosocial/internal/filter/usermute"
"code.superseriousbusiness.org/gotosocial/internal/gtscontext"
"code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
@@ -119,11 +118,12 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
// if something is hometimelineable according to this filter,
// it's also eligible to appear in exclusive lists,
// even if it ultimately doesn't appear on the home timeline.
timelineable, err := s.VisFilter.StatusHomeTimelineable(
ctx, follow.Account, status,
timelineable, err := s.VisFilter.StatusHomeTimelineable(ctx,
follow.Account,
status,
)
if err != nil {
log.Errorf(ctx, "error checking status home visibility for follow: %v", err)
log.Errorf(ctx, "error checking status home visibility: %v", err)
continue
}
@@ -132,9 +132,24 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
continue
}
// Get relevant filters and mutes for this follow's account.
// Check if the status is muted by this follower.
muted, err := s.MuteFilter.StatusMuted(ctx,
follow.Account,
status,
)
if err != nil {
log.Errorf(ctx, "error checking status mute: %v", err)
continue
}
if muted {
// Nothing to do.
continue
}
// Get relevant filters for this follow's account.
// (note the origin account of the follow is receiver of status).
filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
filters, err := s.getFilters(ctx, follow.AccountID)
if err != nil {
log.Error(ctx, err)
continue
@@ -145,7 +160,6 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
status,
follow,
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error list timelining status: %v", err)
@@ -168,7 +182,6 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
stream.TimelineHome,
statusfilter.FilterContextHome,
filters,
mutes,
); homeTimelined {
// If hometimelined, add to list of returned account IDs.
@@ -205,7 +218,8 @@ func (s *Surface) timelineAndNotifyStatusForFollowers(
gtsmodel.NotificationStatus,
follow.Account,
status.Account,
status.ID,
status,
nil,
); err != nil {
log.Errorf(ctx, "error notifying status for account: %v", err)
continue
@@ -226,7 +240,6 @@ func (s *Surface) listTimelineStatusForFollow(
status *gtsmodel.Status,
follow *gtsmodel.Follow,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (timelined bool, exclusive bool, err error) {
// Get all lists that contain this given follow.
@@ -264,7 +277,6 @@ func (s *Surface) listTimelineStatusForFollow(
stream.TimelineList+":"+list.ID, // key streamType to this specific list
statusfilter.FilterContextHome,
filters,
mutes,
)
// Update flag based on if timelined.
@@ -275,19 +287,12 @@ func (s *Surface) listTimelineStatusForFollow(
}
// getFiltersAndMutes returns an account's filters and mutes.
func (s *Surface) getFiltersAndMutes(ctx context.Context, accountID string) ([]*gtsmodel.Filter, *usermute.CompiledUserMuteList, error) {
func (s *Surface) getFilters(ctx context.Context, accountID string) ([]*gtsmodel.Filter, error) {
filters, err := s.State.DB.GetFiltersForAccountID(ctx, accountID)
if err != nil {
return nil, nil, gtserror.Newf("couldn't retrieve filters for account %s: %w", accountID, err)
return nil, gtserror.Newf("couldn't retrieve filters for account %s: %w", accountID, err)
}
mutes, err := s.State.DB.GetAccountMutes(gtscontext.SetBarebones(ctx), accountID, nil)
if err != nil {
return nil, nil, gtserror.Newf("couldn't retrieve mutes for account %s: %w", accountID, err)
}
compiledMutes := usermute.NewCompiledUserMuteList(mutes)
return filters, compiledMutes, err
return filters, err
}
// listEligible checks if the given status is eligible
@@ -366,7 +371,6 @@ func (s *Surface) timelineStatus(
streamType string,
filterCtx statusfilter.FilterContext,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) bool {
// Attempt to convert status to frontend API representation,
@@ -376,7 +380,6 @@ func (s *Surface) timelineStatus(
account,
filterCtx,
filters,
mutes,
)
if err != nil && !errors.Is(err, statusfilter.ErrHideStatus) {
log.Error(ctx, "error converting status %s to frontend: %v", status.URI, err)
@@ -388,7 +391,7 @@ func (s *Surface) timelineStatus(
if apiModel == nil {
// Status was
// filtered / muted.
// filtered.
return false
}
@@ -422,7 +425,7 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
// Insert the status into the home timeline of each tag follower.
errs := gtserror.MultiError{}
for _, tagFollowerAccount := range tagFollowerAccounts {
filters, mutes, err := s.getFiltersAndMutes(ctx, tagFollowerAccount.ID)
filters, err := s.getFilters(ctx, tagFollowerAccount.ID)
if err != nil {
errs.Append(err)
continue
@@ -435,7 +438,6 @@ func (s *Surface) timelineAndNotifyStatusForTagFollowers(
stream.TimelineHome,
statusfilter.FilterContextHome,
filters,
mutes,
)
}
@@ -605,7 +607,7 @@ func (s *Surface) timelineStatusUpdateForFollowers(
// Get relevant filters and mutes for this follow's account.
// (note the origin account of the follow is receiver of status).
filters, mutes, err := s.getFiltersAndMutes(ctx, follow.AccountID)
filters, err := s.getFilters(ctx, follow.AccountID)
if err != nil {
log.Error(ctx, err)
continue
@@ -616,7 +618,6 @@ func (s *Surface) timelineStatusUpdateForFollowers(
status,
follow,
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error list timelining status: %v", err)
@@ -637,7 +638,6 @@ func (s *Surface) timelineStatusUpdateForFollowers(
status,
stream.TimelineHome,
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error home timelining status: %v", err)
@@ -663,7 +663,6 @@ func (s *Surface) listTimelineStatusUpdateForFollow(
status *gtsmodel.Status,
follow *gtsmodel.Follow,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (bool, bool, error) {
// Get all lists that contain this given follow.
@@ -703,7 +702,6 @@ func (s *Surface) listTimelineStatusUpdateForFollow(
status,
stream.TimelineList+":"+list.ID, // key streamType to this specific list
filters,
mutes,
)
if err != nil {
log.Errorf(ctx, "error adding status to list timeline: %v", err)
@@ -727,7 +725,6 @@ func (s *Surface) timelineStreamStatusUpdate(
status *gtsmodel.Status,
streamType string,
filters []*gtsmodel.Filter,
mutes *usermute.CompiledUserMuteList,
) (bool, error) {
// Convert updated database model to frontend model.
@@ -736,7 +733,6 @@ func (s *Surface) timelineStreamStatusUpdate(
account,
statusfilter.FilterContextHome,
filters,
mutes,
)
switch {
@@ -778,7 +774,7 @@ func (s *Surface) timelineStatusUpdateForTagFollowers(
// Stream the update to the home timeline of each tag follower.
errs := gtserror.MultiError{}
for _, tagFollowerAccount := range tagFollowerAccounts {
filters, mutes, err := s.getFiltersAndMutes(ctx, tagFollowerAccount.ID)
filters, err := s.getFilters(ctx, tagFollowerAccount.ID)
if err != nil {
errs.Append(err)
continue
@@ -790,7 +786,6 @@ func (s *Surface) timelineStatusUpdateForTagFollowers(
status,
stream.TimelineHome,
filters,
mutes,
); err != nil {
errs.Appendf(
"error updating status %s on home timeline for account %s: %w",

View File

@@ -20,6 +20,7 @@ package workers
import (
"code.superseriousbusiness.org/gotosocial/internal/email"
"code.superseriousbusiness.org/gotosocial/internal/federation"
"code.superseriousbusiness.org/gotosocial/internal/filter/mutes"
"code.superseriousbusiness.org/gotosocial/internal/filter/visibility"
"code.superseriousbusiness.org/gotosocial/internal/processing/account"
"code.superseriousbusiness.org/gotosocial/internal/processing/common"
@@ -44,6 +45,7 @@ func New(
federator *federation.Federator,
converter *typeutils.Converter,
visFilter *visibility.Filter,
muteFilter *mutes.Filter,
emailSender email.Sender,
webPushSender webpush.Sender,
account *account.Processor,
@@ -66,6 +68,7 @@ func New(
Converter: converter,
Stream: stream,
VisFilter: visFilter,
MuteFilter: muteFilter,
EmailSender: emailSender,
WebPushSender: webPushSender,
Conversations: conversations,