Skip to content

Commit

Permalink
Add avatar to AccountMeta (#779)
Browse files Browse the repository at this point in the history
smol change. couple of new rules need avatar cids. currently have to
fetch from public api before the rule processes in a separate request
which is meh, since we have the info here already.
  • Loading branch information
bnewbold authored Nov 5, 2024
2 parents 6ac14ce + 73bb35e commit fc4b12c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions automod/engine/account_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type AccountMeta struct {

type ProfileSummary struct {
HasAvatar bool
AvatarCid *string
BannerCid *string
Description *string
DisplayName *string
}
Expand Down
42 changes: 42 additions & 0 deletions automod/engine/cid_from_cdn_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
2 changes: 2 additions & 0 deletions automod/engine/fetch_account_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
24 changes: 24 additions & 0 deletions automod/engine/util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package engine

import (
"net/url"
"strings"
)

func dedupeStrings(in []string) []string {
var out []string
seen := make(map[string]bool)
Expand All @@ -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]
}

0 comments on commit fc4b12c

Please sign in to comment.