diff --git a/src/activitypub/activity.go b/src/activitypub/activity.go index 77f16d9..1906f65 100644 --- a/src/activitypub/activity.go +++ b/src/activitypub/activity.go @@ -227,147 +227,203 @@ type Question struct { Closed bool `jsonld:"closed,omitempty"` } +// AcceptNew initializes an Accept activity func AcceptNew(id ObjectId, ob ObjectOrLink) *Accept { a := ActivityNew(id, AcceptType, ob) o := Accept(*a) return &o } + +// AddNew initializes an Add activity func AddNew(id ObjectId, ob ObjectOrLink) *Add { a := ActivityNew(id, AddType, ob) o := Add(*a) return &o } + +// AnnounceNew initializes an Announce activity func AnnounceNew(id ObjectId, ob ObjectOrLink) *Announce { a := ActivityNew(id, AnnounceType, ob) o := Announce(*a) return &o } + +// ArriveNew initializes an Arrive activity func ArriveNew(id ObjectId) *Arrive { a := IntransitiveActivityNew(id, ArriveType) o := Arrive(*a) return &o } + +// BlockNew initializes a Block activity func BlockNew(id ObjectId, ob ObjectOrLink) *Block { a := ActivityNew(id, BlockType, ob) o := Block(*a) return &o } + +// CreateNew initializes a Create activity func CreateNew(id ObjectId, ob ObjectOrLink) *Create { a := ActivityNew(id, CreateType, ob) o := Create(*a) return &o } + +// DeleteNew initializes a Delete activity func DeleteNew(id ObjectId, ob ObjectOrLink) *Delete { a := ActivityNew(id, DeleteType, ob) o := Delete(*a) return &o } + +// DislikeNew initializes a Dislike activity func DislikeNew(id ObjectId, ob ObjectOrLink) *Dislike { a := ActivityNew(id, DislikeType, ob) o := Dislike(*a) return &o } + +// FlagNew initializes a Flag activity func FlagNew(id ObjectId, ob ObjectOrLink) *Flag { a := ActivityNew(id, FlagType, ob) o := Flag(*a) return &o } + +// FollowNew initializes a Follow activity func FollowNew(id ObjectId, ob ObjectOrLink) *Follow { a := ActivityNew(id, FollowType, ob) o := Follow(*a) return &o } + +// IgnoreNew initializes an Ignore activity func IgnoreNew(id ObjectId, ob ObjectOrLink) *Ignore { a := ActivityNew(id, IgnoreType, ob) o := Ignore(*a) return &o } + +// InviteNew initializes an Invite activity func InviteNew(id ObjectId, ob ObjectOrLink) *Invite { a := ActivityNew(id, InviteType, ob) o := Invite(*a) return &o } + +// JoinNew initializes a Join activity func JoinNew(id ObjectId, ob ObjectOrLink) *Join { a := ActivityNew(id, JoinType, ob) o := Join(*a) return &o } + +// LeaveNew initializes a Leave activity func LeaveNew(id ObjectId, ob ObjectOrLink) *Leave { a := ActivityNew(id, LeaveType, ob) o := Leave(*a) return &o } + +// LikeNew initializes a Like activity func LikeNew(id ObjectId, ob ObjectOrLink) *Like { a := ActivityNew(id, LikeType, ob) o := Like(*a) return &o } + +// ListenNew initializes a Listen activity func ListenNew(id ObjectId, ob ObjectOrLink) *Listen { a := ActivityNew(id, ListenType, ob) o := Listen(*a) return &o } + +// MoveNew initializes a Move activity func MoveNew(id ObjectId, ob ObjectOrLink) *Move { a := ActivityNew(id, MoveType, ob) o := Move(*a) return &o } + +// OfferNew initializes an Offer activity func OfferNew(id ObjectId, ob ObjectOrLink) *Offer { a := ActivityNew(id, OfferType, ob) o := Offer(*a) return &o } + +// RejectNew initializes a Reject activity func RejectNew(id ObjectId, ob ObjectOrLink) *Reject { a := ActivityNew(id, RejectType, ob) o := Reject(*a) return &o } + +// ReadNew initializes a Read activity func ReadNew(id ObjectId, ob ObjectOrLink) *Read { a := ActivityNew(id, ReadType, ob) o := Read(*a) return &o } + +// RemoveNew initializes a Remove activity func RemoveNew(id ObjectId, ob ObjectOrLink) *Remove { a := ActivityNew(id, RemoveType, ob) o := Remove(*a) return &o } + +// TentativeRejectNew initializes a TentativeReject activity func TentativeRejectNew(id ObjectId, ob ObjectOrLink) *TentativeReject { a := ActivityNew(id, TentativeRejectType, ob) o := TentativeReject(*a) return &o } + +// TentativeAcceptNew initializes a TentativeAccept activity func TentativeAcceptNew(id ObjectId, ob ObjectOrLink) *TentativeAccept { a := ActivityNew(id, TentativeAcceptType, ob) o := TentativeAccept(*a) return &o } + +// TravelNew initializes a Travel activity func TravelNew(id ObjectId) *Travel { a := IntransitiveActivityNew(id, TravelType) o := Travel(*a) return &o } + +// UndoNew initializes an Undo activity func UndoNew(id ObjectId, ob ObjectOrLink) *Undo { a := ActivityNew(id, UndoType, ob) o := Undo(*a) return &o } + +// UpdateNew initializes an Update activity func UpdateNew(id ObjectId, ob ObjectOrLink) *Accept { a := ActivityNew(id, UpdateType, ob) o := Accept(*a) return &o } + +// ViewNew initializes a View activity func ViewNew(id ObjectId, ob ObjectOrLink) *View { a := ActivityNew(id, ViewType, ob) o := View(*a) return &o } + +// QuestionNew initializes a Question activity func QuestionNew(id ObjectId) *Question { a := IntransitiveActivityNew(id, QuestionType) o := Question{IntransitiveActivity: a} return &o } +// ValidActivityType is a validation function for Activity objects func ValidActivityType(_type ActivityVocabularyType) bool { for _, v := range validActivityTypes { if v == _type { @@ -377,6 +433,7 @@ func ValidActivityType(_type ActivityVocabularyType) bool { return false } +// ActivityNew initializes a basic activity func ActivityNew(id ObjectId, _type ActivityVocabularyType, ob ObjectOrLink) *Activity { if !ValidActivityType(_type) { _type = ActivityType @@ -390,6 +447,7 @@ func ActivityNew(id ObjectId, _type ActivityVocabularyType, ob ObjectOrLink) *Ac return &a } +// IntransitiveActivityNew initializes a intransitive activity func IntransitiveActivityNew(id ObjectId, _type ActivityVocabularyType) *IntransitiveActivity { if !ValidActivityType(_type) { _type = IntransitiveActivityType diff --git a/src/activitypub/actors.go b/src/activitypub/actors.go index 7237e56..7f13043 100644 --- a/src/activitypub/actors.go +++ b/src/activitypub/actors.go @@ -87,6 +87,7 @@ type ( Service Actor ) +// ValidActorType validates the passed type against the valid actor types func ValidActorType(_type ActivityVocabularyType) bool { for _, v := range validActorTypes { if v == _type { @@ -96,6 +97,7 @@ func ValidActorType(_type ActivityVocabularyType) bool { return false } +// ActorNew initializes an Actor type actor func ActorNew(id ObjectId, _type ActivityVocabularyType) *Actor { if !ValidActorType(_type) { _type = ActorType @@ -106,30 +108,35 @@ func ActorNew(id ObjectId, _type ActivityVocabularyType) *Actor { return &a } +// ApplicationNew initializes an Application type actor func ApplicationNew(id ObjectId) *Application { a := ActorNew(id, ApplicationType) o := Application(*a) return &o } +// GroupNew initializes a Group type actor func GroupNew(id ObjectId) *Group { a := ActorNew(id, GroupType) o := Group(*a) return &o } +// OrganizationNew initializes an Organization type actor func OrganizationNew(id ObjectId) *Organization { a := ActorNew(id, OrganizationType) o := Organization(*a) return &o } +// PersonNew initializes a Person type actor func PersonNew(id ObjectId) *Person { a := ActorNew(id, PersonType) o := Person(*a) return &o } +// ServiceNew initializes a Service type actor func ServiceNew(id ObjectId) *Service { a := ActorNew(id, ServiceType) o := Service(*a) diff --git a/src/activitypub/collections.go b/src/activitypub/collections.go index 1e16fa4..5432ffd 100644 --- a/src/activitypub/collections.go +++ b/src/activitypub/collections.go @@ -2,8 +2,10 @@ package activitypub var validCollectionTypes = [...]ActivityVocabularyType{CollectionType, OrderedCollectionType} +// Page type Page ObjectOrLink +// Collection type Collection struct { *APObject // A non-negative integer specifying the total number of objects contained by the logical view of the collection. @@ -13,6 +15,7 @@ type Collection struct { Items ItemCollection `jsonld:"items,omitempty"` } +// OrderedCollection type OrderedCollection struct { *APObject // A non-negative integer specifying the total number of objects contained by the logical view of the collection. @@ -22,6 +25,7 @@ type OrderedCollection struct { OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"` } +// CollectionPage type CollectionPage struct { PartOf *Collection // In a paged Collection, indicates the page that contains the most recently updated member items. @@ -36,6 +40,7 @@ type CollectionPage struct { Prev Page `jsonld:"prev,omitempty"` } +// OrderedCollectionPage type OrderedCollectionPage struct { PartOf *OrderedCollection // In a paged Collection, indicates the page that contains the most recently updated member items. @@ -52,6 +57,7 @@ type OrderedCollectionPage struct { StartIndex uint `jsonld:"startIndex,omitempty"` } +// ValidCollectionType validates against the valid collection types func ValidCollectionType(_type ActivityVocabularyType) bool { for _, v := range validCollectionTypes { if v == _type { @@ -61,22 +67,26 @@ func ValidCollectionType(_type ActivityVocabularyType) bool { return false } +// CollectionNew initializes a new Collection func CollectionNew(id ObjectId) *Collection { o := ObjectNew(id, CollectionType) return &Collection{APObject: o} } +// CollectionNew initializes a new Collection func OrderedCollectionNew(id ObjectId) *OrderedCollection { o := ObjectNew(id, OrderedCollectionType) return &OrderedCollection{APObject: o} } +// CollectionNew initializes a new Collection func CollectionPageNew(parent *Collection) *CollectionPage { return &CollectionPage{PartOf: parent} } +// CollectionNew initializes a new Collection func OrderedCollectionPageNew(parent *OrderedCollection) *OrderedCollectionPage { return &OrderedCollectionPage{PartOf: parent} } diff --git a/src/activitypub/create_activity.go b/src/activitypub/create_activity.go index 9aac88f..365d354 100644 --- a/src/activitypub/create_activity.go +++ b/src/activitypub/create_activity.go @@ -2,6 +2,7 @@ package activitypub import "time" +// CreateActivity is the type for a create activity message type CreateActivity struct { Activity *Create Published time.Time @@ -9,6 +10,7 @@ type CreateActivity struct { CC *Actor } +// CreateActivityNew initializes a new CreateActivity message func CreateActivityNew(id ObjectId, o ObjectOrLink) *CreateActivity { c := CreateActivity{ Activity: CreateNew(id, o), diff --git a/src/activitypub/followers.go b/src/activitypub/followers.go index 568d827..2a4068e 100644 --- a/src/activitypub/followers.go +++ b/src/activitypub/followers.go @@ -1,7 +1,9 @@ package activitypub type ( + // FollowersCollection is a collection of followers FollowersCollection Followers + // Followers is a Collection type Followers Collection ) diff --git a/src/activitypub/following.go b/src/activitypub/following.go index 1603c4c..2e40c1c 100644 --- a/src/activitypub/following.go +++ b/src/activitypub/following.go @@ -1,7 +1,9 @@ package activitypub type ( + // FollowingCollection FollowingCollection Following + // Following Following Collection ) diff --git a/src/activitypub/inbox.go b/src/activitypub/inbox.go index 9a96876..6683222 100644 --- a/src/activitypub/inbox.go +++ b/src/activitypub/inbox.go @@ -1,7 +1,9 @@ package activitypub type ( + // InboxStream InboxStream Inbox + // Inbox Inbox OrderedCollection ) diff --git a/src/activitypub/item.go b/src/activitypub/item.go index 6b59331..6416574 100644 --- a/src/activitypub/item.go +++ b/src/activitypub/item.go @@ -1,6 +1,7 @@ package activitypub +// ItemCollection is an array of items type ItemCollection []Item -type Item struct { -} +// Item struct +type Item struct{} diff --git a/src/activitypub/liked.go b/src/activitypub/liked.go index fc1f9db..306103c 100644 --- a/src/activitypub/liked.go +++ b/src/activitypub/liked.go @@ -1,7 +1,9 @@ package activitypub type ( + // LikedCollection LikedCollection Liked + // Liked Liked OrderedCollection ) diff --git a/src/activitypub/likes.go b/src/activitypub/likes.go index 46d9cba..947f319 100644 --- a/src/activitypub/likes.go +++ b/src/activitypub/likes.go @@ -1,7 +1,9 @@ package activitypub type ( + // LikesCollection LikesCollection Likes + // Likes Likes OrderedCollection ) diff --git a/src/activitypub/link.go b/src/activitypub/link.go index e434369..ed43ce5 100644 --- a/src/activitypub/link.go +++ b/src/activitypub/link.go @@ -40,8 +40,10 @@ type Link struct { HrefLang LangRef `jsonld:"hrefLang,omitempty"` } +// Mention type Mention Link +// ValidLinkType validates a type against the valid link types func ValidLinkType(_type ActivityVocabularyType) bool { for _, v := range validLinkTypes { if v == _type { @@ -51,6 +53,7 @@ func ValidLinkType(_type ActivityVocabularyType) bool { return false } +// LinkNew initializes a new Link func LinkNew(id ObjectId, _type ActivityVocabularyType) *Link { if !ValidLinkType(_type) { _type = LinkType @@ -58,22 +61,27 @@ func LinkNew(id ObjectId, _type ActivityVocabularyType) *Link { return &Link{Id: id, Type: _type} } +// MentionNew initializes a new Mention func MentionNew(id ObjectId) *Mention { return &Mention{Id: id, Type: MentionType} } +// IsLink validates if current Link is a Link func (l Link) IsLink() bool { return l.Type == LinkType || ValidLinkType(l.Type) } +// IsObject validates if current Link is an Object func (l Link) IsObject() bool { return l.Type == ObjectType || ValidObjectType(l.Type) } +// IsLink validates if current Mention is a Link func (l Mention) IsLink() bool { return l.Type == MentionType || ValidLinkType(l.Type) } +// IsObject validates if current Mention is an Object func (l Mention) IsObject() bool { return l.Type == ObjectType || ValidObjectType(l.Type) } diff --git a/src/activitypub/object.go b/src/activitypub/object.go index fc2ed9f..b2ef2ce 100644 --- a/src/activitypub/object.go +++ b/src/activitypub/object.go @@ -5,9 +5,11 @@ import ( "time" ) +// ObjectId type ObjectId string const ( + // ActivityBaseURI the basic URI for the activity streams namespaces ActivityBaseURI URI = URI("https://www.w3.org/ns/activitystreams#") ObjectType ActivityVocabularyType = "APObject" LinkType ActivityVocabularyType = "Link" @@ -66,27 +68,38 @@ var validObjectTypes = [...]ActivityVocabularyType{ } type ( + // ActivityVocabularyType ActivityVocabularyType string - ActivityObject interface{} - ObjectOrLink interface { + // ActivityObject + ActivityObject interface{} + // ObjectOrLink + ObjectOrLink interface { IsLink() bool IsObject() bool } - LinkOrUri interface{} - ImageOrLink interface{} - MimeType string - LangRef string + // LinkOrUri + LinkOrUri interface{} + // ImageOrLink + ImageOrLink interface{} + // MimeType + MimeType string + // LangRef + LangRef string + // NaturalLanguageValue NaturalLanguageValue map[LangRef]string ) +// IsLink validates if current APObject is a Link func (o APObject) IsLink() bool { return ValidLinkType(o.Type) } +// IsObject validates if current APObject is an Object func (o APObject) IsObject() bool { return ValidObjectType(o.Type) } +// MarshalJSON serializes the NaturalLanguageValue into JSON func (n NaturalLanguageValue) MarshalJSON() ([]byte, error) { if len(n) == 1 { for _, v := range n { @@ -180,13 +193,16 @@ type APObject struct { Duration time.Duration `jsonld:"duration,omitempty"` } +// ContentType type ContentType string +// Source type Source struct { Content ContentType MediaType string } +// ValidGenericType validates the type against the valid generic object types func ValidGenericType(_type ActivityVocabularyType) bool { for _, v := range validGenericObjectTypes { if v == _type { @@ -196,6 +212,7 @@ func ValidGenericType(_type ActivityVocabularyType) bool { return false } +// ValidObjectType validates the type against the valid object types func ValidObjectType(_type ActivityVocabularyType) bool { for _, v := range validObjectTypes { if v == _type { @@ -205,6 +222,7 @@ func ValidObjectType(_type ActivityVocabularyType) bool { return ValidActivityType(_type) || ValidActorType(_type) || ValidCollectionType(_type) || ValidGenericType(_type) } +// ObjectNew initializes a new Object func ObjectNew(id ObjectId, _type ActivityVocabularyType) *APObject { if !(ValidObjectType(_type)) { _type = ObjectType diff --git a/src/activitypub/outbox.go b/src/activitypub/outbox.go index 8317dd1..392e15d 100644 --- a/src/activitypub/outbox.go +++ b/src/activitypub/outbox.go @@ -1,7 +1,9 @@ package activitypub type ( + // OutboxStream OutboxStream Outbox + // Outbox Outbox OrderedCollection ) diff --git a/src/activitypub/shares.go b/src/activitypub/shares.go index c718fcd..c39b345 100644 --- a/src/activitypub/shares.go +++ b/src/activitypub/shares.go @@ -1,7 +1,9 @@ package activitypub type ( + // SharesCollection SharesCollection Shares + // Shares Shares OrderedCollection ) diff --git a/src/activitypub/uri.go b/src/activitypub/uri.go index 6b2b928..3f4357a 100644 --- a/src/activitypub/uri.go +++ b/src/activitypub/uri.go @@ -1,7 +1,9 @@ package activitypub type ( + // IRI IRI URI + // URI URI string ) diff --git a/src/jsonld/context.go b/src/jsonld/context.go index 20de459..60c7344 100644 --- a/src/jsonld/context.go +++ b/src/jsonld/context.go @@ -6,26 +6,32 @@ import ( "activitypub" ) +// Ref basic type type Ref string +// Context is the basic JSON-LD element. It is used to map terms to IRIs. +// Terms are case sensitive and any valid string that is not a reserved JSON-LD +// keyword can be used as a term. type Context struct { URL Ref `jsonld:"@url"` Language activitypub.NaturalLanguageValue `jsonld:"@language,omitempty,collapsible"` } +// Ref returns a new Ref object based on Context URL func (c *Context) Ref() Ref { return Ref(c.URL) } +// MarshalText basic stringify function func (r *Ref) MarshalText() ([]byte, error) { return []byte(*r), nil } +// MarshalJSON returns the JSON document represented by the current Context func (c *Context) MarshalJSON() ([]byte, error) { a := reflectToJSONValue(c) if a.isScalar { return json.Marshal(a.scalar) - } else { - return json.Marshal(a.object) } + return json.Marshal(a.object) } diff --git a/src/jsonld/encode.go b/src/jsonld/encode.go index 88f72b3..93181d7 100644 --- a/src/jsonld/encode.go +++ b/src/jsonld/encode.go @@ -183,8 +183,6 @@ func (p *payloadWithContext) MarshalJSON() ([]byte, error) { return json.Marshal(a.object) } -type Encoder struct{} - type jsonLdTag struct { name string ignore bool diff --git a/src/jsonld/encode_test.go b/src/jsonld/encode_test.go index a97c553..a38dd53 100644 --- a/src/jsonld/encode_test.go +++ b/src/jsonld/encode_test.go @@ -96,7 +96,7 @@ func TestIsEmpty(t *testing.T) { if !isEmptyValue(reflect.ValueOf(d)) { t.Errorf("Invalid empty value %v", d) } - var e *interface{} = nil + var e *interface{} if !isEmptyValue(reflect.ValueOf(e)) { t.Errorf("Invalid empty value %v", e) }