Moved the flatten functionality to this package

This commit is contained in:
Marius Orcsik 2021-04-03 13:43:33 +02:00
parent f09fdb0b8b
commit 558da36360
No known key found for this signature in database
GPG key ID: 7970BDC7D4CB2674
7 changed files with 217 additions and 32 deletions

View file

@ -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")
}

122
flatten.go Normal file
View file

@ -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
}

89
flatten_test.go Normal file
View file

@ -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)
}
})
}
}

View file

@ -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")
}

View file

@ -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) {

View file

@ -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 {

View file

@ -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")
}