diff options
Diffstat (limited to 'src/state/queries')
-rw-r--r-- | src/state/queries/list-members.ts | 41 | ||||
-rw-r--r-- | src/state/queries/starter-packs.ts | 51 |
2 files changed, 68 insertions, 24 deletions
diff --git a/src/state/queries/list-members.ts b/src/state/queries/list-members.ts index 3131a2ec3..b02cc9910 100644 --- a/src/state/queries/list-members.ts +++ b/src/state/queries/list-members.ts @@ -1,9 +1,15 @@ -import {AppBskyActorDefs, AppBskyGraphGetList} from '@atproto/api' +import { + AppBskyActorDefs, + AppBskyGraphDefs, + AppBskyGraphGetList, + BskyAgent, +} from '@atproto/api' import { InfiniteData, QueryClient, QueryKey, useInfiniteQuery, + useQuery, } from '@tanstack/react-query' import {STALE} from '#/state/queries' @@ -14,6 +20,7 @@ type RQPageParam = string | undefined const RQKEY_ROOT = 'list-members' export const RQKEY = (uri: string) => [RQKEY_ROOT, uri] +export const RQKEY_ALL = (uri: string) => [RQKEY_ROOT, uri, 'all'] export function useListMembersQuery(uri?: string, limit: number = PAGE_SIZE) { const agent = useAgent() @@ -40,6 +47,38 @@ export function useListMembersQuery(uri?: string, limit: number = PAGE_SIZE) { }) } +export function useAllListMembersQuery(uri?: string) { + const agent = useAgent() + return useQuery({ + staleTime: STALE.MINUTES.ONE, + queryKey: RQKEY_ALL(uri ?? ''), + queryFn: async () => { + return getAllListMembers(agent, uri!) + }, + enabled: Boolean(uri), + }) +} + +export async function getAllListMembers(agent: BskyAgent, uri: string) { + let hasMore = true + let cursor: string | undefined + const listItems: AppBskyGraphDefs.ListItemView[] = [] + // We want to cap this at 6 pages, just for anything weird happening with the api + let i = 0 + while (hasMore && i < 6) { + const res = await agent.app.bsky.graph.getList({ + list: uri, + limit: 50, + cursor, + }) + listItems.push(...res.data.items) + hasMore = Boolean(res.data.cursor) + cursor = res.data.cursor + } + i++ + return listItems +} + export async function invalidateListMembersQuery({ queryClient, uri, diff --git a/src/state/queries/starter-packs.ts b/src/state/queries/starter-packs.ts index 2cdb6b850..a3795d792 100644 --- a/src/state/queries/starter-packs.ts +++ b/src/state/queries/starter-packs.ts @@ -16,6 +16,7 @@ import { useQuery, useQueryClient, } from '@tanstack/react-query' +import chunk from 'lodash.chunk' import {until} from 'lib/async/until' import {createStarterPackList} from 'lib/generate-starterpack' @@ -200,36 +201,40 @@ export function useEditStarterPackMutation({ i.subject.did !== agent.session?.did && !profiles.find(p => p.did === i.subject.did && p.did), ) - if (removedItems.length !== 0) { - await agent.com.atproto.repo.applyWrites({ - repo: agent.session!.did, - writes: removedItems.map(i => ({ - $type: 'com.atproto.repo.applyWrites#delete', - collection: 'app.bsky.graph.listitem', - rkey: new AtUri(i.uri).rkey, - })), - }) + const chunks = chunk(removedItems, 50) + for (const chunk of chunks) { + await agent.com.atproto.repo.applyWrites({ + repo: agent.session!.did, + writes: chunk.map(i => ({ + $type: 'com.atproto.repo.applyWrites#delete', + collection: 'app.bsky.graph.listitem', + rkey: new AtUri(i.uri).rkey, + })), + }) + } } const addedProfiles = profiles.filter( p => !currentListItems.find(i => i.subject.did === p.did), ) - if (addedProfiles.length > 0) { - await agent.com.atproto.repo.applyWrites({ - repo: agent.session!.did, - writes: addedProfiles.map(p => ({ - $type: 'com.atproto.repo.applyWrites#create', - collection: 'app.bsky.graph.listitem', - value: { - $type: 'app.bsky.graph.listitem', - subject: p.did, - list: currentStarterPack.list?.uri, - createdAt: new Date().toISOString(), - }, - })), - }) + const chunks = chunk(addedProfiles, 50) + for (const chunk of chunks) { + await agent.com.atproto.repo.applyWrites({ + repo: agent.session!.did, + writes: chunk.map(p => ({ + $type: 'com.atproto.repo.applyWrites#create', + collection: 'app.bsky.graph.listitem', + value: { + $type: 'app.bsky.graph.listitem', + subject: p.did, + list: currentStarterPack.list?.uri, + createdAt: new Date().toISOString(), + }, + })), + }) + } } const rkey = parseStarterPackUri(currentStarterPack.uri)!.rkey |