Added Equals method to intransitive activity

This commit is contained in:
mariusor 2022-05-24 11:01:42 +02:00
parent e8c528edf3
commit a60c25cc5c
No known key found for this signature in database
GPG Key ID: DBF5E47F5DBC4D21
3 changed files with 175 additions and 39 deletions

View File

@ -861,8 +861,8 @@ func (a *Activity) GobDecode(data []byte) error {
func (a Activity) Equals(with Item) bool {
result := true
err := OnActivity(with, func(w *Activity) error {
OnObject(a, func(oa *Object) error {
result = oa.Equals(w)
OnIntransitiveActivity(a, func(oi *IntransitiveActivity) error {
result = oi.Equals(w)
return nil
})
if w.Object != nil {
@ -871,42 +871,6 @@ func (a Activity) Equals(with Item) bool {
return nil
}
}
if w.Actor != nil {
if !ItemsEqual(a.Actor, w.Actor) {
result = false
return nil
}
}
if w.Target != nil {
if !ItemsEqual(a.Target, w.Target) {
result = false
return nil
}
}
if w.Result != nil {
if !ItemsEqual(a.Result, w.Result) {
result = false
return nil
}
}
if w.Origin != nil {
if !ItemsEqual(a.Origin, w.Origin) {
result = false
return nil
}
}
if w.Result != nil {
if !ItemsEqual(a.Result, w.Result) {
result = false
return nil
}
}
if w.Instrument != nil {
if !ItemsEqual(a.Instrument, w.Instrument) {
result = false
return nil
}
}
return nil
})
if err != nil {

View File

@ -295,3 +295,49 @@ func TravelNew(id ID) *Travel {
o := Travel(*a)
return &o
}
// Equals verifies if our receiver Object is equals with the "with" Object
func (i IntransitiveActivity) Equals(with Item) bool {
result := true
err := OnActivity(with, func(w *Activity) error {
OnObject(i, func(oa *Object) error {
result = oa.Equals(w)
return nil
})
if w.Actor != nil {
if !ItemsEqual(i.Actor, w.Actor) {
result = false
return nil
}
}
if w.Target != nil {
if !ItemsEqual(i.Target, w.Target) {
result = false
return nil
}
}
if w.Result != nil {
if !ItemsEqual(i.Result, w.Result) {
result = false
return nil
}
}
if w.Origin != nil {
if !ItemsEqual(i.Origin, w.Origin) {
result = false
return nil
}
}
if w.Instrument != nil {
if !ItemsEqual(i.Instrument, w.Instrument) {
result = false
return nil
}
}
return nil
})
if err != nil {
result = false
}
return result
}

View File

@ -1,6 +1,9 @@
package activitypub
import "testing"
import (
"testing"
"time"
)
func TestIntransitiveActivityNew(t *testing.T) {
var testValue = ID("test")
@ -258,3 +261,126 @@ func TestTravelNew(t *testing.T) {
t.Errorf("Activity Type '%v' different than expected '%v'", a.Type, TravelType)
}
}
func TestIntransitiveActivity_Equals(t *testing.T) {
type fields struct {
ID ID
Type ActivityVocabularyType
Name NaturalLanguageValues
Attachment Item
AttributedTo Item
Audience ItemCollection
Content NaturalLanguageValues
Context Item
MediaType MimeType
EndTime time.Time
Generator Item
Icon Item
Image Item
InReplyTo Item
Location Item
Preview Item
Published time.Time
Replies Item
StartTime time.Time
Summary NaturalLanguageValues
Tag ItemCollection
Updated time.Time
URL Item
To ItemCollection
Bto ItemCollection
CC ItemCollection
BCC ItemCollection
Duration time.Duration
Likes Item
Shares Item
Source Source
Actor Item
Target Item
Result Item
Origin Item
Instrument Item
}
tests := []struct {
name string
fields fields
arg Item
want bool
}{
{
name: "equal-empty-intransitive-activity",
fields: fields{},
arg: IntransitiveActivity{},
want: true,
},
{
name: "equal-intransitive-activity-just-id",
fields: fields{ID: "test"},
arg: IntransitiveActivity{ID: "test"},
want: true,
},
{
name: "equal-intransitive-activity-id",
fields: fields{ID: "test", URL: IRI("example.com")},
arg: IntransitiveActivity{ID: "test"},
want: true,
},
{
name: "equal-false-with-id-and-url",
fields: fields{ID: "test"},
arg: IntransitiveActivity{ID: "test", URL: IRI("example.com")},
want: false,
},
{
name: "not a valid intransitive-activity",
fields: fields{ID: "http://example.com"},
arg: Link{ID: "http://example.com"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := IntransitiveActivity{
ID: tt.fields.ID,
Type: tt.fields.Type,
Name: tt.fields.Name,
Attachment: tt.fields.Attachment,
AttributedTo: tt.fields.AttributedTo,
Audience: tt.fields.Audience,
Content: tt.fields.Content,
Context: tt.fields.Context,
MediaType: tt.fields.MediaType,
EndTime: tt.fields.EndTime,
Generator: tt.fields.Generator,
Icon: tt.fields.Icon,
Image: tt.fields.Image,
InReplyTo: tt.fields.InReplyTo,
Location: tt.fields.Location,
Preview: tt.fields.Preview,
Published: tt.fields.Published,
Replies: tt.fields.Replies,
StartTime: tt.fields.StartTime,
Summary: tt.fields.Summary,
Tag: tt.fields.Tag,
Updated: tt.fields.Updated,
URL: tt.fields.URL,
To: tt.fields.To,
Bto: tt.fields.Bto,
CC: tt.fields.CC,
BCC: tt.fields.BCC,
Duration: tt.fields.Duration,
Likes: tt.fields.Likes,
Shares: tt.fields.Shares,
Source: tt.fields.Source,
Actor: tt.fields.Actor,
Target: tt.fields.Target,
Result: tt.fields.Result,
Origin: tt.fields.Origin,
Instrument: tt.fields.Instrument,
}
if got := a.Equals(tt.arg); got != tt.want {
t.Errorf("Equals() = %v, want %v", got, tt.want)
}
})
}
}