go-ap / activitypub

ActivityPub vocabulary for Go
MIT License
128 stars 10 forks source link
activitypub go

About GoActivityPub: Vocabulary

MIT Licensed Build Status Test Coverage Go Report Card

This project is part of the GoActivityPub library which helps with creating ActivityPub applications using the Go programming language.

It contains data types for most of the Activity Vocabulary and the ActivityPub extension. They are documented accordingly with annotations from these specifications.

You can find an expanded documentation about the whole library on SourceHut.

For discussions about the projects you can write to the discussions mailing list: ~mariusor/go-activitypub-discuss@lists.sr.ht

For patches and bug reports please use the dev mailing list: ~mariusor/go-activitypub-dev@lists.sr.ht

Usage

import vocab "github.com/go-ap/activitypub"

follow := vocab.Activity{
    Type: vocab.FollowType,
    Actor: vocab.IRI("https://example.com/alice"),
    Object: vocab.IRI("https://example.com/janedoe"),
}

Note about generics

The module contains helper functions which make it simpler to deal with the vocab.Item interfaces and they come in two flavours: explicit OnXXX and ToXXX functions corresponding to each type and, a generic pair of functions On[T] and To[T].

import (
    "fmt"

    vocab "github.com/go-ap/activitypub"
)

var it vocab.Item = ... // an ActivityPub object unmarshaled from a request

err := vocab.OnActivity(it, func(act *vocab.Activity) error {
    if vocab.ContentManagementActivityTypes.Contains(act.Type) {
        fmt.Printf("This is a Content Management type activity: %q", act.Type)
    }
    return nil
})

err := vocab.On[vocab.Activity](it, func(act *vocab.Activity) error {
    if vocab.ReactionsActivityTypes.Contains(act.Type) {
        fmt.Printf("This is a Reaction type activity: %q", act.Type)
    }
    return nil
})

Before using the generic versions you should consider that they come with a pretty heavy performance penalty:

goos: linux
goarch: amd64
pkg: github.com/go-ap/activitypub
cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
Benchmark_OnT_vs_On_T/OnObject-8                    752387791       1.633 ns/op
Benchmark_OnT_vs_On_T/On_T_Object-8                   4656264     261.8   ns/op
Benchmark_OnT_vs_On_T/OnActor-8                     739833261       1.596 ns/op
Benchmark_OnT_vs_On_T/On_T_Actor-8                    4035148     301.9   ns/op
Benchmark_OnT_vs_On_T/OnActivity-8                  751173854       1.604 ns/op
Benchmark_OnT_vs_On_T/On_T_Activity-8                 4062598     285.9   ns/op
Benchmark_OnT_vs_On_T/OnIntransitiveActivity-8      675824500       1.640 ns/op
Benchmark_OnT_vs_On_T/On_T_IntransitiveActivity-8     4372798     274.1   ns/op
PASS
ok      github.com/go-ap/activitypub    11.350s