diff --git a/pub/fed.go b/pub/fed.go index c678951..675b6c3 100644 --- a/pub/fed.go +++ b/pub/fed.go @@ -624,7 +624,7 @@ func (f *federator) handleClientAdd(c context.Context, deliverable *bool) func(s } obj := raw.GetObject(i) for _, target := range targets { - if !f.SocialAPI.CanAdd(c, obj, target) { + if !f.App.CanAdd(c, obj, target) { continue } if ct, ok := target.(vocab.CollectionType); ok { @@ -688,7 +688,7 @@ func (f *federator) handleClientRemove(c context.Context, deliverable *bool) fun } obj := raw.GetObject(i) for _, target := range targets { - if !f.SocialAPI.CanRemove(c, obj, target) { + if !f.App.CanRemove(c, obj, target) { continue } if ct, ok := target.(vocab.CollectionType); ok { @@ -1028,7 +1028,7 @@ func (f *federator) handleAdd(c context.Context) func(s *streams.Add) error { } obj := raw.GetObject(i) for _, target := range targets { - if !f.FederateAPI.CanFederateAdd(c, obj, target) { + if !f.App.CanAdd(c, obj, target) { continue } if ct, ok := target.(vocab.CollectionType); ok { @@ -1092,7 +1092,7 @@ func (f *federator) handleRemove(c context.Context) func(s *streams.Remove) erro } obj := raw.GetObject(i) for _, target := range targets { - if !f.FederateAPI.CanFederateRemove(c, obj, target) { + if !f.App.CanRemove(c, obj, target) { continue } if ct, ok := target.(vocab.CollectionType); ok { diff --git a/pub/fed_test.go b/pub/fed_test.go index 8fb09d5..16cc66a 100644 --- a/pub/fed_test.go +++ b/pub/fed_test.go @@ -564,6 +564,8 @@ type MockApplication struct { getOutbox func(c context.Context, r *http.Request, rw RWType) (vocab.OrderedCollectionType, error) newId func(c context.Context, t Typer) *url.URL getPublicKey func(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) + canAdd func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool + canRemove func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool } func (m *MockApplication) Owns(c context.Context, id *url.URL) bool { @@ -629,32 +631,30 @@ func (m *MockApplication) GetPublicKey(c context.Context, publicKeyId string) (c return m.getPublicKey(c, publicKeyId) } +func (m *MockApplication) CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + if m.canAdd == nil { + m.t.Fatal("unexpected call to MockApplication CanAdd") + } + return m.canAdd(c, o, t) +} + +func (m *MockApplication) CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + if m.canRemove == nil { + m.t.Fatal("unexpected call to MockApplication CanRemove") + } + return m.canRemove(c, o, t) +} + var _ SocialApplication = &MockSocialApp{} type MockSocialApp struct { *MockApplication t *testing.T - canAdd func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool - canRemove func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool actorIRI func(c context.Context, r *http.Request) (*url.URL, error) getPublicKeyForOutbox func(c context.Context, publicKeyId string, boxIRI *url.URL) (crypto.PublicKey, httpsig.Algorithm, error) getSocialAPIVerifier func(c context.Context) SocialAPIVerifier } -func (m *MockSocialApp) CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { - if m.canAdd == nil { - m.t.Fatal("unexpected call to MockSocialApp CanAdd") - } - return m.canAdd(c, o, t) -} - -func (m *MockSocialApp) CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { - if m.canRemove == nil { - m.t.Fatal("unexpected call to MockSocialApp CanRemove") - } - return m.canRemove(c, o, t) -} - func (m *MockSocialApp) ActorIRI(c context.Context, r *http.Request) (*url.URL, error) { if m.actorIRI == nil { m.t.Fatal("unexpected call to MockSocialApp ActorIRI") @@ -797,8 +797,6 @@ var _ FederateApplication = &MockFederateApp{} type MockFederateApp struct { *MockApplication t *testing.T - canAdd func(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool - canRemove func(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool onFollow func(c context.Context, s *streams.Follow) FollowResponse unblocked func(c context.Context, actorIRIs []*url.URL) error getFollowing func(c context.Context, actor *url.URL) (vocab.CollectionType, error) @@ -807,20 +805,6 @@ type MockFederateApp struct { privateKey func(boxIRI *url.URL) (crypto.PrivateKey, string, error) } -func (m *MockFederateApp) CanFederateAdd(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool { - if m.canAdd == nil { - m.t.Fatal("unexpected call to MockFederateApp CanFederateAdd") - } - return m.canAdd(c, obj, target) -} - -func (m *MockFederateApp) CanFederateRemove(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool { - if m.canRemove == nil { - m.t.Fatal("unexpected call to MockFederateApp CanFederateRemove") - } - return m.canRemove(c, obj, target) -} - func (m *MockFederateApp) OnFollow(c context.Context, s *streams.Follow) FollowResponse { if m.onFollow == nil { m.t.Fatal("unexpected call to MockFederateApp OnFollow") @@ -897,6 +881,12 @@ func (m *MockSocialFederateApp) NewId(c context.Context, t Typer) *url.URL { func (m *MockSocialFederateApp) GetPublicKey(c context.Context, publicKeyId string) (crypto.PublicKey, httpsig.Algorithm, *url.URL, error) { return m.MockFederateApp.GetPublicKey(c, publicKeyId) } +func (m *MockSocialFederateApp) CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + return m.MockFederateApp.CanAdd(c, o, t) +} +func (m *MockSocialFederateApp) CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + return m.MockFederateApp.CanRemove(c, o, t) +} var _ Deliverer = &MockDeliverer{} @@ -3391,7 +3381,7 @@ func TestPostInbox_Add_AddIfTargetOwnedAndAppCanAdd(t *testing.T) { gotCanAdd := 0 var gotCanAddObject vocab.ObjectType var gotCanAddTarget vocab.ObjectType - fedApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { gotCanAdd++ gotCanAddObject = o gotCanAddTarget = t @@ -3460,7 +3450,7 @@ func TestPostInbox_Add_DoesNotAddIfAppCannotAdd(t *testing.T) { gotCanAdd := 0 var gotCanAddObject vocab.ObjectType var gotCanAddTarget vocab.ObjectType - fedApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { gotCanAdd++ gotCanAddObject = o gotCanAddTarget = t @@ -3503,7 +3493,7 @@ func TestPostInbox_Add_CallsCallback(t *testing.T) { v := &vocab.Collection{} return v, nil } - fedApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canAdd = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { return true } gotCallback := 0 @@ -3579,7 +3569,7 @@ func TestPostInbox_Remove_RemoveIfTargetOwnedAndCanRemove(t *testing.T) { gotCanRemove := 0 var gotCanRemoveObject vocab.ObjectType var gotCanRemoveTarget vocab.ObjectType - fedApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { gotCanRemove++ gotCanRemoveObject = o gotCanRemoveTarget = t @@ -3647,7 +3637,7 @@ func TestPostInbox_Remove_DoesNotRemoveIfAppCannotRemove(t *testing.T) { gotCanRemove := 0 var gotCanRemoveObject vocab.ObjectType var gotCanRemoveTarget vocab.ObjectType - fedApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { gotCanRemove++ gotCanRemoveObject = o gotCanRemoveTarget = t @@ -3692,7 +3682,7 @@ func TestPostInbox_Remove_CallsCallback(t *testing.T) { v := &vocab.Collection{} return v, nil } - fedApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canRemove = func(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool { return true } gotCallback := 0 @@ -5316,7 +5306,7 @@ func TestPostOutbox_Add_AddsIfTargetOwnedAndAppCanAdd(t *testing.T) { gotCanAdd := 0 var canAddObj vocab.ObjectType var canAddTarget vocab.ObjectType - socialApp.canAdd = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canAdd = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { gotCanAdd++ canAddObj = obj canAddTarget = t @@ -5379,7 +5369,7 @@ func TestPostOutbox_Add_DoesNotAddIfAppCannotAdd(t *testing.T) { gotCanAdd := 0 var canAddObj vocab.ObjectType var canAddTarget vocab.ObjectType - socialApp.canAdd = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canAdd = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { gotCanAdd++ canAddObj = obj canAddTarget = t @@ -5534,7 +5524,7 @@ func TestPostOutbox_Remove_RemoveIfTargetOwnedAndCanRemove(t *testing.T) { gotCanRemove := 0 var canRemoveObj vocab.ObjectType var canRemoveTarget vocab.ObjectType - socialApp.canRemove = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canRemove = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { gotCanRemove++ canRemoveObj = obj canRemoveTarget = t @@ -5597,7 +5587,7 @@ func TestPostOutbox_Remove_DoesNotRemoveIfAppCannotRemove(t *testing.T) { gotCanRemove := 0 var canRemoveObj vocab.ObjectType var canRemoveTarget vocab.ObjectType - socialApp.canRemove = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { + app.MockFederateApp.canRemove = func(c context.Context, obj vocab.ObjectType, t vocab.ObjectType) bool { gotCanRemove++ canRemoveObj = obj canRemoveTarget = t diff --git a/pub/interfaces.go b/pub/interfaces.go index 2940269..40c6d47 100644 --- a/pub/interfaces.go +++ b/pub/interfaces.go @@ -105,6 +105,14 @@ type Application interface { // key id. It also determines which algorithm to use to verify the // signature. GetPublicKey(c context.Context, publicKeyId string) (pubKey crypto.PublicKey, algo httpsig.Algorithm, user *url.URL, err error) + // CanAdd returns true if the provided object is allowed to be added to + // the given target collection. Applicable to either or both of the + // SocialAPI and FederateAPI. + CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool + // CanRemove returns true if the provided object is allowed to be + // removed from the given target collection. Applicable to either or + // both of the SocialAPI and FederateAPI. + CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool } // RWType indicates the kind of reading being done. @@ -120,12 +128,6 @@ const ( // SocialAPI is provided by users of this library and designed to handle // receiving messages from ActivityPub clients through the Social API. type SocialAPI interface { - // CanAdd returns true if the provided object is allowed to be added to - // the given target collection. - CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool - // CanRemove returns true if the provided object is allowed to be - // removed from the given target collection. - CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool // AddToOutboxResolver(c context.Context) (*streams.Resolver, error) // ActorIRI returns the actor's IRI associated with the given request. ActorIRI(c context.Context, r *http.Request) (*url.URL, error) @@ -149,12 +151,6 @@ type SocialAPI interface { // FederateAPI is provided by users of this library and designed to handle // receiving messages from ActivityPub servers through the Federative API. type FederateAPI interface { - // CanFederateAdd returns true if the provided object is allowed to be added to - // the given target collection. - CanFederateAdd(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool - // CanFederateRemove returns true if the provided object is allowed to be added to - // the given target collection. - CanFederateRemove(c context.Context, obj vocab.ObjectType, target vocab.ObjectType) bool // OnFollow determines whether to take any automatic reactions in // response to this follow. Note that if this application does not own // an object on the activity, then the 'AutomaticAccept' and