Update post embed fetching to use new methods (#1916)
* Update post embed fetching to use new methods * Use session agent
This commit is contained in:
parent
9bcd00b831
commit
f23e9978d8
3 changed files with 51 additions and 22 deletions
|
@ -1,10 +1,11 @@
|
||||||
|
import {AppBskyFeedPost} from '@atproto/api'
|
||||||
import * as apilib from 'lib/api/index'
|
import * as apilib from 'lib/api/index'
|
||||||
import {LikelyType, LinkMeta} from './link-meta'
|
import {LikelyType, LinkMeta} from './link-meta'
|
||||||
// import {match as matchRoute} from 'view/routes'
|
// import {match as matchRoute} from 'view/routes'
|
||||||
import {convertBskyAppUrlIfNeeded, makeRecordUri} from '../strings/url-helpers'
|
import {convertBskyAppUrlIfNeeded, makeRecordUri} from '../strings/url-helpers'
|
||||||
import {RootStoreModel} from 'state/index'
|
import {RootStoreModel} from 'state/index'
|
||||||
import {PostThreadModel} from 'state/models/content/post-thread'
|
|
||||||
import {ComposerOptsQuote} from 'state/shell/composer'
|
import {ComposerOptsQuote} from 'state/shell/composer'
|
||||||
|
import {useGetPost} from '#/state/queries/post'
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// import {Home} from 'view/screens/Home'
|
// import {Home} from 'view/screens/Home'
|
||||||
|
@ -102,27 +103,19 @@ export async function extractBskyMeta(
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPostAsQuote(
|
export async function getPostAsQuote(
|
||||||
store: RootStoreModel,
|
getPost: ReturnType<typeof useGetPost>,
|
||||||
url: string,
|
url: string,
|
||||||
): Promise<ComposerOptsQuote> {
|
): Promise<ComposerOptsQuote> {
|
||||||
url = convertBskyAppUrlIfNeeded(url)
|
url = convertBskyAppUrlIfNeeded(url)
|
||||||
const [_0, user, _1, rkey] = url.split('/').filter(Boolean)
|
const [_0, user, _1, rkey] = url.split('/').filter(Boolean)
|
||||||
const threadUri = makeRecordUri(user, 'app.bsky.feed.post', rkey)
|
const uri = makeRecordUri(user, 'app.bsky.feed.post', rkey)
|
||||||
|
const post = await getPost({uri: uri})
|
||||||
const threadView = new PostThreadModel(store, {
|
|
||||||
uri: threadUri,
|
|
||||||
depth: 0,
|
|
||||||
})
|
|
||||||
await threadView.setup()
|
|
||||||
if (!threadView.thread || threadView.notFound) {
|
|
||||||
throw new Error('Not found')
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
uri: threadView.thread.post.uri,
|
uri: post.uri,
|
||||||
cid: threadView.thread.post.cid,
|
cid: post.cid,
|
||||||
text: threadView.thread.postRecord?.text || '',
|
text: AppBskyFeedPost.isRecord(post.record) ? post.record.text : '',
|
||||||
indexedAt: threadView.thread.post.indexedAt,
|
indexedAt: post.indexedAt,
|
||||||
author: threadView.thread.post.author,
|
author: post.author,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import {AppBskyFeedDefs} from '@atproto/api'
|
import React from 'react'
|
||||||
import {useQuery, useMutation} from '@tanstack/react-query'
|
import {AppBskyFeedDefs, AtUri} from '@atproto/api'
|
||||||
|
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
|
||||||
import {useSession} from '../session'
|
import {useSession} from '../session'
|
||||||
import {updatePostShadow} from '../cache/post-shadow'
|
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() {
|
export function usePostLikeMutation() {
|
||||||
const {agent} = useSession()
|
const {agent} = useSession()
|
||||||
return useMutation<
|
return useMutation<
|
||||||
|
|
|
@ -17,6 +17,7 @@ import {
|
||||||
import {ComposerOpts} from 'state/shell/composer'
|
import {ComposerOpts} from 'state/shell/composer'
|
||||||
import {POST_IMG_MAX} from 'lib/constants'
|
import {POST_IMG_MAX} from 'lib/constants'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
|
import {useGetPost} from '#/state/queries/post'
|
||||||
|
|
||||||
export function useExternalLinkFetch({
|
export function useExternalLinkFetch({
|
||||||
setQuote,
|
setQuote,
|
||||||
|
@ -27,6 +28,7 @@ export function useExternalLinkFetch({
|
||||||
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
|
const [extLink, setExtLink] = useState<apilib.ExternalEmbedDraft | undefined>(
|
||||||
undefined,
|
undefined,
|
||||||
)
|
)
|
||||||
|
const getPost = useGetPost()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let aborted = false
|
let aborted = false
|
||||||
|
@ -38,7 +40,7 @@ export function useExternalLinkFetch({
|
||||||
}
|
}
|
||||||
if (!extLink.meta) {
|
if (!extLink.meta) {
|
||||||
if (isBskyPostUrl(extLink.uri)) {
|
if (isBskyPostUrl(extLink.uri)) {
|
||||||
getPostAsQuote(store, extLink.uri).then(
|
getPostAsQuote(getPost, extLink.uri).then(
|
||||||
newQuote => {
|
newQuote => {
|
||||||
if (aborted) {
|
if (aborted) {
|
||||||
return
|
return
|
||||||
|
@ -48,7 +50,7 @@ export function useExternalLinkFetch({
|
||||||
},
|
},
|
||||||
err => {
|
err => {
|
||||||
logger.error('Failed to fetch post for quote embedding', {
|
logger.error('Failed to fetch post for quote embedding', {
|
||||||
error: err,
|
error: err.toString(),
|
||||||
})
|
})
|
||||||
setExtLink(undefined)
|
setExtLink(undefined)
|
||||||
},
|
},
|
||||||
|
@ -132,7 +134,7 @@ export function useExternalLinkFetch({
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return cleanup
|
return cleanup
|
||||||
}, [store, extLink, setQuote])
|
}, [store, extLink, setQuote, getPost])
|
||||||
|
|
||||||
return {extLink, setExtLink}
|
return {extLink, setExtLink}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue