mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[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:
@@ -20,6 +20,7 @@ package typeutils
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/state"
|
||||
)
|
||||
@@ -28,13 +29,15 @@ type Converter struct {
|
||||
state *state.State
|
||||
defaultAvatars []string
|
||||
randAvatars sync.Map
|
||||
filter *visibility.Filter
|
||||
visFilter *visibility.Filter
|
||||
intFilter *interaction.Filter
|
||||
}
|
||||
|
||||
func NewConverter(state *state.State) *Converter {
|
||||
return &Converter{
|
||||
state: state,
|
||||
defaultAvatars: populateDefaultAvatars(),
|
||||
filter: visibility.NewFilter(state),
|
||||
visFilter: visibility.NewFilter(state),
|
||||
intFilter: interaction.NewFilter(state),
|
||||
}
|
||||
}
|
||||
|
@@ -861,7 +861,7 @@ func (c *Converter) statusToAPIFilterResults(
|
||||
|
||||
for _, account := range otherAccounts {
|
||||
// Is this account visible?
|
||||
visible, err := c.filter.AccountVisible(ctx, requestingAccount, account)
|
||||
visible, err := c.visFilter.AccountVisible(ctx, requestingAccount, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -2382,8 +2382,8 @@ func (c *Converter) ThemesToAPIThemes(themes []*gtsmodel.Theme) []apimodel.Theme
|
||||
func (c *Converter) InteractionPolicyToAPIInteractionPolicy(
|
||||
ctx context.Context,
|
||||
policy *gtsmodel.InteractionPolicy,
|
||||
_ *gtsmodel.Status, // Used in upcoming PR.
|
||||
_ *gtsmodel.Account, // Used in upcoming PR.
|
||||
status *gtsmodel.Status,
|
||||
requester *gtsmodel.Account,
|
||||
) (*apimodel.InteractionPolicy, error) {
|
||||
apiPolicy := &apimodel.InteractionPolicy{
|
||||
CanFavourite: apimodel.PolicyRules{
|
||||
@@ -2400,6 +2400,75 @@ func (c *Converter) InteractionPolicyToAPIInteractionPolicy(
|
||||
},
|
||||
}
|
||||
|
||||
if status == nil || requester == nil {
|
||||
// We're done here!
|
||||
return apiPolicy, nil
|
||||
}
|
||||
|
||||
// Status and requester are both defined,
|
||||
// so we can add the "me" Value to the policy
|
||||
// for each interaction type, if applicable.
|
||||
|
||||
likeable, err := c.intFilter.StatusLikeable(ctx, requester, status)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error checking status likeable by requester: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if likeable.Permission == gtsmodel.PolicyPermissionPermitted {
|
||||
// We can do this!
|
||||
apiPolicy.CanFavourite.Always = append(
|
||||
apiPolicy.CanFavourite.Always,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
} else if likeable.Permission == gtsmodel.PolicyPermissionWithApproval {
|
||||
// We can do this with approval.
|
||||
apiPolicy.CanFavourite.WithApproval = append(
|
||||
apiPolicy.CanFavourite.WithApproval,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
}
|
||||
|
||||
replyable, err := c.intFilter.StatusReplyable(ctx, requester, status)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error checking status replyable by requester: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if replyable.Permission == gtsmodel.PolicyPermissionPermitted {
|
||||
// We can do this!
|
||||
apiPolicy.CanReply.Always = append(
|
||||
apiPolicy.CanReply.Always,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
} else if replyable.Permission == gtsmodel.PolicyPermissionWithApproval {
|
||||
// We can do this with approval.
|
||||
apiPolicy.CanReply.WithApproval = append(
|
||||
apiPolicy.CanReply.WithApproval,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
}
|
||||
|
||||
boostable, err := c.intFilter.StatusBoostable(ctx, requester, status)
|
||||
if err != nil {
|
||||
err := gtserror.Newf("error checking status boostable by requester: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if boostable.Permission == gtsmodel.PolicyPermissionPermitted {
|
||||
// We can do this!
|
||||
apiPolicy.CanReblog.Always = append(
|
||||
apiPolicy.CanReblog.Always,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
} else if boostable.Permission == gtsmodel.PolicyPermissionWithApproval {
|
||||
// We can do this with approval.
|
||||
apiPolicy.CanReblog.WithApproval = append(
|
||||
apiPolicy.CanReblog.WithApproval,
|
||||
apimodel.PolicyValueMe,
|
||||
)
|
||||
}
|
||||
|
||||
return apiPolicy, nil
|
||||
}
|
||||
|
||||
|
@@ -551,19 +551,22 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontend() {
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -747,19 +750,22 @@ func (suite *InternalToFrontendTestSuite) TestWarnFilteredStatusToFrontend() {
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -927,19 +933,22 @@ func (suite *InternalToFrontendTestSuite) TestWarnFilteredBoostToFrontend() {
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -1010,19 +1019,22 @@ func (suite *InternalToFrontendTestSuite) TestWarnFilteredBoostToFrontend() {
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -1257,19 +1269,22 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownAttachments
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -1560,19 +1575,22 @@ func (suite *InternalToFrontendTestSuite) TestStatusToFrontendUnknownLanguage()
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
@@ -2561,19 +2579,22 @@ func (suite *InternalToFrontendTestSuite) TestAdminReportToFrontend2() {
|
||||
"interaction_policy": {
|
||||
"can_favourite": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reply": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
},
|
||||
"can_reblog": {
|
||||
"always": [
|
||||
"public"
|
||||
"public",
|
||||
"me"
|
||||
],
|
||||
"with_approval": []
|
||||
}
|
||||
|
Reference in New Issue
Block a user