inbox

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 2

README

Hannibal / inbox

The Inbox library gives you: 1) an ActivityPub inbox handler that parses and validates incoming ActivityPub messages, and 2) a router that identifies messages by their type and object, and routes them to the correct business logic in your application



// Set up handlers for different kinds of activities and documents
activityHandler := inbox.NewRouter[CustomContextType]()

// Here's a handler to accept Create/Note messages
activityHandler.Add(vocab.ActivityTypeCreate, vocab.ObjectTypeNote, func(context CustomContextType, activity streams.Document) error {
	// do something with the activity
})

// You can do wildcards too.  Here's a handler to accept 
// Follow/Any messages
activityHandler.Add(vocab.ActivityTypeFollow, vocab.Any, func(context CustomContextType, activity streams.Document) error {
	// do something with the follow request.
	// remember to send an "Accept" message back to the sender
})

// Here's a catch-all handler that receives any uncaught messages
activityHandler.Add(vocab.Any, vocab.Any, func(context CustomContextType, activity streams.Document) error {
	// do something with this activity
})

// Add routes to your web server
myAppRouter.POST("/my/inbox",func (r *http.Request, w *http.Response) {

	// Parse and validate the posted activity
	activity, err := pub.ReceiveRequest(r)
	
	// Handle errors however you like
	if err != nil {
		...
	}

	context := // create custom "Context" value for this request
	
	// Pass the activity to the activityHandler, that will figure
	// out what kind of activity/object we have and pass it to the 
	// previously registered handler function
	if err := activityHandler.Handle(context, actitity); err != nil {
		// do something with the error
	}
	
	// Success!
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsActivityPubContentType

func IsActivityPubContentType(contentType string) bool

IsActivityPubContentType returns true if the specified content-type is an ActivityPub content-type

func ReceiveRequest

func ReceiveRequest(request *http.Request, client streams.Client) (document streams.Document, err error)

ReceiveRequest reads an incoming HTTP request and returns a parsed and validated ActivityPub activity

Types

type DebugLevel

type DebugLevel uint8

DebugLevel is a custom enumeration that defines various levels of debug output.

const DebugLevelNone DebugLevel = 0

DebugLevelNone is the default debug level. Using this setting, no debug messages will be printed.

const DebugLevelTerse DebugLevel = 100

DebugLevelTerse is a debug level that prints only the most important messages.

const DebugLevelVerbose DebugLevel = 200

DebugLevelVerbose is a debug level that prints all debug messages.

func (DebugLevel) String

func (debugLevel DebugLevel) String() string

String implements the Stringer interface, and returns a human-readable version of the DebugLevel.

type RouteHandler

type RouteHandler[T any] func(context T, activity streams.Document) error

RouteHandler is a function that handles a specific type of ActivityPub activity. RouteHandlers are registered with the Router object along with the names of the activity types that they correspond to.

type Router

type Router[T any] struct {
	// contains filtered or unexported fields
}

Router is a simple object that routes incoming ActivityPub activities to the appropriate handler

func NewRouter

func NewRouter[T any]() Router[T]

NewRouter creates a new Router object

func (*Router[T]) Add

func (router *Router[T]) Add(activityType string, objectType string, routeHandler RouteHandler[T])

Add puts a new route to the router. You can use "*" as a wildcard for either the activityType or objectType. The Handler method tries to match handlers from most specific to least specific. activity/object activity/* */object */*

For performance reasons, this function is not thread-safe. So, you should add all routes before starting the server, for instance, in your app's `init` functions.

func (*Router[T]) Handle

func (router *Router[T]) Handle(context T, activity streams.Document) error

Handle takes an ActivityPub activity and routes it to the appropriate handler

Jump to

Keyboard shortcuts

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