diff --git a/package.json b/package.json index 657f5e73e..8707372df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blacksky.community", - "version": "1.128.1", + "version": "1.129.0", "private": true, "packageManager": "pnpm@11.7.0", "engines": { diff --git a/src/lib/api/__tests__/post-routing.test.ts b/src/lib/api/__tests__/post-routing.test.ts new file mode 100644 index 000000000..9a18b9eaf --- /dev/null +++ b/src/lib/api/__tests__/post-routing.test.ts @@ -0,0 +1,88 @@ +import {type AtpAgent} from '@atproto/api' +import {type QueryClient} from '@tanstack/react-query' + +import {post} from '../index' +import {postToSpace} from '../space-post' + +// `post()`'s module graph reaches the image picker, which pulls in native UI +// modules that cannot load under jest. Only the write routing is under test. +jest.mock('#/state/gallery', () => ({compressImage: jest.fn()})) +jest.mock('#/state/queries/resolve-link', () => ({ + fetchResolveGifQuery: jest.fn(), + fetchResolveLinkQuery: jest.fn(), +})) +jest.mock('#/state/queries/threadgate', () => ({ + createThreadgateRecord: jest.fn(), + threadgateAllowUISettingToAllowRecordValue: jest.fn(() => []), +})) + +jest.mock('../space-post', () => ({ + postToSpace: jest.fn(() => Promise.resolve({uris: ['at://space/post']})), +})) + +const SPACE = 'at://did:plc:community/space/community.blacksky.feed/private' + +const feedConfig = (space?: string) => ({ + $type: 'community.blacksky.feed.config', + contentType: 'communityRecord', + visibility: 'gated', + group: 'at://did:plc:community/community.blacksky.group/community', + createdAt: '2026-08-09T12:00:00.000Z', + ...(space ? {space} : {}), +}) + +const threadWith = (space?: string) => + ({ + posts: [], + postgate: {}, + threadgate: [], + blackskyOnly: false, + communityFeed: { + feed: 'at://did:plc:community/app.bsky.feed.generator/3m2feed', + name: 'Private', + serviceDid: 'did:web:feeds.example.com', + config: feedConfig(space), + }, + }) as never + +function mockAgent() { + const fetchHandler = jest.fn() + const applyWrites = jest.fn(() => Promise.resolve({})) + return { + agent: { + assertDid: 'did:plc:alice', + fetchHandler, + com: {atproto: {repo: {applyWrites}}}, + } as unknown as AtpAgent, + fetchHandler, + applyWrites, + } +} + +const queryClient = {invalidateQueries: jest.fn()} as unknown as QueryClient + +describe('post routing', () => { + beforeEach(() => jest.clearAllMocks()) + + it('sends a space-backed feed down the space write path', async () => { + const {agent, fetchHandler, applyWrites} = mockAgent() + + await post(agent, queryClient, {thread: threadWith(SPACE)}) + + expect(postToSpace).toHaveBeenCalledWith(agent, queryClient, SPACE, { + thread: threadWith(SPACE), + }) + // Nothing may reach the appview or the public repo on this path. + expect(fetchHandler).not.toHaveBeenCalled() + expect(applyWrites).not.toHaveBeenCalled() + }) + + it('leaves a feed without a space on the community path', async () => { + const {agent, applyWrites} = mockAgent() + + await post(agent, queryClient, {thread: threadWith(undefined)}) + + expect(postToSpace).not.toHaveBeenCalled() + expect(applyWrites).toHaveBeenCalled() + }) +}) diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 4f07d97a7..dd63988f7 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -26,7 +26,12 @@ import {CID} from 'multiformats/cid' import * as Hasher from 'multiformats/hashes/hasher' import {communityXrpc} from '#/lib/api/community' -import {admitFeedPost, fetchCommunityFeedTarget} from '#/lib/api/community-feed' +import { + admitFeedPost, + fetchCommunityFeedTarget, + isSpaceBackedFeed, +} from '#/lib/api/community-feed' +import {postToSpace} from '#/lib/api/space-post' import {IMAGE_SIZE_CONFIG_POSTS} from '#/lib/constants' import {isNetworkError} from '#/lib/strings/errors' import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip' @@ -81,6 +86,14 @@ export async function post( } opts.onStateChange?.(t`Processing...`) + // A space-backed feed keeps its content in the author's permissioned repo, + // so it takes an entirely different write path and must be checked before + // the community routing below, which would otherwise claim it. + const config = thread.communityFeed?.config + if (isSpaceBackedFeed(config)) { + return postToSpace(agent, queryClient, config.space, opts) + } + // Route to community post endpoint if the user explicitly toggled // Blacksky-Only, is replying to a community post, or is quoting one. const isReplyToCommunityPost = @@ -355,9 +368,6 @@ async function postCommunity( createdAt, expectedCid: cid, // Appview will verify this matches } - if (!reply && thread.communityFeed) { - submitBody.feed = thread.communityFeed.feed - } if (rt.facets?.length) { submitBody.facets = rt.facets }