From e009b968ece79984bb933234df8a4c27f33b0715 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Wed, 16 Sep 2026 23:40:31 -0400 Subject: [PATCH] feat(composer): mark private-space videos on upload --- src/lib/media/video/__tests__/upload.test.ts | 47 ++++++++++ src/lib/media/video/upload.ts | 3 + src/lib/media/video/upload.web.ts | 3 + src/view/com/composer/Composer.tsx | 10 +- .../composer/state/__tests__/composer.test.ts | 91 +++++++++++++++++++ src/view/com/composer/state/composer.ts | 78 +++++++++++----- src/view/com/composer/state/video.ts | 2 + 7 files changed, 211 insertions(+), 23 deletions(-) create mode 100644 src/lib/media/video/__tests__/upload.test.ts create mode 100644 src/view/com/composer/state/__tests__/composer.test.ts diff --git a/src/lib/media/video/__tests__/upload.test.ts b/src/lib/media/video/__tests__/upload.test.ts new file mode 100644 index 000000000..1381e2617 --- /dev/null +++ b/src/lib/media/video/__tests__/upload.test.ts @@ -0,0 +1,47 @@ +import {uploadVideo} from '../upload' + +const mockCreateUploadTask = jest.fn() + +jest.mock('expo-file-system/legacy', () => ({ + FileSystemUploadType: {BINARY_CONTENT: 0}, + createUploadTask: mockCreateUploadTask, +})) +jest.mock('#/lib/api/service-auth', () => ({ + getServiceAuthToken: jest.fn().mockResolvedValue('token'), +})) +jest.mock('../upload.shared', () => ({ + getVideoUploadLimits: jest.fn().mockResolvedValue(undefined), +})) + +describe('uploadVideo', () => { + beforeEach(() => { + mockCreateUploadTask.mockClear() + mockCreateUploadTask.mockReturnValue({ + uploadAsync: jest.fn().mockResolvedValue({body: '{"jobId":"job"}'}), + }) + }) + + it.each([ + [true, 'true'], + [false, null], + ])( + 'sets private=%s only for private uploads', + async (isPrivate, expected) => { + await uploadVideo({ + video: {uri: 'file:///video.mp4', mimeType: 'video/mp4'} as never, + agent: {} as never, + did: 'did:plc:test', + setProgress: jest.fn(), + signal: new AbortController().signal, + i18n: {_: (value: unknown) => value} as never, + isPrivate, + }) + + const calls = mockCreateUploadTask.mock.calls as Array< + [string, ...unknown[]] + > + const url = new URL(calls[0][0]) + expect(url.searchParams.get('private')).toBe(expected) + }, + ) +}) diff --git a/src/lib/media/video/upload.ts b/src/lib/media/video/upload.ts index c3504d379..88c429538 100644 --- a/src/lib/media/video/upload.ts +++ b/src/lib/media/video/upload.ts @@ -18,6 +18,7 @@ export async function uploadVideo({ setProgress, signal, i18n, + isPrivate, }: { video: CompressedVideo agent: AtpAgent @@ -25,6 +26,7 @@ export async function uploadVideo({ setProgress: (progress: number) => void signal: AbortSignal i18n: I18n + isPrivate: boolean }) { if (signal.aborted) { throw new AbortError() @@ -34,6 +36,7 @@ export async function uploadVideo({ const uri = createVideoEndpointUrl('/xrpc/app.bsky.video.uploadVideo', { did, name: `${nanoid(12)}.${mimeToExt(video.mimeType)}`, + ...(isPrivate ? {private: 'true'} : {}), }) if (signal.aborted) { diff --git a/src/lib/media/video/upload.web.ts b/src/lib/media/video/upload.web.ts index 271827c5e..c4b02b08e 100644 --- a/src/lib/media/video/upload.web.ts +++ b/src/lib/media/video/upload.web.ts @@ -17,6 +17,7 @@ export async function uploadVideo({ setProgress, signal, i18n, + isPrivate, }: { video: CompressedVideo agent: AtpAgent @@ -24,6 +25,7 @@ export async function uploadVideo({ setProgress: (progress: number) => void signal: AbortSignal i18n: I18n + isPrivate: boolean }) { if (signal.aborted) { throw new AbortError() @@ -33,6 +35,7 @@ export async function uploadVideo({ const uri = createVideoEndpointUrl('/xrpc/app.bsky.video.uploadVideo', { did, name: `${nanoid(12)}.${mimeToExt(video.mimeType)}`, + ...(isPrivate ? {private: 'true'} : {}), }) // Stream the Blob directly; loading the whole file as ArrayBuffer OOMs mobile Safari. diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx index ba2ff2982..12d925f90 100644 --- a/src/view/com/composer/Composer.tsx +++ b/src/view/com/composer/Composer.tsx @@ -422,6 +422,12 @@ export const ComposePost = ({ ) const thread = composerState.thread + const spaceTarget = + thread.communitySpaceUri ?? + (isSpaceBackedFeed(thread.communityFeed?.config) + ? thread.communityFeed.config.space + : undefined) + const isPrivateVideoTarget = !!spaceTarget // Clear error when composer content changes, but only if all posts are // back within the character limit. @@ -471,9 +477,10 @@ export const ComposePost = ({ currentDid, abortController.signal, i18n, + isPrivateVideoTarget, ) }, - [i18n, agent, currentDid, composerDispatch], + [i18n, agent, currentDid, composerDispatch, isPrivateVideoTarget], ) const onInitVideo = useNonReactiveCallback(() => { @@ -618,6 +625,7 @@ export const ComposePost = ({ currentDid, abortController.signal, i18n, + isPrivateVideoTarget, ) } catch (e) { logger.error('Failed to restore video from draft', { diff --git a/src/view/com/composer/state/__tests__/composer.test.ts b/src/view/com/composer/state/__tests__/composer.test.ts new file mode 100644 index 000000000..09aa905fa --- /dev/null +++ b/src/view/com/composer/state/__tests__/composer.test.ts @@ -0,0 +1,91 @@ +jest.mock('#/lib/api/community-feed', () => ({ + isSpaceBackedFeed: (config: {space?: string} | undefined) => !!config?.space, +})) +jest.mock('#/state/gallery', () => ({createInitialImages: jest.fn()})) +jest.mock('#/lib/strings/url-helpers', () => ({ + isBskyPostUrl: jest.fn(), + postUriToRelativePath: jest.fn(), +})) +jest.mock('#/state/queries/postgate/util', () => ({ + createPostgateRecord: jest.fn(() => ({})), +})) +jest.mock('#/state/queries/threadgate', () => ({ + threadgateRecordToAllowUISetting: jest.fn(() => []), +})) +jest.mock('#/state/shell/composer', () => ({})) +jest.mock('#/view/com/composer/text-input/text-input-util', () => ({ + shortenLinks: jest.fn(value => value), +})) + +import {composerReducer, type ComposerState} from '../composer' + +const privateTarget = { + feed: 'at://did:plc:test/app.bsky.feed.generator/private', + name: 'Private', + serviceDid: 'did:web:feeds.test', + config: { + $type: 'community.blacksky.feed.config' as const, + contentType: 'communityRecord' as const, + visibility: 'gated' as const, + space: 'at://did:plc:test/space/community.blacksky.feed/private', + group: 'members', + createdAt: '2026-08-30T00:00:00.000Z', + }, +} + +function stateWithVideo(): ComposerState { + const abortController = new AbortController() + return { + activePostIndex: 0, + mutableNeedsFocusActive: false, + isDirty: false, + thread: { + posts: [ + { + id: 'post', + richtext: {} as never, + shortenedGraphemeLength: 0, + labels: [], + embed: { + quote: undefined, + link: undefined, + media: { + type: 'video', + video: { + status: 'uploading', + progress: 0, + abortController, + asset: {} as never, + video: {} as never, + altText: '', + captions: [], + }, + }, + }, + }, + ], + postgate: {} as never, + threadgate: [], + blackskyOnly: false, + }, + } +} + +describe('composer target changes', () => { + it('clears and aborts a pending video when entering a space-backed feed', () => { + const state = stateWithVideo() + const next = composerReducer(state, { + type: 'set_post_target', + target: privateTarget, + }) + + expect(next.thread.posts[0].embed.media).toBeUndefined() + expect( + ( + state.thread.posts[0].embed.media as { + video: {abortController: AbortController} + } + ).video.abortController.signal.aborted, + ).toBe(true) + }) +}) diff --git a/src/view/com/composer/state/composer.ts b/src/view/com/composer/state/composer.ts index 3f0684bb1..d0ac8d6c4 100644 --- a/src/view/com/composer/state/composer.ts +++ b/src/view/com/composer/state/composer.ts @@ -8,7 +8,10 @@ import { } from '@atproto/api' import {nanoid} from 'nanoid/non-secure' -import {type CommunityFeedTarget} from '#/lib/api/community-feed' +import { + type CommunityFeedTarget, + isSpaceBackedFeed, +} from '#/lib/api/community-feed' import {type SelfLabel} from '#/lib/moderation' import {insertMentionAt} from '#/lib/strings/mention-manip' import {shortenLinks} from '#/lib/strings/rich-text-manip' @@ -175,6 +178,33 @@ export type ComposerAction = draftId: string } +function hasSpaceVideoTarget(thread: ThreadDraft): boolean { + return ( + !!thread.communitySpaceUri || + isSpaceBackedFeed(thread.communityFeed?.config) + ) +} + +function clearVideosOnTargetChange( + state: ComposerState, + nextThread: ThreadDraft, +): ComposerState { + if (hasSpaceVideoTarget(state.thread) === hasSpaceVideoTarget(nextThread)) { + return state + } + + const posts = state.thread.posts.map(post => + post.embed.media?.type === 'video' + ? postReducer(post, {type: 'embed_remove_video'}) + : post, + ) + return { + ...state, + isDirty: true, + thread: {...nextThread, posts}, + } +} + /** * Threshold for picking between embed variants. <= this count uses the * legacy `app.bsky.embed.images` shape; > this count promotes to @@ -227,31 +257,35 @@ export function composerReducer( } } case 'toggle_blacksky_only': { - return { - ...state, - isDirty: true, - thread: { - ...state.thread, - blackskyOnly: !state.thread.blackskyOnly, - communityFeed: undefined, - communityFeedUri: undefined, - communitySpaceUri: undefined, + const nextThread = { + ...state.thread, + blackskyOnly: !state.thread.blackskyOnly, + communityFeed: undefined, + communityFeedUri: undefined, + communitySpaceUri: undefined, + } + return clearVideosOnTargetChange( + { + ...state, }, - } + nextThread, + ) } case 'set_post_target': { - return { - ...state, - isDirty: true, - thread: { - ...state.thread, - blackskyOnly: action.target === 'blacksky', - communityFeed: - typeof action.target === 'string' ? undefined : action.target, - communityFeedUri: - typeof action.target === 'string' ? undefined : action.target.feed, + const nextThread = { + ...state.thread, + blackskyOnly: action.target === 'blacksky', + communityFeed: + typeof action.target === 'string' ? undefined : action.target, + communityFeedUri: + typeof action.target === 'string' ? undefined : action.target.feed, + } + return clearVideosOnTargetChange( + { + ...state, }, - } + nextThread, + ) } case 'update_post': { let nextPosts = state.thread.posts diff --git a/src/view/com/composer/state/video.ts b/src/view/com/composer/state/video.ts index 54bfcd67a..27dd1733a 100644 --- a/src/view/com/composer/state/video.ts +++ b/src/view/com/composer/state/video.ts @@ -265,6 +265,7 @@ export async function processVideo( did: string, signal: AbortSignal, i18n: I18n, + isPrivate: boolean, ) { let video: CompressedVideo | undefined try { @@ -299,6 +300,7 @@ export async function processVideo( did, signal, i18n, + isPrivate, setProgress: p => { dispatch({type: 'update_progress', progress: p, signal}) }, -- knotmirror 0.0.1