From 8786840e05b92079106c9a01a8bbc263e8b21b89 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sat, 3 May 2025 21:29:43 +0300 Subject: [PATCH] appview: state: use avatar service urls in profiles --- .gitignore | 1 + appview/config.go | 18 ++++++++++-------- appview/state/profile.go | 17 ++++++++++++----- appview/state/state.go | 4 ---- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 1df590cf..98360c07 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ result !.gitkeep out/ ./camo/node_modules/* +./avatar/node_modules/* diff --git a/appview/config.go b/appview/config.go index 3fd3dc81..c7d321ca 100644 --- a/appview/config.go +++ b/appview/config.go @@ -7,14 +7,16 @@ import ( ) type Config struct { - CookieSecret string `env:"TANGLED_COOKIE_SECRET, default=00000000000000000000000000000000"` - DbPath string `env:"TANGLED_DB_PATH, default=appview.db"` - ListenAddr string `env:"TANGLED_LISTEN_ADDR, default=0.0.0.0:3000"` - Dev bool `env:"TANGLED_DEV, default=false"` - JetstreamEndpoint string `env:"TANGLED_JETSTREAM_ENDPOINT, default=wss://jetstream1.us-east.bsky.network/subscribe"` - ResendApiKey string `env:"TANGLED_RESEND_API_KEY"` - CamoHost string `env:"TANGLED_CAMO_HOST, default=https://camo.tangled.sh"` - CamoSharedSecret string `env:"TANGLED_CAMO_SHARED_SECRET"` + CookieSecret string `env:"TANGLED_COOKIE_SECRET, default=00000000000000000000000000000000"` + DbPath string `env:"TANGLED_DB_PATH, default=appview.db"` + ListenAddr string `env:"TANGLED_LISTEN_ADDR, default=0.0.0.0:3000"` + Dev bool `env:"TANGLED_DEV, default=false"` + JetstreamEndpoint string `env:"TANGLED_JETSTREAM_ENDPOINT, default=wss://jetstream1.us-east.bsky.network/subscribe"` + ResendApiKey string `env:"TANGLED_RESEND_API_KEY"` + CamoHost string `env:"TANGLED_CAMO_HOST, default=https://camo.tangled.sh"` + CamoSharedSecret string `env:"TANGLED_CAMO_SHARED_SECRET"` + AvatarSharedSecret string `env:"TANGLED_AVATAR_SHARED_SECRET"` + AvatarHost string `env:"TANGLED_AVATAR_HOST, default=https://avatar.tangled.sh"` } func LoadConfig(ctx context.Context) (*Config, error) { diff --git a/appview/state/profile.go b/appview/state/profile.go index 404f30ff..f26fe043 100644 --- a/appview/state/profile.go +++ b/appview/state/profile.go @@ -1,6 +1,9 @@ package state import ( + "crypto/hmac" + "crypto/sha256" + "encoding/hex" "fmt" "log" "net/http" @@ -79,11 +82,7 @@ func (s *State) ProfilePage(w http.ResponseWriter, r *http.Request) { followStatus = db.GetFollowStatus(s.db, loggedInUser.Did, ident.DID.String()) } - profileAvatarUri, err := GetAvatarUri(ident.Handle.String()) - if err != nil { - log.Println("failed to fetch bsky avatar", err) - } - + profileAvatarUri := s.GetAvatarUri(ident.Handle.String()) s.pages.ProfilePage(w, pages.ProfilePageParams{ LoggedInUser: loggedInUser, UserDid: ident.DID.String(), @@ -100,3 +99,11 @@ func (s *State) ProfilePage(w http.ResponseWriter, r *http.Request) { ProfileTimeline: timeline, }) } + +func (s *State) GetAvatarUri(handle string) string { + secret := s.config.AvatarSharedSecret + h := hmac.New(sha256.New, []byte(secret)) + h.Write([]byte(handle)) + signature := hex.EncodeToString(h.Sum(nil)) + return fmt.Sprintf("%s/%s/%s", s.config.AvatarHost, signature, handle) +} diff --git a/appview/state/state.go b/appview/state/state.go index fbc9f795..460a2bba 100644 --- a/appview/state/state.go +++ b/appview/state/state.go @@ -742,7 +742,3 @@ func (s *State) NewRepo(w http.ResponseWriter, r *http.Request) { return } } - -func GetAvatarUri(handle string) (string, error) { - return fmt.Sprintf("https://avatars.dog/%s@webp", handle), nil -} -- 2.51.2