-
Notifications
You must be signed in to change notification settings - Fork 89
/
bsky.go
353 lines (296 loc) · 7.27 KB
/
bsky.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
comatproto "github.com/bluesky-social/indigo/api/atproto"
appbsky "github.com/bluesky-social/indigo/api/bsky"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/util"
"github.com/bluesky-social/indigo/util/cliutil"
cli "github.com/urfave/cli/v2"
)
var bskyCmd = &cli.Command{
Name: "bsky",
Usage: "sub-commands for bsky-specific endpoints",
Subcommands: []*cli.Command{
bskyFollowCmd,
bskyListFollowsCmd,
bskyPostCmd,
bskyGetFeedCmd,
bskyLikeCmd,
bskyDeletePostCmd,
bskyActorGetSuggestionsCmd,
bskyNotificationsCmd,
},
}
var bskyFollowCmd = &cli.Command{
Name: "follow",
Usage: "create a follow relationship (auth required)",
Flags: []cli.Flag{},
ArgsUsage: `<user>`,
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
user := cctx.Args().First()
follow := appbsky.GraphFollow{
LexiconTypeID: "app.bsky.graph.follow",
CreatedAt: time.Now().Format(time.RFC3339),
Subject: user,
}
resp, err := comatproto.RepoCreateRecord(context.TODO(), xrpcc, &comatproto.RepoCreateRecord_Input{
Collection: "app.bsky.graph.follow",
Repo: xrpcc.Auth.Did,
Record: &lexutil.LexiconTypeDecoder{Val: &follow},
})
if err != nil {
return err
}
fmt.Println(resp.Uri)
return nil
},
}
var bskyListFollowsCmd = &cli.Command{
Name: "list-follows",
Usage: "print list of follows for account",
ArgsUsage: `[actor]`,
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, false)
if err != nil {
return err
}
user := cctx.Args().First()
if user == "" {
user = xrpcc.Auth.Did
}
ctx := context.TODO()
resp, err := appbsky.GraphGetFollows(ctx, xrpcc, user, "", 100)
if err != nil {
return err
}
for _, f := range resp.Follows {
fmt.Println(f.Did, f.Handle)
}
return nil
},
}
var bskyPostCmd = &cli.Command{
Name: "post",
Usage: "create a post record",
ArgsUsage: `<text>`,
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
auth := xrpcc.Auth
text := strings.Join(cctx.Args().Slice(), " ")
resp, err := comatproto.RepoCreateRecord(context.TODO(), xrpcc, &comatproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: auth.Did,
Record: &lexutil.LexiconTypeDecoder{Val: &appbsky.FeedPost{
Text: text,
CreatedAt: time.Now().Format(util.ISO8601),
}},
})
if err != nil {
return fmt.Errorf("failed to create post: %w", err)
}
fmt.Println(resp.Cid)
fmt.Println(resp.Uri)
return nil
},
}
func prettyPrintPost(p *appbsky.FeedDefs_FeedViewPost, uris bool) {
fmt.Println(strings.Repeat("-", 60))
rec := p.Post.Record.Val.(*appbsky.FeedPost)
fmt.Printf("%s (%s)", p.Post.Author.Handle, rec.CreatedAt)
if uris {
fmt.Println(" -- ", p.Post.Uri)
} else {
fmt.Println(":")
}
fmt.Println(rec.Text)
}
var bskyGetFeedCmd = &cli.Command{
Name: "get-feed",
Usage: "fetch bsky feed",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "count",
Value: 100,
},
&cli.StringFlag{
Name: "author",
Usage: "specify handle of user to list their authored feed",
},
&cli.BoolFlag{
Name: "raw",
Usage: "print out feed in raw json",
},
&cli.BoolFlag{
Name: "uris",
Usage: "include URIs in pretty print output",
},
},
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
ctx := context.TODO()
raw := cctx.Bool("raw")
uris := cctx.Bool("uris")
author := cctx.String("author")
if author != "" {
if author == "self" {
author = xrpcc.Auth.Did
}
tl, err := appbsky.FeedGetAuthorFeed(ctx, xrpcc, author, "", "", 99)
if err != nil {
return err
}
for i := len(tl.Feed) - 1; i >= 0; i-- {
it := tl.Feed[i]
if raw {
jsonPrint(it)
} else {
prettyPrintPost(it, uris)
}
}
} else {
algo := "reverse-chronological"
tl, err := appbsky.FeedGetTimeline(ctx, xrpcc, algo, "", int64(cctx.Int("count")))
if err != nil {
return err
}
for i := len(tl.Feed) - 1; i >= 0; i-- {
it := tl.Feed[i]
if raw {
jsonPrint(it)
} else {
prettyPrintPost(it, uris)
}
}
}
return nil
},
}
var bskyActorGetSuggestionsCmd = &cli.Command{
Name: "actor-get-suggestions",
ArgsUsage: "[author]",
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
ctx := context.TODO()
author := cctx.Args().First()
if author == "" {
author = xrpcc.Auth.Did
}
resp, err := appbsky.ActorGetSuggestions(ctx, xrpcc, "", 100)
if err != nil {
return err
}
b, err := json.MarshalIndent(resp.Actors, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
},
}
var bskyLikeCmd = &cli.Command{
Name: "like",
Usage: "create bsky 'like' record",
ArgsUsage: "<post>",
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
arg := cctx.Args().First()
parts := strings.Split(arg, "/")
if len(parts) < 3 {
return fmt.Errorf("invalid post uri: %q", arg)
}
rkey := parts[len(parts)-1]
collection := parts[len(parts)-2]
did := parts[2]
fmt.Println(did, collection, rkey)
ctx := context.TODO()
resp, err := comatproto.RepoGetRecord(ctx, xrpcc, "", collection, did, rkey)
if err != nil {
return fmt.Errorf("getting record: %w", err)
}
out, err := comatproto.RepoCreateRecord(ctx, xrpcc, &comatproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.like",
Repo: xrpcc.Auth.Did,
Record: &lexutil.LexiconTypeDecoder{
Val: &appbsky.FeedLike{
CreatedAt: time.Now().Format(util.ISO8601),
Subject: &comatproto.RepoStrongRef{Uri: resp.Uri, Cid: *resp.Cid},
},
},
})
if err != nil {
return fmt.Errorf("creating like failed: %w", err)
}
_ = out
return nil
},
}
var bskyDeletePostCmd = &cli.Command{
Name: "delete-post",
ArgsUsage: `<rkey>`,
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
rkey := cctx.Args().First()
if rkey == "" {
return fmt.Errorf("must specify rkey of post to delete")
}
schema := "app.bsky.feed.post"
if strings.Contains(rkey, "/") {
parts := strings.Split(rkey, "/")
schema = parts[0]
rkey = parts[1]
}
return comatproto.RepoDeleteRecord(context.TODO(), xrpcc, &comatproto.RepoDeleteRecord_Input{
Repo: xrpcc.Auth.Did,
Collection: schema,
Rkey: rkey,
})
},
}
var bskyNotificationsCmd = &cli.Command{
Name: "notifs",
Usage: "fetch bsky notifications (requires auth)",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
ctx := context.TODO()
xrpcc, err := cliutil.GetXrpcClient(cctx, true)
if err != nil {
return err
}
notifs, err := appbsky.NotificationListNotifications(ctx, xrpcc, "", 50, "")
if err != nil {
return err
}
for _, n := range notifs.Notifications {
b, err := json.Marshal(n)
if err != nil {
return err
}
fmt.Println(string(b))
}
return nil
},
}