From f13c9f7b5ec51f561ae8d6279fbccb0cdb4e2133 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Thu, 8 Oct 2026 13:41:34 -0400 Subject: [PATCH] fix(web): fall back to the status thumbnail, banner, or default card when the Streamplace card fails --- bskyweb/cmd/bskyweb/live_test.go | 51 ++++++++++++++++++++++++++++++++ bskyweb/cmd/bskyweb/server.go | 51 +++++++++++++++++++++++++++----- 2 files changed, 95 insertions(+), 7 deletions(-) diff --git a/bskyweb/cmd/bskyweb/live_test.go b/bskyweb/cmd/bskyweb/live_test.go index 67933a00a..9003818f5 100644 --- a/bskyweb/cmd/bskyweb/live_test.go +++ b/bskyweb/cmd/bskyweb/live_test.go @@ -1,7 +1,11 @@ package main import ( + "context" + "net/http" + "net/http/httptest" "testing" + "time" appbsky "github.com/bluesky-social/indigo/api/bsky" ) @@ -34,3 +38,50 @@ func TestLiveStreamTitle(t *testing.T) { } } } + +func TestLiveImageFallbacks(t *testing.T) { + ok := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "image/jpeg") + })) + defer ok.Close() + broken := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "image/jpeg") + w.WriteHeader(http.StatusInternalServerError) + })) + defer broken.Close() + slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(200 * time.Millisecond) + })) + defer slow.Close() + + client := &http.Client{Timeout: 50 * time.Millisecond} + active := true + thumb, banner := "https://cdn.example/thumb.jpg", "https://cdn.example/banner.jpg" + live := &appbsky.ActorDefs_ProfileViewDetailed{ + Banner: &banner, + Status: &appbsky.ActorDefs_StatusView{ + Status: "app.bsky.actor.status#live", + IsActive: &active, + Embed: &appbsky.ActorDefs_StatusView_Embed{EmbedExternal_View: &appbsky.EmbedExternal_View{ + External: &appbsky.EmbedExternal_ViewExternal{Thumb: &thumb}, + }}, + }, + } + cases := []struct { + name string + pv *appbsky.ActorDefs_ProfileViewDetailed + cardURL string + want string + }{ + {"card ok", live, ok.URL, ok.URL}, + {"card error uses status thumb", live, broken.URL, thumb}, + {"card timeout uses status thumb", live, slow.URL, thumb}, + {"no status uses banner", &appbsky.ActorDefs_ProfileViewDetailed{Banner: &banner}, broken.URL, banner}, + {"nothing uses default", &appbsky.ActorDefs_ProfileViewDetailed{}, broken.URL, "https://static/default.png"}, + } + for _, c := range cases { + if got := liveImage(context.Background(), client, c.pv, c.cardURL, "https://static/default.png"); got != c.want { + t.Errorf("%s: got %q, want %q", c.name, got, c.want) + } + } +} diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index 12d30a303..625866726 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -51,6 +51,8 @@ type Server struct { ipccClient http.Client + liveCardClient http.Client + // sitemapClient is used for fetching sitemaps from the appview. It has // DisableCompression set to true so that gzipped responses are passed // through without being decompressed. @@ -161,6 +163,7 @@ func serve(cctx *cli.Context) error { }, }, }, + liveCardClient: http.Client{Timeout: time.Second}, sitemapClient: http.Client{ Transport: &http.Transport{ MaxIdleConns: 100, @@ -1111,18 +1114,51 @@ func streamplaceProfileCardURL(did string) string { return "https://stream.place/xrpc/place.stream.live.getProfileCard?id=" + url.QueryEscape(did) } -func liveStreamTitle(pv *appbsky.ActorDefs_ProfileViewDetailed) string { +func liveStatusExternal(pv *appbsky.ActorDefs_ProfileViewDetailed) *appbsky.EmbedExternal_ViewExternal { st := pv.Status if st == nil || st.Status != "app.bsky.actor.status#live" { - return "" + return nil } if st.IsActive != nil && !*st.IsActive { - return "" + return nil + } + if st.Embed == nil || st.Embed.EmbedExternal_View == nil { + return nil + } + return st.Embed.EmbedExternal_View.External +} + +func liveStreamTitle(pv *appbsky.ActorDefs_ProfileViewDetailed) string { + if ext := liveStatusExternal(pv); ext != nil { + return ext.Title + } + return "" +} + +func imageAvailable(ctx context.Context, client *http.Client, imageURL string) bool { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, imageURL, nil) + if err != nil { + return false + } + resp, err := client.Do(req) + if err != nil { + return false + } + resp.Body.Close() + return resp.StatusCode == http.StatusOK && strings.HasPrefix(resp.Header.Get("Content-Type"), "image/") +} + +func liveImage(ctx context.Context, client *http.Client, pv *appbsky.ActorDefs_ProfileViewDetailed, cardURL, defaultImage string) string { + if imageAvailable(ctx, client, cardURL) { + return cardURL + } + if ext := liveStatusExternal(pv); ext != nil && ext.Thumb != nil && *ext.Thumb != "" { + return *ext.Thumb } - if st.Embed == nil || st.Embed.EmbedExternal_View == nil || st.Embed.EmbedExternal_View.External == nil { - return "" + if pv.Banner != nil && *pv.Banner != "" { + return *pv.Banner } - return st.Embed.EmbedExternal_View.External.Title + return defaultImage } func (srv *Server) WebLive(c echo.Context) error { @@ -1148,7 +1184,8 @@ func (srv *Server) WebLive(c echo.Context) error { req := c.Request() data["profileView"] = pv data["requestURI"] = fmt.Sprintf("https://%s%s", req.Host, req.URL.Path) - data["liveImage"] = streamplaceProfileCardURL(pv.Did) + defaultImage, _ := data["brandSocialCard"].(string) + data["liveImage"] = liveImage(ctx, &srv.liveCardClient, pv, streamplaceProfileCardURL(pv.Did), defaultImage) data["liveTitle"] = liveStreamTitle(pv) return c.Render(http.StatusOK, "live.html", data) -- knotmirror 0.0.1