From 8fa0f2395c40c7df29d969b6a1f463668c6197f3 Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Thu, 2 Aug 2018 22:29:03 +0200 Subject: [PATCH] Add Like activities to the likes collection. Previously, it was incorrectly adding the actors to the likes collection. --- pub/fed.go | 2 +- pub/fed_test.go | 23 ++++++-------- pub/internal.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 15 deletions(-) diff --git a/pub/fed.go b/pub/fed.go index f8adb24..51dd5dd 100644 --- a/pub/fed.go +++ b/pub/fed.go @@ -1226,7 +1226,7 @@ func (f *federator) handleLike(c context.Context) func(s *streams.Like) error { } return false, fmt.Errorf("cannot determine type of object likes") } - if _, err := f.addAllActorsToObjectCollection(c, getter, s.Raw(), true); err != nil { + if _, err := f.addActivityToObjectCollection(c, getter, s.Raw(), true); err != nil { return err } return f.ServerCallbacker.Like(c, s) diff --git a/pub/fed_test.go b/pub/fed_test.go index 65fccb1..15818a0 100644 --- a/pub/fed_test.go +++ b/pub/fed_test.go @@ -3770,7 +3770,7 @@ func TestPostInbox_Like_AddsToLikeCollection(t *testing.T) { } handled, err := p.PostInbox(context.Background(), resp, req) expected := &vocab.Collection{} - expected.AppendItemsIRI(sallyIRI) + expected.AppendItemsIRI(noteActivityIRI) expectedNote := &vocab.Note{} expectedNote.SetId(noteIRI) expectedNote.AppendNameString(noteName) @@ -3805,7 +3805,7 @@ func TestPostInbox_Like_DoesNotAddLikeToCollectionIfAlreadyPresent(t *testing.T) } app.MockFederateApp.get = func(c context.Context, id *url.URL, rw RWType) (PubObject, error) { likes := &vocab.Collection{} - likes.AppendItemsIRI(sallyIRI) + likes.AppendItemsIRI(noteActivityIRI) v := &vocab.Note{} v.SetId(noteIRI) v.AppendNameString(noteName) @@ -3826,20 +3826,15 @@ func TestPostInbox_Like_DoesNotAddLikeToCollectionIfAlreadyPresent(t *testing.T) return nil } handled, err := p.PostInbox(context.Background(), resp, req) - expected := &vocab.Collection{} - expected.AppendItemsIRI(sallyIRI) - expectedNote := &vocab.Note{} - expectedNote.SetId(noteIRI) - expectedNote.AppendNameString(noteName) - expectedNote.AppendContentString("This is a simple note") - expectedNote.SetLikesCollection(expected) + expected := &vocab.OrderedCollection{} + expected.AppendOrderedItemsIRI(noteActivityIRI) if err != nil { t.Fatal(err) } else if !handled { t.Fatalf("expected handled, got !handled") - } else if gotSet != 2 { - t.Fatalf("expected %d, got %d", 2, gotSet) - } else if err := PubObjectEquals(gotSetObject, expectedNote); err != nil { + } else if gotSet != 1 { + t.Fatalf("expected %d, got %d", 1, gotSet) + } else if err := PubObjectEquals(gotSetObject, expected); err != nil { t.Fatalf("unexpected callback object: %s", err) } } @@ -3874,7 +3869,7 @@ func TestPostInbox_Like_AddsToLikeOrderedCollection(t *testing.T) { } handled, err := p.PostInbox(context.Background(), resp, req) expected := &vocab.OrderedCollection{} - expected.AppendOrderedItemsIRI(sallyIRI) + expected.AppendOrderedItemsIRI(noteActivityIRI) expectedNote := &vocab.Note{} expectedNote.SetId(noteIRI) expectedNote.AppendNameString(noteName) @@ -3925,7 +3920,7 @@ func TestPostInbox_Like_AddsToLikeIRI(t *testing.T) { } handled, err := p.PostInbox(context.Background(), resp, req) expected := &vocab.OrderedCollection{} - expected.AppendOrderedItemsIRI(sallyIRI) + expected.AppendOrderedItemsIRI(noteActivityIRI) if err != nil { t.Fatal(err) } else if !handled { diff --git a/pub/internal.go b/pub/internal.go index 76d7393..4da0c6b 100644 --- a/pub/internal.go +++ b/pub/internal.go @@ -1646,6 +1646,88 @@ func (f *federator) addAllActorsToObjectCollection(ctx context.Context, getter g return ownsAny, nil } +func (f *federator) addActivityToObjectCollection(ctx context.Context, getter getObjectCollectionFn, c vocab.ActivityType, prepend bool) (bool, error) { + ownsAny := false + for i := 0; i < c.ObjectLen(); i++ { + var objIri *url.URL + if c.IsObject(i) { + obj := c.GetObject(i) + if !obj.HasId() { + return ownsAny, fmt.Errorf("object does not have id") + } + objIri = obj.GetId() + } else if c.IsObjectIRI(i) { + objIri = c.GetObjectIRI(i) + } + if !f.App.Owns(ctx, objIri) { + // TODO: Fetch or just store + continue + } + ownsAny = true + var object vocab.ObjectType + pObj, err := f.App.Get(ctx, objIri, ReadWrite) + if err != nil { + return ownsAny, err + } + ok := false + object, ok = pObj.(vocab.ObjectType) + if !ok { + return ownsAny, fmt.Errorf("object is not vocab.ObjectType") + } + // Obtain ordered/unordered collection + var lc vocab.CollectionType + var loc vocab.OrderedCollectionType + isIRI := false + if isIRI, err = getter(object, &lc, &loc); err != nil { + return ownsAny, err + } + // Duplication detection + var iriSet map[string]bool + if lc != nil { + iriSet, err = getIRISetFromItems(lc) + } else if loc != nil { + iriSet, err = getIRISetFromOrderedItems(loc) + } + if err != nil { + return ownsAny, err + } + // Add activity to collection if not a duplicate + if !c.HasId() { + return ownsAny, fmt.Errorf("activity has no id") + } + iri := c.GetId() + if iriSet[iri.String()] { + continue + } + if lc != nil { + if prepend { + lc.AppendItemsIRI(iri) + } else { + lc.PrependItemsIRI(iri) + } + } else if loc != nil { + if prepend { + loc.PrependOrderedItemsIRI(iri) + } else { + loc.AppendOrderedItemsIRI(iri) + } + } + if isIRI { + if lc != nil { + err = f.App.Set(ctx, lc) + } else if loc != nil { + err = f.App.Set(ctx, loc) + } + if err != nil { + return ownsAny, err + } + } else if err := f.App.Set(ctx, object); err != nil { + return ownsAny, err + } + } + return ownsAny, nil +} + func (f *federator) ownsAnyObjects(c context.Context, a vocab.ActivityType) (bool, error) { var iris []*url.URL for i := 0; i < a.ObjectLen(); i++ {