[SDK] Migrate discovery and post-thread queries to the appview client (#11355)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-08-13 22:26:14 +03:00
committed by GitHub
parent b804f267cc
commit 855457c9fa
12 changed files with 137 additions and 126 deletions
+21 -17
View File
@@ -1,10 +1,5 @@
import {useCallback, useMemo, useRef} from 'react'
import {
type AppBskyFeedDefs,
type AppBskyFeedSearchPostsV2,
AtUri,
moderatePost,
} from '@atproto/api'
import {type AppBskyFeedDefs, AtUri, moderatePost} from '@atproto/api'
import {
type InfiniteData,
type QueryClient,
@@ -13,8 +8,9 @@ import {
} from '@tanstack/react-query'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {type SearchFilters} from '#/screens/Search/searchParams'
import {app} from '#/lexicons'
import {
appendFromMe,
buildSearchPostsV2Filters,
@@ -48,7 +44,7 @@ export function useSearchPostsV2Query({
enabled?: boolean
filters?: SearchFilters
}) {
const agent = useAgent()
const client = useAppviewClient()
const moderationOpts = useModerationOpts()
const selectArgs = useMemo(
() => ({
@@ -59,15 +55,15 @@ export function useSearchPostsV2Query({
[query, filters?.author, filters?.from, moderationOpts],
)
const lastRun = useRef<{
data: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
data: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
args: typeof selectArgs
result: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
result: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
} | null>(null)
return useInfiniteQuery<
AppBskyFeedSearchPostsV2.OutputSchema,
app.bsky.feed.searchPostsV2.$OutputBody,
Error,
InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>,
InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>,
QueryKey,
string | undefined
>({
@@ -79,9 +75,18 @@ export function useSearchPostsV2Query({
* dialog; see buildSearchPostsV2Filters for how the two sources combine.
*/
const {q, ...embedded} = extractSearchPostsParams(query)
const builtFilters = buildSearchPostsV2Filters(embedded, filters)
/*
* The generated params brand format-constrained strings (uri,
* at-identifier, language) as template-literal types. The builder emits
* plain strings parsed from user input, which are runtime-identical, so
* the brand is asserted rather than re-validated here.
*/
const builtFilters = buildSearchPostsV2Filters(
embedded,
filters,
) as app.bsky.feed.searchPostsV2.$Params
const finalQuery = appendFromMe(q, filters?.from === 'me')
const res = await agent.app.bsky.feed.searchPostsV2({
return await client.call(app.bsky.feed.searchPostsV2, {
...builtFilters,
query: finalQuery,
limit: 25,
@@ -93,13 +98,12 @@ export function useSearchPostsV2Query({
sort: sort === 'latest' ? 'recent' : sort,
allTime: true,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
enabled: enabled ?? !!moderationOpts,
select: useCallback(
(data: InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>) => {
(data: InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>) => {
const {moderationOpts, isSearchingSpecificUser} = selectArgs
/*
@@ -177,7 +181,7 @@ export function* findAllPostsInQueryData(
uri: string,
): Generator<AppBskyFeedDefs.PostView, undefined> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyFeedSearchPostsV2.OutputSchema>
InfiniteData<app.bsky.feed.searchPostsV2.$OutputBody>
>({
queryKey: [searchPostsQueryKeyRoot],
})
+5 -4
View File
@@ -1,7 +1,8 @@
import {useQuery} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
type ServiceConfig = {
checkEmailConfirmed: boolean
@@ -13,17 +14,17 @@ type ServiceConfig = {
}
export function useServiceConfigQuery() {
const agent = useAgent()
const client = useAppviewClient()
return useQuery<ServiceConfig>({
refetchOnWindowFocus: true,
staleTime: STALE.MINUTES.FIVE,
queryKey: ['service-config'],
queryFn: async () => {
try {
const {data} = await agent.api.app.bsky.unspecced.getConfig()
const data = await client.call(app.bsky.unspecced.getConfig)
return {
checkEmailConfirmed: Boolean(data.checkEmailConfirmed),
// @ts-expect-error not included in types atm
// @ts-expect-error not included in the lexicon atm
topicsEnabled: Boolean(data.topicsEnabled),
liveNow: data.liveNow ?? [],
}
+7 -8
View File
@@ -1,4 +1,3 @@
import {type AppBskyGraphSearchStarterPacksV2} from '@atproto/api'
import {
type InfiniteData,
keepPreviousData,
@@ -7,7 +6,8 @@ import {
} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export const RQKEY_ROOT = 'starter-pack-search'
export const RQKEY = (query: string, limit?: number) => [
@@ -27,23 +27,22 @@ export function useStarterPackSearch({
maintainData?: boolean
limit?: number
}) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphSearchStarterPacksV2.OutputSchema,
app.bsky.graph.searchStarterPacksV2.$OutputBody,
Error,
InfiniteData<AppBskyGraphSearchStarterPacksV2.OutputSchema>,
InfiniteData<app.bsky.graph.searchStarterPacksV2.$OutputBody>,
QueryKey,
string | undefined
>({
staleTime: STALE.MINUTES.FIVE,
queryKey: RQKEY(query, limit),
queryFn: async ({pageParam}) => {
const res = await agent.app.bsky.graph.searchStarterPacksV2({
return await client.call(app.bsky.graph.searchStarterPacksV2, {
q: query,
limit,
cursor: pageParam,
})
return res.data
},
enabled: enabled && !!query,
initialPageParam: undefined,
@@ -54,7 +53,7 @@ export function useStarterPackSearch({
}
function select(
data: InfiniteData<AppBskyGraphSearchStarterPacksV2.OutputSchema>,
data: InfiniteData<app.bsky.graph.searchStarterPacksV2.$OutputBody>,
) {
// enforce uniqueness
const uris = new Set()
@@ -7,14 +7,15 @@ import {
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export const DEFAULT_LIMIT = 15
export const createGetSuggestedFeedsQueryKey = () => ['suggested-feeds']
export function useGetSuggestedFeedsQuery({enabled}: {enabled?: boolean}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
const savedFeeds = preferences?.savedFeeds
@@ -24,7 +25,8 @@ export function useGetSuggestedFeedsQuery({enabled}: {enabled?: boolean}) {
queryKey: createGetSuggestedFeedsQueryKey(),
queryFn: async () => {
const contentLangs = getContentLanguages().join(',')
const {data} = await agent.app.bsky.unspecced.getSuggestedFeeds(
const data = await client.call(
app.bsky.unspecced.getSuggestedFeeds,
{
limit: DEFAULT_LIMIT,
},
@@ -1,7 +1,3 @@
import {
type AppBskyActorDefs,
type AppBskyUnspeccedGetSuggestedOnboardingUsers,
} from '@atproto/api'
import {type QueryClient, useQuery} from '@tanstack/react-query'
import {createBskyTopicsHeader} from '#/lib/api/feed/utils'
@@ -9,7 +5,8 @@ import {logger} from '#/logger'
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export type QueryProps = {
category?: string | null
@@ -30,7 +27,7 @@ export const createGetSuggestedOnboardingUsersQueryKey = (
]
export function useGetSuggestedOnboardingUsersQuery(props: QueryProps) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
return useQuery({
@@ -42,7 +39,8 @@ export function useGetSuggestedOnboardingUsersQuery(props: QueryProps) {
const overrideInterests = props.overrideInterests.join(',')
const {data} = await agent.app.bsky.unspecced.getSuggestedOnboardingUsers(
const data = await client.call(
app.bsky.unspecced.getSuggestedOnboardingUsers,
{
category: props.category ?? undefined,
limit: props.limit || 10,
@@ -66,9 +64,9 @@ export function useGetSuggestedOnboardingUsersQuery(props: QueryProps) {
export function* findAllProfilesInQueryData(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
): Generator<app.bsky.actor.defs.ProfileView, void> {
const responses =
queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedOnboardingUsers.OutputSchema>(
queryClient.getQueriesData<app.bsky.unspecced.getSuggestedOnboardingUsers.$OutputBody>(
{
queryKey: [getSuggestedOnboardingUsersQueryKeyRoot],
},
@@ -1,7 +1,3 @@
import {
type AppBskyActorDefs,
type AppBskyUnspeccedGetSuggestedUsersForDiscover,
} from '@atproto/api'
import {type QueryClient, useQuery} from '@tanstack/react-query'
import {
@@ -12,7 +8,8 @@ import {logger} from '#/logger'
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export type QueryProps = {
limit?: number
@@ -25,7 +22,7 @@ export const createGetSuggestedUsersForDiscoverQueryKey = (props: {
}) => [getSuggestedUsersForDiscoverQueryKeyRoot, props.limit]
export function useGetSuggestedUsersForDiscoverQuery(props: QueryProps = {}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
return useQuery({
@@ -35,18 +32,18 @@ export function useGetSuggestedUsersForDiscoverQuery(props: QueryProps = {}) {
const contentLangs = getContentLanguages().join(',')
const userInterests = aggregateUserInterests(preferences)
const {data} =
await agent.app.bsky.unspecced.getSuggestedUsersForDiscover(
{
limit: props.limit || 10,
const data = await client.call(
app.bsky.unspecced.getSuggestedUsersForDiscover,
{
limit: props.limit || 10,
},
{
headers: {
...createBskyTopicsHeader(userInterests),
'Accept-Language': contentLangs,
},
{
headers: {
...createBskyTopicsHeader(userInterests),
'Accept-Language': contentLangs,
},
},
)
},
)
if (!data.recIdStr) {
logger.debug('getSuggestedUsersForDiscover response missing recIdStr')
}
@@ -58,9 +55,9 @@ export function useGetSuggestedUsersForDiscoverQuery(props: QueryProps = {}) {
export function* findAllProfilesInQueryData(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
): Generator<app.bsky.actor.defs.ProfileView, void> {
const responses =
queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForDiscover.OutputSchema>(
queryClient.getQueriesData<app.bsky.unspecced.getSuggestedUsersForDiscover.$OutputBody>(
{
queryKey: [getSuggestedUsersForDiscoverQueryKeyRoot],
},
@@ -1,7 +1,3 @@
import {
type AppBskyActorDefs,
type AppBskyUnspeccedGetSuggestedUsersForExplore,
} from '@atproto/api'
import {type QueryClient, useQuery} from '@tanstack/react-query'
import {
@@ -12,7 +8,8 @@ import {logger} from '#/logger'
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export type QueryProps = {
category?: string | null
@@ -26,7 +23,7 @@ export const createGetSuggestedUsersForExploreQueryKey = (
) => [getSuggestedUsersForExploreQueryKeyRoot, props.category, props.limit]
export function useGetSuggestedUsersForExploreQuery(props: QueryProps = {}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
return useQuery({
@@ -36,7 +33,8 @@ export function useGetSuggestedUsersForExploreQuery(props: QueryProps = {}) {
const contentLangs = getContentLanguages().join(',')
const userInterests = aggregateUserInterests(preferences)
const {data} = await agent.app.bsky.unspecced.getSuggestedUsersForExplore(
const data = await client.call(
app.bsky.unspecced.getSuggestedUsersForExplore,
{
category: props.category ?? undefined,
limit: props.limit || 10,
@@ -60,9 +58,9 @@ export function useGetSuggestedUsersForExploreQuery(props: QueryProps = {}) {
export function* findAllProfilesInQueryData(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
): Generator<app.bsky.actor.defs.ProfileView, void> {
const responses =
queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForExplore.OutputSchema>(
queryClient.getQueriesData<app.bsky.unspecced.getSuggestedUsersForExplore.$OutputBody>(
{
queryKey: [getSuggestedUsersForExploreQueryKeyRoot],
},
@@ -1,7 +1,3 @@
import {
type AppBskyActorDefs,
type AppBskyUnspeccedGetSuggestedUsersForSeeMore,
} from '@atproto/api'
import {type QueryClient, useQuery} from '@tanstack/react-query'
import {
@@ -12,7 +8,8 @@ import {logger} from '#/logger'
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export type QueryProps = {
category?: string | null
@@ -28,7 +25,7 @@ export const createGetSuggestedUsersForSeeMoreQueryKey = (props: {
}) => [getSuggestedUsersForSeeMoreQueryKeyRoot, props.category, props.limit]
export function useGetSuggestedUsersForSeeMoreQuery(props: QueryProps = {}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
return useQuery({
@@ -42,7 +39,8 @@ export function useGetSuggestedUsersForSeeMoreQuery(props: QueryProps = {}) {
const contentLangs = getContentLanguages().join(',')
const userInterests = aggregateUserInterests(preferences)
const {data} = await agent.app.bsky.unspecced.getSuggestedUsersForSeeMore(
const data = await client.call(
app.bsky.unspecced.getSuggestedUsersForSeeMore,
{
category: props.category ?? undefined,
limit: props.limit || 50,
@@ -66,9 +64,9 @@ export function useGetSuggestedUsersForSeeMoreQuery(props: QueryProps = {}) {
export function* findAllProfilesInQueryData(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
): Generator<app.bsky.actor.defs.ProfileView, void> {
const responses =
queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForSeeMore.OutputSchema>(
queryClient.getQueriesData<app.bsky.unspecced.getSuggestedUsersForSeeMore.$OutputBody>(
{
queryKey: [getSuggestedUsersForSeeMoreQueryKeyRoot],
},
@@ -7,7 +7,8 @@ import {
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export const createOnboardingSuggestedStarterPacksQueryKey = (
interests?: string[],
@@ -20,7 +21,7 @@ export function useOnboardingSuggestedStarterPacksQuery({
enabled?: boolean
overrideInterests?: string[]
}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
const contentLangs = getContentLanguages().join(',')
@@ -29,21 +30,20 @@ export function useOnboardingSuggestedStarterPacksQuery({
staleTime: STALE.MINUTES.THREE,
queryKey: createOnboardingSuggestedStarterPacksQueryKey(overrideInterests),
queryFn: async () => {
const {data} =
await agent.app.bsky.unspecced.getOnboardingSuggestedStarterPacks(
{limit: 6},
{
headers: {
...createBskyTopicsHeader(
overrideInterests
? overrideInterests.join(',')
: aggregateUserInterests(preferences),
),
'Accept-Language': contentLangs,
},
return await client.call(
app.bsky.unspecced.getOnboardingSuggestedStarterPacks,
{limit: 6},
{
headers: {
...createBskyTopicsHeader(
overrideInterests
? overrideInterests.join(',')
: aggregateUserInterests(preferences),
),
'Accept-Language': contentLangs,
},
)
return data
},
)
},
})
}
+16 -9
View File
@@ -1,4 +1,5 @@
import {useCallback, useMemo, useState} from 'react'
import {type AtUriString} from '@atproto/syntax'
import {useQuery, useQueryClient} from '@tanstack/react-query'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
@@ -27,10 +28,11 @@ import {
} from '#/state/queries/usePostThread/types'
import {getThreadgateRecord} from '#/state/queries/usePostThread/utils'
import * as views from '#/state/queries/usePostThread/views'
import {useAgent, useSession} from '#/state/session'
import {useAppviewClient, useSession} from '#/state/session'
import {useMergeThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'
import {useBreakpoints} from '#/alf'
import {IS_WEB} from '#/env'
import {app} from '#/lexicons'
export * from '#/state/queries/usePostThread/context'
export {useUpdatePostThreadThreadgateQueryCache} from '#/state/queries/usePostThread/queryCache'
@@ -38,7 +40,7 @@ export * from '#/state/queries/usePostThread/types'
export function usePostThread({anchor}: {anchor?: string}) {
const qc = useQueryClient()
const agent = useAgent()
const client = useAppviewClient()
const {hasSession} = useSession()
const {gtPhone} = useBreakpoints()
const moderationOpts = useModerationOpts()
@@ -71,8 +73,8 @@ export function usePostThread({anchor}: {anchor?: string}) {
enabled: isThreadPreferencesLoaded && !!anchor && !!moderationOpts,
queryKey: postThreadQueryKey,
async queryFn(ctx) {
const {data} = await agent.app.bsky.unspecced.getPostThreadV2({
anchor: anchor!,
const data = await client.call(app.bsky.unspecced.getPostThreadV2, {
anchor: anchor! as AtUriString,
branchingFactor: view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF,
below,
sort: sort,
@@ -93,18 +95,24 @@ export function usePostThread({anchor}: {anchor?: string}) {
ctx.meta.hasOtherReplies = true
}
/*
* The generated views type `threadgate.record` as an opaque lex map and
* brand at-uris, so the response is asserted to the exported result type
* here; the record swap-in below and every downstream consumer of the
* thread read that narrower contract.
*/
const result = {
thread: data.thread || [],
threadgate: data.threadgate,
hasOtherReplies: !!ctx.meta.hasOtherReplies,
}
} as UsePostThreadQueryResult
const record = getThreadgateRecord(result.threadgate)
if (result.threadgate && record) {
result.threadgate.record = record
}
return result as UsePostThreadQueryResult
return result
},
placeholderData() {
if (!anchor) return
@@ -161,10 +169,9 @@ export function usePostThread({anchor}: {anchor?: string}) {
enabled: additionalQueryEnabled,
queryKey: postThreadOtherQueryKey,
async queryFn() {
const {data} = await agent.app.bsky.unspecced.getPostThreadOtherV2({
anchor: anchor!,
return await client.call(app.bsky.unspecced.getPostThreadOtherV2, {
anchor: anchor! as AtUriString,
})
return data
},
})
const serverOtherThreadItems: ThreadItem[] = useMemo(() => {
@@ -7,7 +7,8 @@ import {
import {getContentLanguages} from '#/state/preferences/languages'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export const createSuggestedStarterPacksQueryKey = (interests?: string[]) => [
'suggested-starter-packs',
@@ -21,7 +22,7 @@ export function useSuggestedStarterPacksQuery({
enabled?: boolean
overrideInterests?: string[]
}) {
const agent = useAgent()
const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery()
const contentLangs = getContentLanguages().join(',')
@@ -30,8 +31,9 @@ export function useSuggestedStarterPacksQuery({
staleTime: STALE.MINUTES.THREE,
queryKey: createSuggestedStarterPacksQueryKey(overrideInterests),
queryFn: async () => {
const {data} = await agent.app.bsky.unspecced.getSuggestedStarterPacks(
undefined,
return await client.call(
app.bsky.unspecced.getSuggestedStarterPacks,
{},
{
headers: {
...createBskyTopicsHeader(
@@ -43,7 +45,6 @@ export function useSuggestedStarterPacksQuery({
},
},
)
return data
},
})
}