add some works

This commit is contained in:
inhosin 2019-09-07 17:54:53 +03:00
parent cd9ec0c774
commit 8807e5aaab
22 changed files with 1074 additions and 3 deletions

3
activitypub/README.md Normal file
View file

@ -0,0 +1,3 @@
# ActivityPub
This package contains functions for handling the ActivityPub spec.

145
activitypub/activity.go Normal file
View file

@ -0,0 +1,145 @@
package activitypub
import "time"
const (
Namespace = "https://www.w3.org/ns/activitystreams"
toPublic = "https://www.w3.org/ns/activitystreams#Public"
)
var Extensions = map[string]string{
"sc": "http://schema.org#",
"commentsEnabled": "sc:Boolean",
}
// Activity describes actions that have either already occurred, are in the
// process of occurring, or may occur in the future.
type Activity struct {
BaseObject
Actor string `json:"actor"`
Published time.Time `json:"published,omitempty"`
To []string `json:"to,omitempty"`
CC []string `json:"cc,omitempty"`
Object *Object `json:"object"`
}
type FollowActivity struct {
BaseObject
Actor string `json:"actor"`
Published time.Time `json:"published,omitempty"`
To []string `json:"to,omitempty"`
CC []string `json:"cc,omitempty"`
Object string `json:"object"`
}
// NewCreateActivity builds a basic Create activity that includes the given
// Object and the Object's AttributedTo property as the Actor.
func NewCreateActivity(o *Object) *Activity {
a := Activity{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
Extensions,
},
ID: o.ID,
Type: "Create",
},
Actor: o.AttributedTo,
Object: o,
Published: o.Published,
}
return &a
}
// NewUpdateActivity builds a basic Update activity that includes the given
// Object and the Object's AttributedTo property as the Actor.
func NewUpdateActivity(o *Object) *Activity {
a := Activity{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
Extensions,
},
ID: o.ID,
Type: "Update",
},
Actor: o.AttributedTo,
Object: o,
Published: o.Published,
}
return &a
}
// NewDeleteActivity builds a basic Delete activity that includes the given
// Object and the Object's AttributedTo property as the Actor.
func NewDeleteActivity(o *Object) *Activity {
a := Activity{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
},
ID: o.ID,
Type: "Delete",
},
Actor: o.AttributedTo,
Object: o,
}
return &a
}
// NewFollowActivity builds a basic Follow activity.
func NewFollowActivity(actorIRI, followeeIRI string) *FollowActivity {
a := FollowActivity{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
},
Type: "Follow",
},
Actor: actorIRI,
Object: followeeIRI,
}
return &a
}
// NewNoteObject creates a basic Note object that includes the public
// namespace in IRIs it's addressed to.
func NewNoteObject() *Object {
o := Object{
BaseObject: BaseObject{
Type: "Note",
},
To: []string{
toPublic,
},
}
return &o
}
// NewArticleObject creates a basic Article object that includes the public
// namespace in IRIs it's addressed to.
func NewArticleObject() *Object {
o := Object{
BaseObject: BaseObject{
Type: "Article",
},
To: []string{
toPublic,
},
}
return &o
}
// NewAnounceObject creates a basic Anounce object that includes the public
// namespace in IRIs it's addressed to.
func NewAnounceObject() *Object {
o := Object{
BaseObject: BaseObject{
Type: "Anounce",
},
To: []string{
toPublic,
},
}
return &o
}

57
activitypub/person.go Normal file
View file

@ -0,0 +1,57 @@
package activitypub
// An Person actor
// Ref: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person
type Person struct {
BaseObject
Inbox string `json:"inbox"`
Outbox string `json:"outbox"`
PreferredUsername string `json:"preferredUsername"`
URL string `json:"url"`
Name string `json:"name"`
Icon Image `json:"icon"`
Following string `json:"following"`
Followers string `json:"followers"`
Summary string `json:"summary"`
PublicKey PublicKey `json:"publicKey"`
Endpoints Endpoints `json:"endpoints"`
}
// NewPerson creates an ActivityPub Person
func NewPerson(id string) *Person {
p := Person{
BaseObject: BaseObject{
Type: "Person",
Context: []interface{}{
Namespace,
},
ID: id,
},
Following: id + "/following",
Followers: id + "/followers",
Inbox: id + "/inbox",
Outbox: id + "/outbox",
}
return &p
}
// AddPubKey add public key to Person
func (p *Person) AddPubKey(k []byte) {
p.Context = append(p.Context, "https://w3id.org/security/v1")
p.PublicKey = PublicKey{
ID: p.ID + "#main-key",
Owner: p.ID,
PublicKeyPEM: string(k),
}
}
// SetPrivateKey set private key to Person
func (p *Person) SetPrivateKey(k []byte) {
p.PublicKey.privateKey = k
}
// GetPrivateKey get private key
func (p *Person) GetPrivateKey() []byte {
return p.PublicKey.privateKey
}

107
activitypub/vocab.go Normal file
View file

@ -0,0 +1,107 @@
package activitypub
import (
"fmt"
"net/url"
"time"
)
const context = "https://ww.w3.org/ns/activitystreams"
type (
BaseObject struct {
Context []interface{} `json:"@context,omitempty"`
Type string `json:"type"`
ID string `json:"id"`
}
PublicKey struct {
ID string `json:"id"`
Owner string `json:"owner"`
PublicKeyPEM string `json:"publicKeyPem"`
privateKey []byte
}
Endpoints struct {
SharedInbox string `json:"sharedInbox,omitempty"`
}
Image struct {
Type string `json:"type"`
MediaType string `json:"mediaType"`
URL string `json:"url"`
}
// Object is the primary base type for the Activity Streams vocabulary.
Object struct {
BaseObject
Published time.Time `json:"published"`
Summary *string `json:"summary,omitempty"`
InReplyTo *string `json:"inReplyTo"`
URL string `json:"url"`
AttributedTo string `json:"attributedTo"`
To []string `json:"to"`
CC []string `json:"cc,omitempty"`
Name string `json:"name,omitempty"`
Content string `json:"content"`
ContentMap map[string]string `json:"contentMap,omitempty"`
//Tag []Tag `json:"tag"`
// Extensions
CommentsEnabled bool `json:"commentsEnabled"`
}
Group struct {
Context string `json:"@context"`
ID *url.URL `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
}
)
type OrderedCollection struct {
BaseObject
TotalItems int `json:"totalItems"`
First string `json:"first"`
Last string `json:"last,omitempty"`
}
func NewOrderedCollection(accountRoot, collType string, items int) *OrderedCollection {
oc := OrderedCollection{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
},
ID: accountRoot + "/" + collType,
Type: "OrderedCollection",
},
First: accountRoot + "/" + collType + "?page=1",
TotalItems: items,
}
return &oc
}
type OrderedCollectionPage struct {
BaseObject
TotalItems int `json:"totalItems"`
PartOf string `json:"partOf"`
Next string `json:"next,omitempty"`
Prev string `json:"prev,omitempty"`
OrderedItems []interface{} `json:"orderedItems,omitempty"`
}
func NewOrderedCollectionPage(accountRoot, collType string, items, page int) *OrderedCollectionPage {
ocp := OrderedCollectionPage{
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
},
ID: fmt.Sprintf("%s/%s?page=%d", accountRoot, collType, page),
Type: "OrderedCollectionPage",
},
TotalItems: items,
PartOf: accountRoot + "/" + collType,
Next: fmt.Sprintf("%s/%s?page=%d", accountRoot, collType, page+1),
}
return &ocp
}