ap

package
v0.21.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package ap implements types that represent the ActivityPub vocabulary.

These types implement database/sql.Scanner and database/sql/driver.Valuer, allowing them to be stored in a SQL database as a JSON column.

Index

Constants

View Source
const (
	OrderedCollection     CollectionType     = "OrderedCollection"
	OrderedCollectionPage CollectionPageType = "OrderedCollectionPage"
)
View Source
const (
	// Offline disables fetching of remote actors and forces use of local or cached actors.
	Offline ResolverFlag = 1

	// InstanceActor enables discovery of the "instance actor" instead of the regular actor discovery flow.
	InstanceActor = 2

	// GroupActor makes [Resolver] prefer the first [Group] actor in the WebFinger response.
	GroupActor = 4
)
View Source
const Public = "https://www.w3.org/ns/activitystreams#Public"

Public is the special ActivityPub collection used for public addressing.

Variables

View Source
var (
	ErrInvalidActivity     = errors.New("invalid activity")
	ErrUnsupportedActivity = errors.New("unsupported activity")
)
View Source
var (
	// KeyRegex matches a Multibase-encoded Ed25519 public key.
	KeyRegex = regexp.MustCompile(`\b(z6Mk[a-km-zA-HJ-NP-Z1-9]+|u7Q[A-Za-z0-9_-]+)\b`)

	// GatewayURLRegex matches an https:// gateway URL.
	GatewayURLRegex = regexp.MustCompile(`^https:\/\/[a-z0-9-]+(?:\.[a-z0-9-]+)+\/\.well-known\/apgateway\/did:key:(z6Mk[a-km-zA-HJ-NP-Z1-9]+)((?:[\/#?].*){0,1})`)
)

Functions

func Canonical added in v0.19.0

func Canonical(id string) string

Canonical returns an ID in canonical form: if portable, it's converted to an ap:// URL.

func Gateway added in v0.19.0

func Gateway(gw, id string) string

Gateway returns a https:// gateway URL for a portable ActivityPub ID.

func IsPortable added in v0.19.0

func IsPortable(id string) bool

IsPortable determines whether or not an ActivityPub ID is portable.

func Origin added in v0.19.0

func Origin(id string) (string, error)

Origin returns the origin of an ActivityPub ID.

func Origins added in v0.19.8

func Origins(id string) (string, string, error)

Origins returns the origin and the host of an ActivityPub ID.

func ValidateOrigin added in v0.21.0

func ValidateOrigin(domain string, activity *Activity, origin string) error

ValidateOrigin validates the origin of an Activity.

In other words, it can be used to enforce a same-owner policy and prevent actors from creating, editing or deleting entities owned by other actors. See https://fediverse.codeberg.page/fep/fep/fe34/ for more details.

Types

type Activity

type Activity struct {
	Context   any          `json:"@context,omitempty"`
	ID        string       `json:"id"`
	Type      ActivityType `json:"type"`
	Actor     string       `json:"actor"`
	Object    any          `json:"object"`
	Target    string       `json:"target,omitempty"`
	To        Audience     `json:"to"`
	CC        Audience     `json:"cc"`
	Published Time         `json:"published,omitzero"`
	Proof     Proof        `json:"proof,omitzero"`
}

Activity represents an ActivityPub activity. Object can point to another Activity, an Object or a string.

func (*Activity) IsPublic

func (a *Activity) IsPublic() bool

func (*Activity) LogValue

func (a *Activity) LogValue() slog.Value

func (*Activity) Scan

func (a *Activity) Scan(src any) error

func (*Activity) UnmarshalJSON

func (a *Activity) UnmarshalJSON(b []byte) error

func (*Activity) Value

func (a *Activity) Value() (driver.Value, error)

type ActivityType

