activitypub

package
v0.0.0-...-7fd3dae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2023 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package activitypub implements ActivityPub types, handling and c2s (client-to-server) functionality

Index

Constants

View Source
const (
	// ActivityContentType is the content type for all ActivityPub activities
	ActivityContentType = "application/activity+json"

	// LinkedDataContentType is the JSON Linked Data type
	LinkedDataContentType = "application/ld+json"

	// ActivityBaseURI the URI for the ActivityStreams namespace
	ActivityBaseURI = "https://www.w3.org/ns/activitystreams"

	// SecurityContextURI the URI for the security namespace (for an Actor's PublicKey)
	SecurityContextURI = "https://w3id.org/security/v1"

	// PublicNS is the reference to the Public entity in the ActivityStreams namespace.
	PublicNS = ActivityBaseURI + "#Public"
)

Variables

DefaultContext is a slice of URIs that form the default context for all objects

View Source
var (
	// ErrUnsupportedObject is the error returned when parsing objects that are not supported
	ErrUnsupportedObject = errors.New("error: unsupported object")
)

Functions

func DiscoverActor

func DiscoverActor(uri string) (string, error)

DiscoverActor performs a webfinger lookup for `uri` and returns the actor's id

func EncodePublicKey

func EncodePublicKey(key *rsa.PrivateKey) (string, error)

EncodePublicKey encodes the public key of a private rsa key

func FastHash

func FastHash(data []byte) string

func FastHashString

func FastHashString(s string) string

func GenerateKeys

func GenerateKeys(bits int) (*rsa.PrivateKey, error)

GenerateKeys generates a private rsa key

func GetOrCreateKey

func GetOrCreateKey(fn string) (key *rsa.PrivateKey, err error)

GetOrCreateKey reads the private rsa key from a file or creates a new one if it doesn't exist

func IsWebFingerURI

func IsWebFingerURI(uri string) bool

IsWebFingerURI returns true if the `uri` looks like a webfinger URI

func ParseObject

func ParseObject(data []byte) (v any, err error)

ParseObject parses the JSON object into an ActivityPub activity object by first inspecting its type before un-marshalling into the correct type.

func ReadKey

func ReadKey(fn string) (*rsa.PrivateKey, error)

ReadKey reads the private rsa key from a file

func SignedRequest

func SignedRequest(actor *Actor, method, uri string, body io.Reader) (*http.Response, error)

SignedRequest performs a signed request using the Actor's private key

func WriteKey

func WriteKey(fn string, key *rsa.PrivateKey) error

WriteKey writes the private rsa key to the file

Types

type Accept

type Accept struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object any    `json:"object"`
}

Accept is an Accept activity

func AcceptFollow

func AcceptFollow(id, actor string, follow *Follow) *Accept

AcceptFollow creates a new ActivityPub Accept Activity with an embedded Follow object

type ActivityPub

type ActivityPub struct {
	sync.RWMutex

	// HandleFollow is a function that handles an incoming Follow from an Activity Pub actor
	// Consumers should override this field with a custom handler that suits the application.
	HandleFollow func(actor *Actor, follow *Follow) error

	// HandleNote is a function that handles an incoming Note from an Activity Pub actor
	// Consumers should override this field with a custom handler that suits the application.
	HandleNote func(actor *Actor, note *Note) error

	// LoadActor is a function that takes an actor `id` (URI) as input and returns an `*Actor` object
	// Consumers should override this field with a custom actor loader that suits the application.
	LoadActor func(id string) (*Actor, error)

	// NewID is a function that returns a new unique identifier for a new activity.
	// In the case of arguments supplied, a hash should be generated from the arguments supplied
	// to create a content addressable identifier.
	// Consumers should override this field with a custom function that suits the application.
	NewID func(args ...string) string
	// contains filtered or unexported fields
}

func New

func New(fn string, actor *Actor) *ActivityPub

New returns a new ActivityPub endpoint and processor

func (*ActivityPub) AddFollower

func (ap *ActivityPub) AddFollower(actor string, follower *Follower)

func (*ActivityPub) Broadcast

func (ap *ActivityPub) Broadcast(actor *Actor, obj any)

func (*ActivityPub) DebugHandler

func (ap *ActivityPub) DebugHandler(w http.ResponseWriter, r *http.Request)

func (*ActivityPub) DelFollower

func (ap *ActivityPub) DelFollower(id string, idx int)

func (*ActivityPub) GetAllFollowers

func (ap *ActivityPub) GetAllFollowers() Followers

func (*ActivityPub) GetFollowerFor

func (ap *ActivityPub) GetFollowerFor(id, actor string) (*Follower, int)

func (*ActivityPub) GetFollowers

func (ap *ActivityPub) GetFollowers(id string) Followers

func (*ActivityPub) GetNote

func (ap *ActivityPub) GetNote(uri string) (*Note, error)

func (*ActivityPub) InboxHandler

func (ap *ActivityPub) InboxHandler(w http.ResponseWriter, r *http.Request)

func (*ActivityPub) IsActor

func (ap *ActivityPub) IsActor(uri string) bool

func (*ActivityPub) IsFollowing

