diff --git a/src/state/queries/__tests__/list-block.test.tsx b/src/state/queries/__tests__/list-block.test.tsx new file mode 100644 index 000000000..787c937ef --- /dev/null +++ b/src/state/queries/__tests__/list-block.test.tsx @@ -0,0 +1,82 @@ +import {type PropsWithChildren} from 'react' +import {QueryClient, QueryClientProvider} from '@tanstack/react-query' +import {act, renderHook} from '@testing-library/react-native' + +import {until} from '#/lib/async/until' +import {useAppviewClient, usePdsClient} from '#/state/session' +import {app} from '#/lexicons' +import {useListBlockMutation} from '../list' + +jest.mock('#/lib/async/until', () => ({until: jest.fn()})) +jest.mock('#/lib/api', () => ({uploadBlob: jest.fn()})) +jest.mock('../feed', () => ({FEED_INFO_RQKEY_ROOT: 'feed-info'})) +jest.mock('../my-lists', () => ({invalidate: jest.fn()})) +jest.mock('../profile-lists', () => ({RQKEY: jest.fn()})) +jest.mock('#/state/session', () => ({ + useAppviewClient: jest.fn(), + usePdsClient: jest.fn(), + useSession: jest.fn(), +})) + +const LIST = 'at://did:plc:creator/app.bsky.graph.list/list' +const BLOCK = 'at://did:plc:viewer/app.bsky.graph.listblock/3kblock' + +function setup(blocked?: string) { + const queryClient = new QueryClient({ + defaultOptions: { + queries: {gcTime: Infinity, retry: false}, + mutations: {gcTime: Infinity, retry: false}, + }, + }) + const pdsClient = {call: jest.fn(), delete: jest.fn()} + const appviewClient = { + call: jest.fn().mockResolvedValue({list: {uri: LIST, viewer: {blocked}}}), + } + jest.mocked(usePdsClient).mockReturnValue(pdsClient as never) + jest.mocked(useAppviewClient).mockReturnValue(appviewClient as never) + jest.mocked(until).mockResolvedValue(true) + const wrapper = ({children}: PropsWithChildren) => ( + {children} + ) + const hook = renderHook(() => useListBlockMutation(), {wrapper}) + return {hook, pdsClient, appviewClient} +} + +beforeEach(() => jest.clearAllMocks()) + +describe('useListBlockMutation', () => { + it('finds the block on the appview and deletes it on the account server', async () => { + const {hook, pdsClient, appviewClient} = setup(BLOCK) + + await act(() => hook.result.current.mutateAsync({uri: LIST, block: false})) + + expect(appviewClient.call).toHaveBeenCalledWith(app.bsky.graph.getList, { + list: LIST, + limit: 1, + }) + expect(pdsClient.call).not.toHaveBeenCalled() + expect(pdsClient.delete).toHaveBeenCalledWith(app.bsky.graph.listblock, { + rkey: '3kblock', + }) + }) + + it('deletes nothing when the list is not blocked', async () => { + const {hook, pdsClient} = setup(undefined) + + await act(() => hook.result.current.mutateAsync({uri: LIST, block: false})) + + expect(pdsClient.delete).not.toHaveBeenCalled() + }) + + it('creates the block on the account server', async () => { + const {hook, pdsClient, appviewClient} = setup(undefined) + + await act(() => hook.result.current.mutateAsync({uri: LIST, block: true})) + + expect(pdsClient.call).toHaveBeenCalledTimes(1) + expect(pdsClient.call).toHaveBeenCalledWith(expect.anything(), { + list: LIST, + }) + expect(appviewClient.call).not.toHaveBeenCalled() + }) +}) diff --git a/src/state/queries/list.ts b/src/state/queries/list.ts index a40f7b3fc..529f241af 100644 --- a/src/state/queries/list.ts +++ b/src/state/queries/list.ts @@ -1,11 +1,6 @@ import {type $Typed, type Client} from '@atproto/lex' import {AtUri, type AtUriString, toDatetimeString} from '@atproto/syntax' -import { - blockActorList, - muteActorList, - unblockActorList, - unmuteActorList, -} from '@bsky/sdk' +import {blockActorList, muteActorList, unmuteActorList} from '@bsky/sdk' import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' import chunk from 'lodash.chunk' @@ -387,7 +382,21 @@ export function useListBlockMutation() { if (block) { await pdsClient.call(blockActorList, {list: uri as AtUriString}) } else { - await pdsClient.call(unblockActorList, {list: uri as AtUriString}) + /* + * The SDK's unblock helper looks the block up through the client it + * deletes with, which would ask the account server's own appview + * rather than the one this app reads from. + */ + const res = await appviewClient.call(app.bsky.graph.getList, { + list: uri as AtUriString, + limit: 1, + }) + const blocked = res.list.viewer?.blocked + if (blocked) { + await pdsClient.delete(app.bsky.graph.listblock, { + rkey: new AtUri(blocked).rkey, + }) + } } await whenAppViewReady(appviewClient, uri, v => {