2018-01-26 08:19:21 +09:00
|
|
|
package pub
|
|
|
|
|
|
|
|
import (
|
2018-02-09 05:12:52 +09:00
|
|
|
"fmt"
|
2018-01-26 08:19:21 +09:00
|
|
|
"github.com/go-fed/activity/streams"
|
|
|
|
"github.com/go-fed/activity/vocab"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2018-02-16 07:38:58 +09:00
|
|
|
// ToPubObject transforms a json-deserialized ActivityStream object into a
|
|
|
|
// PubObject for use with the pub library. Note that for an object to be an
|
|
|
|
// ActivityPub object, it must have an 'id' and at least one 'type'.
|
|
|
|
func ToPubObject(m map[string]interface{}) (t []PubObject, e error) {
|
|
|
|
r := &streams.Resolver{
|
|
|
|
AnyObjectCallback: func(i vocab.ObjectType) error {
|
|
|
|
if !i.HasId() {
|
|
|
|
return fmt.Errorf("object type does not have an id: %q", i)
|
|
|
|
} else if i.TypeLen() == 0 {
|
|
|
|
return fmt.Errorf("object type does not have a type: %q", i)
|
|
|
|
}
|
|
|
|
t = append(t, i)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
AnyLinkCallback: func(i vocab.LinkType) error {
|
|
|
|
if !i.HasId() {
|
|
|
|
return fmt.Errorf("link type does not have an id: %q", i)
|
|
|
|
} else if i.TypeLen() == 0 {
|
|
|
|
return fmt.Errorf("link type does not have a type: %q", i)
|
|
|
|
}
|
|
|
|
t = append(t, i)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
e = r.Deserialize(m)
|
|
|
|
return t, e
|
|
|
|
}
|
|
|
|
|
2018-02-20 08:09:29 +09:00
|
|
|
func getActorObject(m map[string]interface{}) (actorObject, error) {
|
|
|
|
var a actorObject
|
2018-04-04 07:23:55 +09:00
|
|
|
err := toActorObjectResolver(&a).Deserialize(m)
|
2018-02-20 08:09:29 +09:00
|
|
|
return a, err
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:23:55 +09:00
|
|
|
func toActorObjectResolver(a *actorObject) *streams.Resolver {
|
2018-01-26 08:19:21 +09:00
|
|
|
return &streams.Resolver{
|
|
|
|
AnyObjectCallback: func(i vocab.ObjectType) error {
|
2018-02-13 05:19:35 +09:00
|
|
|
if o, ok := i.(actorObject); ok {
|
2018-01-26 08:19:21 +09:00
|
|
|
*a = o
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 07:23:55 +09:00
|
|
|
func toActorResolver(a *actor) *streams.Resolver {
|
|
|
|
return &streams.Resolver{
|
|
|
|
AnyObjectCallback: func(i vocab.ObjectType) error {
|
|
|
|
if o, ok := i.(actor); ok {
|
|
|
|
*a = o
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toActorCollectionResolver(a *actor, c **streams.Collection, oc **streams.OrderedCollection, cp **streams.CollectionPage, ocp **streams.OrderedCollectionPage) *streams.Resolver {
|
2018-01-26 08:19:21 +09:00
|
|
|
r := toActorResolver(a)
|
|
|
|
r.CollectionCallback = func(i *streams.Collection) error {
|
|
|
|
*c = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.OrderedCollectionCallback = func(i *streams.OrderedCollection) error {
|
|
|
|
*oc = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.CollectionPageCallback = func(i *streams.CollectionPage) error {
|
|
|
|
*cp = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.OrderedCollectionPageCallback = func(i *streams.OrderedCollectionPage) error {
|
|
|
|
*ocp = i
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func toIdResolver(ok *bool, u *url.URL) *streams.Resolver {
|
|
|
|
return &streams.Resolver{
|
|
|
|
AnyObjectCallback: func(i vocab.ObjectType) error {
|
|
|
|
*ok = i.HasId()
|
|
|
|
if *ok {
|
|
|
|
*u = i.GetId()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCollectionPage(m map[string]interface{}) (c *streams.CollectionPage, err error) {
|
|
|
|
r := &streams.Resolver{
|
|
|
|
CollectionPageCallback: func(i *streams.CollectionPage) error {
|
|
|
|
c = i
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r.Deserialize(m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func toOrderedCollectionPage(m map[string]interface{}) (c *streams.OrderedCollectionPage, err error) {
|
|
|
|
r := &streams.Resolver{
|
|
|
|
OrderedCollectionPageCallback: func(i *streams.OrderedCollectionPage) error {
|
|
|
|
c = i
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r.Deserialize(m)
|
|
|
|
return
|
|
|
|
}
|
2018-02-05 01:42:22 +09:00
|
|
|
|
2018-02-09 05:12:52 +09:00
|
|
|
func toTypeIder(m map[string]interface{}) (tid typeIder, err error) {
|
|
|
|
var t []typeIder
|
2018-02-05 01:42:22 +09:00
|
|
|
r := &streams.Resolver{
|
|
|
|
AnyObjectCallback: func(i vocab.ObjectType) error {
|
2018-02-09 05:12:52 +09:00
|
|
|
t = append(t, i)
|
2018-02-05 01:42:22 +09:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
AnyLinkCallback: func(i vocab.LinkType) error {
|
2018-02-09 05:12:52 +09:00
|
|
|
t = append(t, i)
|
2018-02-05 01:42:22 +09:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r.Deserialize(m)
|
2018-02-09 05:12:52 +09:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Support more than one, which will enable creating multiple
|
|
|
|
// objects simultaneously.
|
|
|
|
if len(t) != 1 {
|
|
|
|
err = fmt.Errorf("too many object/links: %d", len(t))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tid = t[0]
|
2018-02-05 01:42:22 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func toAnyActivity(m map[string]interface{}) (o vocab.ActivityType, err error) {
|
|
|
|
r := &streams.Resolver{
|
|
|
|
AnyActivityCallback: func(i vocab.ActivityType) error {
|
|
|
|
o = i
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r.Deserialize(m)
|
|
|
|
return
|
|
|
|
}
|