about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2023-11-15 19:09:13 -0600
committerGitHub <noreply@github.com>2023-11-15 17:09:13 -0800
commitf23e9978d839322aab7304d2b6f183722e3ad4c1 (patch)
treea21f5e4056182c3f9b3bff6ef16246d4100a23d0 /src
parent9bcd00b83174cca3baef0519ba688e403662eea5 (diff)
downloadvoidsky-f23e9978d839322aab7304d2b6f183722e3ad4c1.tar.zst
Update post embed fetching to use new methods (#1916)
* Update post embed fetching to use new methods

* Use session agent
Diffstat (limited to 'src')
-rw-r--r--src/lib/link-meta/bsky.ts27
-rw-r--r--src/state/queries/post.ts38
-rw-r--r--src/view/com/composer/useExternalLinkFetch.ts8
3 files changed, 51 insertions, 22 deletions
diff --git a/src/lib/link-meta/bsky.ts b/src/lib/link-meta/bsky.ts
index 2134c3292..78d755331 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<typeof useGetPost>,
   url: string,
 ): Promise<ComposerOptsQuote> {
   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 ffff7f967..2aaa9a347 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 274b4dff6..0c96e4727 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<apilib.ExternalEmbedDraft | undefined>(
     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}
 }