forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {
2 type AppBskyActorDefs,
3 type AppBskyUnspeccedGetSuggestedUsersForDiscover,
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 limit?: number
19}
20
21export const getSuggestedUsersForDiscoverQueryKeyRoot =
22 'unspecced-suggested-users-for-explore'
23export const createGetSuggestedUsersForDiscoverQueryKey = (props: {
24 limit?: number
25}) => [getSuggestedUsersForDiscoverQueryKeyRoot, props.limit]
26
27export function useGetSuggestedUsersForDiscoverQuery(props: QueryProps = {}) {
28 const agent = useAgent()
29 const {data: preferences} = usePreferencesQuery()
30
31 return useQuery({
32 staleTime: STALE.MINUTES.THREE,
33 queryKey: createGetSuggestedUsersForDiscoverQueryKey({limit: props.limit}),
34 queryFn: async () => {
35 const contentLangs = getContentLanguages().join(',')
36 const userInterests = aggregateUserInterests(preferences)
37
38 const {data} =
39 await agent.app.bsky.unspecced.getSuggestedUsersForDiscover(
40 {
41 limit: props.limit || 10,
42 },
43 {
44 headers: {
45 ...createBskyTopicsHeader(userInterests),
46 'Accept-Language': contentLangs,
47 },
48 },
49 )
50 if (!data.recIdStr) {
51 logger.debug('getSuggestedUsersForDiscover response missing recIdStr')
52 }
53 return {...data, recId: data.recIdStr}
54 },
55 })
56}
57
58export function* findAllProfilesInQueryData(
59 queryClient: QueryClient,
60 did: string,
61): Generator<AppBskyActorDefs.ProfileView, void> {
62 const responses =
63 queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForDiscover.OutputSchema>(
64 {
65 queryKey: [getSuggestedUsersForDiscoverQueryKeyRoot],
66 },
67 )
68 for (const [_key, response] of responses) {
69 if (!response) {
70 continue
71 }
72
73 for (const actor of response.actors) {
74 if (actor.did === did) {
75 yield actor
76 }
77 }
78 }
79}