Integrate live threadgate query

This commit is contained in:
Eric Bailey
2024-08-02 16:20:31 -05:00
parent c3d8beee6d
commit 380c4dbf24
3 changed files with 60 additions and 2 deletions
+4
View File
@@ -18,6 +18,7 @@ import {makeListLink, makeProfileLink} from '#/lib/routes/links'
import {logger} from '#/logger' import {logger} from '#/logger'
import {isNative} from '#/platform/detection' import {isNative} from '#/platform/detection'
import {RQKEY_ROOT as POST_THREAD_RQKEY_ROOT} from '#/state/queries/post-thread' import {RQKEY_ROOT as POST_THREAD_RQKEY_ROOT} from '#/state/queries/post-thread'
import {threadgateRecordQueryKeyRoot} from '#/state/queries/threadgate'
import { import {
ThreadgateSetting, ThreadgateSetting,
threadgateViewToSettings, threadgateViewToSettings,
@@ -110,6 +111,9 @@ export function WhoCanReply({post, isThreadAuthor, style}: WhoCanReplyProps) {
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: [POST_THREAD_RQKEY_ROOT], queryKey: [POST_THREAD_RQKEY_ROOT],
}) })
queryClient.invalidateQueries({
queryKey: [threadgateRecordQueryKeyRoot],
})
} catch (err) { } catch (err) {
Toast.show( Toast.show(
_( _(
+43 -1
View File
@@ -1,4 +1,7 @@
import {AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api' import {AppBskyFeedDefs, AppBskyFeedThreadgate, AtUri} from '@atproto/api'
import {useQuery} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
export type ThreadgateSetting = export type ThreadgateSetting =
| {type: 'nobody'} | {type: 'nobody'}
@@ -36,3 +39,42 @@ export function threadgateViewToSettings(
.filter(n => !!n) .filter(n => !!n)
return settings return settings
} }
export const threadgateRecordQueryKeyRoot = 'threadgate-record'
export const createThreadgateRecordQueryKey = (uri: string) => [
threadgateRecordQueryKeyRoot,
uri,
]
export function useThreadgateRecordQuery({
postUri,
initialData,
}: {
postUri?: string
initialData?: AppBskyFeedThreadgate.Record
} = {}) {
const agent = useAgent()
return useQuery({
enabled: !!postUri,
queryKey: createThreadgateRecordQueryKey(postUri || ''),
placeholderData: initialData,
async queryFn() {
const urip = new AtUri(postUri!)
if (!urip.host.startsWith('did:')) {
const res = await agent.resolveHandle({
handle: urip.host,
})
urip.host = res.data.did
}
const {value} = await agent.api.app.bsky.feed.threadgate.get({
repo: urip.host,
rkey: urip.rkey,
})
return value
},
})
}
+13 -1
View File
@@ -1,7 +1,7 @@
import React, {useEffect, useRef} from 'react' import React, {useEffect, useRef} from 'react'
import {useWindowDimensions, View} from 'react-native' import {useWindowDimensions, View} from 'react-native'
import {runOnJS} from 'react-native-reanimated' import {runOnJS} from 'react-native-reanimated'
import {AppBskyFeedDefs} from '@atproto/api' import {AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@@ -20,6 +20,7 @@ import {
usePostThreadQuery, usePostThreadQuery,
} from '#/state/queries/post-thread' } from '#/state/queries/post-thread'
import {usePreferencesQuery} from '#/state/queries/preferences' import {usePreferencesQuery} from '#/state/queries/preferences'
import {useThreadgateRecordQuery} from '#/state/queries/threadgate'
import {useSession} from '#/state/session' import {useSession} from '#/state/session'
import {useInitialNumToRender} from 'lib/hooks/useInitialNumToRender' import {useInitialNumToRender} from 'lib/hooks/useInitialNumToRender'
import {useSetTitle} from 'lib/hooks/useSetTitle' import {useSetTitle} from 'lib/hooks/useSetTitle'
@@ -117,6 +118,11 @@ export function PostThread({
const rootPost = thread?.type === 'post' ? thread.post : undefined const rootPost = thread?.type === 'post' ? thread.post : undefined
const rootPostRecord = thread?.type === 'post' ? thread.record : undefined const rootPostRecord = thread?.type === 'post' ? thread.record : undefined
const {data: threadgateRecord} = useThreadgateRecordQuery({
postUri: uri,
initialData: rootPost?.threadgate?.record as AppBskyFeedThreadgate.Record,
})
const moderationOpts = useModerationOpts() const moderationOpts = useModerationOpts()
const isNoPwi = React.useMemo(() => { const isNoPwi = React.useMemo(() => {
const mod = const mod =
@@ -172,6 +178,7 @@ export function PostThread({
treeView, treeView,
threadModerationCache, threadModerationCache,
hiddenRepliesState !== HiddenRepliesState.Hide, hiddenRepliesState !== HiddenRepliesState.Hide,
threadgateRecord,
) )
}, [ }, [
thread, thread,
@@ -180,6 +187,7 @@ export function PostThread({
treeView, treeView,
threadModerationCache, threadModerationCache,
hiddenRepliesState, hiddenRepliesState,
threadgateRecord,
]) ])
const error = React.useMemo(() => { const error = React.useMemo(() => {
@@ -494,6 +502,7 @@ function createThreadSkeleton(
treeView: boolean, treeView: boolean,
modCache: ThreadModerationCache, modCache: ThreadModerationCache,
showHiddenReplies: boolean, showHiddenReplies: boolean,
threadgateRecord: AppBskyFeedThreadgate.Record | undefined,
): ThreadSkeletonParts | null { ): ThreadSkeletonParts | null {
if (!node) return null if (!node) return null
@@ -507,6 +516,7 @@ function createThreadSkeleton(
treeView, treeView,
modCache, modCache,
showHiddenReplies, showHiddenReplies,
threadgateRecord,
), ),
), ),
} }
@@ -543,6 +553,7 @@ function* flattenThreadReplies(
treeView: boolean, treeView: boolean,
modCache: ThreadModerationCache, modCache: ThreadModerationCache,
showHiddenReplies: boolean, showHiddenReplies: boolean,
threadgateRecord: AppBskyFeedThreadgate.Record | undefined,
): Generator<YieldedItem, HiddenReplyType> { ): Generator<YieldedItem, HiddenReplyType> {
if (node.type === 'post') { if (node.type === 'post') {
// dont show pwi-opted-out posts to logged out users // dont show pwi-opted-out posts to logged out users
@@ -576,6 +587,7 @@ function* flattenThreadReplies(
treeView, treeView,
modCache, modCache,
showHiddenReplies, showHiddenReplies,
threadgateRecord,
) )
if (hiddenReply > hiddenReplies) { if (hiddenReply > hiddenReplies) {
hiddenReplies = hiddenReply hiddenReplies = hiddenReply