Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323import { type $Typed, type AppBskyGraphDefs, type AppBskyGraphGetList, type AppBskyGraphList, type AtpAgent, AtUri, type ComAtprotoRepoApplyWrites, type Facet, type Un$Typed,} from '@atproto/api'import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'import chunk from 'lodash.chunk'
import {uploadBlob} from '#/lib/api'import {until} from '#/lib/async/until'import {type ImageMeta} from '#/state/gallery'import {STALE} from '#/state/queries'import {useAgent, useSession} from '#/state/session'import {FEED_INFO_RQKEY_ROOT} from './feed'import {invalidate as invalidateMyLists} from './my-lists'import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'
export const RQKEY_ROOT = 'list'export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
export function useListQuery(uri?: string) { const agent = useAgent() return useQuery<AppBskyGraphDefs.ListView, Error>({ staleTime: STALE.MINUTES.ONE, queryKey: RQKEY(uri || ''), async queryFn() { if (!uri) { throw new Error('URI not provided') } const res = await agent.app.bsky.graph.getList({ list: uri, limit: 1, }) return res.data.list }, enabled: !!uri, })}
export interface ListCreateMutateParams { purpose: string name: string description: string descriptionFacets: Facet[] | undefined avatar: ImageMeta | null | undefined}export function useListCreateMutation() { const {currentAccount} = useSession() const queryClient = useQueryClient() const agent = useAgent() return useMutation<{uri: string; cid: string}, Error, ListCreateMutateParams>( { async mutationFn({ purpose, name, description, descriptionFacets, avatar, }) { if (!currentAccount) { throw new Error('Not signed in') } if ( purpose !== 'app.bsky.graph.defs#curatelist' && purpose !== 'app.bsky.graph.defs#modlist' ) { throw new Error('Invalid list purpose: must be curatelist or modlist') } const record: Un$Typed<AppBskyGraphList.Record> = { purpose, name, description, descriptionFacets, avatar: undefined, createdAt: new Date().toISOString(), } if (avatar) { const blobRes = await uploadBlob(agent, avatar.path, avatar.mime) record.avatar = blobRes.data.blob } const res = await agent.app.bsky.graph.list.create( { repo: currentAccount.did, }, record, )
// wait for the appview to update await whenAppViewReady( agent, res.uri, (v: AppBskyGraphGetList.Response) => { return typeof v?.data?.list.uri === 'string' }, ) return res }, onSuccess() { invalidateMyLists(queryClient) queryClient.invalidateQueries({ queryKey: PROFILE_LISTS_RQKEY(currentAccount!.did), }) }, }, )}
export interface ListMetadataMutateParams { uri: string name: string description: string descriptionFacets: Facet[] | undefined avatar: ImageMeta | null | undefined}export function useListMetadataMutation() { const {currentAccount} = useSession() const agent = useAgent() const queryClient = useQueryClient() return useMutation< {uri: string; cid: string}, Error, ListMetadataMutateParams >({ async mutationFn({uri, name, description, descriptionFacets, avatar}) { const {hostname, rkey} = new AtUri(uri) if (!currentAccount) { throw new Error('Not signed in') } if (currentAccount.did !== hostname) { throw new Error('You do not own this list') }
// get the current record const {value: record} = await agent.app.bsky.graph.list.get({ repo: currentAccount.did, rkey, })
// update the fields record.name = name record.description = description record.descriptionFacets = descriptionFacets if (avatar) { const blobRes = await uploadBlob(agent, avatar.path, avatar.mime) record.avatar = blobRes.data.blob } else if (avatar === null) { record.avatar = undefined } const res = ( await agent.com.atproto.repo.putRecord({ repo: currentAccount.did, collection: 'app.bsky.graph.list', rkey, record, }) ).data
// wait for the appview to update await whenAppViewReady( agent, res.uri, (v: AppBskyGraphGetList.Response) => { const list = v.data.list return ( list.name === record.name && list.description === record.description ) }, ) return res }, onSuccess(data, variables) { invalidateMyLists(queryClient) queryClient.invalidateQueries({ queryKey: PROFILE_LISTS_RQKEY(currentAccount!.did), }) queryClient.invalidateQueries({ queryKey: RQKEY(variables.uri), }) queryClient.invalidateQueries({ queryKey: [FEED_INFO_RQKEY_ROOT], }) }, })}
export function useListDeleteMutation() { const {currentAccount} = useSession() const agent = useAgent() const queryClient = useQueryClient() return useMutation<void, Error, {uri: string}>({ mutationFn: async ({uri}) => { if (!currentAccount) { return } // fetch all the listitem records that belong to this list let cursor let listitemRecordUris: string[] = [] for (let i = 0; i < 100; i++) { const res = await agent.app.bsky.graph.listitem.list({ repo: currentAccount.did, cursor, limit: 100, }) listitemRecordUris = listitemRecordUris.concat( res.records .filter(record => record.value.list === uri) .map(record => record.uri), ) cursor = res.cursor if (!cursor) { break } }
// batch delete the list and listitem records const createDel = ( uri: string, ): $Typed<ComAtprotoRepoApplyWrites.Delete> => { const urip = new AtUri(uri) return { $type: 'com.atproto.repo.applyWrites#delete', collection: urip.collection, rkey: urip.rkey, } } const writes = listitemRecordUris .map(uri => createDel(uri)) .concat([createDel(uri)])
// apply in chunks for (const writesChunk of chunk(writes, 10)) { await agent.com.atproto.repo.applyWrites({ repo: currentAccount.did, writes: writesChunk, }) }
// wait for the appview to update await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => { return !v?.success }) }, onSuccess() { invalidateMyLists(queryClient) queryClient.invalidateQueries({ queryKey: PROFILE_LISTS_RQKEY(currentAccount!.did), }) // TODO!! /* dont await */ this.rootStore.preferences.removeSavedFeed(this.uri) }, })}
export function useListMuteMutation() { const queryClient = useQueryClient() const agent = useAgent() return useMutation<void, Error, {uri: string; mute: boolean}>({ mutationFn: async ({uri, mute}) => { if (mute) { await agent.muteModList(uri) } else { await agent.unmuteModList(uri) }
await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => { return Boolean(v?.data.list.viewer?.muted) === mute }) }, onSuccess(data, variables) { queryClient.invalidateQueries({ queryKey: RQKEY(variables.uri), }) }, })}
export function useListBlockMutation() { const queryClient = useQueryClient() const agent = useAgent() return useMutation<void, Error, {uri: string; block: boolean}>({ mutationFn: async ({uri, block}) => { if (block) { await agent.blockModList(uri) } else { await agent.unblockModList(uri) }
await whenAppViewReady(agent, uri, (v: AppBskyGraphGetList.Response) => { return block ? typeof v?.data.list.viewer?.blocked === 'string' : !v?.data.list.viewer?.blocked }) }, onSuccess(data, variables) { queryClient.invalidateQueries({ queryKey: RQKEY(variables.uri), }) }, })}
async function whenAppViewReady( agent: AtpAgent, uri: string, fn: (res: AppBskyGraphGetList.Response) => boolean,) { await until( 5, // 5 tries 1e3, // 1s delay between tries fn, () => agent.app.bsky.graph.getList({ list: uri, limit: 1, }), )}