From 571d00cf41ffd291bb506226cb66f23db6137976 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 5 Aug 2024 09:55:59 -0500 Subject: [PATCH] Thread through threadgate record, validate approach --- src/state/queries/threadgate.ts | 32 ++++++++++++++++++++- src/view/com/post-thread/PostThread.tsx | 6 +++- src/view/com/post-thread/PostThreadItem.tsx | 2 ++ src/view/com/util/forms/PostDropdownBtn.tsx | 24 +++++++++++++++- src/view/com/util/post-ctrls/PostCtrls.tsx | 3 ++ 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/state/queries/threadgate.ts b/src/state/queries/threadgate.ts index a04ec183a7..d591d35834 100644 --- a/src/state/queries/threadgate.ts +++ b/src/state/queries/threadgate.ts @@ -1,4 +1,9 @@ -import {AppBskyFeedDefs, AppBskyFeedThreadgate, AtUri} from '@atproto/api' +import { + AppBskyFeedDefs, + AppBskyFeedThreadgate, + AtUri, + BskyAgent, +} from '@atproto/api' import {useQuery} from '@tanstack/react-query' import {useAgent} from '#/state/session' @@ -78,3 +83,28 @@ export function useThreadgateRecordQuery({ }, }) } + +export function createThreadgate({ + agent, + postUri, + threadgate, +}: { + agent: BskyAgent + postUri: string + threadgate: Partial +}) { + const urip = new AtUri(postUri) + const record = { + ...threadgate, + post: postUri, + createdAt: new Date().toISOString(), + } + + return agent.api.app.bsky.feed.threadgate.create( + { + repo: urip.host, + rkey: urip.rkey, + }, + record, + ) +} diff --git a/src/view/com/post-thread/PostThread.tsx b/src/view/com/post-thread/PostThread.tsx index 573a479fd6..9518f1f7cf 100644 --- a/src/view/com/post-thread/PostThread.tsx +++ b/src/view/com/post-thread/PostThread.tsx @@ -119,7 +119,7 @@ export function PostThread({ const rootPostRecord = thread?.type === 'post' ? thread.record : undefined const {data: threadgateRecord} = useThreadgateRecordQuery({ - postUri: uri, + postUri: rootPost?.record?.reply?.root?.uri, initialData: rootPost?.threadgate?.record as AppBskyFeedThreadgate.Record, }) @@ -572,6 +572,10 @@ function* flattenThreadReplies( return HiddenReplyType.Hidden } } + + if (threadgateRecord?.hiddenReplies?.includes(node.post.uri)) { + return HiddenReplyType.Hidden + } } if (!node.ctx.isHighlightedPost) { diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index 63dac300a8..47bdf185e3 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -392,6 +392,7 @@ let PostThreadItemLoaded = ({ richText={richText} onPressReply={onPressReply} logContext="PostThreadItem" + rootPostUri={rootUri} /> @@ -543,6 +544,7 @@ let PostThreadItemLoaded = ({ richText={richText} onPressReply={onPressReply} logContext="PostThreadItem" + rootPostUri={rootUri} /> diff --git a/src/view/com/util/forms/PostDropdownBtn.tsx b/src/view/com/util/forms/PostDropdownBtn.tsx index 6c82ec8cc2..b639372c52 100644 --- a/src/view/com/util/forms/PostDropdownBtn.tsx +++ b/src/view/com/util/forms/PostDropdownBtn.tsx @@ -31,7 +31,8 @@ import { usePostDeleteMutation, useThreadMuteMutationQueue, } from '#/state/queries/post' -import {useSession} from '#/state/session' +import {createThreadgate} from '#/state/queries/threadgate' +import {useAgent, useSession} from '#/state/session' import {getCurrentRoute} from 'lib/routes/helpers' import {shareUrl} from 'lib/sharing' import {toShareUrl} from 'lib/strings/url-helpers' @@ -73,6 +74,7 @@ let PostDropdownBtn = ({ hitSlop, size, timestamp, + rootPostUri, }: { testID: string post: Shadow @@ -83,8 +85,10 @@ let PostDropdownBtn = ({ hitSlop?: PressableProps['hitSlop'] size?: 'lg' | 'md' | 'sm' timestamp: string + rootPostUri?: string }): React.ReactNode => { const {hasSession, currentAccount} = useSession() + const agent = useAgent() const theme = useTheme() const alf = useAlf() const {gtMobile} = useBreakpoints() @@ -393,6 +397,24 @@ let PostDropdownBtn = ({ )} + + {!isAuthor && !isPostHidden && rootPostUri && ( + { + createThreadgate({ + agent, + postUri: rootPostUri, + threadgate: { + hiddenReplies: [postUri], + }, + }) + }}> + {_(msg`Hide reply`)} + + + )} )} diff --git a/src/view/com/util/post-ctrls/PostCtrls.tsx b/src/view/com/util/post-ctrls/PostCtrls.tsx index 478b8f0f89..c6e2dd1c51 100644 --- a/src/view/com/util/post-ctrls/PostCtrls.tsx +++ b/src/view/com/util/post-ctrls/PostCtrls.tsx @@ -59,6 +59,7 @@ let PostCtrls = ({ style, onPressReply, logContext, + rootPostUri, }: { big?: boolean post: Shadow @@ -68,6 +69,7 @@ let PostCtrls = ({ style?: StyleProp onPressReply: () => void logContext: 'FeedItem' | 'PostThreadItem' | 'Post' + rootPostUri?: string }): React.ReactNode => { const t = useTheme() const {_} = useLingui() @@ -338,6 +340,7 @@ let PostCtrls = ({ style={{padding: 5}} hitSlop={POST_CTRL_HITSLOP} timestamp={post.indexedAt} + rootPostUri={rootPostUri} /> {gate('debug_show_feedcontext') && feedContext && (