type ActivityType string
const (
	MaxActivityDepth = 3

	Create   ActivityType = "Create"
	Follow   ActivityType = "Follow"
	Accept   ActivityType = "Accept"
	Reject   ActivityType = "Reject"
	Undo     ActivityType = "Undo"
	Delete   ActivityType = "Delete"
	Announce ActivityType = "Announce"
	Update   ActivityType = "Update"
	Move     ActivityType = "Move"

	Like       ActivityType = "Like"
	Dislike    ActivityType = "Dislike"
	EmojiReact ActivityType = "EmojiReact"
	Add        ActivityType = "Add"
	Remove     ActivityType = "Remove"
)

type Actor

type Actor struct {
	Context                   any               `json:"@context"`
	ID                        string            `json:"id"`
	Type                      ActorType         `json:"type"`
	Inbox                     string            `json:"inbox"`
	Outbox                    string            `json:"outbox"`
	Endpoints                 map[string]string `json:"endpoints,omitempty"`
	PreferredUsername         string            `json:"preferredUsername"`
	Name                      string            `json:"name,omitempty"`
	Summary                   string            `json:"summary,omitempty"`
	Followers                 string            `json:"followers,omitempty"`
	PublicKey                 PublicKey         `json:"publicKey"`
	Icon                      Array[Attachment] `json:"icon,omitempty"`
	Image                     *Attachment       `json:"image,omitempty"`
	ManuallyApprovesFollowers bool              `json:"manuallyApprovesFollowers"`
	AlsoKnownAs               Audience          `json:"alsoKnownAs,omitzero"`
	Published                 Time              `json:"published,omitzero"`
	Updated                   Time              `json:"updated,omitzero"`
	MovedTo                   string            `json:"movedTo,omitempty"`
	Suspended                 bool              `json:"suspended,omitempty"`
	Attachment                []Attachment      `json:"attachment,omitempty"`
	AssertionMethod           []AssertionMethod `json:"assertionMethod,omitempty"`
	Implements                Array[Implement]  `json:"implements,omitzero"`
	Generator                 Generator         `json:"generator,omitzero"`
	Gateways                  []string          `json:"gateways,omitempty"`
	Proof                     Proof             `json:"proof,omitzero"`
}

Actor represents an ActivityPub actor.

func (*Actor) Scan

func (a *Actor) Scan(src any) error

func (*Actor) Value

func (a *Actor) Value() (driver.Value, error)

type ActorType

type ActorType string
const (
	Person      ActorType = "Person"
	Group       ActorType = "Group"
	Application ActorType = "Application"
	Service     ActorType = "Service"
)

type Array

type Array[T any] []T

Array is an array or a single item.

func (Array[T]) IsZero added in v0.19.0

func (a Array[T]) IsZero() bool

func (Array[T]) MarshalJSON

func (a Array[T]) MarshalJSON() ([]byte, error)

func (*Array[T]) UnmarshalJSON

func (a *Array[T]) UnmarshalJSON(b []byte) error

type AssertionMethod added in v0.19.0

type AssertionMethod struct {
	ID                 string `json:"id"`
	Type               string `json:"type"`
	Controller         string `json:"controller"`
	PublicKeyMultibase string `json:"publicKeyMultibase"`
}

AssertionMethod contains a public key used to verify requests sent on behalf of an Actor.

type Attachment

type Attachment struct {
	Type      AttachmentType `json:"type,omitempty"`
	MediaType string         `json:"mediaType,omitempty"`
	URL       string         `json:"url,omitempty"`
	Href      string         `json:"href,omitempty"`
	Name      string         `json:"name,omitempty"`
	Val       string         `json:"value,omitempty"`
}

func (*Attachment) Scan added in v0.19.0

func (a *Attachment) Scan(src any) error

func (*Attachment) Value

func (a *Attachment) Value() (driver.Value, error)

type AttachmentType

type AttachmentType string
const (
	Image         AttachmentType = "Image"
	PropertyValue AttachmentType = "PropertyValue"
)

type Audience

type Audience struct {
	data.OrderedMap[string, struct{}]
}

Audience is an ordered, unique list of actor IDs.

func (*Audience) Add

func (a *Audience) Add(s string)

func (Audience) IsZero added in v0.19.0

func (a Audience) IsZero() bool

func (Audience) MarshalJSON

