remove resolution from post thread (#4297)

* remove resolution from post thread

nit

completely remove did cache lookup

move cache check for did to `usePostThreadQuery`

remove resolution from post thread

* helper function

* simplify

* simplify search too

* fix missing check for root or parent quoted post 🤯

* fix thread traversal
zio/stable
Hailey 2024-06-03 15:58:16 -07:00 committed by GitHub
parent 21d4d5f600
commit 8d8323421c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 112 additions and 65 deletions

View File

@ -17,7 +17,7 @@
*/ */
import {useEffect, useRef} from 'react' import {useEffect, useRef} from 'react'
import {AppBskyActorDefs, AppBskyFeedDefs} from '@atproto/api' import {AppBskyActorDefs, AppBskyFeedDefs, AtUri} from '@atproto/api'
import { import {
InfiniteData, InfiniteData,
QueryClient, QueryClient,
@ -30,7 +30,11 @@ import {useMutedThreads} from '#/state/muted-threads'
import {useAgent} from '#/state/session' import {useAgent} from '#/state/session'
import {useModerationOpts} from '../../preferences/moderation-opts' import {useModerationOpts} from '../../preferences/moderation-opts'
import {STALE} from '..' import {STALE} from '..'
import {embedViewRecordToPostView, getEmbeddedPost} from '../util' import {
didOrHandleUriMatches,
embedViewRecordToPostView,
getEmbeddedPost,
} from '../util'
import {FeedPage} from './types' import {FeedPage} from './types'
import {useUnreadNotificationsApi} from './unread' import {useUnreadNotificationsApi} from './unread'
import {fetchPage} from './util' import {fetchPage} from './util'
@ -142,6 +146,8 @@ export function* findAllPostsInQueryData(
queryClient: QueryClient, queryClient: QueryClient,
uri: string, uri: string,
): Generator<AppBskyFeedDefs.PostView, void> { ): Generator<AppBskyFeedDefs.PostView, void> {
const atUri = new AtUri(uri)
const queryDatas = queryClient.getQueriesData<InfiniteData<FeedPage>>({ const queryDatas = queryClient.getQueriesData<InfiniteData<FeedPage>>({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })
@ -149,14 +155,16 @@ export function* findAllPostsInQueryData(
if (!queryData?.pages) { if (!queryData?.pages) {
continue continue
} }
for (const page of queryData?.pages) { for (const page of queryData?.pages) {
for (const item of page.items) { for (const item of page.items) {
if (item.subject?.uri === uri) { if (item.subject && didOrHandleUriMatches(atUri, item.subject)) {
yield item.subject yield item.subject
} }
const quotedPost = getEmbeddedPost(item.subject?.embed) const quotedPost = getEmbeddedPost(item.subject?.embed)
if (quotedPost?.uri === uri) { if (quotedPost && didOrHandleUriMatches(atUri, quotedPost)) {
yield embedViewRecordToPostView(quotedPost) yield embedViewRecordToPostView(quotedPost!)
} }
} }
} }

View File

@ -35,7 +35,11 @@ import {KnownError} from '#/view/com/posts/FeedErrorMessage'
import {useFeedTuners} from '../preferences/feed-tuners' import {useFeedTuners} from '../preferences/feed-tuners'
import {useModerationOpts} from '../preferences/moderation-opts' import {useModerationOpts} from '../preferences/moderation-opts'
import {usePreferencesQuery} from './preferences' import {usePreferencesQuery} from './preferences'
import {embedViewRecordToPostView, getEmbeddedPost} from './util' import {
didOrHandleUriMatches,
embedViewRecordToPostView,
getEmbeddedPost,
} from './util'
type ActorDid = string type ActorDid = string
type AuthorFilter = type AuthorFilter =
@ -448,6 +452,8 @@ export function* findAllPostsInQueryData(
queryClient: QueryClient, queryClient: QueryClient,
uri: string, uri: string,
): Generator<AppBskyFeedDefs.PostView, undefined> { ): Generator<AppBskyFeedDefs.PostView, undefined> {
const atUri = new AtUri(uri)
const queryDatas = queryClient.getQueriesData< const queryDatas = queryClient.getQueriesData<
InfiniteData<FeedPageUnselected> InfiniteData<FeedPageUnselected>
>({ >({
@ -459,24 +465,38 @@ export function* findAllPostsInQueryData(
} }
for (const page of queryData?.pages) { for (const page of queryData?.pages) {
for (const item of page.feed) { for (const item of page.feed) {
if (item.post.uri === uri) { if (didOrHandleUriMatches(atUri, item.post)) {
yield item.post yield item.post
} }
const quotedPost = getEmbeddedPost(item.post.embed) const quotedPost = getEmbeddedPost(item.post.embed)
if (quotedPost?.uri === uri) { if (quotedPost && didOrHandleUriMatches(atUri, quotedPost)) {
yield embedViewRecordToPostView(quotedPost) yield embedViewRecordToPostView(quotedPost)
} }
if (
AppBskyFeedDefs.isPostView(item.reply?.parent) && if (AppBskyFeedDefs.isPostView(item.reply?.parent)) {
item.reply?.parent?.uri === uri if (didOrHandleUriMatches(atUri, item.reply.parent)) {
) { yield item.reply.parent
yield item.reply.parent }
const parentQuotedPost = getEmbeddedPost(item.reply.parent.embed)
if (
parentQuotedPost &&
didOrHandleUriMatches(atUri, parentQuotedPost)
) {
yield embedViewRecordToPostView(parentQuotedPost)
}
} }
if (
AppBskyFeedDefs.isPostView(item.reply?.root) && if (AppBskyFeedDefs.isPostView(item.reply?.root)) {
item.reply?.root?.uri === uri if (didOrHandleUriMatches(atUri, item.reply.root)) {
) { yield item.reply.root
yield item.reply.root }
const rootQuotedPost = getEmbeddedPost(item.reply.root.embed)
if (rootQuotedPost && didOrHandleUriMatches(atUri, rootQuotedPost)) {
yield embedViewRecordToPostView(rootQuotedPost)
}
} }
} }
} }

View File

@ -4,6 +4,7 @@ import {
AppBskyFeedDefs, AppBskyFeedDefs,
AppBskyFeedGetPostThread, AppBskyFeedGetPostThread,
AppBskyFeedPost, AppBskyFeedPost,
AtUri,
ModerationDecision, ModerationDecision,
ModerationOpts, ModerationOpts,
} from '@atproto/api' } from '@atproto/api'
@ -24,7 +25,11 @@ import {
findAllPostsInQueryData as findAllPostsInFeedQueryData, findAllPostsInQueryData as findAllPostsInFeedQueryData,
findAllProfilesInQueryData as findAllProfilesInFeedQueryData, findAllProfilesInQueryData as findAllProfilesInFeedQueryData,
} from './post-feed' } from './post-feed'
import {embedViewRecordToPostView, getEmbeddedPost} from './util' import {
didOrHandleUriMatches,
embedViewRecordToPostView,
getEmbeddedPost,
} from './util'
const RQKEY_ROOT = 'post-thread' const RQKEY_ROOT = 'post-thread'
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri] export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
@ -91,14 +96,10 @@ export function usePostThreadQuery(uri: string | undefined) {
}, },
enabled: !!uri, enabled: !!uri,
placeholderData: () => { placeholderData: () => {
if (!uri) { if (!uri) return
return undefined const post = findPostInQueryData(queryClient, uri)
} if (post) {
{ return post
const post = findPostInQueryData(queryClient, uri)
if (post) {
return post
}
} }
return undefined return undefined
}, },
@ -271,6 +272,8 @@ export function* findAllPostsInQueryData(
queryClient: QueryClient, queryClient: QueryClient,
uri: string, uri: string,
): Generator<ThreadNode, void> { ): Generator<ThreadNode, void> {
const atUri = new AtUri(uri)
const queryDatas = queryClient.getQueriesData<ThreadNode>({ const queryDatas = queryClient.getQueriesData<ThreadNode>({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })
@ -279,7 +282,7 @@ export function* findAllPostsInQueryData(
continue continue
} }
for (const item of traverseThread(queryData)) { for (const item of traverseThread(queryData)) {
if (item.uri === uri) { if (item.type === 'post' && didOrHandleUriMatches(atUri, item.post)) {
const placeholder = threadNodeToPlaceholderThread(item) const placeholder = threadNodeToPlaceholderThread(item)
if (placeholder) { if (placeholder) {
yield placeholder yield placeholder
@ -287,7 +290,7 @@ export function* findAllPostsInQueryData(
} }
const quotedPost = const quotedPost =
item.type === 'post' ? getEmbeddedPost(item.post.embed) : undefined item.type === 'post' ? getEmbeddedPost(item.post.embed) : undefined
if (quotedPost?.uri === uri) { if (quotedPost && didOrHandleUriMatches(atUri, quotedPost)) {
yield embedViewRecordToPlaceholderThread(quotedPost) yield embedViewRecordToPlaceholderThread(quotedPost)
} }
} }

View File

@ -2,6 +2,7 @@ import {
AppBskyActorDefs, AppBskyActorDefs,
AppBskyFeedDefs, AppBskyFeedDefs,
AppBskyFeedSearchPosts, AppBskyFeedSearchPosts,
AtUri,
} from '@atproto/api' } from '@atproto/api'
import { import {
InfiniteData, InfiniteData,
@ -11,7 +12,11 @@ import {
} from '@tanstack/react-query' } from '@tanstack/react-query'
import {useAgent} from '#/state/session' import {useAgent} from '#/state/session'
import {embedViewRecordToPostView, getEmbeddedPost} from './util' import {
didOrHandleUriMatches,
embedViewRecordToPostView,
getEmbeddedPost,
} from './util'
const searchPostsQueryKeyRoot = 'search-posts' const searchPostsQueryKeyRoot = 'search-posts'
const searchPostsQueryKey = ({query, sort}: {query: string; sort?: string}) => [ const searchPostsQueryKey = ({query, sort}: {query: string; sort?: string}) => [
@ -62,17 +67,20 @@ export function* findAllPostsInQueryData(
>({ >({
queryKey: [searchPostsQueryKeyRoot], queryKey: [searchPostsQueryKeyRoot],
}) })
const atUri = new AtUri(uri)
for (const [_queryKey, queryData] of queryDatas) { for (const [_queryKey, queryData] of queryDatas) {
if (!queryData?.pages) { if (!queryData?.pages) {
continue continue
} }
for (const page of queryData?.pages) { for (const page of queryData?.pages) {
for (const post of page.posts) { for (const post of page.posts) {
if (post.uri === uri) { if (didOrHandleUriMatches(atUri, post)) {
yield post yield post
} }
const quotedPost = getEmbeddedPost(post.embed) const quotedPost = getEmbeddedPost(post.embed)
if (quotedPost?.uri === uri) { if (quotedPost && didOrHandleUriMatches(atUri, quotedPost)) {
yield embedViewRecordToPostView(quotedPost) yield embedViewRecordToPostView(quotedPost)
} }
} }

View File

@ -1,8 +1,10 @@
import { import {
AppBskyActorDefs,
AppBskyEmbedRecord, AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia, AppBskyEmbedRecordWithMedia,
AppBskyFeedDefs, AppBskyFeedDefs,
AppBskyFeedPost, AppBskyFeedPost,
AtUri,
} from '@atproto/api' } from '@atproto/api'
import {InfiniteData, QueryClient, QueryKey} from '@tanstack/react-query' import {InfiniteData, QueryClient, QueryKey} from '@tanstack/react-query'
@ -22,6 +24,23 @@ export function truncateAndInvalidate<T = any>(
queryClient.invalidateQueries({queryKey}) queryClient.invalidateQueries({queryKey})
} }
// Given an AtUri, this function will check if the AtUri matches a
// hit regardless of whether the AtUri uses a DID or handle as a host.
//
// AtUri should be the URI that is being searched for, while currentUri
// is the URI that is being checked. currentAuthor is the author
// of the currentUri that is being checked.
export function didOrHandleUriMatches(
atUri: AtUri,
record: {uri: string; author: AppBskyActorDefs.ProfileViewBasic},
) {
if (atUri.host.startsWith('did:')) {
return atUri.href === record.uri
}
return atUri.host === record.author.handle && record.uri.endsWith(atUri.rkey)
}
export function getEmbeddedPost( export function getEmbeddedPost(
v: unknown, v: unknown,
): AppBskyEmbedRecord.ViewRecord | undefined { ): AppBskyEmbedRecord.ViewRecord | undefined {

View File

@ -1,28 +1,26 @@
import React from 'react' import React from 'react'
import {StyleSheet, View} from 'react-native' import {StyleSheet, View} from 'react-native'
import Animated from 'react-native-reanimated' import Animated from 'react-native-reanimated'
import {useSafeAreaInsets} from 'react-native-safe-area-context'
import {useFocusEffect} from '@react-navigation/native' import {useFocusEffect} from '@react-navigation/native'
import {useQueryClient} from '@tanstack/react-query' import {useQueryClient} from '@tanstack/react-query'
import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types' import {clamp} from 'lodash'
import {makeRecordUri} from 'lib/strings/url-helpers'
import {PostThread as PostThreadComponent} from '../com/post-thread/PostThread' import {isWeb} from '#/platform/detection'
import {ComposePrompt} from 'view/com/composer/Prompt'
import {s} from 'lib/styles'
import {useSafeAreaInsets} from 'react-native-safe-area-context'
import { import {
RQKEY as POST_THREAD_RQKEY, RQKEY as POST_THREAD_RQKEY,
ThreadNode, ThreadNode,
} from '#/state/queries/post-thread' } from '#/state/queries/post-thread'
import {clamp} from 'lodash'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {useMinimalShellMode} from 'lib/hooks/useMinimalShellMode'
import {useSetMinimalShellMode} from '#/state/shell'
import {useResolveUriQuery} from '#/state/queries/resolve-uri'
import {ErrorMessage} from '../com/util/error/ErrorMessage'
import {CenteredView} from '../com/util/Views'
import {useComposerControls} from '#/state/shell/composer'
import {useSession} from '#/state/session' import {useSession} from '#/state/session'
import {isWeb} from '#/platform/detection' import {useSetMinimalShellMode} from '#/state/shell'
import {useComposerControls} from '#/state/shell/composer'
import {useMinimalShellMode} from 'lib/hooks/useMinimalShellMode'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {CommonNavigatorParams, NativeStackScreenProps} from 'lib/routes/types'
import {makeRecordUri} from 'lib/strings/url-helpers'
import {s} from 'lib/styles'
import {ComposePrompt} from 'view/com/composer/Prompt'
import {PostThread as PostThreadComponent} from '../com/post-thread/PostThread'
type Props = NativeStackScreenProps<CommonNavigatorParams, 'PostThread'> type Props = NativeStackScreenProps<CommonNavigatorParams, 'PostThread'>
export function PostThreadScreen({route}: Props) { export function PostThreadScreen({route}: Props) {
@ -35,7 +33,6 @@ export function PostThreadScreen({route}: Props) {
const {name, rkey} = route.params const {name, rkey} = route.params
const {isMobile} = useWebMediaQueries() const {isMobile} = useWebMediaQueries()
const uri = makeRecordUri(name, 'app.bsky.feed.post', rkey) const uri = makeRecordUri(name, 'app.bsky.feed.post', rkey)
const {data: resolvedUri, error: uriError} = useResolveUriQuery(uri)
const [canReply, setCanReply] = React.useState(false) const [canReply, setCanReply] = React.useState(false)
useFocusEffect( useFocusEffect(
@ -45,12 +42,10 @@ export function PostThreadScreen({route}: Props) {
) )
const onPressReply = React.useCallback(() => { const onPressReply = React.useCallback(() => {
if (!resolvedUri) { if (!uri) {
return return
} }
const thread = queryClient.getQueryData<ThreadNode>( const thread = queryClient.getQueryData<ThreadNode>(POST_THREAD_RQKEY(uri))
POST_THREAD_RQKEY(resolvedUri.uri),
)
if (thread?.type !== 'post') { if (thread?.type !== 'post') {
return return
} }
@ -64,25 +59,19 @@ export function PostThreadScreen({route}: Props) {
}, },
onPost: () => onPost: () =>
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: POST_THREAD_RQKEY(resolvedUri.uri || ''), queryKey: POST_THREAD_RQKEY(uri),
}), }),
}) })
}, [openComposer, queryClient, resolvedUri]) }, [openComposer, queryClient, uri])
return ( return (
<View style={s.hContentRegion}> <View style={s.hContentRegion}>
<View style={s.flex1}> <View style={s.flex1}>
{uriError ? ( <PostThreadComponent
<CenteredView> uri={uri}
<ErrorMessage message={String(uriError)} /> onPressReply={onPressReply}
</CenteredView> onCanReply={setCanReply}
) : ( />
<PostThreadComponent
uri={resolvedUri?.uri}
onPressReply={onPressReply}
onCanReply={setCanReply}
/>
)}
</View> </View>
{isMobile && canReply && hasSession && ( {isMobile && canReply && hasSession && (
<Animated.View <Animated.View