Fix threadgate read after write (#4577)

* Fix threadgate read-after-write problem

* Fix React key (drive-by)
zio/stable
dan 2024-06-20 04:19:37 +03:00 committed by GitHub
parent 7d8fca56dc
commit 75aec19230
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 4 deletions

View File

@ -1,12 +1,19 @@
import React from 'react' import React from 'react'
import {Keyboard, StyleProp, View, ViewStyle} from 'react-native' 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 {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {useQueryClient} from '@tanstack/react-query' import {useQueryClient} from '@tanstack/react-query'
import {useAnalytics} from '#/lib/analytics/analytics' import {useAnalytics} from '#/lib/analytics/analytics'
import {createThreadgate} from '#/lib/api' import {createThreadgate} from '#/lib/api'
import {until} from '#/lib/async/until'
import {useColorSchemeStyle} from '#/lib/hooks/useColorSchemeStyle' import {useColorSchemeStyle} from '#/lib/hooks/useColorSchemeStyle'
import {usePalette} from '#/lib/hooks/usePalette' import {usePalette} from '#/lib/hooks/usePalette'
import {makeListLink, makeProfileLink} from '#/lib/routes/links' import {makeListLink, makeProfileLink} from '#/lib/routes/links'
@ -85,6 +92,18 @@ export function WhoCanReply({
rkey: new AtUri(post.uri).rkey, 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') Toast.show('Thread settings updated')
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: [POST_THREAD_RQKEY_ROOT], queryKey: [POST_THREAD_RQKEY_ROOT],
@ -133,15 +152,14 @@ export function WhoCanReply({
<Trans> <Trans>
Only{' '} Only{' '}
{settings.map((rule, i) => ( {settings.map((rule, i) => (
<> <React.Fragment key={`rule-${i}`}>
<Rule <Rule
key={`rule-${i}`}
rule={rule} rule={rule}
post={post} post={post}
lists={post.threadgate!.lists} lists={post.threadgate!.lists}
/> />
<Separator key={`sep-${i}`} i={i} length={settings.length} /> <Separator key={`sep-${i}`} i={i} length={settings.length} />
</> </React.Fragment>
))}{' '} ))}{' '}
can reply. can reply.
</Trans> </Trans>
@ -227,3 +245,20 @@ function Separator({i, length}: {i: number; length: number}) {
} }
return <>, </> 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,
}),
)
}