Skip to content

Commit

Permalink
Make composer state source of truth for images and video publish
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Oct 4, 2024
1 parent b38f789 commit ebb0cc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 34 deletions.
32 changes: 15 additions & 17 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
AppBskyEmbedDefs,
AppBskyEmbedExternal,
AppBskyEmbedImages,
AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia,
AppBskyEmbedVideo,
AppBskyFeedPostgate,
AtUri,
BlobRef,
BskyAgent,
ComAtprotoLabelDefs,
RichText,
Expand Down Expand Up @@ -46,14 +44,7 @@ interface PostOpts {
uri: string
cid: string
}
video?: {
blobRef: BlobRef
altText: string
captions: {lang: string; file: File}[]
aspectRatio?: AppBskyEmbedDefs.AspectRatio
}
extLink?: ExternalEmbedDraft
images?: ComposerImage[]
labels?: string[]
threadgate: ThreadgateAllowUISetting[]
postgate: AppBskyFeedPostgate.Record
Expand Down Expand Up @@ -230,13 +221,15 @@ async function resolveMedia(
| AppBskyEmbedVideo.Main
| undefined
> {
if (opts.images?.length) {
const state = opts.composerState
const media = state.embed.media
if (media?.type === 'images') {
logger.debug(`Uploading images`, {
count: opts.images.length,
count: media.images.length,
})
opts.onStateChange?.(`Uploading images...`)
const images: AppBskyEmbedImages.Image[] = await Promise.all(
opts.images.map(async (image, i) => {
media.images.map(async (image, i) => {
logger.debug(`Compressing image #${i}`)
const {path, width, height, mime} = await compressImage(image)
logger.debug(`Uploading image #${i}`)
Expand All @@ -253,9 +246,10 @@ async function resolveMedia(
images,
}
}
if (opts.video) {
if (media?.type === 'video' && media.video.status === 'done') {
const video = media.video
const captions = await Promise.all(
opts.video.captions
video.captions
.filter(caption => caption.lang !== '')
.map(async caption => {
const {data} = await agent.uploadBlob(caption.file, {
Expand All @@ -266,13 +260,17 @@ async function resolveMedia(
)
return {
$type: 'app.bsky.embed.video',
video: opts.video.blobRef,
alt: opts.video.altText || undefined,
video: video.pendingPublish.blobRef,
alt: video.altText || undefined,
captions: captions.length === 0 ? undefined : captions,
aspectRatio: opts.video.aspectRatio,
aspectRatio: {
width: video.asset.width,
height: video.asset.height,
},
}
}
if (opts.extLink) {
// TODO: Read this from composer state as well.
if (opts.extLink.embed) {
return undefined
}
Expand Down
18 changes: 1 addition & 17 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,29 +419,16 @@ export const ComposePost = ({
try {
postUri = (
await apilib.post(agent, {
composerState, // TODO: not used yet.
composerState, // TODO: move more state here.
rawText: richtext.text,
replyTo: replyTo?.uri,
images,
quote,
extLink,
labels,
threadgate: threadgateAllowUISettings,
postgate,
onStateChange: setProcessingState,
langs: toPostLanguages(langPrefs.postLanguage),
video:
videoState.status === 'done'
? {
blobRef: videoState.pendingPublish.blobRef,
altText: videoState.altText,
captions: videoState.captions,
aspectRatio: {
width: videoState.asset.width,
height: videoState.asset.height,
},
}
: undefined,
})
).uri
try {
Expand Down Expand Up @@ -538,10 +525,7 @@ export const ComposePost = ({
setLangPrefs,
threadgateAllowUISettings,
videoState.asset,
videoState.pendingPublish,
videoState.status,
videoState.altText,
videoState.captions,
],
)

Expand Down

0 comments on commit ebb0cc5

Please sign in to comment.