diff --git a/src/lib/link-meta/bsky.ts b/src/lib/link-meta/bsky.ts index 2134c32920..78d755331c 100644 --- a/src/lib/link-meta/bsky.ts +++ b/src/lib/link-meta/bsky.ts @@ -1,10 +1,11 @@ +import {AppBskyFeedPost} from '@atproto/api' import * as apilib from 'lib/api/index' import {LikelyType, LinkMeta} from './link-meta' // import {match as matchRoute} from 'view/routes' import {convertBskyAppUrlIfNeeded, makeRecordUri} from '../strings/url-helpers' import {RootStoreModel} from 'state/index' -import {PostThreadModel} from 'state/models/content/post-thread' import {ComposerOptsQuote} from 'state/shell/composer' +import {useGetPost} from '#/state/queries/post' // TODO // import {Home} from 'view/screens/Home' @@ -102,27 +103,19 @@ export async function extractBskyMeta( } export async function getPostAsQuote( - store: RootStoreModel, + getPost: ReturnType, url: string, ): Promise { url = convertBskyAppUrlIfNeeded(url) const [_0, user, _1, rkey] = url.split('/').filter(Boolean) - const threadUri = makeRecordUri(user, 'app.bsky.feed.post', rkey) - - const threadView = new PostThreadModel(store, { - uri: threadUri, - depth: 0, - }) - await threadView.setup() - if (!threadView.thread || threadView.notFound) { - throw new Error('Not found') - } + const uri = makeRecordUri(user, 'app.bsky.feed.post', rkey) + const post = await getPost({uri: uri}) return { - uri: threadView.thread.post.uri, - cid: threadView.thread.post.cid, - text: threadView.thread.postRecord?.text || '', - indexedAt: threadView.thread.post.indexedAt, - author: threadView.thread.post.author, + uri: post.uri, + cid: post.cid, + text: AppBskyFeedPost.isRecord(post.record) ? post.record.text : '', + indexedAt: post.indexedAt, + author: post.author, } } diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts index ffff7f9675..2aaa9a3476 100644 --- a/src/state/queries/post.ts +++ b/src/state/queries/post.ts @@ -1,5 +1,6 @@ -import {AppBskyFeedDefs} from '@atproto/api' -import {useQuery, useMutation} from '@tanstack/react-query' +import React from 'react' +import {AppBskyFeedDefs, AtUri} from '@atproto/api' +import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query' import {useSession} from '../session' import {updatePostShadow} from '../cache/post-shadow' @@ -21,6 +22,39 @@ export function usePostQuery(uri: string | undefined) { }) } +export function useGetPost() { + const queryClient = useQueryClient() + const {agent} = useSession() + return React.useCallback( + async ({uri}: {uri: string}) => { + return queryClient.fetchQuery({ + queryKey: RQKEY(uri || ''), + async queryFn() { + const urip = new AtUri(uri) + + if (!urip.host.startsWith('did:')) { + const res = await agent.resolveHandle({ + handle: urip.host, + }) + urip.host = res.data.did + } + + const res = await agent.getPosts({ + uris: [urip.toString()!], + }) + + if (res.success && res.data.posts[0]) { + return res.data.posts[0] + } + + throw new Error('useGetPost: post not found') + }, + }) + }, + [agent, queryClient], + ) +} + export function usePostLikeMutation() { const {agent} = useSession() return useMutation< diff --git a/src/view/com/composer/useExternalLinkFetch.ts b/src/view/com/composer/useExternalLinkFetch.ts index 274b4dff6b..0c96e4727a 100644 --- a/src/view/com/composer/useExternalLinkFetch.ts +++ b/src/view/com/composer/useExternalLinkFetch.ts @@ -17,6 +17,7 @@ import { import {ComposerOpts} from 'state/shell/composer' import {POST_IMG_MAX} from 'lib/constants' import {logger} from '#/logger' +import {useGetPost} from '#/state/queries/post' export function useExternalLinkFetch({ setQuote, @@ -27,6 +28,7 @@ export function useExternalLinkFetch({ const [extLink, setExtLink] = useState( undefined, ) + const getPost = useGetPost() useEffect(() => { let aborted = false @@ -38,7 +40,7 @@ export function useExternalLinkFetch({ } if (!extLink.meta) { if (isBskyPostUrl(extLink.uri)) { - getPostAsQuote(store, extLink.uri).then( + getPostAsQuote(getPost, extLink.uri).then( newQuote => { if (aborted) { return @@ -48,7 +50,7 @@ export function useExternalLinkFetch({ }, err => { logger.error('Failed to fetch post for quote embedding', { - error: err, + error: err.toString(), }) setExtLink(undefined) }, @@ -132,7 +134,7 @@ export function useExternalLinkFetch({ }) } return cleanup - }, [store, extLink, setQuote]) + }, [store, extLink, setQuote, getPost]) return {extLink, setExtLink} }