forked from karalabe/go-bluesky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
389 lines (358 loc) · 12.7 KB
/
profile.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// Copyright 2023 go-bluesky authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bluesky
import (
"context"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"strings"
"github.com/bluesky-social/indigo/api/bsky"
)
const (
// maxProfileAvatarBytes is the maximum number of bytes a profile avatar might
// have before it's rejected by the library.
maxProfileAvatarBytes = 8 * 1024 * 1024
// maxProfileBannerBytes is the maximum number of bytes a profile banner might
// have before it's rejected by the library.
maxProfileBannerBytes = 8 * 1024 * 1024
)
// Profile represents a user profile on a Bluesky server.
type Profile struct {
client *Client // Embedded API client to lazy-load pictures
Handle string // User-friendly - unstable - identifier for the user
DID string // Machine friendly - stable - identifier for the user
Name string // Display name to use in various apps
Bio string // Profile description to use in various apps
AvatarURL string // CDN URL to the user's profile picture, empty if unset
Avatar image.Image // Profile picture, nil if unset or not yet resolved
BannerURL string // CDN URL to the user's banner picture, empty if unset
Banner image.Image // Banner picture, nil if unset ot not yet resolved
FollowerCount uint // Number of people who follow this user
Followers []*User // Actual list of followers, nil if not yet resolved
FolloweeCount uint // Number of people who this user follows
Followees []*User // Actual list of followees, nil if not yet resolved
PostCount uint // Number of posts this user made
}
// User tracks some metadata about a user on a Bluesky server.
type User struct {
client *Client // Embedded API client to lazy-load pictures
Handle string // User-friendly - unstable - identifier for the follower
DID string // Machine friendly - stable - identifier for the follower
Name string // Display name to use in various apps
Bio string // Profile description to use in various apps
AvatarURL string // CDN URL to the user's profile picture, empty if unset
Avatar image.Image // Profile picture, nil if unset or not yet fetched
}
// FetchProfile retrieves all the metadata about a specific user.
//
// Supported IDs are the Bluesky handles or atproto DIDs.
func (c *Client) FetchProfile(ctx context.Context, id string) (*Profile, error) {
// The API only supports the non-prefixed forms. Seems a bit wonky, but trim
// manually for now until it's decided whether this is a feature or a bug.
// https://github.com/bluesky-social/atproto/issues/989
if strings.HasPrefix(id, "@") {
id = id[1:]
}
if strings.HasPrefix(id, "at://") {
id = id[5:]
}
// Retrieve the remote profile
profile, err := bsky.ActorGetProfile(ctx, c.client, id)
if err != nil {
return nil, err
}
// Dig out the relevant fields and drop pointless pointers
p := &Profile{
client: c,
Handle: profile.Handle,
DID: profile.Did,
FollowerCount: uint(*profile.FollowersCount),
FolloweeCount: uint(*profile.FollowsCount),
PostCount: uint(*profile.PostsCount),
}
if profile.DisplayName != nil {
p.Name = *profile.DisplayName
}
if profile.Description != nil {
p.Bio = *profile.Description
}
if profile.Avatar != nil {
p.AvatarURL = *profile.Avatar
}
if profile.Banner != nil {
p.BannerURL = *profile.Banner
}
return p, nil
}
// String implements the stringer interface to help debug things.
func (p *Profile) String() string {
if p.Name == "" {
return fmt.Sprintf("%s (%s)", p.Handle, p.DID)
}
return fmt.Sprintf("%s (%s/%s)", maybeEscape(p.Name), p.Handle, p.DID)
}
// ResolveAvatar resolves the profile avatar from the server URL and injects it
// into the profile itself. If the avatar (URL) is unset, the method will return
// success and leave the image in the profile nil.
//
// Note, the method will place a sanity limit on the maximum size of the image
// in bytes to avoid malicious content. You may use the ResolveAvatarWithLimit to
// override and potentially disable this protection.
func (p *Profile) ResolveAvatar(ctx context.Context) error {
return p.ResolveAvatarWithLimit(ctx, maxProfileAvatarBytes)
}
// ResolveAvatarWithLimit resolves the profile avatar from the server URL using a
// custom data download limit (set to 0 to disable entirely) and injects it into
// the profile itself. If the avatar (URL) is unset, the method will return success
// and leave the image in the profile nil.
func (p *Profile) ResolveAvatarWithLimit(ctx context.Context, bytes uint64) error {
if p.AvatarURL == "" {
return nil
}
avatar, err := fetchImage(ctx, p.client, p.AvatarURL, bytes)
if err != nil {
return err
}
p.Avatar = avatar
return nil
}
// ResolveBanner resolves the profile banner from the server URL and injects it
// into the profile itself. If the banner (URL) is unset, the method will return
// success and leave the image in the profile nil.
//
// Note, the method will place a sanity limit on the maximum size of the image
// in bytes to avoid malicious content. You may use the ResolveBannerWithLimit to
// override and potentially disable this protection.
func (p *Profile) ResolveBanner(ctx context.Context) error {
return p.ResolveBannerWithLimit(ctx, maxProfileBannerBytes)
}
// ResolveBannerWithLimit resolves the profile banner from the server URL using a
// custom data download limit (set to 0 to disable entirely) and injects it into
// the profile itself. If the banner (URL) is unset, the method will return success
// and leave the image in the profile nil.
func (p *Profile) ResolveBannerWithLimit(ctx context.Context, bytes uint64) error {
if p.BannerURL == "" {
return nil
}
banner, err := fetchImage(ctx, p.client, p.BannerURL, bytes)
if err != nil {
return err
}
p.Banner = banner
return nil
}
// ResolveFollowers resolves the full list of followers of a profile and injects
// it into the profile itself.
//
// Note, since there is a fairly low limit on retrievable followers per API call,
// this method might take a while to complete on larger accounts. You may use the
// StreamFollowers to have finer control over the rate of retrievals,
// interruptions and memory usage.
func (p *Profile) ResolveFollowers(ctx context.Context) error {
followerc, errc := p.StreamFollowers(ctx)
followers := make([]*User, 0, p.FollowerCount)
for follower := range followerc {
followers = append(followers, follower)
}
if err := <-errc; err != nil {
return err
}
p.Followers = followers
return nil
}
// StreamFollowers gradually resolves the full list of followers of
// a profile, feeding them async into a result channel, closing the channel when
// there are no more followers left. An error channel is also returned and will
// receive (optionally, only ever one) error in case of a failure.
//
// Note, this method is meant to process the follower list as a stream, and will
// thus not populate the profile's followers field.
func (p *Profile) StreamFollowers(ctx context.Context) (<-chan *User, <-chan error) {
var (
cursor string
followers = make(chan *User, 100) // Ensure all results fit to unblock a second call
errc = make(chan error, 1) // Ensure the failure fits to unblock termination
)
go func() {
// No matter what happens, close both channels
defer func() {
close(followers)
close(errc)
}()
for {
// Resolve the followers from the Bluesky server
res, err := bsky.GraphGetFollowers(ctx, p.client.client, p.DID, cursor, 100)
if err != nil {
errc <- err
return
}
// Parse the followers and feed them one by one to the sink channel
for _, follower := range res.Followers {
f := &User{
client: p.client,
Handle: follower.Handle,
DID: follower.Did,
}
if follower.DisplayName != nil {
f.Name = *follower.DisplayName
}
if follower.Description != nil {
f.Bio = *follower.Description
}
if follower.Avatar != nil {
f.AvatarURL = *follower.Avatar
}
select {
case <-ctx.Done():
// Request is being torn down, abort
errc <- ctx.Err()
return
case followers <- f:
// Follower read, get the next one
}
}
// If there are further followers to parse, repeat
if res.Cursor == nil {
break
}
cursor = *res.Cursor
}
}()
return followers, errc
}
// ResolveFollowees resolves the full list of followees of a profile and injects
// it into the profile itself.
//
// Note, since there is a fairly low limit on retrievable followees per API call,
// this method might take a while to complete on larger accounts. You may use the
// StreamFollowees to have finer control over the rate of retrievals,
// interruptions and memory usage.
func (p *Profile) ResolveFollowees(ctx context.Context) error {
followeec, errc := p.StreamFollowees(ctx)
followees := make([]*User, 0, p.FolloweeCount)
for followee := range followeec {
followees = append(followees, followee)
}
if err := <-errc; err != nil {
return err
}
p.Followees = followees
return nil
}
// StreamFollowees gradually resolves the full list of followees of
// a profile, feeding them async into a result channel, closing the channel when
// there are no more followees left. An error channel is also returned and will
// receive (optionally, only ever one) error in case of a failure.
//
// Note, this method is meant to process the followeer list as a stream, and will
// thus not populate the profile's followees field.
func (p *Profile) StreamFollowees(ctx context.Context) (<-chan *User, <-chan error) {
var (
cursor string
followees = make(chan *User, 100) // Ensure all results fit to unblock a second call
errc = make(chan error, 1) // Ensure the failure fits to unblock termination
)
go func() {
// No matter what happens, close both channels
defer func() {
close(followees)
close(errc)
}()
for {
// Resolve the followees from the Bluesky server
res, err := bsky.GraphGetFollows(ctx, p.client.client, p.DID, cursor, 100)
if err != nil {
errc <- err
return
}
// Parse the followers and feed them one by one to the sink channel
for _, followee := range res.Follows {
f := &User{
client: p.client,
Handle: followee.Handle,
DID: followee.Did,
}
if followee.DisplayName != nil {
f.Name = *followee.DisplayName
}
if followee.Description != nil {
f.Bio = *followee.Description
}
if followee.Avatar != nil {
f.AvatarURL = *followee.Avatar
}
select {
case <-ctx.Done():
// Request is being torn down, abort
errc <- ctx.Err()
return
case followees <- f:
// Followee read, get the next one
}
}
// If there are further followees to parse, repeat
if res.Cursor == nil {
break
}
cursor = *res.Cursor
}
}()
return followees, errc
}
// String implements the stringer interface to help debug things.
func (u *User) String() string {
if u.Name == "" {
return fmt.Sprintf("%s (%s)", u.Handle, u.DID)
}
return fmt.Sprintf("%s (%s/%s)", maybeEscape(u.Name), u.Handle, u.DID)
}
// ResolveAvatar resolves the user avatar from the server URL and injects it into
// the user itself. If the avatar (URL) is unset, the method will return success
// and leave the image in the user nil.
//
// Note, the method will place a sanity limit on the maximum size of the image
// in bytes to avoid malicious content. You may use the ResolveAvatarWithLimit to
// override and potentially disable this protection.
func (u *User) ResolveAvatar(ctx context.Context) error {
return u.ResolveAvatarWithLimit(ctx, maxProfileAvatarBytes)
}
// ResolveAvatarWithLimit resolves the user avatar from the server URL using a
// custom data download limit (set to 0 to disable entirely) and injects it into
// the user itself. If the avatar (URL) is unset, the method will return success
// and leave the image in the user nil.
func (u *User) ResolveAvatarWithLimit(ctx context.Context, bytes uint64) error {
if u.AvatarURL == "" {
return nil
}
avatar, err := fetchImage(ctx, u.client, u.AvatarURL, bytes)
if err != nil {
return err
}
u.Avatar = avatar
return nil
}
// fetchImage resolves a remote image via a URL and a set byte cap.
func fetchImage(ctx context.Context, client *Client, url string, bytes uint64) (image.Image, error) {
// Initiate the remote image retrieval
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
res, err := client.client.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// Read the image with a cap on the max data size if requested
in := io.Reader(res.Body)
if bytes != 0 {
in = io.LimitReader(res.Body, int64(bytes))
}
img, _, err := image.Decode(in)
return img, err
}