[chore] Add interaction filter to complement existing visibility filter (#3111)

* [chore] Add interaction filter to complement existing visibility filter

* pass in ptr to visibility and interaction filters to Processor{} to ensure shared

* use int constants for for match type, cache db calls in filterctx

* function name typo 😇

---------

Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
tobi
2024-07-24 13:27:42 +02:00
committed by GitHub
parent 63fc9b6c3e
commit c9b6220fef
61 changed files with 1661 additions and 585 deletions

View File

@ -72,28 +72,73 @@ func (p *Processor) getFaveableStatus(
}
// FaveCreate adds a fave for the requestingAccount, targeting the given status (no-op if fave already exists).
func (p *Processor) FaveCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Status, gtserror.WithCode) {
targetStatus, existingFave, errWithCode := p.getFaveableStatus(ctx, requestingAccount, targetStatusID)
func (p *Processor) FaveCreate(
ctx context.Context,
requester *gtsmodel.Account,
targetStatusID string,
) (*apimodel.Status, gtserror.WithCode) {
status, existingFave, errWithCode := p.getFaveableStatus(ctx, requester, targetStatusID)
if errWithCode != nil {
return nil, errWithCode
}
if existingFave != nil {
// Status is already faveed.
return p.c.GetAPIStatus(ctx, requestingAccount, targetStatus)
return p.c.GetAPIStatus(ctx, requester, status)
}
// Create and store a new fave
// Ensure valid fave target for requester.
policyResult, err := p.intFilter.StatusLikeable(ctx,
requester,
status,
)
if err != nil {
err := gtserror.Newf("error seeing if status %s is likeable: %w", status.ID, err)
return nil, gtserror.NewErrorInternalError(err)
}
if policyResult.Forbidden() {
const errText = "you do not have permission to fave this status"
err := gtserror.New(errText)
return nil, gtserror.NewErrorForbidden(err, errText)
}
// Derive pendingApproval status.
var pendingApproval bool
switch {
case policyResult.WithApproval():
// We're allowed to do
// this pending approval.
pendingApproval = true
case policyResult.MatchedOnCollection():
// We're permitted to do this, but since
// we matched due to presence in a followers
// or following collection, we should mark
// as pending approval and wait for an accept.
pendingApproval = true
case policyResult.Permitted():
// We're permitted to do this
// based on another kind of match.
pendingApproval = false
}
status.PendingApproval = &pendingApproval
// Create a new fave, marking it
// as pending approval if necessary.
faveID := id.NewULID()
gtsFave := &gtsmodel.StatusFave{
ID: faveID,
AccountID: requestingAccount.ID,
Account: requestingAccount,
TargetAccountID: targetStatus.AccountID,
TargetAccount: targetStatus.Account,
StatusID: targetStatus.ID,
Status: targetStatus,
URI: uris.GenerateURIForLike(requestingAccount.Username, faveID),
AccountID: requester.ID,
Account: requester,
TargetAccountID: status.AccountID,
TargetAccount: status.Account,
StatusID: status.ID,
Status: status,
URI: uris.GenerateURIForLike(requester.Username, faveID),
PendingApproval: &pendingApproval,
}
if err := p.state.DB.PutStatusFave(ctx, gtsFave); err != nil {
@ -106,11 +151,11 @@ func (p *Processor) FaveCreate(ctx context.Context, requestingAccount *gtsmodel.
APObjectType: ap.ActivityLike,
APActivityType: ap.ActivityCreate,
GTSModel: gtsFave,
Origin: requestingAccount,
Target: targetStatus.Account,
Origin: requester,
Target: status.Account,
})
return p.c.GetAPIStatus(ctx, requestingAccount, targetStatus)
return p.c.GetAPIStatus(ctx, requester, status)
}
// FaveRemove removes a fave for the requesting account, targeting the given status (no-op if fave doesn't exist).