From 75aec19230609f8ae689cd1ec0b2697c5233bdeb Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 20 Jun 2024 04:19:37 +0300 Subject: Fix threadgate read after write (#4577) * Fix threadgate read-after-write problem * Fix React key (drive-by) --- src/view/com/threadgate/WhoCanReply.tsx | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/view/com/threadgate/WhoCanReply.tsx b/src/view/com/threadgate/WhoCanReply.tsx index 3ffbaa7ae..7e3528d92 100644 --- a/src/view/com/threadgate/WhoCanReply.tsx +++ b/src/view/com/threadgate/WhoCanReply.tsx @@ -1,12 +1,19 @@ import React from 'react' import {Keyboard, StyleProp, View, ViewStyle} from 'react-native' -import {AppBskyFeedDefs, AppBskyGraphDefs, AtUri} from '@atproto/api' +import { + AppBskyFeedDefs, + AppBskyFeedGetPostThread, + AppBskyGraphDefs, + AtUri, + BskyAgent, +} from '@atproto/api' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useQueryClient} from '@tanstack/react-query' import {useAnalytics} from '#/lib/analytics/analytics' import {createThreadgate} from '#/lib/api' +import {until} from '#/lib/async/until' import {useColorSchemeStyle} from '#/lib/hooks/useColorSchemeStyle' import {usePalette} from '#/lib/hooks/usePalette' import {makeListLink, makeProfileLink} from '#/lib/routes/links' @@ -85,6 +92,18 @@ export function WhoCanReply({ rkey: new AtUri(post.uri).rkey, }) } + await whenAppViewReady(agent, post.uri, res => { + const thread = res.data.thread + if (AppBskyFeedDefs.isThreadViewPost(thread)) { + const fetchedSettings = threadgateViewToSettings( + thread.post.threadgate, + ) + return ( + JSON.stringify(fetchedSettings) === JSON.stringify(newSettings) + ) + } + return false + }) Toast.show('Thread settings updated') queryClient.invalidateQueries({ queryKey: [POST_THREAD_RQKEY_ROOT], @@ -133,15 +152,14 @@ export function WhoCanReply({ Only{' '} {settings.map((rule, i) => ( - <> + - + ))}{' '} can reply. @@ -227,3 +245,20 @@ function Separator({i, length}: {i: number; length: number}) { } return <>, } + +async function whenAppViewReady( + agent: BskyAgent, + uri: string, + fn: (res: AppBskyFeedGetPostThread.Response) => boolean, +) { + await until( + 5, // 5 tries + 1e3, // 1s delay between tries + fn, + () => + agent.app.bsky.feed.getPostThread({ + uri, + depth: 0, + }), + ) +} -- cgit 1.4.1