func (a Audience) MarshalJSON() ([]byte, error)

func (*Audience) Scan

func (a *Audience) Scan(src any) error

func (*Audience) UnmarshalJSON

func (a *Audience) UnmarshalJSON(b []byte) error

func (*Audience) Value

func (a *Audience) Value() (driver.Value, error)

type Capability added in v0.19.0

type Capability uint

Capability is a capability that may be supported by an ActivityPub server.

const (
	// CavageDraftSignatures is support for draft-cavage-http-signatures, with rsa-sha256.
	CavageDraftSignatures Capability = 1 << iota

	// RFC9421RSASignatures is support for RFC9421 HTTP signatures, with rsa-v1_5-sha256.
	RFC9421RSASignatures

	// RFC9421Ed25519Signatures is support for RFC9421 HTTP signatures, with Ed25119 keys.
	RFC9421Ed25519Signatures
)

type Collection added in v0.21.0

type Collection struct {
	Context      any            `json:"@context"`
	ID           string         `json:"id"`
	Type         CollectionType `json:"type"`
	First        string         `json:"first,omitempty"`
	Last         string         `json:"last,omitempty"`
	TotalItems   *int64         `json:"totalItems,omitempty"`
	OrderedItems any            `json:"orderedItems,omitzero"`
}

Collection represents an ActivityPub collection.

type CollectionPage added in v0.21.0

type CollectionPage struct {
	Context      any                `json:"@context"`
	ID           string             `json:"id"`
	Type         CollectionPageType `json:"type"`
	Next         string             `json:"next,omitempty"`
	Prev         string             `json:"prev,omitempty"`
	PartOf       string             `json:"partOf,omitempty"`
	OrderedItems any                `json:"orderedItems,omitzero"`
}

CollectionPage represents a Collection page.

type CollectionPageType added in v0.21.0

type CollectionPageType string

type CollectionType added in v0.21.0

type CollectionType string

type Generator added in v0.19.0

type Generator struct {
	Type       ActorType        `json:"type"`
	Implements Array[Implement] `json:"implements,omitzero"`
}

Generator generates Object objects.

type Implement added in v0.19.0

type Implement struct {
	Href string `json:"href,omitempty"`
	Name string `json:"name,omitempty"`
}

Implement is a Generator capability.

type Inbox added in v0.19.0

type Inbox interface {
	NewID(actorID, prefix string) (string, error)
	Accept(ctx context.Context, followed *Actor, key httpsig.Key, follower, followID string, tx *sql.Tx) error
	Announce(ctx context.Context, tx *sql.Tx, actor *Actor, key httpsig.Key, note *Object) error
	Create(ctx context.Context, cfg *cfg.Config, post *Object, author *Actor, key httpsig.Key) error
	Delete(ctx context.Context, actor *Actor, key httpsig.Key, note *Object) error
	Follow(ctx context.Context, follower *Actor, key httpsig.Key, followed string) error
	Move(ctx context.Context, from *Actor, key httpsig.Key, to string) error
	Reject(ctx context.Context, followed *Actor, key httpsig.Key, follower, followID string, tx *sql.Tx) error
	Undo(ctx context.Context, actor *Actor, key httpsig.Key, activity *Activity) error
	UpdateActorTx(ctx context.Context, tx *sql.Tx, actor *Actor, key httpsig.Key) error
	UpdateActor(ctx context.Context, actor *Actor, key httpsig.Key) error
	UpdateNote(ctx context.Context, actor *Actor, key httpsig.Key, note *Object) error
	Unfollow(ctx context.Context, follower *Actor, key httpsig.Key, followed, followID string) error
	ProcessActivity(ctx context.Context, tx *sql.Tx, path sql.NullString, sender *Actor, activity *Activity, rawActivity string, depth int, shared bool) error
}

Inbox creates and processes activities.

type InteractionPolicy added in v0.19.0

type InteractionPolicy struct {
	CanQuote QuotePolicy `json:"canQuote,omitzero"`
}

InteractionPolicy describes interaction policies for an Object.

type Object

