This repository has no description
1package spindle
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io"
9 "net/http"
10
11 "github.com/bluesky-social/indigo/atproto/syntax"
12)
13
14func AdminAllowMember(ctx context.Context, url, password, did string) error {
15 if _, err := syntax.ParseDID(did); err != nil {
16 return fmt.Errorf("invalid did %q: %w", did, err)
17 }
18
19 return postAdmin(ctx, password, url+"/admin/member/allow", adminReq{Did: did})
20}
21
22func AdminBlockMember(ctx context.Context, url, password, did string) error {
23 if _, err := syntax.ParseDID(did); err != nil {
24 return fmt.Errorf("invalid did %q: %w", did, err)
25 }
26
27 return postAdmin(ctx, password, url+"/admin/member/block", adminReq{Did: did})
28}
29
30func postAdmin(ctx context.Context, password, url string, body any) error {
31 encoded, _ := json.Marshal(body)
32 req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(encoded))
33 if err != nil {
34 return err
35 }
36 req.Header.Set("Content-Type", "application/json")
37 req.SetBasicAuth("admin", password)
38
39 resp, err := http.DefaultClient.Do(req)
40 if err != nil {
41 return err
42 }
43 defer resp.Body.Close()
44
45 if resp.StatusCode/100 != 2 {
46 msg, _ := io.ReadAll(resp.Body)
47 return fmt.Errorf("spindle returned %s: %s", resp.Status, bytes.TrimSpace(msg))
48 }
49 return nil
50}