-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsky_client.go
145 lines (115 loc) · 3.13 KB
/
bsky_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bytes"
"context"
"io"
"sync"
"time"
"net/http"
"github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/xrpc"
"github.com/palantir/stacktrace"
)
// largely inspired by github.com/danrusei/gobot-bsky
type BskyClient struct {
mu sync.Mutex
client *xrpc.Client
handle string
apiKey string
lastConnected time.Time
}
func NewBskyClient(ctx context.Context, server, handle, apiKey string) *BskyClient {
return &BskyClient{
client: &xrpc.Client{
Client: &http.Client{},
Host: server,
},
handle: handle,
apiKey: apiKey,
}
}
func (b *BskyClient) Connect(ctx context.Context) error {
b.mu.Lock()
defer b.mu.Unlock()
return stacktrace.Propagate(b.connect(ctx), "")
}
func (b *BskyClient) connect(ctx context.Context) error {
serverCreateSessionInput := &atproto.ServerCreateSession_Input{
Identifier: b.handle,
Password: b.apiKey,
}
session, err := atproto.ServerCreateSession(ctx, b.client, serverCreateSessionInput)
if err != nil {
return stacktrace.Propagate(err, "")
}
b.client.Auth = &xrpc.AuthInfo{
AccessJwt: session.AccessJwt,
RefreshJwt: session.RefreshJwt,
Handle: session.Handle,
Did: session.Did,
}
b.lastConnected = time.Now()
return nil
}
func (b *BskyClient) connectIfNeeded(ctx context.Context) error {
if time.Since(b.lastConnected) < 1*time.Minute {
return nil
}
return stacktrace.Propagate(b.connect(ctx), "")
}
func (b *BskyClient) Post(ctx context.Context, post bsky.FeedPost) (string, string, error) {
b.mu.Lock()
defer b.mu.Unlock()
b.connectIfNeeded(ctx)
postInput := &atproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: b.client.Auth.Did,
Record: &util.LexiconTypeDecoder{
Val: &post,
},
}
response, err := atproto.RepoCreateRecord(ctx, b.client, postInput)
if err != nil {
return "", "", stacktrace.Propagate(err, "")
}
return response.Cid, response.Uri, nil
}
func (b *BskyClient) UploadImage(ctx context.Context, client *http.Client, imageURL string) (*util.LexBlob, error) {
b.mu.Lock()
defer b.mu.Unlock()
b.connectIfNeeded(ctx)
getImage, err := getImageAsBuffer(client, imageURL)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
resp, err := atproto.RepoUploadBlob(ctx, b.client, bytes.NewReader(getImage))
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
blob := util.LexBlob{
Ref: resp.Blob.Ref,
MimeType: resp.Blob.MimeType,
Size: resp.Blob.Size,
}
return &blob, nil
}
func getImageAsBuffer(client *http.Client, imageURL string) ([]byte, error) {
// Fetch image
response, err := client.Get(imageURL)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
defer response.Body.Close()
// Check response status
if response.StatusCode != http.StatusOK {
return nil, stacktrace.NewError("failed to fetch image: %s", response.Status)
}
// Read response body
imageData, err := io.ReadAll(response.Body)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
return imageData, nil
}