func (ap *ActivityPub) IsFollowing(id, actor string) bool

func (*ActivityPub) Load

func (ap *ActivityPub) Load() error

func (*ActivityPub) Lookup

func (ap *ActivityPub) Lookup(uri string) (*Actor, error)

func (*ActivityPub) Save

func (ap *ActivityPub) Save() error

func (*ActivityPub) Send

func (ap *ActivityPub) Send(actor *Actor, to string, obj any)

func (*ActivityPub) Stats

func (ap *ActivityPub) Stats() (stats Stats)

type Actor

type Actor struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Icon              Icon   `json:"icon"`
	Summary           string `json:"summary"`
	PreferredUsername string `json:"preferredUsername"`

	Inbox     string    `json:"inbox"`
	Outbox    string    `json:"outbox"`
	Following string    `json:"following"`
	Followers string    `json:"followers"`
	PublicKey PublicKey `json:"publicKey"`
	// contains filtered or unexported fields
}

Actor is a basic ActivityPub Actor

func LookupActor

func LookupActor(actor *Actor, uri string) (*Actor, error)

LookupActor looks up an actor by its URI and returns an Actor object

func NewPerson

func NewPerson(id string) *Actor

NewPerson creates a new ActivityPub Actor of Type Person

func NewService

func NewService(id string) *Actor

NewService creates a new ActivityPub Actor of Type Service

func (*Actor) SetIcon

func (a *Actor) SetIcon(url string)

SetIcon sets the Actor's Icon (Avatar)

func (*Actor) SetKey

func (a *Actor) SetKey(key *rsa.PrivateKey) error

SetKey sets private key of this actor and encodes the public key

func (*Actor) String

func (a *Actor) String() string

String implements the fmt.Stringer interface

type Create

type Create struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object any    `json:"object"`

	// XXX: Can be "foo" or ["foo", "bar"]
	// TODO: Do a better job of handling this?
	To any `json:"to"`
}

Create is a Create activity

func CreateNote

func CreateNote(id, actor string, created time.Time, content string, inReplyTo string, tags ...Tag) *Create

CreateNote creates a new ActivityPub Create Activity with an embedded Note object

type Delete

type Delete struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object any    `json:"object"`
}

Delete is a Delete activity

type Follow

type Follow struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object string `json:"object"`
	To     string `json:"to"`
}

Follow is a Follow activity

func NewFollow

func NewFollow(id, followID, actor, to string) *Follow

NewFollow creates a new Follow activity

func (*Follow) FromMap

func (f *Follow) FromMap(m map[string]any) error

type Follower

type Follower struct {
	Actor     string    `json:"actor"`
	Inbox     string    `json:"inbox"`
	CreatedAt time.Time `json:"created_at"`
}

func NewFollower

func NewFollower(actor string) *Follower

type Followers

type Followers []*Follower

func (Followers) Len

func (f Followers) Len() int

func (Followers) Less

func (f Followers) Less(i, j int) bool

func (Followers) Swap

func (f Followers) Swap(i, j int)

type Icon

type Icon struct {
	Type      string `json:"type"`
	MediaType string `json:"mediaType"`
	URL       string `json:"url"`
}

Icon is an Icon

type Note

type Note struct {
	ID   string `json:"id"`
	Type string `json:"type"`

	To          any       `json:"to"`
	Tag         Tags      `json:"tag"`
	Content     string    `json:"content"`
	Published   time.Time `json:"published"`
	AttributeTo string    `json:"attributedTo"`
	InReplyTo   string    `json:"inReplyTo"`
}

Note is a Note object type embedded in an Create Activity

func GetNote

func GetNote(actor *Actor, uri string) (*Note, error)

GetNote retrieves a note given its uri

func (*Note) FromMap

func (n *Note) FromMap(m map[string]any) error

type OrderedCollection

type OrderedCollection struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	First string `json:"first"`
}

OrderedCollection is an Ordered Collection for Outboxes

func NewOrderedCollection

func NewOrderedCollection(id string) *OrderedCollection

NewOrderedCollection creates a new ActivityPub OrderedCollection

type PublicKey

type PublicKey struct {
	ID        string `json:"id"`
	Owner     string `json:"owner"`
	PublicKey string `json:"publicKeyPem"`
}

PublicKey is a a Person's Public RSA Key

type Reject

type Reject struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object any    `json:"object"`
}

Reject is an Reject activity

type Stats

type Stats struct {
	Actors    int
	Followers int
}

type Tag

type Tag struct {
	Type string `json:"type"`
	Name string `json:"name"`
	HRef string `json:"href"`
}

Tag is a Tag object

type Tags

type Tags []Tag

func (*Tags) UnmarshalJSON

func (tags *Tags) UnmarshalJSON(data []byte) error

type Undo

type Undo struct {
	Context any `json:"@context"`

	ID   string `json:"id"`
	Type string `json:"type"`

	Actor  string `json:"actor"`
	Object any    `json:"object"`
	To     string `json:"to"`
}

Undo is an Undo activity

func NewUnFollow

func NewUnFollow(id, followID, actor, to string) *Undo

NewUnFollow creates a new Undo+Follow activity

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL