Federated via graft
1.0 kB
35 lines
1package activitypub
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7)
8
9// FetchActor retrieves a remote actor object. An unsigned GET is used —
10// acceptable here since this is only for resolving a Follow request's
11// actor (to get their public key and inbox URL) and most servers permit
12// unauthenticated actor fetches; authenticated fetches ("secure mode") are
13// out of scope for now.
14func FetchActor(client *http.Client, actorURL string) (*Actor, error) {
15 req, err := http.NewRequest(http.MethodGet, actorURL, nil)
16 if err != nil {
17 return nil, fmt.Errorf("build request: %w", err)
18 }
19 req.Header.Set("Accept", activityContentType)
20
21 resp, err := client.Do(req)
22 if err != nil {
23 return nil, fmt.Errorf("fetch actor %s: %w", actorURL, err)
24 }
25 defer resp.Body.Close()
26 if resp.StatusCode >= 300 {
27 return nil, fmt.Errorf("fetch actor %s: HTTP %d", actorURL, resp.StatusCode)
28 }
29
30 var a Actor
31 if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
32 return nil, fmt.Errorf("decode actor %s: %w", actorURL, err)
33 }
34 return &a, nil
35}