[chore] Update interactionPolicy sub-policy parsing in line with documented defaults (#4229)

# Description

> If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements.
>
> If this is a documentation change, please briefly describe what you've changed and why.

Brings our parsing of unset sub-policies in line with the defaults documented here: https://docs.gotosocial.org/en/v0.19.1/federation/interaction_policy/#defaults-per-sub-policy

Closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4146

Part of https://codeberg.org/superseriousbusiness/gotosocial/issues/4026

## Checklist

Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]`

If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want).

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [x] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4229
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
This commit is contained in:
tobi 2025-06-10 14:29:42 +02:00 committed by tobi
commit 1dc79c9586
10 changed files with 613 additions and 187 deletions

View file

@ -84,8 +84,8 @@ func (f *Filter) StatusLikeable(
}
switch {
// If status has policy set, check against that.
case status.InteractionPolicy != nil:
// If status has canLike sub-policy set, check against that.
case status.InteractionPolicy != nil && status.InteractionPolicy.CanLike != nil:
return f.checkPolicy(
ctx,
requester,
@ -95,19 +95,18 @@ func (f *Filter) StatusLikeable(
// If status is local and has no policy set,
// check against the default policy for this
// visibility, as we're interaction-policy aware.
// visibility, as we're canLike sub-policy aware.
case *status.Local:
policy := gtsmodel.DefaultInteractionPolicyFor(status.Visibility)
return f.checkPolicy(
ctx,
requester,
status,
policy.CanLike,
gtsmodel.DefaultCanLikeFor(status.Visibility),
)
// Otherwise, assume the status is from an
// instance that does not use / does not care
// about interaction policies, and just return OK.
// about canLike sub-policy, and just return OK.
default:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionAutomaticApproval,
@ -235,8 +234,8 @@ func (f *Filter) StatusReplyable(
}
switch {
// If status has policy set, check against that.
case status.InteractionPolicy != nil:
// If status has canReply sub-policy set, check against that.
case status.InteractionPolicy != nil && status.InteractionPolicy.CanReply != nil:
return f.checkPolicy(
ctx,
requester,
@ -245,20 +244,19 @@ func (f *Filter) StatusReplyable(
)
// If status is local and has no policy set,
// check against the default policy for this
// visibility, as we're interaction-policy aware.
// check against the default canReply for this
// visibility, as we're canReply sub-policy aware.
case *status.Local:
policy := gtsmodel.DefaultInteractionPolicyFor(status.Visibility)
return f.checkPolicy(
ctx,
requester,
status,
policy.CanReply,
gtsmodel.DefaultCanReplyFor(status.Visibility),
)
// Otherwise, assume the status is from an
// instance that does not use / does not care
// about interaction policies, and just return OK.
// about canReply sub-policy, and just return OK.
default:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionAutomaticApproval,
@ -297,8 +295,8 @@ func (f *Filter) StatusBoostable(
}
switch {
// If status has policy set, check against that.
case status.InteractionPolicy != nil:
// If status has canAnnounce sub-policy set, check against that.
case status.InteractionPolicy != nil && status.InteractionPolicy.CanAnnounce != nil:
return f.checkPolicy(
ctx,
requester,
@ -319,7 +317,7 @@ func (f *Filter) StatusBoostable(
)
// Status is from an instance that does not use
// or does not care about interaction policies.
// or does not care about canAnnounce sub-policy.
// We can boost it if it's unlisted or public.
case status.Visibility == gtsmodel.VisibilityPublic ||
status.Visibility == gtsmodel.VisibilityUnlocked:
@ -340,7 +338,7 @@ func (f *Filter) checkPolicy(
ctx context.Context,
requester *gtsmodel.Account,
status *gtsmodel.Status,
rules gtsmodel.PolicyRules,
rules *gtsmodel.PolicyRules,
) (*gtsmodel.PolicyCheckResult, error) {
// Wrap context to be able to
@ -349,8 +347,8 @@ func (f *Filter) checkPolicy(
fctx.Context = ctx
// Check if requester matches a PolicyValue
// to be always allowed to do this.
matchAlways, matchAlwaysValue, err := f.matchPolicy(fctx,
// to be automatically approved for this.
matchAutomatic, matchAutomaticValue, err := f.matchPolicy(fctx,
requester,
status,
rules.AutomaticApproval,
@ -360,40 +358,40 @@ func (f *Filter) checkPolicy(
}
// Check if requester matches a PolicyValue
// to be allowed to do this pending approval.
matchWithApproval, _, err := f.matchPolicy(fctx,
// to be manually approved for this.
matchManual, _, err := f.matchPolicy(fctx,
requester,
status,
rules.ManualApproval,
)
if err != nil {
return nil, gtserror.Newf("error checking policy approval match: %w", err)
return nil, gtserror.Newf("error checking policy match: %w", err)
}
switch {
// Prefer explicit match,
// prioritizing "always".
case matchAlways == explicit:
// prioritizing automatic.
case matchAutomatic == explicit:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionAutomaticApproval,
PermissionMatchedOn: &matchAlwaysValue,
PermissionMatchedOn: &matchAutomaticValue,
}, nil
case matchWithApproval == explicit:
case matchManual == explicit:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionManualApproval,
}, nil
// Then try implicit match,
// prioritizing "always".
case matchAlways == implicit:
// prioritizing automatic.
case matchAutomatic == implicit:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionAutomaticApproval,
PermissionMatchedOn: &matchAlwaysValue,
PermissionMatchedOn: &matchAutomaticValue,
}, nil
case matchWithApproval == implicit:
case matchManual == implicit:
return &gtsmodel.PolicyCheckResult{
Permission: gtsmodel.PolicyPermissionManualApproval,
}, nil

View file

@ -0,0 +1,207 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package interaction_test
import (
"strconv"
"testing"
"code.superseriousbusiness.org/gotosocial/internal/gtsmodel"
"code.superseriousbusiness.org/gotosocial/testrig"
"github.com/stretchr/testify/suite"
)
const (
rMediaPath = "../../../testrig/media"
rTemplatePath = "../../../web/template"
)
type InteractionTestSuite struct {
suite.Suite
testStatuses map[string]*gtsmodel.Status
testAccounts map[string]*gtsmodel.Account
}
func (suite *InteractionTestSuite) SetupSuite() {
testrig.InitTestConfig()
testrig.InitTestLog()
suite.testStatuses = testrig.NewTestStatuses()
suite.testAccounts = testrig.NewTestAccounts()
}
func (suite *InteractionTestSuite) TestInteractable() {
testStructs := testrig.SetupTestStructs(rMediaPath, rTemplatePath)
defer testrig.TearDownTestStructs(testStructs)
// Take zork's introduction post
// as the base post for these tests.
modelStatus := suite.testStatuses["local_account_1_status_1"]
ctx := suite.T().Context()
for i, test := range []struct {
policy *gtsmodel.InteractionPolicy
account *gtsmodel.Account
likeable gtsmodel.PolicyPermission
replyable gtsmodel.PolicyPermission
boostable gtsmodel.PolicyPermission
}{
{
// Nil policy. Should all be fine as
// it will fall back to the default then.
policy: nil,
account: suite.testAccounts["admin_account"],
likeable: gtsmodel.PolicyPermissionAutomaticApproval,
replyable: gtsmodel.PolicyPermissionAutomaticApproval,
boostable: gtsmodel.PolicyPermissionAutomaticApproval,
},
{
// Nil canLike, everything else
// restricted to author only.
// Only the nil sub-policy should be OK.
policy: &gtsmodel.InteractionPolicy{
CanLike: nil,
CanReply: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
},
CanAnnounce: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
},
},
account: suite.testAccounts["admin_account"],
likeable: gtsmodel.PolicyPermissionAutomaticApproval,
replyable: gtsmodel.PolicyPermissionForbidden,
boostable: gtsmodel.PolicyPermissionForbidden,
},
{
// All restricted it's the author's own
// account checking, so all should be fine.
policy: &gtsmodel.InteractionPolicy{
CanLike: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
},
CanReply: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
},
CanAnnounce: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
},
},
account: suite.testAccounts["local_account_1"],
likeable: gtsmodel.PolicyPermissionAutomaticApproval,
replyable: gtsmodel.PolicyPermissionAutomaticApproval,
boostable: gtsmodel.PolicyPermissionAutomaticApproval,
},
{
// Followers can like automatically,
// everything else requires manual approval.
policy: &gtsmodel.InteractionPolicy{
CanLike: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
gtsmodel.PolicyValueFollowers,
},
ManualApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValuePublic,
},
},
CanReply: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
ManualApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValuePublic,
},
},
CanAnnounce: &gtsmodel.PolicyRules{
AutomaticApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValueAuthor,
},
ManualApproval: gtsmodel.PolicyValues{
gtsmodel.PolicyValuePublic,
},
},
},
account: suite.testAccounts["admin_account"],
likeable: gtsmodel.PolicyPermissionAutomaticApproval,
replyable: gtsmodel.PolicyPermissionManualApproval,
boostable: gtsmodel.PolicyPermissionManualApproval,
},
} {
// Copy model status.
status := new(gtsmodel.Status)
*status = *modelStatus
// Set test policy on it.
status.InteractionPolicy = test.policy
// Check likeableRes.
likeableRes, err := testStructs.InteractionFilter.StatusLikeable(ctx, test.account, status)
if err != nil {
suite.FailNow(err.Error())
}
if likeableRes.Permission != test.likeable {
suite.Fail(
"failure in case "+strconv.FormatInt(int64(i), 10),
"expected likeable result \"%s\", got \"%s\"",
likeableRes.Permission, test.likeable,
)
}
// Check replable.
replyableRes, err := testStructs.InteractionFilter.StatusReplyable(ctx, test.account, status)
if err != nil {
suite.FailNow(err.Error())
}
if replyableRes.Permission != test.replyable {
suite.Fail(
"failure in case "+strconv.FormatInt(int64(i), 10),
"expected replyable result \"%s\", got \"%s\"",
replyableRes.Permission, test.replyable,
)
}
// Check boostable.
boostableRes, err := testStructs.InteractionFilter.StatusBoostable(ctx, test.account, status)
if err != nil {
suite.FailNow(err.Error())
}
if boostableRes.Permission != test.boostable {
suite.Fail(
"failure in case "+strconv.FormatInt(int64(i), 10),
"expected boostable result \"%s\", got \"%s\"",
boostableRes.Permission, test.boostable,
)
}
}
}
func TestInteractionTestSuite(t *testing.T) {
suite.Run(t, new(InteractionTestSuite))
}