Adding Tag property unmarshalling for Objects

This commit is contained in:
Marius Orcsik 2018-11-14 13:42:04 +01:00
parent c53d21a18e
commit 9dcc89db96
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
5 changed files with 61 additions and 2 deletions

View file

@ -122,7 +122,7 @@ func (l *Link) UnmarshalJSON(data []byte) error {
l.Name = getAPNaturalLanguageField(data, "name")
l.HrefLang = getAPLangRefField(data, "hrefLang")
u := getURIField(data, "href")
if !u.IsObject() {
if u != nil && !u.IsObject() {
l.Href = u.GetLink()
}
@ -130,3 +130,13 @@ func (l *Link) UnmarshalJSON(data []byte) error {
return nil
}
// UnmarshalJSON
func (m *Mention) UnmarshalJSON(data []byte) error {
l := Link{}
err := l.UnmarshalJSON(data)
*m = Mention(l)
return err
}

View file

@ -198,6 +198,9 @@ func getAPObjectByType(typ ActivityVocabularyType) (Item, error) {
ret = &View{}
o := ret.(*View)
o.Type = typ
case "":
// when no type is available use a plain Object
ret = &Object{}
default:
ret = nil
err = fmt.Errorf("unrecognized ActivityPub type %q", typ)

View file

@ -629,6 +629,9 @@ func (o *Object) UnmarshalJSON(data []byte) error {
o.Replies = &r
}
}
tag := getAPItemCollection(data, "tag")
if tag != nil {
o.Tag = tag
}
return nil
}

View file

@ -0,0 +1,17 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"type": "Object",
"id": "http://www.test.example/object/1",
"name": "A Simple, non-specific object",
"tag": [
{
"name": "#my_tag",
"ID": "http://example.com/tag/my_tag"
},
{
"name": "@ana",
"type": "Mention",
"ID": "http://example.com/users/ana"
}
]
}

View file

@ -234,6 +234,32 @@ var allTests = tests{
}},
},
},
"object_with_tags": testPair{
expected: true,
blank: &a.Object{},
result: &a.Object{
Type: a.ObjectType,
ID: a.ObjectID("http://www.test.example/object/1"),
Name: a.NaturalLanguageValue{{
a.NilLangRef, "A Simple, non-specific object",
}},
Tag: a.ItemCollection{
&a.Object{
Name: a.NaturalLanguageValue{{
a.NilLangRef, "#my_tag",
}},
ID: a.ObjectID("http://example.com/tag/my_tag"),
},
&a.Mention{
Name: a.NaturalLanguageValue{{
a.NilLangRef, "@ana",
}},
Type: a.MentionType,
ID: a.ObjectID("http://example.com/users/ana"),
},
},
},
},
"object_with_replies": testPair{
expected: true,
blank: &a.Object{},