From 558da36360169c3b23c5ca679fd58049a6ecad17 Mon Sep 17 00:00:00 2001 From: Marius Orcsik Date: Sat, 3 Apr 2021 13:43:33 +0200 Subject: [PATCH] Moved the flatten functionality to this package --- activity_test.go | 4 -- flatten.go | 122 ++++++++++++++++++++++++++++++++++ flatten_test.go | 89 +++++++++++++++++++++++++ intransitive_activity_test.go | 4 -- item_collection.go | 12 +--- item_collection_test.go | 14 ++-- object_test.go | 4 -- 7 files changed, 217 insertions(+), 32 deletions(-) create mode 100644 flatten.go create mode 100644 flatten_test.go diff --git a/activity_test.go b/activity_test.go index 7fe8c29..e3f4124 100644 --- a/activity_test.go +++ b/activity_test.go @@ -953,10 +953,6 @@ func TestToActivity(t *testing.T) { } } -func TestFlattenActivityProperties(t *testing.T) { - t.Skipf("TODO") -} - func TestValidEventRSVPActivityType(t *testing.T) { t.Skipf("TODO") } diff --git a/flatten.go b/flatten.go new file mode 100644 index 0000000..58593e8 --- /dev/null +++ b/flatten.go @@ -0,0 +1,122 @@ +package activitypub + +// FlattenActivityProperties flattens the Activity's properties from Object type to IRI +func FlattenActivityProperties(act *Activity) *Activity { + OnIntransitiveActivity(act, func(in *IntransitiveActivity) error { + FlattenIntransitiveActivityProperties(in) + return nil + }) + act.Object = FlattenToIRI(act.Object) + return act +} + +// FlattenIntransitiveActivityProperties flattens the Activity's properties from Object type to IRI +func FlattenIntransitiveActivityProperties(act *IntransitiveActivity) *IntransitiveActivity { + act.Actor = FlattenToIRI(act.Actor) + act.Target = FlattenToIRI(act.Target) + act.Result = FlattenToIRI(act.Result) + act.Origin = FlattenToIRI(act.Origin) + act.Result = FlattenToIRI(act.Result) + act.Instrument = FlattenToIRI(act.Instrument) + OnObject(act, func(o *Object) error { + o = FlattenObjectProperties(o) + return nil + }) + return act +} + +// FlattenItemCollection flattens an Item Collection to their respective IRIs +func FlattenItemCollection(col ItemCollection) ItemCollection { + if col == nil { + return col + } + for k, it := range ItemCollectionDeduplication(&col) { + if iri := it.GetLink(); iri != "" { + col[k] = iri + } + } + return col +} + +// FlattenCollection flattens a Collection's objects to their respective IRIs +func FlattenCollection(col *Collection) *Collection { + if col == nil { + return col + } + col.Items = FlattenItemCollection(col.Items) + + return col +} + +// FlattenOrderedCollection flattens an OrderedCollection's objects to their respective IRIs +func FlattenOrderedCollection(col *OrderedCollection) *OrderedCollection { + if col == nil { + return col + } + col.OrderedItems = FlattenItemCollection(col.OrderedItems) + + return col +} + +// FlattenActorProperties flattens the Actor's properties from Object types to IRI +func FlattenActorProperties(a *Actor) *Actor { + OnObject(a, func(o *Object) error { + o = FlattenObjectProperties(o) + return nil + }) + return a +} + +// FlattenObjectProperties flattens the Object's properties from Object types to IRI +func FlattenObjectProperties(o *Object) *Object { + o.Replies = Flatten(o.Replies) + o.Shares = Flatten(o.Shares) + o.Likes = Flatten(o.Likes) + o.AttributedTo = Flatten(o.AttributedTo) + o.To = FlattenItemCollection(o.To) + o.Bto = FlattenItemCollection(o.Bto) + o.CC = FlattenItemCollection(o.CC) + o.BCC = FlattenItemCollection(o.BCC) + o.Audience = FlattenItemCollection(o.Audience) + //o.Tag = FlattenItemCollection(o.Tag) + return o +} + +// FlattenProperties flattens the Item's properties from Object types to IRI +func FlattenProperties(it Item) Item { + if ActivityTypes.Contains(it.GetType()) { + OnActivity(it, func(a *Activity) error { + a = FlattenActivityProperties(a) + return nil + }) + } + if ActorTypes.Contains(it.GetType()) { + OnActor(it, func(a *Actor) error { + a = FlattenActorProperties(a) + return nil + }) + } + if ObjectTypes.Contains(it.GetType()) { + OnObject(it, func(o *Object) error { + o = FlattenObjectProperties(o) + return nil + }) + } + return it +} + +// Flatten checks if Item can be flatten to an IRI or array of IRIs and returns it if so +func Flatten(it Item) Item { + if it == nil { + return nil + } + if it.IsCollection() { + if c, ok := it.(CollectionInterface); ok { + it = FlattenItemCollection(c.Collection()) + } + } + if it != nil && len(it.GetLink()) > 0 { + return it.GetLink() + } + return it +} diff --git a/flatten_test.go b/flatten_test.go new file mode 100644 index 0000000..5bcfc10 --- /dev/null +++ b/flatten_test.go @@ -0,0 +1,89 @@ +package activitypub + +import ( + "reflect" + "testing" +) + +func TestFlattenPersonProperties(t *testing.T) { + t.Skipf("TODO") +} + +func TestFlattenProperties(t *testing.T) { + t.Skipf("TODO") +} + +func TestFlattenItemCollection(t *testing.T) { + t.Skipf("TODO") +} + +func TestFlattenCollection(t *testing.T) { + t.Skipf("TODO") +} + +func TestFlattenOrderedCollection(t *testing.T) { + t.Skipf("TODO") +} + +func TestFlattenIntransitiveActivityProperties(t *testing.T) { + type args struct { + act *IntransitiveActivity + } + tests := []struct { + name string + args args + want *IntransitiveActivity + }{ + { + name: "blank", + args: args{&IntransitiveActivity{}}, + want: &IntransitiveActivity{}, + }, + { + name: "flatten-actor", + args: args{&IntransitiveActivity{Actor: &Actor{ID: "example-actor-iri"}}}, + want: &IntransitiveActivity{Actor: IRI("example-actor-iri")}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FlattenIntransitiveActivityProperties(tt.args.act); !reflect.DeepEqual(got, tt.want) { + t.Errorf("FlattenIntransitiveActivityProperties() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestFlattenActivityProperties(t *testing.T) { + type args struct { + act *Activity + } + tests := []struct { + name string + args args + want *Activity + }{ + { + name: "blank", + args: args{&Activity{}}, + want: &Activity{}, + }, + { + name: "flatten-actor", + args: args{&Activity{Actor: &Actor{ID: "example-actor-iri"}}}, + want: &Activity{Actor: IRI("example-actor-iri")}, + }, + { + name: "flatten-object", + args: args{&Activity{Object: &Object{ID: "example-actor-iri"}}}, + want: &Activity{Object: IRI("example-actor-iri")}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FlattenActivityProperties(tt.args.act); !reflect.DeepEqual(got, tt.want) { + t.Errorf("FlattenActivityProperties() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/intransitive_activity_test.go b/intransitive_activity_test.go index 71a8a03..b59f22e 100644 --- a/intransitive_activity_test.go +++ b/intransitive_activity_test.go @@ -221,10 +221,6 @@ func TestToIntransitiveActivity(t *testing.T) { } } -func TestFlattenIntransitiveActivityProperties(t *testing.T) { - t.Skipf("TODO") -} - func TestIntransitiveActivity_Clean(t *testing.T) { t.Skipf("TODO") } diff --git a/item_collection.go b/item_collection.go index d919aff..3406354 100644 --- a/item_collection.go +++ b/item_collection.go @@ -107,7 +107,7 @@ func (i *ItemCollection) Remove(r Item) { if remIdx == -1 { return } - if remIdx < li - 1 { + if remIdx < li-1 { *i = append((*i)[:remIdx], (*i)[remIdx+1:]...) } else { *i = (*i)[:remIdx] @@ -157,16 +157,6 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) ItemCollection { return rec } -// FlattenItemCollection flattens the Collection's properties from Object type to IRI -func FlattenItemCollection(c ItemCollection) ItemCollection { - if c != nil && len(c) > 0 { - for i, it := range c { - c[i] = FlattenToIRI(it) - } - } - return c -} - // ToItemCollection func ToItemCollection(it Item) (*ItemCollection, error) { switch i := it.(type) { diff --git a/item_collection_test.go b/item_collection_test.go index a46cf7d..88d318e 100644 --- a/item_collection_test.go +++ b/item_collection_test.go @@ -34,10 +34,6 @@ func TestItemCollection_First(t *testing.T) { t.Skipf("TODO") } -func TestFlattenItemCollection(t *testing.T) { - t.Skipf("TODO") -} - func TestItemCollection_Count(t *testing.T) { t.Skipf("TODO") } @@ -62,13 +58,13 @@ func TestItemCollection_Remove(t *testing.T) { }{ { name: "empty_collection_nil_item", - i: ItemCollection{}, - arg: nil, + i: ItemCollection{}, + arg: nil, }, { name: "empty_collection_non_nil_item", - i: ItemCollection{}, - arg: &Object{}, + i: ItemCollection{}, + arg: &Object{}, }, { name: "non_empty_collection_nil_item", @@ -140,7 +136,7 @@ func TestItemCollection_Remove(t *testing.T) { t.Errorf("%T should%s contain %T, but it does%s: %#v", tt.i, should, tt.arg, does, tt.i) } if origContains { - if tt.i.Count() > origLen - 1 { + if tt.i.Count() > origLen-1 { t.Errorf("%T should have a count lower than %d, got %d", tt.i, origLen, tt.i.Count()) } } else { diff --git a/object_test.go b/object_test.go index 34dbfdc..cb15d39 100644 --- a/object_test.go +++ b/object_test.go @@ -436,10 +436,6 @@ func TestFlattenObjectProperties(t *testing.T) { t.Skipf("TODO") } -func TestFlattenProperties(t *testing.T) { - t.Skipf("TODO") -} - func TestToTombstone(t *testing.T) { t.Skipf("TODO") }