Changes
2 changed files (+71/-10)
-
-
@@ -2,6 +2,7 @@ package activitypubimport ( "encoding/json" "sort" "time" )
-
@@ -262,39 +263,50 @@ func (o apObject) Link() Link {return Link{} } // Append facilitates adding elements to ObjectOrLink arrays // and ensures ObjectsArr implements the Collection interface func (c *ObjectsArr) Append(o ObjectOrLink) error { old_len := len(*c) d := make(ObjectsArr, old_len+1) oldLen := len(*c) d := make(ObjectsArr, oldLen+1) for k, it := range *c { d[k] = it } d[old_len] = o d[oldLen] = o *c = d return nil } // RecipientsDeduplication normalizes the received recipient lists func RecipientsDeduplication(others ...*ObjectsArr) error { func RecipientsDeduplication(recArgs ...*ObjectsArr) error { recIds := make([]ObjectID, 0) for _, list := range others { if list == nil { for _, recList := range recArgs { if recList == nil { continue } for i, rec := range *list { toRemove := make([]int, 0) for i, rec := range *recList { save := true for _, id := range recIds { if rec.Object().ID == id { // remove the element *list = append((*list)[:i], (*list)[i+1:]...) // mark the element for removal toRemove = append(toRemove, i) save = false continue } } if save { recIds = append(recIds, rec.Object().ID) } } sort.Sort(sort.Reverse(sort.IntSlice(toRemove))) for _, idx := range toRemove { if idx == len(*recList) { *recList = (*recList)[:idx] } else { *recList = append((*recList)[:idx], (*recList)[idx+1:]...) } } } return nil }
-
-
-
@@ -133,3 +133,52 @@ func TestObject_Link(t *testing.T) {t.Errorf("%#v should be an empty Link object", o.Link()) } } func TestObjectsArr_Append(t *testing.T) { d := make(ObjectsArr, 0) val := apObject{ID: ObjectID("grrr")} d.Append(val) if len(d) != 1 { t.Errorf("Objects array should have exactly an element") } if !reflect.DeepEqual(d[0], val) { t.Errorf("First item in object array does not match %q", val.ID) } } func TestRecipientsDeduplication(t *testing.T) { bob := PersonNew("bob") alice := PersonNew("alice") foo := OrganizationNew("foo") bar := GroupNew("bar") first := make(ObjectsArr, 0) if len(first) != 0 { t.Errorf("Objects array should have exactly an element") } first.Append(bob) first.Append(alice) first.Append(foo) first.Append(bar) if len(first) != 4 { t.Errorf("Objects array should have exactly 4(four) elements, not %d", len(first)) } first.Append(bar) first.Append(alice) first.Append(foo) first.Append(bob) if len(first) != 8 { t.Errorf("Objects array should have exactly 8(eight) elements, not %d", len(first)) } RecipientsDeduplication(&first) if len(first) != 4 { t.Errorf("Objects array should have exactly 8(eight) elements, not %d", len(first)) } }
-