From d1c690ef3c73afa59c70fac55edd3b1b66f11a7d Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 02:04:26 +0000 Subject: [PATCH] feat(activitypub): add ActivityPub actor service Co-Authored-By: Virgil --- activitypub.go | 67 +++++++++++++++++++++++++++++++++++++++++++++ activitypub_test.go | 59 +++++++++++++++++++++++++++++++++++++++ forge.go | 2 ++ forge_test.go | 3 ++ 4 files changed, 131 insertions(+) create mode 100644 activitypub.go create mode 100644 activitypub_test.go diff --git a/activitypub.go b/activitypub.go new file mode 100644 index 0000000..e3141cb --- /dev/null +++ b/activitypub.go @@ -0,0 +1,67 @@ +package forge + +import ( + "context" + + "dappco.re/go/core/forge/types" +) + +// ActivityPubService handles ActivityPub actor and inbox endpoints. +// +// Usage: +// +// f := forge.NewForge("https://forge.lthn.ai", "token") +// _, err := f.ActivityPub.GetInstanceActor(ctx) +type ActivityPubService struct { + client *Client +} + +func newActivityPubService(c *Client) *ActivityPubService { + return &ActivityPubService{client: c} +} + +// GetInstanceActor returns the instance's ActivityPub actor. +func (s *ActivityPubService) GetInstanceActor(ctx context.Context) (*types.ActivityPub, error) { + var out types.ActivityPub + if err := s.client.Get(ctx, "/activitypub/actor", &out); err != nil { + return nil, err + } + return &out, nil +} + +// SendInstanceActorInbox sends an ActivityPub object to the instance inbox. +func (s *ActivityPubService) SendInstanceActorInbox(ctx context.Context, body *types.ForgeLike) error { + return s.client.Post(ctx, "/activitypub/actor/inbox", body, nil) +} + +// GetRepositoryActor returns the ActivityPub actor for a repository. +func (s *ActivityPubService) GetRepositoryActor(ctx context.Context, repositoryID int64) (*types.ActivityPub, error) { + path := ResolvePath("/activitypub/repository-id/{repository-id}", Params{"repository-id": int64String(repositoryID)}) + var out types.ActivityPub + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + +// SendRepositoryInbox sends an ActivityPub object to a repository inbox. +func (s *ActivityPubService) SendRepositoryInbox(ctx context.Context, repositoryID int64, body *types.ForgeLike) error { + path := ResolvePath("/activitypub/repository-id/{repository-id}/inbox", Params{"repository-id": int64String(repositoryID)}) + return s.client.Post(ctx, path, body, nil) +} + +// GetPersonActor returns the Person actor for a user. +func (s *ActivityPubService) GetPersonActor(ctx context.Context, userID int64) (*types.ActivityPub, error) { + path := ResolvePath("/activitypub/user-id/{user-id}", Params{"user-id": int64String(userID)}) + var out types.ActivityPub + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + +// SendPersonInbox sends an ActivityPub object to a user's inbox. +func (s *ActivityPubService) SendPersonInbox(ctx context.Context, userID int64, body *types.ForgeLike) error { + path := ResolvePath("/activitypub/user-id/{user-id}/inbox", Params{"user-id": int64String(userID)}) + return s.client.Post(ctx, path, body, nil) +} diff --git a/activitypub_test.go b/activitypub_test.go new file mode 100644 index 0000000..6ad2442 --- /dev/null +++ b/activitypub_test.go @@ -0,0 +1,59 @@ +package forge + +import ( + "context" + json "github.com/goccy/go-json" + "net/http" + "net/http/httptest" + "testing" + + "dappco.re/go/core/forge/types" +) + +func TestActivityPubService_GetInstanceActor_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/activitypub/actor" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + json.NewEncoder(w).Encode(types.ActivityPub{Context: "https://www.w3.org/ns/activitystreams"}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + actor, err := f.ActivityPub.GetInstanceActor(context.Background()) + if err != nil { + t.Fatal(err) + } + if actor.Context != "https://www.w3.org/ns/activitystreams" { + t.Fatalf("got context=%q", actor.Context) + } +} + +func TestActivityPubService_SendRepositoryInbox_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Errorf("expected POST, got %s", r.Method) + } + if r.URL.Path != "/activitypub/repository-id/42/inbox" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + var body types.ForgeLike + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Fatalf("decode body: %v", err) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.ActivityPub.SendRepositoryInbox(context.Background(), 42, &types.ForgeLike{}); err != nil { + t.Fatal(err) + } +} diff --git a/forge.go b/forge.go index becab42..1e6a9ba 100644 --- a/forge.go +++ b/forge.go @@ -28,6 +28,7 @@ type Forge struct { Misc *MiscService Commits *CommitService Milestones *MilestoneService + ActivityPub *ActivityPubService } // NewForge creates a new Forge client. @@ -58,6 +59,7 @@ func NewForge(url, token string, opts ...Option) *Forge { f.Misc = newMiscService(c) f.Commits = newCommitService(c) f.Milestones = newMilestoneService(c) + f.ActivityPub = newActivityPubService(c) return f } diff --git a/forge_test.go b/forge_test.go index 5ed95f5..b77ad93 100644 --- a/forge_test.go +++ b/forge_test.go @@ -19,6 +19,9 @@ func TestForge_NewForge_Good(t *testing.T) { if f.Issues == nil { t.Fatal("Issues service is nil") } + if f.ActivityPub == nil { + t.Fatal("ActivityPub service is nil") + } } func TestForge_Client_Good(t *testing.T) {