Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Implement deliveryRecipientPreSort to prioritize delivery to mentioned accounts #3668

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/superseriousbusiness/activity v1.9.0-gts
github.com/superseriousbusiness/activity v1.10.0-gts
github.com/superseriousbusiness/httpsig v1.2.0-SSB
github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB.0.20230227143000-f4900831d6c8
github.com/tdewolff/minify/v2 v2.21.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 57 additions & 1 deletion internal/federation/federatingactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import (
"fmt"
"net/http"
"net/url"
"slices"

errorsv2 "codeberg.org/gruf/go-errors/v2"
"codeberg.org/gruf/go-kv"
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/ap"
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/uris"
)

// federatingActor wraps the pub.FederatingActor
Expand All @@ -42,10 +45,63 @@ type federatingActor struct {
wrapped pub.FederatingActor
}

func deliveryRecipientPreSort(actorAndCollectionIRIs []*url.URL) []*url.URL {
var (
thisHost = config.GetHost()
thisAcctDomain = config.GetAccountDomain()
)

slices.SortFunc(
actorAndCollectionIRIs,
func(a *url.URL, b *url.URL) int {
// We want to sort by putting more specific actor URIs *before* collection URIs.
// Since the only collection URIs we ever address are our own followers URIs, we
// can just use host and regexes to identify these collections, and shove them
// to the back of the slice. This ensures that directly addressed (ie., mentioned)
// accounts get delivery-attempted *first*, and then delivery attempts move on to
// followers of the author. This should have the effect of making conversation
/// threads feel more snappy, as replies will be sent quicker to participants.
var (
aIsFollowers = (a.Host == thisHost || a.Host == thisAcctDomain) && uris.IsFollowersPath(a)
bIsFollowers = (b.Host == thisHost || b.Host == thisAcctDomain) && uris.IsFollowersPath(b)
)

switch {
case aIsFollowers == bIsFollowers:
// Both followers URIs or
// both not followers URIs,
// order doesn't matter.
return 0

case aIsFollowers:
// a is followers
// URI, b is not.
//
// Sort b before a.
return 1

default:
// b is followers
// URI, a is not.
//
// Sort a before b.
return -1
}
},
)

return actorAndCollectionIRIs
}

// newFederatingActor returns a federatingActor.
func newFederatingActor(c pub.CommonBehavior, s2s pub.FederatingProtocol, db pub.Database, clock pub.Clock) pub.FederatingActor {
sideEffectActor := pub.NewSideEffectActor(c, s2s, nil, db, clock)
sideEffectActor.Serialize = ap.Serialize // hook in our own custom Serialize function

// Hook in our own custom Serialize function.
sideEffectActor.Serialize = ap.Serialize

// Hook in our own custom recipient pre-sort function.
sideEffectActor.DeliveryRecipientPreSort = deliveryRecipientPreSort

return &federatingActor{
sideEffectActor: sideEffectActor,
Expand Down
Loading