diff --git a/src/lib/api/__tests__/space-write.test.ts b/src/lib/api/__tests__/space-write.test.ts index 0057c3a6c..1fd4a1f21 100644 --- a/src/lib/api/__tests__/space-write.test.ts +++ b/src/lib/api/__tests__/space-write.test.ts @@ -5,9 +5,12 @@ import { LIKE_COLLECTION, POST_COLLECTION, spaceCreateRecord, + spaceDeleteIfSpace, spaceDeleteRecord, spaceLike, + spaceLikeIfSpace, spaceUnlike, + spaceUnlikeIfSpace, SpaceUnsupportedError, } from '#/lib/api/space-write' @@ -138,3 +141,52 @@ describe('space writes', () => { ).rejects.toThrow(/no record reference/) }) }) + +const PUBLIC_POST = 'at://did:plc:alice/app.bsky.feed.post/3kabc' +const PUBLIC_LIKE = 'at://did:plc:alice/app.bsky.feed.like/3klike' + +describe('interaction routing', () => { + it('likes a space post into its own space', async () => { + const {agent, calls} = agentWith() + + await spaceLikeIfSpace(agent, SUBJECT.uri, SUBJECT.cid) + + // The leak this prevents: a like in the public repo whose subject is a + // space URI announces that the private post exists. + expect(calls[0].path).toBe('/xrpc/com.atproto.space.createRecord') + expect(bodyOf(calls[0])).toMatchObject({ + space: SPACE, + collection: LIKE_COLLECTION, + }) + }) + + it('unlikes and deletes within the space the record belongs to', async () => { + const {agent, calls} = agentWith({body: {}}) + + await spaceUnlikeIfSpace( + agent, + `${SPACE}/did:plc:alice/${LIKE_COLLECTION}/3klike`, + ) + await spaceDeleteIfSpace( + agent, + `${SPACE}/did:plc:alice/${POST_COLLECTION}/3kpost`, + ) + + expect(calls.map(bodyOf)).toEqual([ + {space: SPACE, collection: LIKE_COLLECTION, rkey: '3klike'}, + {space: SPACE, collection: POST_COLLECTION, rkey: '3kpost'}, + ]) + }) + + it.each([ + ['like', (a: BskyAgent) => spaceLikeIfSpace(a, PUBLIC_POST, 'bafy')], + ['unlike', (a: BskyAgent) => spaceUnlikeIfSpace(a, PUBLIC_LIKE)], + ['delete', (a: BskyAgent) => spaceDeleteIfSpace(a, PUBLIC_POST)], + ])('leaves a public %s to the public repo', (_name, run) => { + const {agent, calls} = agentWith() + + // null means "not mine" — the caller falls back to the ordinary agent call. + expect(run(agent)).toBeNull() + expect(calls).toHaveLength(0) + }) +}) diff --git a/src/lib/api/space-write.ts b/src/lib/api/space-write.ts index 75b053e0d..9ebcf58ea 100644 --- a/src/lib/api/space-write.ts +++ b/src/lib/api/space-write.ts @@ -1,5 +1,7 @@ import {type BskyAgent} from '@atproto/api' +import {parseSpaceRecordUri, spaceUriOf} from '#/lib/api/space-uri' + /** * Writes into a permissioned space. * @@ -129,3 +131,39 @@ export async function spaceUnlike( if (!rkey) throw new Error(`Unusable like uri: ${likeUri}`) return spaceDeleteRecord(agent, space, LIKE_COLLECTION, rkey) } + +/* + * The three interactions below answer `null` for anything that is not a space + * record, so a caller reads as `spaceX(...) ?? publicX(...)`. The routing lives + * here rather than in the mutation hooks so it can be tested without them. + */ + +/** Like `uri` in its own space, or null if it is not a space record. */ +export function spaceLikeIfSpace( + agent: BskyAgent, + uri: string, + cid: string, +): Promise | null { + const ref = parseSpaceRecordUri(uri) + return ref ? spaceLike(agent, spaceUriOf(ref), {uri, cid}) : null +} + +/** Undo a like held in a space, or null if `likeUri` is not a space record. */ +export function spaceUnlikeIfSpace( + agent: BskyAgent, + likeUri: string, +): Promise | null { + const ref = parseSpaceRecordUri(likeUri) + return ref ? spaceUnlike(agent, spaceUriOf(ref), likeUri) : null +} + +/** Delete one's own space record, or null if `uri` is not one. */ +export function spaceDeleteIfSpace( + agent: BskyAgent, + uri: string, +): Promise | null { + const ref = parseSpaceRecordUri(uri) + return ref + ? spaceDeleteRecord(agent, spaceUriOf(ref), ref.collection, ref.rkey) + : null +} diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts index 7b936299a..d545f93d3 100644 --- a/src/state/queries/post.ts +++ b/src/state/queries/post.ts @@ -8,6 +8,11 @@ import { } from '@tanstack/react-query' import {communityXrpc} from '#/lib/api/community' +import { + spaceDeleteIfSpace, + spaceLikeIfSpace, + spaceUnlikeIfSpace, +} from '#/lib/api/space-write' import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue' import {updatePostShadow} from '#/state/cache/post-shadow' import {type Shadow} from '#/state/cache/types' @@ -216,7 +221,10 @@ function usePostLikeMutation( : undefined, feedDescriptor: feedDescriptor, }) - return agent.like(uri, cid, via) + // A like on a permissioned post is written into the space, never the + // public repo: a public like naming a space URI would announce that the + // private post exists, and who can see it. + return spaceLikeIfSpace(agent, uri, cid) ?? agent.like(uri, cid, via) }, }) } @@ -236,7 +244,7 @@ function usePostUnlikeMutation( logContext, feedDescriptor, }) - return agent.deleteLike(likeUri) + return spaceUnlikeIfSpace(agent, likeUri) ?? agent.deleteLike(likeUri) }, }) } @@ -356,6 +364,12 @@ export function usePostDeleteMutation() { const agent = useAgent() return useMutation({ mutationFn: async ({uri}) => { + // Checked before AtUri sees the URI: a space URI misparses there. + const spaceDelete = spaceDeleteIfSpace(agent, uri) + if (spaceDelete) { + await spaceDelete + return + } const parsedUri = new AtUri(uri) if (parsedUri.collection === COMMUNITY_POST_COLLECTION) { // Both deletes are idempotent, so ordering is safe and a retry after