forked from
vt3e.cat/bbell
wip bsky client for the web & android
1import { useAuthStore } from '@/stores/auth'
2import type { ViewerState } from '@atcute/bluesky/types/app/actor/defs'
3
4export function useFollowToggle() {
5 const auth = useAuthStore()
6 async function toggle(profile: { did: string; viewer?: ViewerState }) {
7 if (!auth.isAuthenticated || !auth.session) return
8 const rpc = auth.getRpc()
9 const original = profile.viewer?.following
10 profile.viewer = profile.viewer || {}
11 if (original) profile.viewer.following = undefined
12 else profile.viewer.following = `at://${profile.did}/app.bsky.graph.follow/temporary`
13
14 try {
15 if (original) {
16 const rkey = original.split('/').pop()!
17 await rpc.post('com.atproto.repo.deleteRecord', {
18 input: { collection: 'app.bsky.graph.follow', repo: auth.session.info.sub, rkey },
19 })
20 } else {
21 const { data, ok } = await rpc.post('com.atproto.repo.createRecord', {
22 input: {
23 collection: 'app.bsky.graph.follow',
24 repo: auth.session.info.sub,
25 record: {
26 $type: 'app.bsky.graph.follow',
27 subject: profile.did,
28 createdAt: new Date().toISOString(),
29 },
30 },
31 })
32 if (ok) profile.viewer.following = data.uri
33 }
34 } catch (e) {
35 // revert
36 profile.viewer.following = original
37 throw e
38 }
39 }
40
41 return { toggle }
42}