feat(activitypub): add ActivityPub actor service
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
a383ece924
commit
d1c690ef3c
4 changed files with 131 additions and 0 deletions
67
activitypub.go
Normal file
67
activitypub.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
59
activitypub_test.go
Normal file
59
activitypub_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
2
forge.go
2
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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue