migrate the post social queries and feed api construction sites

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-08-03 18:45:01 +03:00
parent 8b4f284786
commit c857a534ec
5 changed files with 75 additions and 55 deletions
+5 -4
View File
@@ -5,6 +5,7 @@ import {
AtUri, AtUri,
moderatePost, moderatePost,
} from '@atproto/api' } from '@atproto/api'
import {type AtUriString} from '@atproto/syntax'
import {msg} from '@lingui/core/macro' import {msg} from '@lingui/core/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import { import {
@@ -28,7 +29,7 @@ import {
embedViewRecordToPostView, embedViewRecordToPostView,
getEmbeddedPost, getEmbeddedPost,
} from '#/state/queries/util' } from '#/state/queries/util'
import {useAgent} from '#/state/session' import {useAppviewClient} from '#/state/session'
const RQKEY_ROOT = 'feed-previews' const RQKEY_ROOT = 'feed-previews'
const RQKEY = (feeds: string[]) => [RQKEY_ROOT, feeds] const RQKEY = (feeds: string[]) => [RQKEY_ROOT, feeds]
@@ -121,7 +122,7 @@ export function useFeedPreviews(
const uris = feeds.map(feed => feed.uri) const uris = feeds.map(feed => feed.uri)
const {_} = useLingui() const {_} = useLingui()
const agent = useAgent() const client = useAppviewClient()
const {data: preferences} = usePreferencesQuery() const {data: preferences} = usePreferencesQuery()
const userInterests = aggregateUserInterests(preferences) const userInterests = aggregateUserInterests(preferences)
const moderationOpts = useModerationOpts() const moderationOpts = useModerationOpts()
@@ -143,8 +144,8 @@ export function useFeedPreviews(
queryFn: async ({pageParam}) => { queryFn: async ({pageParam}) => {
const feed = feeds[pageParam] const feed = feeds[pageParam]
const api = new CustomFeedAPI({ const api = new CustomFeedAPI({
agent, client,
feedParams: {feed: feed.uri}, feedParams: {feed: feed.uri as AtUriString},
userInterests, userInterests,
}) })
const data = await api.fetch({cursor: undefined, limit: LIMIT}) const data = await api.fetch({cursor: undefined, limit: LIMIT})
+31 -16
View File
@@ -4,12 +4,13 @@ import {
type AppBskyActorDefs, type AppBskyActorDefs,
AppBskyFeedDefs, AppBskyFeedDefs,
type AppBskyFeedPost, type AppBskyFeedPost,
type AtpAgent,
AtUri, AtUri,
moderatePost, moderatePost,
type ModerationDecision, type ModerationDecision,
type ModerationPrefs, type ModerationPrefs,
} from '@atproto/api' } from '@atproto/api'
import {type Client} from '@atproto/lex'
import {type AtIdentifierString, type AtUriString} from '@atproto/syntax'
import { import {
type InfiniteData, type InfiniteData,
type QueryClient, type QueryClient,
@@ -33,7 +34,7 @@ import {DISCOVER_FEED_URI} from '#/lib/constants'
import {logger} from '#/logger' import {logger} from '#/logger'
import {STALE} from '#/state/queries' import {STALE} from '#/state/queries'
import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const' import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const'
import {useAgent} from '#/state/session' import {useAgent, useAppviewClient} from '#/state/session'
import * as userActionHistory from '#/state/userActionHistory' import * as userActionHistory from '#/state/userActionHistory'
import {KnownError} from '#/view/com/posts/PostFeedErrorMessage' import {KnownError} from '#/view/com/posts/PostFeedErrorMessage'
import {useFeedTuners} from '../preferences/feed-tuners' import {useFeedTuners} from '../preferences/feed-tuners'
@@ -149,6 +150,7 @@ export function usePostFeedQuery(
) ?? -1 ) ?? -1
const enableFollowingToDiscoverFallback = followingPinnedIndex === 0 const enableFollowingToDiscoverFallback = followingPinnedIndex === 0
const agent = useAgent() const agent = useAgent()
const client = useAppviewClient()
const lastRun = useRef<{ const lastRun = useRef<{
data: InfiniteData<FeedPageUnselected> data: InfiniteData<FeedPageUnselected>
args: typeof selectArgs args: typeof selectArgs
@@ -193,7 +195,7 @@ export function usePostFeedQuery(
feedDesc, feedDesc,
feedParams: params || {}, feedParams: params || {},
feedTuners, feedTuners,
agent, client,
// Not in the query key because they don't change: // Not in the query key because they don't change:
userInterests, userInterests,
// Not in the query key. Reacting to it switching isn't important: // Not in the query key. Reacting to it switching isn't important:
@@ -443,55 +445,68 @@ function createApi({
feedParams, feedParams,
feedTuners, feedTuners,
userInterests, userInterests,
agent, client,
enableFollowingToDiscoverFallback, enableFollowingToDiscoverFallback,
}: { }: {
feedDesc: FeedDescriptor feedDesc: FeedDescriptor
feedParams: FeedParams feedParams: FeedParams
feedTuners: FeedTunerFn[] feedTuners: FeedTunerFn[]
userInterests?: string userInterests?: string
agent: AtpAgent client: Client
enableFollowingToDiscoverFallback: boolean enableFollowingToDiscoverFallback: boolean
}) { }) {
if (feedDesc === 'following') { if (feedDesc === 'following') {
if (feedParams.mergeFeedEnabled) { if (feedParams.mergeFeedEnabled) {
return new MergeFeedAPI({ return new MergeFeedAPI({
agent, client,
feedParams, feedParams,
feedTuners, feedTuners,
userInterests, userInterests,
}) })
} else { } else {
if (enableFollowingToDiscoverFallback) { if (enableFollowingToDiscoverFallback) {
return new HomeFeedAPI({agent, userInterests}) return new HomeFeedAPI({client, userInterests})
} else { } else {
return new FollowingFeedAPI({agent}) return new FollowingFeedAPI({client})
} }
} }
} else if (feedDesc.startsWith('author')) { } else if (feedDesc.startsWith('author')) {
const [__, actor, filter] = feedDesc.split('|') const [__, actor, filter] = feedDesc.split('|')
return new AuthorFeedAPI({agent, feedParams: {actor, filter}}) /*
* The descriptor is split out of an internally-built string, so neither the
* actor identifier nor the filter token is narrowed by the compiler here.
*/
return new AuthorFeedAPI({
client,
feedParams: {actor: actor as AtIdentifierString, filter},
})
} else if (feedDesc.startsWith('likes')) { } else if (feedDesc.startsWith('likes')) {
const [__, actor] = feedDesc.split('|') const [__, actor] = feedDesc.split('|')
return new LikesFeedAPI({agent, feedParams: {actor}}) return new LikesFeedAPI({
client,
feedParams: {actor: actor as AtIdentifierString},
})
} else if (feedDesc.startsWith('feedgen')) { } else if (feedDesc.startsWith('feedgen')) {
const [__, feed] = feedDesc.split('|') const [__, feed] = feedDesc.split('|')
return new CustomFeedAPI({ return new CustomFeedAPI({
agent, client,
feedParams: {feed}, feedParams: {feed: feed as AtUriString},
userInterests, userInterests,
}) })
} else if (feedDesc.startsWith('list')) { } else if (feedDesc.startsWith('list')) {
const [__, list] = feedDesc.split('|') const [__, list] = feedDesc.split('|')
return new ListFeedAPI({agent, feedParams: {list}}) return new ListFeedAPI({client, feedParams: {list: list as AtUriString}})
} else if (feedDesc.startsWith('posts')) { } else if (feedDesc.startsWith('posts')) {
const [__, uriList] = feedDesc.split('|') const [__, uriList] = feedDesc.split('|')
return new PostListFeedAPI({agent, feedParams: {uris: uriList.split(',')}}) return new PostListFeedAPI({
client,
feedParams: {uris: uriList.split(',') as AtUriString[]},
})
} else if (feedDesc === 'demo') { } else if (feedDesc === 'demo') {
return new DemoFeedAPI({agent}) return new DemoFeedAPI({client})
} else { } else {
// shouldnt happen // shouldnt happen
return new FollowingFeedAPI({agent}) return new FollowingFeedAPI({client})
} }
} }
+17 -13
View File
@@ -1,4 +1,5 @@
import {type AppBskyActorDefs, type AppBskyFeedGetLikes} from '@atproto/api' import {type AppBskyActorDefs} from '@atproto/api'
import {type AtUriString} from '@atproto/syntax'
import { import {
type InfiniteData, type InfiniteData,
type QueryClient, type QueryClient,
@@ -9,7 +10,8 @@ import {
import {STALE} from '#/state/queries' import {STALE} from '#/state/queries'
import {createQueryKey} from '#/state/queries/util' import {createQueryKey} from '#/state/queries/util'
import {useAgent} from '#/state/session' import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const PAGE_SIZE = 30 const PAGE_SIZE = 30
type RQPageParam = string | undefined type RQPageParam = string | undefined
@@ -19,22 +21,22 @@ const RQKEY_ROOT = 'liked-by'
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri] export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
export function useLikedByQuery(resolvedUri: string | undefined) { export function useLikedByQuery(resolvedUri: string | undefined) {
const agent = useAgent() const client = useAppviewClient()
return useInfiniteQuery< return useInfiniteQuery<
AppBskyFeedGetLikes.OutputSchema, app.bsky.feed.getLikes.$OutputBody,
Error, Error,
InfiniteData<AppBskyFeedGetLikes.OutputSchema>, InfiniteData<app.bsky.feed.getLikes.$OutputBody>,
QueryKey, QueryKey,
RQPageParam RQPageParam
>({ >({
queryKey: RQKEY(resolvedUri || ''), queryKey: RQKEY(resolvedUri || ''),
async queryFn({pageParam}: {pageParam: RQPageParam}) { async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.getLikes({ return await client.call(app.bsky.feed.getLikes, {
uri: resolvedUri || '', // the enabled flag prevents this from running until resolvedUri is set
uri: (resolvedUri || '') as AtUriString,
limit: PAGE_SIZE, limit: PAGE_SIZE,
cursor: pageParam, cursor: pageParam,
}) })
return res.data
}, },
initialPageParam: undefined, initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
@@ -59,12 +61,14 @@ export const createLikedBySampleQueryKey = (args: {uri: string}) =>
* perturb the liked-by screen's pagination. * perturb the liked-by screen's pagination.
*/ */
export function useLikedBySampleQuery({uri}: {uri: string | undefined}) { export function useLikedBySampleQuery({uri}: {uri: string | undefined}) {
const agent = useAgent() const client = useAppviewClient()
return useQuery({ return useQuery({
queryKey: createLikedBySampleQueryKey({uri: uri ?? ''}), queryKey: createLikedBySampleQueryKey({uri: uri ?? ''}),
queryFn: async () => { queryFn: async () => {
const res = await agent.getLikes({uri: uri ?? '', limit: SAMPLE_SIZE}) return await client.call(app.bsky.feed.getLikes, {
return res.data uri: (uri ?? '') as AtUriString,
limit: SAMPLE_SIZE,
})
}, },
staleTime: STALE.MINUTES.FIVE, staleTime: STALE.MINUTES.FIVE,
enabled: !!uri, enabled: !!uri,
@@ -81,7 +85,7 @@ export function* findAllProfilesInQueryData(
did: string, did: string,
): Generator<AppBskyActorDefs.ProfileView, void> { ): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData< const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyFeedGetLikes.OutputSchema> InfiniteData<app.bsky.feed.getLikes.$OutputBody>
>({ >({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })
@@ -98,7 +102,7 @@ export function* findAllProfilesInQueryData(
} }
} }
const sampleQueryDatas = const sampleQueryDatas =
queryClient.getQueriesData<AppBskyFeedGetLikes.OutputSchema>({ queryClient.getQueriesData<app.bsky.feed.getLikes.$OutputBody>({
queryKey: [likedBySampleQueryKeyRoot], queryKey: [likedBySampleQueryKeyRoot],
}) })
for (const [_queryKey, queryData] of sampleQueryDatas) { for (const [_queryKey, queryData] of sampleQueryDatas) {
+11 -10
View File
@@ -2,9 +2,9 @@ import {
type AppBskyActorDefs, type AppBskyActorDefs,
AppBskyEmbedRecord, AppBskyEmbedRecord,
type AppBskyFeedDefs, type AppBskyFeedDefs,
type AppBskyFeedGetQuotes,
AtUri, AtUri,
} from '@atproto/api' } from '@atproto/api'
import {type AtUriString} from '@atproto/syntax'
import { import {
type InfiniteData, type InfiniteData,
type QueryClient, type QueryClient,
@@ -12,7 +12,8 @@ import {
useInfiniteQuery, useInfiniteQuery,
} from '@tanstack/react-query' } from '@tanstack/react-query'
import {useAgent} from '#/state/session' import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
import { import {
didOrHandleUriMatches, didOrHandleUriMatches,
embedViewRecordToPostView, embedViewRecordToPostView,
@@ -26,22 +27,22 @@ const RQKEY_ROOT = 'post-quotes'
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri] export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
export function usePostQuotesQuery(resolvedUri: string | undefined) { export function usePostQuotesQuery(resolvedUri: string | undefined) {
const agent = useAgent() const client = useAppviewClient()
return useInfiniteQuery< return useInfiniteQuery<
AppBskyFeedGetQuotes.OutputSchema, app.bsky.feed.getQuotes.$OutputBody,
Error, Error,
InfiniteData<AppBskyFeedGetQuotes.OutputSchema>, InfiniteData<app.bsky.feed.getQuotes.$OutputBody>,
QueryKey, QueryKey,
RQPageParam RQPageParam
>({ >({
queryKey: RQKEY(resolvedUri || ''), queryKey: RQKEY(resolvedUri || ''),
async queryFn({pageParam}: {pageParam: RQPageParam}) { async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.api.app.bsky.feed.getQuotes({ return await client.call(app.bsky.feed.getQuotes, {
uri: resolvedUri || '', // the enabled flag prevents this from running until resolvedUri is set
uri: (resolvedUri || '') as AtUriString,
limit: PAGE_SIZE, limit: PAGE_SIZE,
cursor: pageParam, cursor: pageParam,
}) })
return res.data
}, },
initialPageParam: undefined, initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
@@ -72,7 +73,7 @@ export function* findAllProfilesInQueryData(
did: string, did: string,
): Generator<AppBskyActorDefs.ProfileViewBasic, void> { ): Generator<AppBskyActorDefs.ProfileViewBasic, void> {
const queryDatas = queryClient.getQueriesData< const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyFeedGetQuotes.OutputSchema> InfiniteData<app.bsky.feed.getQuotes.$OutputBody>
>({ >({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })
@@ -99,7 +100,7 @@ export function* findAllPostsInQueryData(
uri: string, uri: string,
): Generator<AppBskyFeedDefs.PostView, undefined> { ): Generator<AppBskyFeedDefs.PostView, undefined> {
const queryDatas = queryClient.getQueriesData< const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyFeedGetQuotes.OutputSchema> InfiniteData<app.bsky.feed.getQuotes.$OutputBody>
>({ >({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })
+11 -12
View File
@@ -1,7 +1,5 @@
import { import {type AppBskyActorDefs} from '@atproto/api'
type AppBskyActorDefs, import {type AtUriString} from '@atproto/syntax'
type AppBskyFeedGetRepostedBy,
} from '@atproto/api'
import { import {
type InfiniteData, type InfiniteData,
type QueryClient, type QueryClient,
@@ -9,7 +7,8 @@ import {
useInfiniteQuery, useInfiniteQuery,
} from '@tanstack/react-query' } from '@tanstack/react-query'
import {useAgent} from '#/state/session' import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const PAGE_SIZE = 30 const PAGE_SIZE = 30
type RQPageParam = string | undefined type RQPageParam = string | undefined
@@ -19,22 +18,22 @@ const RQKEY_ROOT = 'post-reposted-by'
export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri] export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
export function usePostRepostedByQuery(resolvedUri: string | undefined) { export function usePostRepostedByQuery(resolvedUri: string | undefined) {
const agent = useAgent() const client = useAppviewClient()
return useInfiniteQuery< return useInfiniteQuery<
AppBskyFeedGetRepostedBy.OutputSchema, app.bsky.feed.getRepostedBy.$OutputBody,
Error, Error,
InfiniteData<AppBskyFeedGetRepostedBy.OutputSchema>, InfiniteData<app.bsky.feed.getRepostedBy.$OutputBody>,
QueryKey, QueryKey,
RQPageParam RQPageParam
>({ >({
queryKey: RQKEY(resolvedUri || ''), queryKey: RQKEY(resolvedUri || ''),
async queryFn({pageParam}: {pageParam: RQPageParam}) { async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.getRepostedBy({ return await client.call(app.bsky.feed.getRepostedBy, {
uri: resolvedUri || '', // the enabled flag prevents this from running until resolvedUri is set
uri: (resolvedUri || '') as AtUriString,
limit: PAGE_SIZE, limit: PAGE_SIZE,
cursor: pageParam, cursor: pageParam,
}) })
return res.data
}, },
initialPageParam: undefined, initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
@@ -47,7 +46,7 @@ export function* findAllProfilesInQueryData(
did: string, did: string,
): Generator<AppBskyActorDefs.ProfileView, void> { ): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData< const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyFeedGetRepostedBy.OutputSchema> InfiniteData<app.bsky.feed.getRepostedBy.$OutputBody>
>({ >({
queryKey: [RQKEY_ROOT], queryKey: [RQKEY_ROOT],
}) })