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