Fixing some tests
This commit is contained in:
parent
c3a38f35e7
commit
e1942ec531
53
activity.go
53
activity.go
|
@ -336,11 +336,58 @@ func (a Activity) IsCollection() bool {
|
||||||
return false
|
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
|
// Recipients performs recipient de-duplication on the Activity's To, Bto, CC and BCC properties
|
||||||
func (a *Activity) Recipients() ItemCollection {
|
func (a *Activity) Recipients() ItemCollection {
|
||||||
actor := make(ItemCollection, 0)
|
var alwaysRemove ItemCollection
|
||||||
actor.Append(a.Actor)
|
if a.GetType() == BlockType && a.Object != nil {
|
||||||
rec, _ := ItemCollectionDeduplication(&actor, &a.To, &a.Bto, &a.CC, &a.BCC, &a.Audience)
|
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
|
return rec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -440,8 +440,8 @@ func TestBlockRecipients(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
a.Recipients()
|
a.Recipients()
|
||||||
if len(a.To) != 4 {
|
if len(a.To) != 3 {
|
||||||
t.Errorf("%T.To should have exactly 3(four) elements, not %d", a, len(a.To))
|
t.Errorf("%T.To should have exactly 3(three) elements, not %d", a, len(a.To))
|
||||||
}
|
}
|
||||||
|
|
||||||
b := BlockNew("t", bob)
|
b := BlockNew("t", bob)
|
||||||
|
@ -464,8 +464,8 @@ func TestBlockRecipients(t *testing.T) {
|
||||||
b.BCC.Append(bob)
|
b.BCC.Append(bob)
|
||||||
|
|
||||||
b.Recipients()
|
b.Recipients()
|
||||||
if len(b.To) != 4 {
|
if len(b.To) != 3 {
|
||||||
t.Errorf("%T.To should have exactly 4(four) elements, not %d", b, len(b.To))
|
t.Errorf("%T.To should have exactly 3(three) elements, not %d", b, len(b.To))
|
||||||
}
|
}
|
||||||
if len(b.Bto) != 0 {
|
if len(b.Bto) != 0 {
|
||||||
t.Errorf("%T.Bto should have exactly 0(zero) elements, not %d", b, len(b.Bto))
|
t.Errorf("%T.Bto should have exactly 0(zero) elements, not %d", b, len(b.Bto))
|
||||||
|
|
6
iri.go
6
iri.go
|
@ -81,6 +81,10 @@ func (i IRIs) Contains(r IRI) bool {
|
||||||
return false
|
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
|
// Equals verifies if our receiver IRI is equals with the "with" IRI
|
||||||
// It ignores the protocol
|
// It ignores the protocol
|
||||||
// It tries to use the URL representation if possible and fallback to string comparison if unable to convert
|
// 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 {
|
func (i IRI) Equals(with IRI, checkScheme bool) bool {
|
||||||
u, e := i.URL()
|
u, e := i.URL()
|
||||||
uw, ew := with.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())
|
return strings.ToLower(i.String()) == strings.ToLower(with.String())
|
||||||
}
|
}
|
||||||
if checkScheme {
|
if checkScheme {
|
||||||
|
|
|
@ -630,16 +630,16 @@ func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, er
|
||||||
if cur == nil {
|
if cur == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var testIt Item
|
var testIt IRI
|
||||||
if cur.IsObject() {
|
if cur.IsObject() {
|
||||||
testIt = cur
|
testIt = IRI(cur.GetID())
|
||||||
} else if cur.IsLink() {
|
} else if cur.IsLink() {
|
||||||
testIt = cur.GetLink()
|
testIt = cur.GetLink()
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, it := range rec {
|
for _, it := range rec {
|
||||||
if testIt == it {
|
if testIt.Equals(IRI(it.GetID()), false) {
|
||||||
// mark the element for removal
|
// mark the element for removal
|
||||||
toRemove = append(toRemove, i)
|
toRemove = append(toRemove, i)
|
||||||
save = false
|
save = false
|
||||||
|
|
Reference in a new issue