Fixing some tests

This commit is contained in:
Marius Orcsik 2019-12-03 19:59:20 +01:00
parent c3a38f35e7
commit e1942ec531
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
4 changed files with 62 additions and 11 deletions

View file

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

View file

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

6
iri.go
View file

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

View file

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