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 {useConstellationInstance} from '#/state/preferences/constellation-instance'
10import {
11 useDeerVerificationEnabled,
12 useDeerVerificationTrusted,
13} from '#/state/preferences/deer-verification'
14import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
15import {useAgent, useSession} from '#/state/session'
16import {useAnalytics} from '#/analytics'
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 ax = useAnalytics()
31 const agent = useAgent()
32 const {currentAccount} = useSession()
33 const updateProfileVerificationCache = useUpdateProfileVerificationCache()
34
35 const qc = useQueryClient()
36 const deerVerificationEnabled = useDeerVerificationEnabled()
37 const deerVerificationTrusted = useDeerVerificationTrusted(
38 currentAccount?.did,
39 )
40 const constellationInstance = useConstellationInstance()
41
42 return useMutation({
43 async mutationFn({
44 profile,
45 verifications,
46 }: {
47 profile: bsky.profile.AnyProfileView
48 verifications: AppBskyActorDefs.VerificationView[]
49 }) {
50 if (!currentAccount) {
51 throw new Error('User not logged in')
52 }
53
54 const uris = new Set(verifications.map(v => v.uri))
55
56 await Promise.all(
57 Array.from(uris).map(uri => {
58 return agent.app.bsky.graph.verification.delete({
59 repo: currentAccount.did,
60 rkey: new AtUri(uri).rkey,
61 })
62 }),
63 )
64
65 if (deerVerificationEnabled) {
66 await until(
67 10,
68 2e3,
69 (link: ConstellationLink[]) => {
70 return link.length === 0
71 },
72 () =>
73 asyncGenCollect(
74 asyncGenFilter(
75 getTrustedConstellationVerifications(
76 constellationInstance,
77 profile.did,
78 deerVerificationTrusted,
79 ),
80 link => uris.has(asUri(link)),
81 ),
82 ),
83 )
84 } else {
85 await until(
86 5,
87 1e3,
88 ({data: profile}: AppBskyActorGetProfile.Response) => {
89 if (
90 !profile.verification?.verifications.some(v => uris.has(v.uri))
91 ) {
92 return true
93 }
94 return false
95 },
96 () => {
97 return agent.getProfile({actor: profile.did ?? ''})
98 },
99 )
100 }
101 },
102 async onSuccess(_, {profile}) {
103 ax.metric('verification:revoke', {})
104 await updateProfileVerificationCache({profile})
105 qc.invalidateQueries({
106 queryKey: DEER_VERIFICATION_RQKEY(profile.did, deerVerificationTrusted),
107 })
108 },
109 })
110}