From 2dba1fafa6a977f84831cea11b3ef0b6f06a2299 Mon Sep 17 00:00:00 2001 From: Michael Black Date: Wed, 28 Oct 2026 11:01:50 -0500 Subject: [PATCH] Add profile tab deep links for native and web (#12230) Co-authored-by: Samuel Newman --- bskyweb/cmd/bskyweb/profile_test.go | 72 +++++++++++++++++++++ bskyweb/cmd/bskyweb/server.go | 73 ++++++++++++++++------ src/lib/routes/profile-tabs.test.ts | 53 ++++++++++++++++ src/lib/routes/profile-tabs.ts | 14 +++++ src/lib/routes/types.ts | 2 +- src/view/com/pager/PagerWithHeader.tsx | 2 +- src/view/com/pager/PagerWithHeader.web.tsx | 2 +- src/view/screens/Profile.tsx | 38 +++++++++-- 8 files changed, 229 insertions(+), 27 deletions(-) create mode 100644 bskyweb/cmd/bskyweb/profile_test.go create mode 100644 src/lib/routes/profile-tabs.test.ts create mode 100644 src/lib/routes/profile-tabs.ts diff --git a/bskyweb/cmd/bskyweb/profile_test.go b/bskyweb/cmd/bskyweb/profile_test.go new file mode 100644 index 000000000..6289365dc --- /dev/null +++ b/bskyweb/cmd/bskyweb/profile_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "strings" + "testing" + + "github.com/flosch/pongo2/v6" +) + +func TestBskyProfileCanonicalURL(t *testing.T) { + baseURL := "https://bsky.app/profile/alice.bsky.social" + for _, test := range []struct { + tab string + want string + }{ + {tab: "", want: baseURL}, + {tab: "posts", want: baseURL}, + {tab: "replies", want: baseURL + "?tab=replies"}, + {tab: "media", want: baseURL + "?tab=media"}, + {tab: "videos", want: baseURL + "?tab=videos"}, + {tab: "feeds", want: baseURL + "?tab=feeds"}, + {tab: "starter-packs", want: baseURL + "?tab=starter-packs"}, + {tab: "lists", want: baseURL + "?tab=lists"}, + {tab: "unknown", want: baseURL}, + } { + t.Run(test.tab, func(t *testing.T) { + if got := bskyProfileCanonicalURL("alice.bsky.social", test.tab); got != test.want { + t.Errorf("bskyProfileCanonicalURL(..., %q) = %q, want %q", test.tab, got, test.want) + } + }) + } +} + +func TestRenderProfileWithTabKeepsCanonicalQuery(t *testing.T) { + canonicalURL := bskyProfileCanonicalURL("alice.bsky.social", "replies") + html := renderTemplate(t, "profile.html", pongo2.Context{ + "requestURI": "https://bsky.app/profile/alice.bsky.social?tab=replies", + "canonicalURL": canonicalURL, + }) + + if !strings.Contains(html, ``) { + t.Errorf("og:url should preserve the selected profile tab; got:\n%s", html) + } + if !strings.Contains(html, ``) { + t.Errorf("canonical URL should preserve the selected profile tab; got:\n%s", html) + } +} + +func TestProfileAuthorFeedFilter(t *testing.T) { + for _, test := range []struct { + tab string + wantFilter string + wantIncluded bool + }{ + {tab: "", wantFilter: "posts_no_replies", wantIncluded: true}, + {tab: "posts", wantFilter: "posts_no_replies", wantIncluded: true}, + {tab: "replies", wantFilter: "posts_with_replies", wantIncluded: true}, + {tab: "media", wantFilter: "posts_with_media", wantIncluded: true}, + {tab: "videos", wantFilter: "posts_with_video", wantIncluded: true}, + {tab: "feeds", wantIncluded: false}, + {tab: "starter-packs", wantIncluded: false}, + {tab: "lists", wantIncluded: false}, + {tab: "unknown", wantFilter: "posts_no_replies", wantIncluded: true}, + } { + t.Run(test.tab, func(t *testing.T) { + gotFilter, gotIncluded := profileAuthorFeedFilter(test.tab) + if gotFilter != test.wantFilter || gotIncluded != test.wantIncluded { + t.Errorf("profileAuthorFeedFilter(%q) = (%q, %t), want (%q, %t)", test.tab, gotFilter, gotIncluded, test.wantFilter, test.wantIncluded) + } + }) + } +} diff --git a/bskyweb/cmd/bskyweb/server.go b/bskyweb/cmd/bskyweb/server.go index 1cabe9998..389938f48 100644 --- a/bskyweb/cmd/bskyweb/server.go +++ b/bskyweb/cmd/bskyweb/server.go @@ -823,6 +823,37 @@ func (srv *Server) WebChatInvite(c echo.Context) error { return c.Render(http.StatusOK, "chatinvite.html", data) } +func bskyProfileCanonicalURL(handle, tab string) string { + canonicalURL := bskyProfileURL(handle) + if canonicalURL == "" { + return "" + } + + switch tab { + case "replies", "media", "videos", "feeds", "starter-packs", "lists": + query := url.Values{} + query.Set("tab", tab) + return canonicalURL + "?" + query.Encode() + default: + return canonicalURL + } +} + +func profileAuthorFeedFilter(tab string) (string, bool) { + switch tab { + case "replies": + return "posts_with_replies", true + case "media": + return "posts_with_media", true + case "videos": + return "posts_with_video", true + case "feeds", "starter-packs", "lists": + return "", false + default: + return "posts_no_replies", true + } +} + func (srv *Server) WebProfile(c echo.Context) error { ctx := c.Request().Context() data := srv.NewTemplateContext() @@ -847,11 +878,11 @@ func (srv *Server) WebProfile(c echo.Context) error { data["requestURI"] = fmt.Sprintf("https://%s%s", req.Host, req.URL.Path) data["requestHost"] = req.Host - // Prefer the handle-form URL so JSON-LD `url` and - // match. Template falls back to requestURI - // when the handle is unusable. - if url := bskyProfileURL(pv.Handle); url != "" { - data["canonicalURL"] = url + tab := c.QueryParam("tab") + // Keep non-default profile tabs distinct for crawlers. `posts`, blank, and + // unknown values all render the default Posts tab and share its canonical. + if canonicalURL := bskyProfileCanonicalURL(pv.Handle, tab); canonicalURL != "" { + data["canonicalURL"] = canonicalURL } // Fetch recent posts for ProfilePage.hasPart. Skipped for auth-required @@ -862,21 +893,23 @@ func (srv *Server) WebProfile(c echo.Context) error { // caching per-profile if upstream load becomes a concern. var recentPosts []*appbsky.FeedDefs_PostView if unauthedViewingOkay { - af, err := appbsky.FeedGetAuthorFeed(ctx, srv.xrpcc, pv.Did, "", "posts_no_replies", false, authorFeedFetchLimit) - if err != nil { - log.Warnf("failed to fetch author feed for: %s\t%v", pv.Did, err) - } else { - for _, p := range af.Feed { - if p == nil || p.Post == nil { - continue - } - // Only the author's own posts (matches RSS handler). - if p.Post.Author == nil || p.Post.Author.Did != pv.Did { - continue - } - recentPosts = append(recentPosts, p.Post) - if len(recentPosts) >= maxRecentPosts { - break + if feedFilter, includeFeed := profileAuthorFeedFilter(tab); includeFeed { + af, err := appbsky.FeedGetAuthorFeed(ctx, srv.xrpcc, pv.Did, "", feedFilter, false, authorFeedFetchLimit) + if err != nil { + log.Warnf("failed to fetch author feed for: %s\t%v", pv.Did, err) + } else { + for _, p := range af.Feed { + if p == nil || p.Post == nil { + continue + } + // Only the author's own posts (matches RSS handler). + if p.Post.Author == nil || p.Post.Author.Did != pv.Did { + continue + } + recentPosts = append(recentPosts, p.Post) + if len(recentPosts) >= maxRecentPosts { + break + } } } } diff --git a/src/lib/routes/profile-tabs.test.ts b/src/lib/routes/profile-tabs.test.ts new file mode 100644 index 000000000..c98f6efdf --- /dev/null +++ b/src/lib/routes/profile-tabs.test.ts @@ -0,0 +1,53 @@ +import {router} from '#/routes' +import {getProfileTabIndex} from './profile-tabs' + +describe('getProfileTabIndex', () => { + const indexes = { + posts: 2, + replies: 3, + media: 4, + videos: 5, + feeds: 6, + 'starter-packs': 7, + lists: 8, + } + + it.each([ + ['posts', 2], + ['replies', 3], + ['media', 4], + ['videos', 5], + ['feeds', 6], + ['starter-packs', 7], + ['lists', 8], + ])('resolves the %s tab', (tab, index) => { + expect(getProfileTabIndex(tab, indexes, 0)).toBe(index) + }) + + it.each([undefined, '', 'unknown'])('defaults %s to posts', tab => { + expect(getProfileTabIndex(tab, indexes, 2)).toBe(2) + }) + + it('defaults to posts when the requested tab is not available', () => { + expect(getProfileTabIndex('replies', {posts: 2, replies: null}, 2)).toBe(2) + }) + + it('defaults inherited property names to posts', () => { + expect(getProfileTabIndex('toString', indexes, 2)).toBe(2) + }) + + it.each([ + 'posts', + 'replies', + 'media', + 'videos', + 'feeds', + 'starter-packs', + 'lists', + ])('parses profile links with tab=%s', tab => { + expect(router.matchPath(`/profile/alice.test?tab=${tab}`)).toEqual([ + 'Profile', + {name: 'alice.test', tab}, + ]) + }) +}) diff --git a/src/lib/routes/profile-tabs.ts b/src/lib/routes/profile-tabs.ts new file mode 100644 index 000000000..80604f3f7 --- /dev/null +++ b/src/lib/routes/profile-tabs.ts @@ -0,0 +1,14 @@ +export type ProfileTab = + 'posts' | 'replies' | 'media' | 'videos' | 'feeds' | 'starter-packs' | 'lists' + +/** Resolve a profile tab index, falling back to the posts tab. */ +export function getProfileTabIndex( + tab: string | undefined, + indexes: Partial>, + defaultIndex: number, +) { + if (!tab || !Object.prototype.hasOwnProperty.call(indexes, tab)) { + return defaultIndex + } + return indexes[tab as ProfileTab] ?? defaultIndex +} diff --git a/src/lib/routes/types.ts b/src/lib/routes/types.ts index d7ddb2193..d81063a06 100644 --- a/src/lib/routes/types.ts +++ b/src/lib/routes/types.ts @@ -36,7 +36,7 @@ export type CommonNavigatorParams = { ModerationInteractionSettings: undefined ModerationVerificationSettings: undefined Settings: undefined - Profile: {name: string; hideBackButton?: boolean} + Profile: {name: string; hideBackButton?: boolean; tab?: string} ProfileFollowers: {name: string} ProfileFollows: {name: string} ProfileKnownFollowers: {name: string} diff --git a/src/view/com/pager/PagerWithHeader.tsx b/src/view/com/pager/PagerWithHeader.tsx index b45c5c5c6..afaba2b88 100644 --- a/src/view/com/pager/PagerWithHeader.tsx +++ b/src/view/com/pager/PagerWithHeader.tsx @@ -71,7 +71,7 @@ export function PagerWithHeader({ allowHeaderOverScroll, headerOverflowInset, }: PagerWithHeaderProps) { - const [currentPage, setCurrentPage] = useState(0) + const [currentPage, setCurrentPage] = useState(initialPage ?? 0) const [tabBarHeight, setTabBarHeight] = useState(0) const [headerOnlyHeight, setHeaderOnlyHeight] = useState(0) const scrollY = useSharedValue(0) diff --git a/src/view/com/pager/PagerWithHeader.web.tsx b/src/view/com/pager/PagerWithHeader.web.tsx index 39eeded04..1dc0dceea 100644 --- a/src/view/com/pager/PagerWithHeader.web.tsx +++ b/src/view/com/pager/PagerWithHeader.web.tsx @@ -54,7 +54,7 @@ export const PagerWithHeader = forwardRef( }: PagerWithHeaderProps, ref, ) { - const [currentPage, setCurrentPage] = useState(0) + const [currentPage, setCurrentPage] = useState(initialPage ?? 0) const renderTabBar = useCallback( (props: RenderTabBarFnProps) => { diff --git a/src/view/screens/Profile.tsx b/src/view/screens/Profile.tsx index 80706e276..9241cfc15 100644 --- a/src/view/screens/Profile.tsx +++ b/src/view/screens/Profile.tsx @@ -13,6 +13,7 @@ import {useQueryClient} from '@tanstack/react-query' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' import {useRequireEmailVerification} from '#/lib/hooks/useRequireEmailVerification' import {useSetTitle} from '#/lib/hooks/useSetTitle' +import {getProfileTabIndex} from '#/lib/routes/profile-tabs' import { type CommonNavigatorParams, type NativeStackScreenProps, @@ -161,10 +162,16 @@ function ProfileScreenInner({route}: Props) { if (profile && moderationOpts) { return ( ) } @@ -187,11 +194,13 @@ function ProfileScreenLoaded({ isPlaceholderProfile, moderationOpts, hideBackButton, + tab, }: { profile: app.bsky.actor.defs.ProfileViewDetailed moderationOpts: ModerationOpts hideBackButton: boolean isPlaceholderProfile: boolean + tab?: string }) { const t = useTheme() const {gtMobile} = useBreakpoints() @@ -209,7 +218,6 @@ function ProfileScreenLoaded({ did: profile.did, enabled: !!profile.associated?.labeler, }) - const [currentPage, setCurrentPage] = useState(0) const [isRefreshing, setIsRefreshing] = useState(false) const {_} = useLingui() @@ -240,7 +248,8 @@ function ProfileScreenLoaded({ const hasLabeler = !!profile.associated?.labeler const showFiltersTab = hasLabeler const showPostsTab = true - const showRepliesTab = hasSession + // Reply deep links must work without a session so crawlers can index reply-heavy profiles. + const showRepliesTab = hasSession || tab === 'replies' const showMediaTab = !hasLabeler const showVideosTab = !hasLabeler const showLikesTab = isMe @@ -250,7 +259,9 @@ function ProfileScreenLoaded({ const showStarterPacksTab = isMe || starterPackCount > 0 // subtract starterpack count from list count, since starterpacks are a type of list const listCount = (profile.associated?.lists || 0) - starterPackCount - const showListsTab = hasSession && (isMe || listCount > 0) + // Lists are public, so direct links can bypass the logged-in-only tab visibility. + const showListsTab = + (hasSession || tab === 'lists') && (isMe || listCount > 0) const sectionTitles = [ showFiltersTab ? _(msg`Labels`) : undefined, @@ -278,6 +289,9 @@ function ProfileScreenLoaded({ if (showFiltersTab) { filtersIndex = nextIndex++ } + if (showListsTab && hasLabeler) { + listsIndex = nextIndex++ + } if (showPostsTab) { postsIndex = nextIndex++ } @@ -299,10 +313,25 @@ function ProfileScreenLoaded({ if (showStarterPacksTab) { starterPacksIndex = nextIndex++ } - if (showListsTab) { + if (showListsTab && !hasLabeler) { listsIndex = nextIndex++ } + const initialPage = getProfileTabIndex( + tab, + { + posts: postsIndex, + replies: repliesIndex, + media: mediaIndex, + videos: videosIndex, + feeds: feedsIndex, + 'starter-packs': starterPacksIndex, + lists: listsIndex, + }, + 0, + ) + const [currentPage, setCurrentPage] = useState(initialPage) + const scrollSectionToTop = useCallback( (index: number) => { if (index === filtersIndex) { @@ -419,6 +448,7 @@ function ProfileScreenLoaded({ modui={moderation.ui('profileView')}>