use pointer for freshness window (#2614)

This commit is contained in:
tobi 2024-02-09 15:24:49 +01:00 committed by GitHub
commit e890169e6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 191 additions and 95 deletions

View file

@ -66,7 +66,7 @@ func (p *Processor) GetTargetAccountBy(
requester.Username,
target,
nil,
false,
nil,
)
}

View file

@ -23,19 +23,24 @@ import (
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
// GetTargetStatusBy fetches the target status with db load function, given the authorized (or, nil) requester's
// account. This returns an approprate gtserror.WithCode accounting for not found and visibility to requester.
// The refresh argument allows specifying whether the returned copy should be force refreshed.
// GetTargetStatusBy fetches the target status with db load
// function, given the authorized (or, nil) requester's
// account. This returns an approprate gtserror.WithCode
// accounting for not found and visibility to requester.
//
// window can be used to force refresh of the target if it's
// deemed to be stale. Falls back to default window if nil.
func (p *Processor) GetTargetStatusBy(
ctx context.Context,
requester *gtsmodel.Account,
getTargetFromDB func() (*gtsmodel.Status, error),
refresh bool,
window *dereferencing.FreshnessWindow,
) (
status *gtsmodel.Status,
visible bool,
@ -68,13 +73,15 @@ func (p *Processor) GetTargetStatusBy(
// a requester (i.e. request is authorized)
// to prevent a possible DOS vector.
if refresh {
// Refresh required, forcibly do synchronously.
if window != nil {
// Window is explicitly set, so likely
// tighter than the default window.
// Do refresh synchronously.
_, _, err := p.federator.RefreshStatus(ctx,
requester.Username,
target,
nil,
true, // force
window,
)
if err != nil {
log.Errorf(ctx, "error refreshing status: %v", err)
@ -85,7 +92,7 @@ func (p *Processor) GetTargetStatusBy(
requester.Username,
target,
nil,
false, // force
nil,
)
}
}
@ -95,11 +102,14 @@ func (p *Processor) GetTargetStatusBy(
// GetVisibleTargetStatus calls GetTargetStatusBy(),
// but converts a non-visible result to not-found error.
//
// window can be used to force refresh of the target if it's
// deemed to be stale. Falls back to default window if nil.
func (p *Processor) GetVisibleTargetStatusBy(
ctx context.Context,
requester *gtsmodel.Account,
getTargetFromDB func() (*gtsmodel.Status, error),
refresh bool,
window *dereferencing.FreshnessWindow,
) (
status *gtsmodel.Status,
errWithCode gtserror.WithCode,
@ -108,7 +118,7 @@ func (p *Processor) GetVisibleTargetStatusBy(
target, visible, errWithCode := p.GetTargetStatusBy(ctx,
requester,
getTargetFromDB,
refresh,
window,
)
if errWithCode != nil {
return nil, errWithCode
@ -128,18 +138,21 @@ func (p *Processor) GetVisibleTargetStatusBy(
// GetVisibleTargetStatus calls GetVisibleTargetStatusBy(),
// passing in a database function that fetches by status ID.
//
// window can be used to force refresh of the target if it's
// deemed to be stale. Falls back to default window if nil.
func (p *Processor) GetVisibleTargetStatus(
ctx context.Context,
requester *gtsmodel.Account,
targetID string,
refresh bool,
window *dereferencing.FreshnessWindow,
) (
status *gtsmodel.Status,
errWithCode gtserror.WithCode,
) {
return p.GetVisibleTargetStatusBy(ctx, requester, func() (*gtsmodel.Status, error) {
return p.state.DB.GetStatusByID(ctx, targetID)
}, refresh)
}, window)
}
// UnwrapIfBoost "unwraps" the given status if
@ -158,7 +171,7 @@ func (p *Processor) UnwrapIfBoost(
return p.GetVisibleTargetStatus(ctx,
requester,
status.BoostOfID,
false,
nil,
)
}

View file

@ -100,7 +100,7 @@ func (p *Processor) StatusRepliesGet(
status, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requester,
statusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -53,7 +53,7 @@ func (p *Processor) getTargetPoll(ctx context.Context, requester *gtsmodel.Accou
func() (*gtsmodel.Status, error) {
return p.state.DB.GetStatusByPollID(ctx, targetID)
},
true, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -33,7 +33,7 @@ func (p *Processor) getBookmarkableStatus(ctx context.Context, requestingAccount
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, "", errWithCode

View file

@ -43,7 +43,7 @@ func (p *Processor) BoostCreate(
ctx,
requester,
targetID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode
@ -113,7 +113,7 @@ func (p *Processor) BoostRemove(
ctx,
requester,
targetID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -47,7 +47,7 @@ func (p *Processor) getFaveableStatus(
ctx,
requester,
targetID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, nil, errWithCode
@ -153,7 +153,7 @@ func (p *Processor) FavedBy(ctx context.Context, requestingAccount *gtsmodel.Acc
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -32,7 +32,7 @@ func (p *Processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode
@ -46,7 +46,7 @@ func (p *Processor) WebGet(ctx context.Context, targetStatusID string) (*apimode
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
nil, // requester
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode
@ -69,7 +69,7 @@ func (p *Processor) contextGet(
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -44,7 +44,7 @@ func (p *Processor) getMuteableStatus(
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -42,7 +42,7 @@ func (p *Processor) getPinnableStatus(ctx context.Context, requestingAccount *gt
targetStatus, errWithCode := p.c.GetVisibleTargetStatus(ctx,
requestingAccount,
targetStatusID,
false, // refresh
nil, // default freshness
)
if errWithCode != nil {
return nil, errWithCode

View file

@ -23,6 +23,8 @@ import (
"codeberg.org/gruf/go-kv"
"codeberg.org/gruf/go-logger/v2/level"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -179,7 +181,8 @@ func (p *fediAPI) CreateStatus(ctx context.Context, fMsg messages.FromFediAPI) e
fMsg.ReceivingAccount.Username,
bareStatus,
statusable,
true,
// Force refresh within 5min window.
dereferencing.Fresh,
)
if err != nil {
return gtserror.Newf("error processing new status %s: %w", bareStatus.URI, err)
@ -487,7 +490,8 @@ func (p *fediAPI) UpdateAccount(ctx context.Context, fMsg messages.FromFediAPI)
fMsg.ReceivingAccount.Username,
account,
apubAcc,
true, // Force refresh.
// Force refresh within 5min window.
dereferencing.Fresh,
)
if err != nil {
log.Errorf(ctx, "error refreshing account: %v", err)
@ -512,7 +516,8 @@ func (p *fediAPI) UpdateStatus(ctx context.Context, fMsg messages.FromFediAPI) e
fMsg.ReceivingAccount.Username,
existing,
apStatus,
true,
// Force refresh within 5min window.
dereferencing.Fresh,
)
if err != nil {
log.Errorf(ctx, "error refreshing status: %v", err)