Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import {type AppBskyLabelerDefs} from '@atproto/api'import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'import {z} from 'zod'
import {MAX_LABELERS} from '#/lib/constants'import {isAppLabeler} from '#/lib/moderation'import { PERSISTED_QUERY_GCTIME, PERSISTED_QUERY_ROOT, STALE,} from '#/state/queries'import { preferencesQueryKey, usePreferencesQuery,} from '#/state/queries/preferences'import {useAgent} from '#/state/session'
const labelerInfoQueryKeyRoot = 'labeler-info'export const labelerInfoQueryKey = (did: string) => [ labelerInfoQueryKeyRoot, did,]
const labelersInfoQueryKeyRoot = 'labelers-info'export const labelersInfoQueryKey = (dids: string[]) => [ labelersInfoQueryKeyRoot, dids.slice().sort(),]
const persistedLabelersDetailedInfoQueryKey = (dids: string[]) => [ PERSISTED_QUERY_ROOT, 'labelers-detailed-info', dids,]
export function useLabelerInfoQuery({ did, enabled,}: { did?: string enabled?: boolean}) { const agent = useAgent() return useQuery({ enabled: !!did && enabled !== false, queryKey: labelerInfoQueryKey(did as string), queryFn: async () => { const res = await agent.app.bsky.labeler.getServices({ dids: [did!], detailed: true, }) return res.data.views[0] as AppBskyLabelerDefs.LabelerViewDetailed }, })}
export function useLabelersInfoQuery({dids}: {dids: string[]}) { const agent = useAgent() return useQuery({ enabled: !!dids.length, queryKey: labelersInfoQueryKey(dids), queryFn: async () => { const res = await agent.app.bsky.labeler.getServices({dids}) return res.data.views as AppBskyLabelerDefs.LabelerView[] }, })}
export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) { const agent = useAgent() return useQuery({ enabled: !!dids.length, queryKey: persistedLabelersDetailedInfoQueryKey(dids), gcTime: PERSISTED_QUERY_GCTIME, staleTime: STALE.MINUTES.ONE, queryFn: async () => { const res = await agent.app.bsky.labeler.getServices({ dids, detailed: true, }) return res.data.views as AppBskyLabelerDefs.LabelerViewDetailed[] }, })}
export function useRemoveLabelersMutation() { const queryClient = useQueryClient() const agent = useAgent()
return useMutation({ async mutationFn({dids}: {dids: string[]}) { await Promise.all(dids.map(did => agent.removeLabeler(did))) }, async onSuccess() { await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, }) }, })}
export function useLabelerSubscriptionMutation() { const queryClient = useQueryClient() const agent = useAgent() const preferences = usePreferencesQuery()
return useMutation({ async mutationFn({did, subscribe}: {did: string; subscribe: boolean}) { // TODO z.object({ did: z.string(), subscribe: z.boolean(), }).parse({did, subscribe})
/** * If a user has invalid/takendown/deactivated labelers, we need to * remove them. We don't have a great way to do this atm on the server, * so we do it here. * * We also need to push validation into this method, since we need to * check {@link MAX_LABELERS} _after_ we've removed invalid or takendown * labelers. */ const labelerDids = ( preferences.data?.moderationPrefs?.labelers ?? [] ).map(l => l.did) const invalidLabelers: string[] = [] if (labelerDids.length) { const profiles = await agent.getProfiles({actors: labelerDids}) if (profiles.data) { for (const did of labelerDids) { const exists = profiles.data.profiles.find(p => p.did === did) if (exists) { // profile came back but it's not a valid labeler if (exists.associated && !exists.associated.labeler) { invalidLabelers.push(did) } } else { // no response came back, might be deactivated or takendown invalidLabelers.push(did) } } } } if (invalidLabelers.length) { await Promise.all(invalidLabelers.map(did => agent.removeLabeler(did))) }
if (subscribe) { const labelerCount = labelerDids.length - invalidLabelers.length if (labelerCount >= MAX_LABELERS) { throw new Error('MAX_LABELERS') } await agent.addLabeler(did) } else { if (isAppLabeler(did)) { return } await agent.removeLabeler(did) } }, async onSuccess() { await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, }) }, })}