forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {
2 type AppBskyActorDefs,
3 type AppBskyUnspeccedGetSuggestedUsersForExplore,
4} from '@atproto/api'
5import {type QueryClient, useQuery} from '@tanstack/react-query'
6
7import {
8 aggregateUserInterests,
9 createBskyTopicsHeader,
10} from '#/lib/api/feed/utils'
11import {logger} from '#/logger'
12import {getContentLanguages} from '#/state/preferences/languages'
13import {STALE} from '#/state/queries'
14import {usePreferencesQuery} from '#/state/queries/preferences'
15import {useAgent} from '#/state/session'
16
17export type QueryProps = {
18 category?: string | null
19 limit?: number
20}
21
22export const getSuggestedUsersForExploreQueryKeyRoot =
23 'unspecced-suggested-users-for-explore'
24export const createGetSuggestedUsersForExploreQueryKey = (
25 props: QueryProps,
26) => [getSuggestedUsersForExploreQueryKeyRoot, props.category, props.limit]
27
28export function useGetSuggestedUsersForExploreQuery(props: QueryProps = {}) {
29 const agent = useAgent()
30 const {data: preferences} = usePreferencesQuery()
31
32 return useQuery({
33 staleTime: STALE.MINUTES.THREE,
34 queryKey: createGetSuggestedUsersForExploreQueryKey(props),
35 queryFn: async () => {
36 const contentLangs = getContentLanguages().join(',')
37 const userInterests = aggregateUserInterests(preferences)
38
39 const {data} = await agent.app.bsky.unspecced.getSuggestedUsersForExplore(
40 {
41 category: props.category ?? undefined,
42 limit: props.limit || 10,
43 },
44 {
45 headers: {
46 ...createBskyTopicsHeader(userInterests),
47 'Accept-Language': contentLangs,
48 },
49 },
50 )
51
52 if (!data.recIdStr) {
53 logger.debug('getSuggestedUsersForExplore response missing recIdStr')
54 }
55 return {...data, recId: data.recIdStr}
56 },
57 })
58}
59
60export function* findAllProfilesInQueryData(
61 queryClient: QueryClient,
62 did: string,
63): Generator<AppBskyActorDefs.ProfileView, void> {
64 const responses =
65 queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForExplore.OutputSchema>(
66 {
67 queryKey: [getSuggestedUsersForExploreQueryKeyRoot],
68 },
69 )
70 for (const [_key, response] of responses) {
71 if (!response) {
72 continue
73 }
74
75 for (const actor of response.actors) {
76 if (actor.did === did) {
77 yield actor
78 }
79 }
80 }
81}