This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/object.go

73 lines
1.7 KiB
Go

package activitypub
import (
"errors"
"github.com/buger/jsonparser"
as "github.com/go-ap/activitystreams"
)
// Source is intended to convey some sort of source from which the content markup was derived,
// as a form of provenance, or to support future editing by clients.
type Source struct {
// Content
Content as.NaturalLanguageValues `jsonld:"content"`
// MediaType
MediaType as.MimeType `jsonld:"mediaType"`
}
type Parent = Object
// Object
type Object struct {
as.Parent
// Source property is intended to convey some sort of source from which the content markup was derived,
// as a form of provenance, or to support future editing by clients.
// In general, clients do the conversion from source to content, not the other way around.
Source Source `jsonld:"source,omitempty"`
}
// GetAPSource
func GetAPSource(data []byte) Source {
s := Source{}
if contBytes, _, _, err := jsonparser.Get(data, "source", "content"); err == nil {
s.Content.UnmarshalJSON(contBytes)
}
if mimeBytes, _, _, err := jsonparser.Get(data, "source", "mediaType"); err == nil {
s.MediaType.UnmarshalJSON(mimeBytes)
}
return s
}
// UnmarshalJSON
func (s *Source) UnmarshalJSON(data []byte) error {
*s = GetAPSource(data)
return nil
}
// UnmarshalJSON
func (o *Object) UnmarshalJSON(data []byte) error {
if as.ItemTyperFunc == nil {
as.ItemTyperFunc = JSONGetItemByType
}
o.Parent.UnmarshalJSON(data)
o.Source = GetAPSource(data)
return nil
}
// ToObject
func ToObject(it as.Item) (*Object, error) {
switch i := it.(type) {
case *as.Object:
return &Object{Parent: *i}, nil
case as.Object:
return &Object{Parent: i}, nil
case *Object:
return i, nil
case Object:
return &i, nil
}
return nil, errors.New("unable to convert object")
}