type Object struct {
	Context           any               `json:"@context,omitempty"`
	ID                string            `json:"id"`
	Type              ObjectType        `json:"type"`
	AttributedTo      string            `json:"attributedTo,omitempty"`
	InReplyTo         string            `json:"inReplyTo,omitempty"`
	Content           string            `json:"content,omitempty"`
	Summary           string            `json:"summary,omitempty"`
	Sensitive         bool              `json:"sensitive,omitempty"`
	Name              string            `json:"name,omitempty"`
	Published         Time              `json:"published,omitzero"`
	Updated           Time              `json:"updated,omitzero"`
	To                Audience          `json:"to,omitzero"`
	CC                Audience          `json:"cc,omitzero"`
	Audience          string            `json:"audience,omitempty"`
	Tag               Array[Tag]        `json:"tag,omitzero"`
	Attachment        []Attachment      `json:"attachment,omitempty"`
	URL               string            `json:"url,omitempty"`
	Quote             string            `json:"quote,omitempty"`
	InteractionPolicy InteractionPolicy `json:"interactionPolicy,omitzero"`
	Proof             Proof             `json:"proof,omitzero"`

	// polls
	VotersCount int64        `json:"votersCount,omitempty"`
	OneOf       []PollOption `json:"oneOf,omitempty"`
	AnyOf       []PollOption `json:"anyOf,omitempty"`
	EndTime     Time         `json:"endTime,omitzero"`
	Closed      Time         `json:"closed,omitzero"`
}

Object represents most ActivityPub objects. Actors are represented by Actor.

func (*Object) CanQuote added in v0.19.0

func (o *Object) CanQuote() bool

CanQuote determines whether or not a post can be quoted.

func (*Object) IsPublic

func (o *Object) IsPublic() bool

func (*Object) Scan

func (o *Object) Scan(src any) error

func (*Object) Value

func (o *Object) Value() (driver.Value, error)

type ObjectType

type ObjectType string
const (
	Note      ObjectType = "Note"
	Page      ObjectType = "Page"
	Article   ObjectType = "Article"
	Question  ObjectType = "Question"
	Tombstone ObjectType = "Tombstone"
)

type PollOption

type PollOption struct {
	Name    string `json:"name"`
	Replies struct {
		TotalItems int64 `json:"totalItems"`
	} `json:"replies"`
}

type Proof added in v0.19.0

type Proof struct {
	Context            any    `json:"@context,omitempty"`
	Type               string `json:"type"`
	CryptoSuite        string `json:"cryptosuite"`
	VerificationMethod string `json:"verificationMethod"`
	Purpose            string `json:"proofPurpose"`
	Value              string `json:"proofValue,omitempty"`
	Created            string `json:"created"`
}

type PublicKey

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

type QuotePolicy added in v0.19.0

type QuotePolicy struct {
	AutomaticApproval Audience `json:"automaticApproval,omitzero"`
	ManualApproval    Audience `json:"manualApproval,omitzero"`
}

QuotePolicy describes quote policies for an Object.

type Resolver

type Resolver interface {
	ResolveID(ctx context.Context, keys [2]httpsig.Key, id string, flags ResolverFlag) (*Actor, error)
	Resolve(ctx context.Context, keys [2]httpsig.Key, host, name string, flags ResolverFlag) (*Actor, error)
	Get(ctx context.Context, keys [2]httpsig.Key, url string) (*http.Response, error)
}

Resolver retrieves Actor, Object and Activity objects.

type ResolverFlag

type ResolverFlag uint

type Tag

type Tag struct {
	Type TagType     `json:"type,omitempty"`
	Name string      `json:"name,omitempty"`
	Href string      `json:"href,omitempty"`
	Icon *Attachment `json:"icon,omitempty"`
}

type TagType

type TagType string
const (
	Mention TagType = "Mention"
	Hashtag TagType = "Hashtag"
	Emoji   TagType = "Emoji"
)

type Time

type Time struct {
	time.Time
}

Time is a wrapper around time.Time with fallback if parsing of RFC3339 fails.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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