forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {
2 type AppBskyActorDefs,
3 type AppBskyActorGetProfile,
4 AtUri,
5} from '@atproto/api'
6import {useMutation, useQueryClient} from '@tanstack/react-query'
7
8import {until} from '#/lib/async/until'
9import {logger} from '#/logger'
10import {useConstellationInstance} from '#/state/preferences/constellation-instance'
11import {
12 useDeerVerificationEnabled,
13 useDeerVerificationTrusted,
14} from '#/state/preferences/deer-verification'
15import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
16import {useAgent, useSession} from '#/state/session'
17import type * as bsky from '#/types/bsky'
18import {
19 asUri,
20 asyncGenCollect,
21 asyncGenFilter,
22 type ConstellationLink,
23} from '../constellation'
24import {
25 getTrustedConstellationVerifications,
26 RQKEY as DEER_VERIFICATION_RQKEY,
27} from '../deer-verification'
28
29export function useVerificationsRemoveMutation() {
30 const agent = useAgent()
31 const {currentAccount} = useSession()
32 const updateProfileVerificationCache = useUpdateProfileVerificationCache()
33
34 const qc = useQueryClient()
35 const deerVerificationEnabled = useDeerVerificationEnabled()
36 const deerVerificationTrusted = useDeerVerificationTrusted(
37 currentAccount?.did,
38 )
39 const constellationInstance = useConstellationInstance()
40
41 return useMutation({
42 async mutationFn({
43 profile,
44 verifications,
45 }: {
46 profile: bsky.profile.AnyProfileView
47 verifications: AppBskyActorDefs.VerificationView[]
48 }) {
49 if (!currentAccount) {
50 throw new Error('User not logged in')
51 }
52
53 const uris = new Set(verifications.map(v => v.uri))
54
55 await Promise.all(
56 Array.from(uris).map(uri => {
57 return agent.app.bsky.graph.verification.delete({
58 repo: currentAccount.did,
59 rkey: new AtUri(uri).rkey,
60 })
61 }),
62 )
63
64 if (deerVerificationEnabled) {
65 await until(
66 10,
67 2e3,
68 (link: ConstellationLink[]) => {
69 return link.length === 0
70 },
71 () =>
72 asyncGenCollect(
73 asyncGenFilter(
74 getTrustedConstellationVerifications(
75 constellationInstance,
76 profile.did,
77 deerVerificationTrusted,
78 ),
79 link => uris.has(asUri(link)),
80 ),
81 ),
82 )
83 } else {
84 await until(
85 5,
86 1e3,
87 ({data: profile}: AppBskyActorGetProfile.Response) => {
88 if (
89 !profile.verification?.verifications.some(v => uris.has(v.uri))
90 ) {
91 return true
92 }
93 return false
94 },
95 () => {
96 return agent.getProfile({actor: profile.did ?? ''})
97 },
98 )
99 }
100 },
101 async onSuccess(_, {profile}) {
102 logger.metric('verification:revoke', {}, {statsig: true})
103 await updateProfileVerificationCache({profile})
104 qc.invalidateQueries({
105 queryKey: DEER_VERIFICATION_RQKEY(profile.did, deerVerificationTrusted),
106 })
107 },
108 })
109}