Skip to content

Commit

Permalink
Update post embed fetching to use new methods (#1916)
Browse files Browse the repository at this point in the history
* Update post embed fetching to use new methods

* Use session agent
  • Loading branch information
estrattonbailey authored Nov 16, 2023
1 parent 9bcd00b commit f23e997
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
27 changes: 10 additions & 17 deletions src/lib/link-meta/bsky.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
}
}

Expand Down
38 changes: 36 additions & 2 deletions src/state/queries/post.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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<
Expand Down
8 changes: 5 additions & 3 deletions src/view/com/composer/useExternalLinkFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +28,7 @@ export function useExternalLinkFetch({
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
undefined,
)
const getPost = useGetPost()

useEffect(() => {
let aborted = false
Expand All @@ -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
Expand All @@ -48,7 +50,7 @@ export function useExternalLinkFetch({
},
err => {
logger.error('Failed to fetch post for quote embedding', {
error: err,
error: err.toString(),
})
setExtLink(undefined)
},
Expand Down Expand Up @@ -132,7 +134,7 @@ export function useExternalLinkFetch({
})
}
return cleanup
}, [store, extLink, setQuote])
}, [store, extLink, setQuote, getPost])

return {extLink, setExtLink}
}

0 comments on commit f23e997

Please sign in to comment.