diff --git a/automod/engine/account_meta.go b/automod/engine/account_meta.go index e7d4e86b..11a7504d 100644 --- a/automod/engine/account_meta.go +++ b/automod/engine/account_meta.go @@ -32,6 +32,8 @@ type AccountMeta struct { type ProfileSummary struct { HasAvatar bool + AvatarCid *string + BannerCid *string Description *string DisplayName *string } diff --git a/automod/engine/cid_from_cdn_test.go b/automod/engine/cid_from_cdn_test.go new file mode 100644 index 00000000..0780403c --- /dev/null +++ b/automod/engine/cid_from_cdn_test.go @@ -0,0 +1,42 @@ +package engine + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCidFromCdnUrl(t *testing.T) { + assert := assert.New(t) + + fixCid := "abcdefghijk" + + fixtures := []struct { + url string + cid *string + }{ + { + url: "https://cdn.bsky.app/img/avatar/plain/did:plc:abc123/abcdefghijk@jpeg", + cid: &fixCid, + }, + { + url: "https://cdn.bsky.app/img/feed_fullsize/plain/did:plc:abc123/abcdefghijk@jpeg", + cid: &fixCid, + }, + { + url: "https://cdn.bsky.app/img/feed_fullsize", + cid: nil, + }, + { + url: "https://cdn.bsky.app/img/feed_fullsize/plain/did:plc:abc123/abcdefghijk", + cid: &fixCid, + }, + { + url: "https://cdn.asky.app/img/feed_fullsize/plain/did:plc:abc123/abcdefghijk@jpeg", + cid: nil, + }, + } + + for _, fix := range fixtures { + assert.Equal(fix.cid, cidFromCdnUrl(&fix.url)) + } +} diff --git a/automod/engine/fetch_account_meta.go b/automod/engine/fetch_account_meta.go index 5bc5c363..1dd1965e 100644 --- a/automod/engine/fetch_account_meta.go +++ b/automod/engine/fetch_account_meta.go @@ -75,6 +75,8 @@ func (e *Engine) GetAccountMeta(ctx context.Context, ident *identity.Identity) ( am.Profile = ProfileSummary{ HasAvatar: pv.Avatar != nil, + AvatarCid: cidFromCdnUrl(pv.Avatar), + BannerCid: cidFromCdnUrl(pv.Banner), Description: pv.Description, DisplayName: pv.DisplayName, } diff --git a/automod/engine/util.go b/automod/engine/util.go index 195454c1..e96c411d 100644 --- a/automod/engine/util.go +++ b/automod/engine/util.go @@ -1,5 +1,10 @@ package engine +import ( + "net/url" + "strings" +) + func dedupeStrings(in []string) []string { var out []string seen := make(map[string]bool) @@ -11,3 +16,22 @@ func dedupeStrings(in []string) []string { } return out } + +// get the cid from a bluesky cdn url +func cidFromCdnUrl(str *string) *string { + if str == nil { + return nil + } + + u, err := url.Parse(*str) + if err != nil || u.Host != "cdn.bsky.app" { + return nil + } + + parts := strings.Split(u.Path, "/") + if len(parts) != 6 { + return nil + } + + return &strings.Split(parts[5], "@")[0] +}