Skip to content

Commit

Permalink
weenus
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmethurst committed Jan 21, 2025
1 parent 5c0514e commit 9784189
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 139 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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.9.0-gts.0.20250121090817-0ef92d24eba1
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

0 comments on commit 9784189

Please sign in to comment.