diff --git a/activity.go b/activity.go index bbc1f7b..c04c941 100644 --- a/activity.go +++ b/activity.go @@ -336,11 +336,58 @@ func (a Activity) IsCollection() bool { return false } +func removeFromCollection(col ItemCollection, items ...Item) ItemCollection { + result := make(ItemCollection, 0) + if len(items) == 0 { + return col + } + for _, ob := range col { + found := false + for _, it := range items { + if IRI(ob.GetID()).Equals(IRI(it.GetID()), false) { + found = true + break + } + } + if !found { + result = append(result, ob) + } + } + return result +} + +func removeFromAudience(a *Activity, items ...Item) error { + if a.To != nil { + a.To = removeFromCollection(a.To, items...) + } + if a.Bto != nil { + a.Bto = removeFromCollection(a.Bto, items...) + } + if a.CC != nil { + a.CC = removeFromCollection(a.CC, items...) + } + if a.BCC != nil { + a.BCC = removeFromCollection(a.BCC, items...) + } + if a.Audience != nil { + a.Audience = removeFromCollection(a.Audience, items...) + } + return nil +} + // Recipients performs recipient de-duplication on the Activity's To, Bto, CC and BCC properties func (a *Activity) Recipients() ItemCollection { - actor := make(ItemCollection, 0) - actor.Append(a.Actor) - rec, _ := ItemCollectionDeduplication(&actor, &a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) + var alwaysRemove ItemCollection + if a.GetType() == BlockType && a.Object != nil { + alwaysRemove = append(alwaysRemove, a.Object) + } + if a.Actor != nil { + alwaysRemove = append(alwaysRemove, a.Actor) + } + if len(alwaysRemove) > 0 { + removeFromAudience(a, alwaysRemove...) + } + rec, _ := ItemCollectionDeduplication(&a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience) return rec } diff --git a/activity_test.go b/activity_test.go index 64151ee..1dd3767 100644 --- a/activity_test.go +++ b/activity_test.go @@ -440,8 +440,8 @@ func TestBlockRecipients(t *testing.T) { } a.Recipients() - if len(a.To) != 4 { - t.Errorf("%T.To should have exactly 3(four) elements, not %d", a, len(a.To)) + if len(a.To) != 3 { + t.Errorf("%T.To should have exactly 3(three) elements, not %d", a, len(a.To)) } b := BlockNew("t", bob) @@ -464,8 +464,8 @@ func TestBlockRecipients(t *testing.T) { b.BCC.Append(bob) b.Recipients() - if len(b.To) != 4 { - t.Errorf("%T.To should have exactly 4(four) elements, not %d", b, len(b.To)) + if len(b.To) != 3 { + t.Errorf("%T.To should have exactly 3(three) elements, not %d", b, len(b.To)) } if len(b.Bto) != 0 { t.Errorf("%T.Bto should have exactly 0(zero) elements, not %d", b, len(b.Bto)) diff --git a/iri.go b/iri.go index 6047c88..a9d80cb 100644 --- a/iri.go +++ b/iri.go @@ -81,6 +81,10 @@ func (i IRIs) Contains(r IRI) bool { return false } +func validURL(u *url.URL) bool { + return len(u.Scheme) > 0 && len(u.Host) > 0 +} + // Equals verifies if our receiver IRI is equals with the "with" IRI // It ignores the protocol // It tries to use the URL representation if possible and fallback to string comparison if unable to convert @@ -88,7 +92,7 @@ func (i IRIs) Contains(r IRI) bool { func (i IRI) Equals(with IRI, checkScheme bool) bool { u, e := i.URL() uw, ew := with.URL() - if e != nil || ew != nil { + if e != nil || ew != nil || !validURL(u) || !validURL(uw) { return strings.ToLower(i.String()) == strings.ToLower(with.String()) } if checkScheme { diff --git a/object.go b/object.go index e194d3c..5ef68a8 100644 --- a/object.go +++ b/object.go @@ -630,16 +630,16 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er if cur == nil { continue } - var testIt Item + var testIt IRI if cur.IsObject() { - testIt = cur + testIt = IRI(cur.GetID()) } else if cur.IsLink() { testIt = cur.GetLink() } else { continue } for _, it := range rec { - if testIt == it { + if testIt.Equals(IRI(it.GetID()), false) { // mark the element for removal toRemove = append(toRemove, i) save = false