diff options
| author | 2025-05-31 17:30:57 +0200 | |
|---|---|---|
| committer | 2025-05-31 17:30:57 +0200 | |
| commit | faed35c9388bc28ea0fdfe3aae3b489ca952c006 (patch) | |
| tree | 989ab37bc3ef1b412ffc509d58939d4f1ac4f5c1 /internal/processing/timeline | |
| parent | [feature] make client-side nonce calculation multi-threaded (#4219) (diff) | |
| download | gotosocial-faed35c9388bc28ea0fdfe3aae3b489ca952c006.tar.xz | |
[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>
Diffstat (limited to 'internal/processing/timeline')
| -rw-r--r-- | internal/processing/timeline/faved.go | 2 | ||||
| -rw-r--r-- | internal/processing/timeline/home.go | 17 | ||||
| -rw-r--r-- | internal/processing/timeline/list.go | 17 | ||||
| -rw-r--r-- | internal/processing/timeline/notification.go | 92 | ||||
| -rw-r--r-- | internal/processing/timeline/public.go | 32 | ||||
| -rw-r--r-- | internal/processing/timeline/tag.go | 17 | ||||
| -rw-r--r-- | internal/processing/timeline/timeline.go | 33 | ||||
| -rw-r--r-- | internal/processing/timeline/timeline_test.go | 2 |
8 files changed, 134 insertions, 78 deletions
diff --git a/internal/processing/timeline/faved.go b/internal/processing/timeline/faved.go index 4cb3c30a5..84788a8fa 100644 --- a/internal/processing/timeline/faved.go +++ b/internal/processing/timeline/faved.go @@ -56,7 +56,7 @@ func (p *Processor) FavedTimelineGet(ctx context.Context, authed *apiutil.Auth, continue } - apiStatus, err := p.converter.StatusToAPIStatus(ctx, s, authed.Account, statusfilter.FilterContextNone, nil, nil) + apiStatus, err := p.converter.StatusToAPIStatus(ctx, s, authed.Account, statusfilter.FilterContextNone, nil) if err != nil { log.Errorf(ctx, "error convering to api status: %v", err) continue diff --git a/internal/processing/timeline/home.go b/internal/processing/timeline/home.go index bcb63fcff..ba74b770c 100644 --- a/internal/processing/timeline/home.go +++ b/internal/processing/timeline/home.go @@ -91,9 +91,22 @@ func (p *Processor) HomeTimelineGet( // Check the visibility of passed status to requesting user. ok, err := p.visFilter.StatusHomeTimelineable(ctx, requester, s) if err != nil { - log.Errorf(ctx, "error filtering status %s: %v", s.URI, err) + log.Errorf(ctx, "error checking status %s visibility: %v", s.URI, err) + return true // default assume not visible + } else if !ok { + return true } - return !ok + + // Check if status been muted by requester from timelines. + muted, err := p.muteFilter.StatusMuted(ctx, requester, s) + if err != nil { + log.Errorf(ctx, "error checking status %s mutes: %v", s.URI, err) + return true // default assume muted + } else if muted { + return true + } + + return false }, // Post filtering funtion, diff --git a/internal/processing/timeline/list.go b/internal/processing/timeline/list.go index dbf07cdd4..c8e6bc5f1 100644 --- a/internal/processing/timeline/list.go +++ b/internal/processing/timeline/list.go @@ -102,9 +102,22 @@ func (p *Processor) ListTimelineGet( // Check the visibility of passed status to requesting user. ok, err := p.visFilter.StatusHomeTimelineable(ctx, requester, s) if err != nil { - log.Errorf(ctx, "error filtering status %s: %v", s.URI, err) + log.Errorf(ctx, "error checking status %s visibility: %v", s.URI, err) + return true // default assume not visible + } else if !ok { + return true } - return !ok + + // Check if status been muted by requester from timelines. + muted, err := p.muteFilter.StatusMuted(ctx, requester, s) + if err != nil { + log.Errorf(ctx, "error checking status %s mutes: %v", s.URI, err) + return true // default assume muted + } else if muted { + return true + } + + return false }, // Post filtering funtion, diff --git a/internal/processing/timeline/notification.go b/internal/processing/timeline/notification.go index 0da9fb55e..ad60fd90c 100644 --- a/internal/processing/timeline/notification.go +++ b/internal/processing/timeline/notification.go @@ -21,14 +21,13 @@ import ( "context" "errors" "fmt" + "net/http" "net/url" apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util" "code.superseriousbusiness.org/gotosocial/internal/db" "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" "code.superseriousbusiness.org/gotosocial/internal/log" @@ -39,14 +38,13 @@ import ( // NotificationsGet ... func (p *Processor) NotificationsGet( ctx context.Context, - authed *apiutil.Auth, + requester *gtsmodel.Account, page *paging.Page, types []gtsmodel.NotificationType, excludeTypes []gtsmodel.NotificationType, ) (*apimodel.PageableResponse, gtserror.WithCode) { - notifs, err := p.state.DB.GetAccountNotifications( - ctx, - authed.Account.ID, + notifs, err := p.state.DB.GetAccountNotifications(ctx, + requester.ID, page, types, excludeTypes, @@ -61,19 +59,12 @@ func (p *Processor) NotificationsGet( return util.EmptyPageableResponse(), nil } - filters, err := p.state.DB.GetFiltersForAccountID(ctx, authed.Account.ID) + filters, err := p.state.DB.GetFiltersForAccountID(ctx, requester.ID) if err != nil { - err = gtserror.Newf("couldn't retrieve filters for account %s: %w", authed.Account.ID, err) + err = gtserror.Newf("error getting account %s filters: %w", requester.ID, err) return nil, gtserror.NewErrorInternalError(err) } - mutes, err := p.state.DB.GetAccountMutes(gtscontext.SetBarebones(ctx), authed.Account.ID, nil) - if err != nil { - err = gtserror.Newf("couldn't retrieve mutes for account %s: %w", authed.Account.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - compiledMutes := usermute.NewCompiledUserMuteList(mutes) - var ( items = make([]interface{}, 0, count) @@ -84,7 +75,7 @@ func (p *Processor) NotificationsGet( ) for _, n := range notifs { - visible, err := p.notifVisible(ctx, n, authed.Account) + visible, err := p.notifVisible(ctx, n, requester) if err != nil { log.Debugf(ctx, "skipping notification %s because of an error checking notification visibility: %v", n.ID, err) continue @@ -94,7 +85,37 @@ func (p *Processor) NotificationsGet( continue } - item, err := p.converter.NotificationToAPINotification(ctx, n, filters, compiledMutes) + // Check whether notification origin account is muted. + muted, err := p.muteFilter.AccountNotificationsMuted(ctx, + requester, + n.OriginAccount, + ) + if err != nil { + log.Errorf(ctx, "error checking account mute: %v", err) + continue + } + + if muted { + continue + } + + if n.Status != nil { + // A status is attached, check whether status muted. + muted, err = p.muteFilter.StatusNotificationsMuted(ctx, + requester, + n.Status, + ) + if err != nil { + log.Errorf(ctx, "error checking status mute: %v", err) + continue + } + + if muted { + continue + } + } + + item, err := p.converter.NotificationToAPINotification(ctx, n, filters) if err != nil { if !errors.Is(err, status.ErrHideStatus) { log.Debugf(ctx, "skipping notification %s because it couldn't be converted to its api representation: %s", n.ID, err) @@ -125,41 +146,24 @@ func (p *Processor) NotificationsGet( func (p *Processor) NotificationGet(ctx context.Context, account *gtsmodel.Account, targetNotifID string) (*apimodel.Notification, gtserror.WithCode) { notif, err := p.state.DB.GetNotificationByID(ctx, targetNotifID) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.NewErrorNotFound(err) - } - - // Real error. + if err != nil && !errors.Is(err, db.ErrNoEntries) { + err := gtserror.Newf("error getting from db: %w", err) return nil, gtserror.NewErrorInternalError(err) } - if notifTargetAccountID := notif.TargetAccountID; notifTargetAccountID != account.ID { - err = fmt.Errorf("account %s does not have permission to view notification belong to account %s", account.ID, notifTargetAccountID) + if notif.TargetAccountID != account.ID { + err := gtserror.New("requester does not match notification target") return nil, gtserror.NewErrorNotFound(err) } - filters, err := p.state.DB.GetFiltersForAccountID(ctx, account.ID) - if err != nil { - err = gtserror.Newf("couldn't retrieve filters for account %s: %w", account.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } + // NOTE: we specifically don't do any filtering + // or mute checking for a notification directly + // fetched by ID. only from timelines etc. - mutes, err := p.state.DB.GetAccountMutes(gtscontext.SetBarebones(ctx), account.ID, nil) + apiNotif, err := p.converter.NotificationToAPINotification(ctx, notif, nil) if err != nil { - err = gtserror.Newf("couldn't retrieve mutes for account %s: %w", account.ID, err) - return nil, gtserror.NewErrorInternalError(err) - } - compiledMutes := usermute.NewCompiledUserMuteList(mutes) - - apiNotif, err := p.converter.NotificationToAPINotification(ctx, notif, filters, compiledMutes) - if err != nil { - if errors.Is(err, db.ErrNoEntries) { - return nil, gtserror.NewErrorNotFound(err) - } - - // Real error. - return nil, gtserror.NewErrorInternalError(err) + err := gtserror.Newf("error converting to api model: %w", err) + return nil, gtserror.WrapWithCode(http.StatusInternalServerError, err) } return apiNotif, nil diff --git a/internal/processing/timeline/public.go b/internal/processing/timeline/public.go index 527000166..cfb58201d 100644 --- a/internal/processing/timeline/public.go +++ b/internal/processing/timeline/public.go @@ -93,9 +93,22 @@ func (p *Processor) publicTimelineGet( // Check the visibility of passed status to requesting user. ok, err := p.visFilter.StatusPublicTimelineable(ctx, requester, s) if err != nil { - log.Errorf(ctx, "error filtering status %s: %v", s.URI, err) + log.Errorf(ctx, "error checking status %s visibility: %v", s.URI, err) + return true // default assume not visible + } else if !ok { + return true } - return !ok + + // Check if status been muted by requester from timelines. + muted, err := p.muteFilter.StatusMuted(ctx, requester, s) + if err != nil { + log.Errorf(ctx, "error checking status %s mutes: %v", s.URI, err) + return true // default assume muted + } else if muted { + return true + } + + return false }, // Post filtering funtion, @@ -149,9 +162,20 @@ func (p *Processor) localTimelineGet( // Check the visibility of passed status to requesting user. ok, err := p.visFilter.StatusPublicTimelineable(ctx, requester, s) if err != nil { - log.Errorf(ctx, "error filtering status %s: %v", s.URI, err) + log.Errorf(ctx, "error checking status %s visibility: %v", s.URI, err) + } else if !ok { + return true } - return !ok + + // Check if status been muted by requester from timelines. + muted, err := p.muteFilter.StatusMuted(ctx, requester, s) + if err != nil { + log.Errorf(ctx, "error checking status %s mutes: %v", s.URI, err) + } else if muted { + return true + } + + return false }, // Post filtering funtion, diff --git a/internal/processing/timeline/tag.go b/internal/processing/timeline/tag.go index f48f89049..88333d343 100644 --- a/internal/processing/timeline/tag.go +++ b/internal/processing/timeline/tag.go @@ -101,9 +101,22 @@ func (p *Processor) TagTimelineGet( // Check the visibility of passed status to requesting user. ok, err := p.visFilter.StatusPublicTimelineable(ctx, requester, s) if err != nil { - log.Errorf(ctx, "error filtering status %s: %v", s.URI, err) + log.Errorf(ctx, "error checking status %s visibility: %v", s.URI, err) + return true // default assume not visible + } else if !ok { + return true } - return !ok + + // Check if status been muted by requester from timelines. + muted, err := p.muteFilter.StatusMuted(ctx, requester, s) + if err != nil { + log.Errorf(ctx, "error checking status %s mutes: %v", s.URI, err) + return true // default assume muted + } else if muted { + return true + } + + return false }, // Post filtering funtion, diff --git a/internal/processing/timeline/timeline.go b/internal/processing/timeline/timeline.go index 2eab57195..a86702d42 100644 --- a/internal/processing/timeline/timeline.go +++ b/internal/processing/timeline/timeline.go @@ -26,8 +26,8 @@ import ( apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" timelinepkg "code.superseriousbusiness.org/gotosocial/internal/cache/timeline" "code.superseriousbusiness.org/gotosocial/internal/db" + "code.superseriousbusiness.org/gotosocial/internal/filter/mutes" statusfilter "code.superseriousbusiness.org/gotosocial/internal/filter/status" - "code.superseriousbusiness.org/gotosocial/internal/filter/usermute" "code.superseriousbusiness.org/gotosocial/internal/filter/visibility" "code.superseriousbusiness.org/gotosocial/internal/gtserror" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" @@ -48,16 +48,18 @@ var ( ) type Processor struct { - state *state.State - converter *typeutils.Converter - visFilter *visibility.Filter + state *state.State + converter *typeutils.Converter + visFilter *visibility.Filter + muteFilter *mutes.Filter } -func New(state *state.State, converter *typeutils.Converter, visFilter *visibility.Filter) Processor { +func New(state *state.State, converter *typeutils.Converter, visFilter *visibility.Filter, muteFilter *mutes.Filter) Processor { return Processor{ - state: state, - converter: converter, - visFilter: visFilter, + state: state, + converter: converter, + visFilter: visFilter, + muteFilter: muteFilter, } } @@ -78,7 +80,6 @@ func (p *Processor) getStatusTimeline( ) { var err error var filters []*gtsmodel.Filter - var mutes *usermute.CompiledUserMuteList if requester != nil { // Fetch all filters relevant for requesting account. @@ -89,19 +90,6 @@ func (p *Processor) getStatusTimeline( err := gtserror.Newf("error getting account filters: %w", err) return nil, gtserror.NewErrorInternalError(err) } - - // Get a list of all account mutes for requester. - allMutes, err := p.state.DB.GetAccountMutes(ctx, - requester.ID, - nil, // i.e. all - ) - if err != nil && !errors.Is(err, db.ErrNoEntries) { - err := gtserror.Newf("error getting account mutes: %w", err) - return nil, gtserror.NewErrorInternalError(err) - } - - // Compile all account mutes to useable form. - mutes = usermute.NewCompiledUserMuteList(allMutes) } // Ensure we have valid @@ -148,7 +136,6 @@ func (p *Processor) getStatusTimeline( requester, filterCtx, filters, - mutes, ) if err != nil && !errors.Is(err, statusfilter.ErrHideStatus) { return nil, err diff --git a/internal/processing/timeline/timeline_test.go b/internal/processing/timeline/timeline_test.go index 5afbb2353..01197b767 100644 --- a/internal/processing/timeline/timeline_test.go +++ b/internal/processing/timeline/timeline_test.go @@ -20,6 +20,7 @@ package timeline_test import ( "code.superseriousbusiness.org/gotosocial/internal/admin" "code.superseriousbusiness.org/gotosocial/internal/db" + "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/processing/timeline" @@ -62,6 +63,7 @@ func (suite *TimelineStandardTestSuite) SetupTest() { &suite.state, typeutils.NewConverter(&suite.state), visibility.NewFilter(&suite.state), + mutes.NewFilter(&suite.state), ) testrig.StandardDBSetup(suite.db, suite.testAccounts) |
