diff --git a/src/components/Post/Embed/index.tsx b/src/components/Post/Embed/index.tsx index 914ffc65c..2fe377379 100644 --- a/src/components/Post/Embed/index.tsx +++ b/src/components/Post/Embed/index.tsx @@ -4,14 +4,13 @@ import { type $Typed, type AppBskyFeedDefs, AppBskyFeedPost, - AtUri, moderatePost, RichText as RichTextAPI, } from '@atproto/api' import {Trans} from '@lingui/react/macro' import {useQueryClient} from '@tanstack/react-query' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {getChatInviteCodeFromUrl} from '#/lib/strings/url-helpers' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useEmbedFallback} from '#/state/queries/embed-fallback' @@ -326,12 +325,7 @@ export function QuoteEmbed({ const t = useTheme() const queryClient = useQueryClient() - const itemUrip = new AtUri(quote.uri) - const isCommunityQuote = - itemUrip.collection === 'community.blacksky.feed.post' - const itemHref = isCommunityQuote - ? `${makeProfileLink(quote.author, 'post', itemUrip.rkey)}?collection=${itemUrip.collection}` - : makeProfileLink(quote.author, 'post', itemUrip.rkey) + const itemHref = postPermalink(quote.author, quote.uri) const itemTitle = `Post by ${quote.author.handle}` const richText = useMemo(() => { diff --git a/src/components/PostControls/PostMenu/PostMenuItems.tsx b/src/components/PostControls/PostMenu/PostMenuItems.tsx index 6323f358f..09c3af88c 100644 --- a/src/components/PostControls/PostMenu/PostMenuItems.tsx +++ b/src/components/PostControls/PostMenu/PostMenuItems.tsx @@ -19,7 +19,7 @@ import {useNavigation} from '@react-navigation/native' import {getCommunityFeedUri} from '#/lib/api/community-post' import {getCurrentRoute} from '#/lib/routes/helpers' -import {makeProfileLink} from '#/lib/routes/links' +import {makeProfileLink, postPermalink} from '#/lib/routes/links' import { type CommonNavigatorParams, type NavigationProp, @@ -190,10 +190,7 @@ let PostMenuItems = ({ }) const href = useMemo(() => { - const urip = new AtUri(postUri) - const link = makeProfileLink(postAuthor, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return isCommunity ? `${link}?collection=${urip.collection}` : link + return postPermalink(postAuthor, postUri) }, [postUri, postAuthor]) const onDeletePost = () => { diff --git a/src/components/PostControls/ShareMenu/ShareMenuItems.tsx b/src/components/PostControls/ShareMenu/ShareMenuItems.tsx index a7b02e547..23b0f8dcd 100644 --- a/src/components/PostControls/ShareMenu/ShareMenuItems.tsx +++ b/src/components/PostControls/ShareMenu/ShareMenuItems.tsx @@ -1,13 +1,12 @@ import {memo, useMemo} from 'react' import * as ExpoClipboard from 'expo-clipboard' -import {AtUri} from '@atproto/api' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' import {Trans} from '@lingui/react/macro' import {useNavigation} from '@react-navigation/native' import {useQueryClient} from '@tanstack/react-query' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {type NavigationProp} from '#/lib/routes/types' import {shareText, shareUrl} from '#/lib/sharing' import {toShareUrl} from '#/lib/strings/url-helpers' @@ -46,10 +45,7 @@ let ShareMenuItems = ({ const postAuthor = useProfileShadow(post.author) const href = useMemo(() => { - const urip = new AtUri(postUri) - const link = makeProfileLink(postAuthor, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return isCommunity ? `${link}?collection=${urip.collection}` : link + return postPermalink(postAuthor, postUri) }, [postUri, postAuthor]) const hideInPWI = useMemo(() => { diff --git a/src/components/PostControls/ShareMenu/ShareMenuItems.web.tsx b/src/components/PostControls/ShareMenu/ShareMenuItems.web.tsx index 963c5bb04..ad10bcafd 100644 --- a/src/components/PostControls/ShareMenu/ShareMenuItems.web.tsx +++ b/src/components/PostControls/ShareMenu/ShareMenuItems.web.tsx @@ -1,11 +1,10 @@ import {memo, useMemo} from 'react' -import {AtUri} from '@atproto/api' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' import {Trans} from '@lingui/react/macro' import {useNavigation} from '@react-navigation/native' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {type NavigationProp} from '#/lib/routes/types' import {shareText, shareUrl} from '#/lib/sharing' import {toShareUrl} from '#/lib/strings/url-helpers' @@ -45,10 +44,7 @@ let ShareMenuItems = ({ const postAuthor = useProfileShadow(post.author) const href = useMemo(() => { - const urip = new AtUri(postUri) - const link = makeProfileLink(postAuthor, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return isCommunity ? `${link}?collection=${urip.collection}` : link + return postPermalink(postAuthor, postUri) }, [postUri, postAuthor]) const hideInPWI = useMemo(() => { diff --git a/src/components/PostControls/ShareMenu/index.tsx b/src/components/PostControls/ShareMenu/index.tsx index 6464d0dd3..7d9536818 100644 --- a/src/components/PostControls/ShareMenu/index.tsx +++ b/src/components/PostControls/ShareMenu/index.tsx @@ -4,13 +4,12 @@ import { type AppBskyFeedDefs, type AppBskyFeedPost, type AppBskyFeedThreadgate, - AtUri, type RichText as RichTextAPI, } from '@atproto/api' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {shareUrl} from '#/lib/sharing' import {toShareUrl} from '#/lib/strings/url-helpers' import {type Shadow} from '#/state/cache/post-shadow' @@ -85,10 +84,7 @@ let ShareMenuButton = ({ const onNativeLongPress = () => { ax.metric('share:press:nativeShare', {}) - const urip = new AtUri(post.uri) - const link = makeProfileLink(post.author, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - const href = isCommunity ? `${link}?collection=${urip.collection}` : link + const href = postPermalink(post.author, post.uri) const url = toShareUrl(href) shareUrl(url) onShare() diff --git a/src/lib/api/__tests__/space-permalink.test.ts b/src/lib/api/__tests__/space-permalink.test.ts new file mode 100644 index 000000000..a0aaa2761 --- /dev/null +++ b/src/lib/api/__tests__/space-permalink.test.ts @@ -0,0 +1,55 @@ +import {postPermalink} from '#/lib/routes/links' +import {spacePostUriFromRoute} from '../space-permalink' + +const SPACE = 'at://did:plc:community/space/community.blacksky.feed/private' +const AUTHOR = {did: 'did:plc:alice', handle: 'alice.test'} +const SPACE_POST = `${SPACE}/did:plc:alice/app.bsky.feed.post/3kabc` + +describe('space permalinks', () => { + it('keeps the ordinary post url and attaches the space', () => { + expect(postPermalink(AUTHOR, SPACE_POST)).toBe( + `/profile/did:plc:alice/post/3kabc?space=${encodeURIComponent(SPACE)}`, + ) + }) + + it('carries a suffix segment before the query', () => { + expect(postPermalink(AUTHOR, SPACE_POST, 'liked-by')).toBe( + `/profile/did:plc:alice/post/3kabc/liked-by?space=${encodeURIComponent( + SPACE, + )}`, + ) + }) + + it.each([ + ['a public post', 'at://did:plc:alice/app.bsky.feed.post/3kabc', ''], + [ + 'the community stub', + 'at://did:plc:alice/community.blacksky.feed.post/3kabc', + '?collection=community.blacksky.feed.post', + ], + ])('leaves %s alone', (_name, uri, query) => { + expect(postPermalink(AUTHOR, uri)).toBe( + `/profile/did:plc:alice/post/3kabc${query}`, + ) + }) + + it('round-trips back to the record uri', () => { + expect(spacePostUriFromRoute(SPACE, 'did:plc:alice', '3kabc')).toBe( + SPACE_POST, + ) + }) + + it.each([ + ['no space', undefined, 'did:plc:alice'], + [ + 'a malformed space', + 'at://did:plc:community/space/onlythree', + 'did:plc:alice', + ], + // A handle cannot stand in for the author: the URI form admits only DIDs, + // so the caller has to resolve it first. + ['an unresolved handle', SPACE, 'alice.test'], + ])('refuses %s', (_name, space, author) => { + expect(spacePostUriFromRoute(space, author, '3kabc')).toBeNull() + }) +}) diff --git a/src/lib/api/community-post.ts b/src/lib/api/community-post.ts index eb4e537b3..a73c8f42e 100644 --- a/src/lib/api/community-post.ts +++ b/src/lib/api/community-post.ts @@ -1,7 +1,15 @@ -import {type AppBskyFeedDefs, AtUri} from '@atproto/api' +import { + type AppBskyFeedDefs, + AtUri, + type BskyAgent, + jsonToLex, +} from '@atproto/api' +import {communityXrpc} from '#/lib/api/community' import {isSpaceRecordUri} from '#/lib/api/space-uri' +export const GET_COMMUNITY_POST = 'community.blacksky.feed.getCommunityPost' + const COMMUNITY_POST_COLLECTION = 'community.blacksky.feed.post' /** @@ -34,3 +42,27 @@ export function getCommunityFeedUri( return undefined } } + +/** + * Read one community post by URI. + * + * The standard `getPosts` cannot serve these: its `uris` are at-uris, and a + * space record URI is not one. This endpoint takes a plain string, and gates + * on membership before returning anything. + */ +export async function fetchCommunityPostView( + agent: BskyAgent, + uri: string, +): Promise { + const res = await communityXrpc(agent, GET_COMMUNITY_POST, {params: {uri}}) + if (!res.ok) { + const body = (await res.json().catch(() => ({}))) as { + message?: string + error?: string + } + throw new Error(body.message || body.error || `HTTP ${res.status}`) + } + const data = jsonToLex(await res.json()) as {post?: AppBskyFeedDefs.PostView} + if (!data.post) throw new Error('Community post not found') + return data.post +} diff --git a/src/lib/api/community-thread.ts b/src/lib/api/community-thread.ts new file mode 100644 index 000000000..c0256da4b --- /dev/null +++ b/src/lib/api/community-thread.ts @@ -0,0 +1,50 @@ +import {type BskyAgent} from '@atproto/api' + +import {communityXrpc} from '#/lib/api/community' + +export const GET_COMMUNITY_THREAD = 'community.blacksky.feed.getCommunityThread' + +export type CommunityThreadResponse = { + thread: unknown[] + hasOtherReplies: boolean + /** Space feeds carry no threadgates (they are deferred), so never set. */ + threadgate?: undefined +} + +/** + * Fetch a thread anchored on a permissioned-space record. + * + * The standard thread endpoint cannot be used: its anchor is declared + * `format: at-uri`, and a space URI is not one, so the request is refused + * before it reaches a handler. This endpoint takes the anchor as a plain + * string and answers with the same item shape. + */ +export async function fetchCommunityThread( + agent: BskyAgent, + params: { + anchor: string + above?: boolean + below?: number + branchingFactor?: number + }, +): Promise { + const query: Record = {anchor: params.anchor} + if (params.above !== undefined) query.above = String(params.above) + if (params.below !== undefined) query.below = String(params.below) + if (params.branchingFactor !== undefined) { + query.branchingFactor = String(params.branchingFactor) + } + const res = await communityXrpc(agent, GET_COMMUNITY_THREAD, {params: query}) + if (!res.ok) { + const body = (await res.json().catch(() => ({}))) as { + message?: string + error?: string + } + throw new Error(body.message || body.error || `HTTP ${res.status}`) + } + const data = (await res.json()) as Partial + return { + thread: data.thread ?? [], + hasOtherReplies: !!data.hasOtherReplies, + } +} diff --git a/src/lib/api/space-permalink.ts b/src/lib/api/space-permalink.ts new file mode 100644 index 000000000..7e4994fc4 --- /dev/null +++ b/src/lib/api/space-permalink.ts @@ -0,0 +1,22 @@ +import {parseSpaceUri, spaceRecordUri} from '#/lib/api/space-uri' +import {POST_COLLECTION} from '#/lib/api/space-write' + +/** + * Rebuild a space post's URI from the parts a route carries. + * + * A space record URI has seven segments, of which the path supplies only the + * author and the rkey; the space itself rides in `?space=`. Returns null when + * the pieces do not add up — including when `author` is still a handle, which + * the caller must resolve to a DID first, since the URI form admits only DIDs. + */ +export function spacePostUriFromRoute( + space: string | undefined, + author: string, + rkey: string, + collection: string = POST_COLLECTION, +): string | null { + if (!space) return null + const ref = parseSpaceUri(space) + if (!ref || !author.startsWith('did:')) return null + return spaceRecordUri({...ref, authorDid: author, collection, rkey}) +} diff --git a/src/lib/routes/links.ts b/src/lib/routes/links.ts index 549e179d4..b1a7e4d94 100644 --- a/src/lib/routes/links.ts +++ b/src/lib/routes/links.ts @@ -1,5 +1,7 @@ import {type AppBskyGraphDefs, AtUri} from '@atproto/api' +import {parseSpaceRecordUri, spaceUriOf} from '#/lib/api/space-uri' + export function makeProfileLink( info: { did: string @@ -10,6 +12,32 @@ export function makeProfileLink( return [`/profile`, info.did, ...segments].join('/') } +/** + * The in-app path for a post, whatever kind it is. + * + * Three shapes share one route: a public post, the community stub (which + * carries its collection as a query param), and a permissioned-space record, + * whose URI has seven segments and cannot be rebuilt from the path alone — + * so the space it belongs to rides along as `?space=`. The author DID and + * rkey come from the path exactly as they always have. + */ +export function postPermalink( + author: {did: string; handle: string}, + postUri: string, + ...segments: string[] +): string { + const spaceRef = parseSpaceRecordUri(postUri) + if (spaceRef) { + const base = makeProfileLink(author, 'post', spaceRef.rkey, ...segments) + return `${base}?space=${encodeURIComponent(spaceUriOf(spaceRef))}` + } + const urip = new AtUri(postUri) + const base = makeProfileLink(author, 'post', urip.rkey, ...segments) + return urip.collection && urip.collection !== 'app.bsky.feed.post' + ? `${base}?collection=${urip.collection}` + : base +} + export function makeCustomFeedLink( did: string, rkey: string, diff --git a/src/lib/routes/types.ts b/src/lib/routes/types.ts index 70c801d2d..adf6baca3 100644 --- a/src/lib/routes/types.ts +++ b/src/lib/routes/types.ts @@ -22,10 +22,15 @@ export type CommonNavigatorParams = { ProfileKnownFollowers: {name: string} ProfileSearch: {name: string; q?: string} ProfileList: {name: string; rkey: string} - PostThread: {name: string; rkey: string; collection?: string} - PostLikedBy: {name: string; rkey: string; collection?: string} - PostRepostedBy: {name: string; rkey: string; collection?: string} - PostQuotes: {name: string; rkey: string; collection?: string} + PostThread: {name: string; rkey: string; collection?: string; space?: string} + PostLikedBy: {name: string; rkey: string; collection?: string; space?: string} + PostRepostedBy: { + name: string + rkey: string + collection?: string + space?: string + } + PostQuotes: {name: string; rkey: string; collection?: string; space?: string} ProfileFeed: { name: string rkey: string diff --git a/src/screens/Messages/components/MessageInputEmbed.tsx b/src/screens/Messages/components/MessageInputEmbed.tsx index 988a57686..414cc0f29 100644 --- a/src/screens/Messages/components/MessageInputEmbed.tsx +++ b/src/screens/Messages/components/MessageInputEmbed.tsx @@ -11,7 +11,7 @@ import {Trans, useLingui} from '@lingui/react/macro' import {type RouteProp, useNavigation, useRoute} from '@react-navigation/native' import {HITSLOP_20} from '#/lib/constants' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import { type CommonNavigatorParams, type NavigationProp, @@ -216,8 +216,8 @@ function MessageInputPostEmbed({ const isCommunityPost = itemUrip.collection === 'community.blacksky.feed.post' const itemHref = isCommunityPost - ? `${makeProfileLink(post.author, 'post', itemUrip.rkey)}?collection=${itemUrip.collection}` - : makeProfileLink(post.author, 'post', itemUrip.rkey) + ? postPermalink(post.author, post.uri) + : postPermalink(post.author, post.uri) if (!post || !moderation || !rt || !record) { return null diff --git a/src/screens/PostThread/components/ThreadItemAnchor.tsx b/src/screens/PostThread/components/ThreadItemAnchor.tsx index 603844807..33092533f 100644 --- a/src/screens/PostThread/components/ThreadItemAnchor.tsx +++ b/src/screens/PostThread/components/ThreadItemAnchor.tsx @@ -10,9 +10,10 @@ import { import {Plural, Trans, useLingui} from '@lingui/react/macro' import {getCommunityFeedUri} from '#/lib/api/community-post' +import {isSpaceRecordUri} from '#/lib/api/space-uri' import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' -import {makeProfileLink} from '#/lib/routes/links' +import {makeProfileLink, postPermalink} from '#/lib/routes/links' import {sanitizeDisplayName} from '#/lib/strings/display-names' import {sanitizeHandle} from '#/lib/strings/handles' import {niceDate} from '#/lib/strings/time' @@ -203,11 +204,11 @@ const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ const statHref = useCallback( (suffix: string) => { - const urip = new AtUri(post.uri) - const link = makeProfileLink(post.author, 'post', urip.rkey, suffix) - return urip.collection === 'community.blacksky.feed.post' - ? `${link}?collection=${urip.collection}` - : link + // Space posts have no liked-by / reposted-by / quotes screens: those + // endpoints take at-uris, and reposts and quotes are deferred there + // anyway. The counts still show, they just do not link anywhere. + if (isSpaceRecordUri(post.uri)) return undefined + return postPermalink(post.author, post.uri, suffix) }, [post.uri, post.author], ) @@ -448,7 +449,7 @@ const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ t.atoms.border_contrast_low, ]}> {post.repostCount != null && post.repostCount !== 0 ? ( - + @@ -464,12 +465,12 @@ const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ /> - + ) : null} {post.quoteCount != null && post.quoteCount !== 0 && !post.viewer?.embeddingDisabled ? ( - + @@ -485,10 +486,10 @@ const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ /> - + ) : null} {post.likeCount != null && post.likeCount !== 0 ? ( - + @@ -504,7 +505,7 @@ const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ /> - + ) : null} {post.bookmarkCount != null && post.bookmarkCount !== 0 ? ( ) } + +/** + * A stat that links to its own screen when there is one to link to. + * + * Space posts have no liked-by / reposted-by / quotes screens, so the count + * renders as plain text rather than a link that would 400. + */ +function StatLink({ + to, + label, + children, +}: { + to?: string + label: string + children: React.ReactElement +}) { + if (!to) return children + return ( + + {children} + + ) +} diff --git a/src/screens/PostThread/components/ThreadItemPost.tsx b/src/screens/PostThread/components/ThreadItemPost.tsx index 9e4c5eb63..d3d89f9d1 100644 --- a/src/screens/PostThread/components/ThreadItemPost.tsx +++ b/src/screens/PostThread/components/ThreadItemPost.tsx @@ -11,7 +11,7 @@ import {Trans} from '@lingui/react/macro' import {getCommunityFeedUri} from '#/lib/api/community-post' import {MAX_POST_LINES} from '#/lib/constants' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {countLines} from '#/lib/strings/helpers' import { POST_TOMBSTONE, @@ -212,10 +212,7 @@ const ThreadItemPostInner = memo(function ThreadItemPostInner({ ) const threadRootUri = record.reply?.root?.uri || post.uri const postHref = useMemo(() => { - const urip = new AtUri(post.uri) - const link = makeProfileLink(post.author, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return isCommunity ? `${link}?collection=${urip.collection}` : link + return postPermalink(post.author, post.uri) }, [post.uri, post.author]) const threadgateHiddenReplies = useMergedThreadgateHiddenReplies({ threadgateRecord, diff --git a/src/screens/PostThread/components/ThreadItemTreePost.tsx b/src/screens/PostThread/components/ThreadItemTreePost.tsx index c29931f2c..28a928907 100644 --- a/src/screens/PostThread/components/ThreadItemTreePost.tsx +++ b/src/screens/PostThread/components/ThreadItemTreePost.tsx @@ -11,7 +11,7 @@ import {Trans} from '@lingui/react/macro' import {getCommunityFeedUri} from '#/lib/api/community-post' import {MAX_POST_LINES} from '#/lib/constants' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {countLines} from '#/lib/strings/helpers' import { POST_TOMBSTONE, @@ -274,10 +274,7 @@ const ThreadItemTreePostInner = memo(function ThreadItemTreePostInner({ ) const threadRootUri = record.reply?.root?.uri || post.uri const postHref = useMemo(() => { - const urip = new AtUri(post.uri) - const link = makeProfileLink(post.author, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return isCommunity ? `${link}?collection=${urip.collection}` : link + return postPermalink(post.author, post.uri) }, [post.uri, post.author]) const threadgateHiddenReplies = useMergedThreadgateHiddenReplies({ threadgateRecord, diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts index d545f93d3..fdcaed9cc 100644 --- a/src/state/queries/post.ts +++ b/src/state/queries/post.ts @@ -8,6 +8,8 @@ import { } from '@tanstack/react-query' import {communityXrpc} from '#/lib/api/community' +import {fetchCommunityPostView} from '#/lib/api/community-post' +import {isSpaceRecordUri} from '#/lib/api/space-uri' import { spaceDeleteIfSpace, spaceLikeIfSpace, @@ -33,6 +35,12 @@ export function usePostQuery(uri: string | undefined) { queryFn: async () => { if (!uri) throw new Error('[unreachable] No URI provided') + // Checked before AtUri, which misparses a space URI, and before + // getPosts, whose uris are at-uris. + if (isSpaceRecordUri(uri)) { + return fetchCommunityPostView(agent, uri) + } + const urip = new AtUri(uri) if (!urip.host.startsWith('did:')) { @@ -70,6 +78,9 @@ export function useGetPost() { return queryClient.fetchQuery({ queryKey: RQKEY(uri || ''), async queryFn() { + if (isSpaceRecordUri(uri)) { + return fetchCommunityPostView(agent, uri) + } const urip = new AtUri(uri) if (!urip.host.startsWith('did:')) { diff --git a/src/state/queries/usePostThread/index.ts b/src/state/queries/usePostThread/index.ts index ceb87fe47..5ab6ccf25 100644 --- a/src/state/queries/usePostThread/index.ts +++ b/src/state/queries/usePostThread/index.ts @@ -1,6 +1,8 @@ import {useCallback, useMemo, useState} from 'react' import {useQuery, useQueryClient} from '@tanstack/react-query' +import {fetchCommunityThread} from '#/lib/api/community-thread' +import {isSpaceRecordUri} from '#/lib/api/space-uri' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useThreadPreferences} from '#/state/queries/preferences/useThreadPreferences' import { @@ -71,12 +73,24 @@ export function usePostThread({anchor}: {anchor?: string}) { enabled: isThreadPreferencesLoaded && !!anchor && !!moderationOpts, queryKey: postThreadQueryKey, async queryFn(ctx) { - const {data} = await agent.app.bsky.unspecced.getPostThreadV2({ - anchor: anchor!, - branchingFactor: view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF, - below, - sort: sort, - }) + const branchingFactor = view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF + // A space record cannot be an anchor on the standard endpoint: that + // parameter is an at-uri and a space URI is not one. The community + // endpoint answers with the same item shape. + const data = isSpaceRecordUri(anchor) + ? await fetchCommunityThread(agent, { + anchor: anchor!, + branchingFactor, + below, + }) + : ( + await agent.app.bsky.unspecced.getPostThreadV2({ + anchor: anchor!, + branchingFactor, + below, + sort: sort, + }) + ).data /* * Initialize `ctx.meta` to track if we know we have additional replies diff --git a/src/view/com/post/Post.tsx b/src/view/com/post/Post.tsx index 87107f23a..5e610fc86 100644 --- a/src/view/com/post/Post.tsx +++ b/src/view/com/post/Post.tsx @@ -13,7 +13,7 @@ import {useQueryClient} from '@tanstack/react-query' import {getCommunityFeedUri} from '#/lib/api/community-post' import {MAX_POST_LINES} from '#/lib/constants' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {countLines} from '#/lib/strings/helpers' import { POST_TOMBSTONE, @@ -123,11 +123,7 @@ function PostInner({ const [limitLines, setLimitLines] = useState( () => countLines(richText?.text) >= MAX_POST_LINES, ) - const itemUrip = new AtUri(post.uri) - const isCommunityPost = itemUrip.collection === 'community.blacksky.feed.post' - const itemHref = isCommunityPost - ? `${makeProfileLink(post.author, 'post', itemUrip.rkey)}?collection=${itemUrip.collection}` - : makeProfileLink(post.author, 'post', itemUrip.rkey) + const itemHref = postPermalink(post.author, post.uri) let replyAuthorDid = '' if (record.reply) { const urip = new AtUri(record.reply.parent?.uri || record.reply.root.uri) diff --git a/src/view/com/posts/PostFeedItem.tsx b/src/view/com/posts/PostFeedItem.tsx index 0897791fb..472e488a0 100644 --- a/src/view/com/posts/PostFeedItem.tsx +++ b/src/view/com/posts/PostFeedItem.tsx @@ -16,7 +16,7 @@ import {type ReasonFeedSource} from '#/lib/api/feed/types' import {MAX_POST_LINES} from '#/lib/constants' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' import {usePalette} from '#/lib/hooks/usePalette' -import {makeProfileLink} from '#/lib/routes/links' +import {postPermalink} from '#/lib/routes/links' import {countLines} from '#/lib/strings/helpers' import { POST_TOMBSTONE, @@ -175,12 +175,8 @@ let FeedItemInner = ({ const [href] = useMemo(() => { const urip = new AtUri(post.uri) - const link = makeProfileLink(post.author, 'post', urip.rkey) - const isCommunity = urip.collection === 'community.blacksky.feed.post' - return [ - isCommunity ? `${link}?collection=${urip.collection}` : link, - urip.rkey, - ] + const link = postPermalink(post.author, post.uri) + return [link, urip.rkey] }, [post.uri, post.author]) const {sendInteraction, feedSourceInfo, feedDescriptor} = useFeedFeedbackContext() diff --git a/src/view/screens/PostThread.tsx b/src/view/screens/PostThread.tsx index c4ba6b7d3..8cd0e4816 100644 --- a/src/view/screens/PostThread.tsx +++ b/src/view/screens/PostThread.tsx @@ -1,8 +1,10 @@ +import {spacePostUriFromRoute} from '#/lib/api/space-permalink' import { type CommonNavigatorParams, type NativeStackScreenProps, } from '#/lib/routes/types' import {makeRecordUri} from '#/lib/strings/url-helpers' +import {useResolveDidQuery} from '#/state/queries/resolve-uri' import {PostThread} from '#/screens/PostThread' import * as Layout from '#/components/Layout' @@ -18,11 +20,48 @@ export function PostThreadScreen({route}: Props) { collection = collection || qs.get('collection') || undefined rkey = rkey.slice(0, qIndex) } - const uri = makeRecordUri(name, collection || 'app.bsky.feed.post', rkey) + const space = route.params.space return ( - + ) } + +function PostThreadRoute({ + name, + rkey, + collection, + space, +}: { + name: string + rkey: string + collection?: string + space?: string +}) { + // A space URI admits only DIDs in its author segment, so a handle in the + // path has to be resolved before the URI can be assembled. Links the app + // builds already carry DIDs; this covers hand-typed and shared URLs. + const needsDid = !!space && !name.startsWith('did:') + const {data: resolvedDid, isPending} = useResolveDidQuery( + needsDid ? name : undefined, + ) + if (needsDid && isPending) return null + + const author = needsDid ? resolvedDid : name + // Falling back to the ordinary URI when the space parts do not add up gives + // the thread's own not-found state rather than a blank screen. + const uri = + (space && + author && + spacePostUriFromRoute(space, author, rkey, collection)) || + makeRecordUri(name, collection || 'app.bsky.feed.post', rkey) + + return +}