Defines most of the logic for handling the ActivityPub interactions for both Social API and Federation Protocol dissemination in the form of a state machine making decisions based on the Activity and Object types pairs.
This state machine can be wrapped in standard HTTP Handlers that are compatible with the rest of the Go ecosystem.
It also contains additional handlers used to output individual objects and collections after retrieving them from storage.
Finally, it defines the interfaces that the storage backends need to implement to be able to work with the rest of the GoActivityPub modules.
On this page you can see a listing of what the default actions are for each supported Activity/Object pair.
Here’s a minimal example of how to process an ActivityPub request:
// The underlying storage backend, see go-ap/storage-* modules.
repo := new(storage.FullStorage)
// The client is used when it's required to dereference properties of the
// incoming that are referred only by their IDs into full ActivityPub
// objects. Some instances require that remote fetching is done using
// requests containing valid HTTP-Signatures, so take that into
// consideration. See the go-ap/client module for how to initialize it for
// such cases.
client := client.New()
// Initializing the ActivityPub state machine, see the go-ap/processing
// module for the relevant initializer options.
processor := processing.New(
processing.WithClient(client),
processing.WithStorage(repo),
)
if err != nil {
return fmt.Errorf("unable to initialize processor: %w", err)
}
// The processor needs to know the the URL on which the current request has
// been operated on.
// If this is a Social API request, it needs to correspond
// to an actor's /outbox.
// If this is a Federated Protocol request, it needs
// to correspond to its /inbox
receivedIn := pub.IRI("https://" + filepath.Join(req.Host, req.URL.Path))
// Authorized is an actor extracted from the HTTP request auhtorization
// headers. To extract it, see the go-ap/authorize module, which can extract
// HTTP-Signatures, and OAuth2 bearer tokens.
authorized := new(vocab.Actor)
// The Activity extracted from the request body
body, _ := io.ReadAll(r.Body)
act, err = vocab.UnmarshalJSON(body)
if err != nil {
return fmt.Errorf("failed to unmarshal activity from request: %w", err)
}
// After processing the Activity, it will have been updated with any
// information the specification mentions for the combination of Activity
// and Object types.
if it, err = processor.ProcessActivity(act, authorized, receivedIn); err != nil {
return fmt.Errorf("error processing activity: %w", err)
}
// NOTE: the application's custom logic for handling the Activity/Object
// pairs after successfully processing them.
switch {
case vocab.CreateType.Match(it.GetType()):
case vocab.UndoType.Match(it.GetType()):
case vocab.LikeType.Match(it.GetType()), vocab.DislikeType.Match(it.GetType()):
}