Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114import {type ImagePickerAsset} from 'expo-image-picker'import {type UriString} from '@atproto/lex'import {type AtUriString, toDatetimeString} from '@atproto/syntax'import {RichText} from '@bsky/sdk/richtext'import {nanoid} from 'nanoid/non-secure'
import { type CommunityFeedTarget, isSpaceBackedFeed,} from '#/lib/api/community-feed'import {isCommunityPostUri} from '#/lib/api/community-post'import { type AssemblyRef, isPollDraftPublishable, isPollTopicPublishable, POLL_MAX_STATEMENTS, type PollDraft,} from '#/lib/api/poll'import {isSpacePostUrl} from '#/lib/api/space-permalink'import {type VideoTelemetry} from '#/lib/media/video/telemetry'import {type SelfLabel} from '#/lib/moderation'import {insertMentionAt} from '#/lib/strings/mention-manip'import {shortenLinks} from '#/lib/strings/rich-text-manip'import { isBskyPostUrl, postUriToRelativePath, toBskyAppUrl,} from '#/lib/strings/url-helpers'import {logger} from '#/logger'import {type ComposerImage, createInitialImages} from '#/state/gallery'import {createPostgateRecord} from '#/state/queries/postgate/util'import {threadgateRecordToAllowUISetting} from '#/state/queries/threadgate'import {type ThreadgateAllowUISetting} from '#/state/queries/threadgate'import {type ComposerOpts} from '#/state/shell/composer'import { type LinkFacetMatch, suggestLinkCardUri,} from '#/view/com/composer/text-input/text-input-util'import { createEventDraft, type EventDraft, type EventDraftFields, type EventDraftStep, isEventDraftPublishable,} from '#/features/events/draft'import {type Gif} from '#/features/gifPicker/types'import {app} from '#/lexicons'import * as bsky from '#/types/bsky'import { createVideoState, type VideoAction, videoReducer, type VideoState,} from './video'
type ImagesMedia = { type: 'images' images: ComposerImage[]}
type GalleryMedia = { type: 'gallery' images: ComposerImage[]}
type VideoMedia = { type: 'video' video: VideoState}
type GifMedia = { type: 'gif' gif: Gif alt: string}
type Link = { type: 'link' /* * A URL the user typed or pasted, validated by the link resolver rather than * at construction, so it carries lex's `uri` brand for the record and view * slots it flows into. */ uri: UriString}
// This structure doesn't exactly correspond to the data model.// Instead, it maps to how the UI is organized, and how we present a post.export type EmbedDraft = { // We'll always submit quote and actual media (images, video, gifs) chosen by the user. quote: Link | undefined media: ImagesMedia | GalleryMedia | VideoMedia | GifMedia | undefined // This field may end up ignored if we have more important things to display than a link card: link: Link | undefined poll?: PollDraft event?: EventDraft}
export type PostDraft = { id: string richtext: RichText labels: SelfLabel[] embed: EmbedDraft shortenedGraphemeLength: number}
export type PostAction = | {type: 'update_richtext'; richtext: RichText} | {type: 'update_labels'; labels: SelfLabel[]} | {type: 'embed_add_images'; images: ComposerImage[]} | {type: 'embed_update_image'; image: ComposerImage} | {type: 'embed_remove_image'; image: ComposerImage} | { type: 'embed_add_video' asset: ImagePickerAsset abortController: AbortController telemetry: VideoTelemetry } | {type: 'embed_remove_video'} | {type: 'embed_update_video'; videoAction: VideoAction} | {type: 'embed_add_uri'; uri: UriString} | {type: 'embed_remove_quote'} | {type: 'embed_remove_link'} | {type: 'embed_add_gif'; gif: Gif} | {type: 'embed_update_gif'; alt: string} | {type: 'embed_remove_gif'} | {type: 'embed_add_poll'} | {type: 'embed_update_poll_statement'; index: number; text: string} | {type: 'embed_add_poll_statement'} | {type: 'embed_remove_poll_statement'; index: number} | {type: 'embed_set_poll_assembly'; assembly: AssemblyRef} | {type: 'embed_remove_poll'} | {type: 'embed_add_event'} | {type: 'embed_update_event'; fields: Partial<EventDraftFields>} | {type: 'embed_set_event_step'; step: EventDraftStep} | {type: 'embed_remove_event'}
export function postHasAttachment(post: PostDraft): boolean { return Boolean( post.embed.media || post.embed.link || post.embed.quote || post.embed.poll || post.embed.event, )}
// A poll or an event fills the post's only external embed, so nothing else// can be attached alongside it.function hasExclusiveEmbed(embed: EmbedDraft): boolean { return !!(embed.poll || embed.event)}
export function postHasContent(post: PostDraft): boolean { return post.richtext.text.trim().length > 0 || postHasAttachment(post)}
export function isPollPostable(post: PostDraft): boolean { const poll = post.embed.poll if (!poll) return true return ( isPollTopicPublishable(post.richtext.text) && isPollDraftPublishable(poll) && !post.embed.media && !post.embed.link && !post.embed.quote )}
export function isEventPostable(post: PostDraft): boolean { const event = post.embed.event if (!event) return true return ( isEventDraftPublishable(event) && !post.embed.media && !post.embed.link && !post.embed.quote )}
export type ThreadDraft = { posts: PostDraft[] postgate: app.bsky.feed.postgate.Main threadgate: ThreadgateAllowUISetting[] blackskyOnly: boolean communityFeed?: CommunityFeedTarget communityFeedUri?: string /** * The space a parent post lives in, inherited when replying to or quoting * one. Distinct from `communityFeedUri`, which names a feed the author * chose to post into: a space is where content goes, a feed is a view. */ communitySpaceUri?: string}
export type ComposerState = { thread: ThreadDraft activePostIndex: number mutableNeedsFocusActive: boolean /** ID of the draft being edited, if any. Used to update existing draft on save. */ draftId?: string /** Whether the composer has been modified since loading a draft. */ isDirty: boolean /** Map of localId -> loaded media path/URL for the current draft. Used for re-saving without re-copying media. */ loadedMediaMap?: Map<string, string> /** Set of original localRef paths from the draft being edited. Used to identify orphaned media on save. */ originalLocalRefs?: Set<string>}
export type ComposerAction = | {type: 'update_postgate'; postgate: app.bsky.feed.postgate.Main} | {type: 'update_threadgate'; threadgate: ThreadgateAllowUISetting[]} | {type: 'toggle_blacksky_only'} | { type: 'set_post_target' target: 'public' | 'blacksky' | CommunityFeedTarget } | { type: 'update_post' postId: string postAction: PostAction } | { type: 'add_post' } | { type: 'remove_post' postId: string } | { type: 'focus_post' postId: string } | { type: 'restore_from_draft' draftId: string posts: PostDraft[] threadgateAllow: app.bsky.draft.defs.Draft['threadgateAllow'] postgateEmbeddingRules: app.bsky.draft.defs.Draft['postgateEmbeddingRules']
/** Map of localRefPath -> loaded media path/URL */ loadedMedia: Map<string, string> /** Set of original localRef paths from the draft. Used to identify orphaned media on save. */ originalLocalRefs: Set<string> } | { type: 'clear' initInteractionSettings: app.bsky.actor.defs.PostInteractionSettingsPref | undefined } | { type: 'mark_saved' draftId: string }
function hasSpaceVideoTarget(thread: ThreadDraft): boolean { return ( !!thread.communitySpaceUri || isSpaceBackedFeed(thread.communityFeed?.config) )}
function isPublicFeedTarget(thread: ThreadDraft): boolean { const feed = thread.communityFeed if (!feed) { // A feed known only by its URI is resolved at publish, so until then // nothing shows that it is public. return !thread.communityFeedUri } return ( feed.config.contentType === 'publicRecord' && !isSpaceBackedFeed(feed.config) )}
function isCommunityQuote(uri: string | undefined): boolean { return isCommunityPostUri(uri) || isSpacePostUrl(uri)}
export function isPublicTarget(thread: ThreadDraft): boolean { return ( !thread.blackskyOnly && !thread.communitySpaceUri && isPublicFeedTarget(thread) )}
export function isPollAllowed( thread: ThreadDraft, replyTo: string | undefined,): boolean { return ( isPublicTarget(thread) && !isCommunityPostUri(replyTo) && !thread.posts.some(post => isCommunityQuote(post.embed.quote?.uri)) )}
export function threadHasPoll(thread: ThreadDraft): boolean { return thread.posts.some(post => post.embed.poll)}
export const isEventAllowed = isPollAllowed
export function threadHasEvent(thread: ThreadDraft): boolean { return thread.posts.some(post => post.embed.event)}
function keepFirst(posts: PostDraft[], key: 'poll' | 'event'): PostDraft[] { const first = posts.findIndex(post => post.embed[key]) return posts.map((post, index) => post.embed[key] && index !== first ? postReducer(post, { type: key === 'poll' ? 'embed_remove_poll' : 'embed_remove_event', }) : post, )}
function applyTargetChange( state: ComposerState, nextThread: ThreadDraft,): ComposerState { const clearVideos = hasSpaceVideoTarget(state.thread) !== hasSpaceVideoTarget(nextThread) const clearPolls = threadHasPoll(nextThread) && !isPublicTarget(nextThread) const clearEvents = threadHasEvent(nextThread) && !isPublicTarget(nextThread) if (!clearVideos && !clearPolls && !clearEvents) { return { ...state, isDirty: true, thread: nextThread, } }
const posts = state.thread.posts.map(post => { const withoutVideo = clearVideos && post.embed.media?.type === 'video' ? postReducer(post, {type: 'embed_remove_video'}) : post const withoutPoll = clearPolls && withoutVideo.embed.poll ? postReducer(withoutVideo, {type: 'embed_remove_poll'}) : withoutVideo return clearEvents && withoutPoll.embed.event ? postReducer(withoutPoll, {type: 'embed_remove_event'}) : withoutPoll }) 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 * `app.bsky.embed.gallery`. Named to flag that if/when we deprecate the * legacy images embed entirely, this constant (and the variant split it * gates) should go away. */export const LEGACY_IMAGES_EMBED_MAX = 4export const MAX_GALLERY_IMAGES = 10
/** * Picks the embed variant for a set of images. <=4 lands in the legacy * `app.bsky.embed.images` shape; >4 promotes to `app.bsky.embed.gallery`. * Anything beyond the gallery cap is dropped by the hard slice; callers * should already have enforced the cap upstream (picker, paste, etc), * and the reducer logs a warning when the cap is exceeded so the UI * layer can surface a toast. */function imagesToMediaVariant( images: ComposerImage[],): ImagesMedia | GalleryMedia { return images.length <= LEGACY_IMAGES_EMBED_MAX ? {type: 'images', images: images.slice(0, LEGACY_IMAGES_EMBED_MAX)} : {type: 'gallery', images: images.slice(0, MAX_GALLERY_IMAGES)}}
export function composerReducer( state: ComposerState, action: ComposerAction,): ComposerState { switch (action.type) { case 'update_postgate': { return { ...state, isDirty: true, thread: { ...state.thread, postgate: action.postgate, }, } } case 'update_threadgate': { return { ...state, isDirty: true, thread: { ...state.thread, threadgate: action.threadgate, }, } } case 'toggle_blacksky_only': { const nextThread = { ...state.thread, blackskyOnly: !state.thread.blackskyOnly, communityFeed: undefined, communityFeedUri: undefined, communitySpaceUri: undefined, } return applyTargetChange( { ...state, }, nextThread, ) } case 'set_post_target': { 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 applyTargetChange( { ...state, }, nextThread, ) } case 'update_post': { if ( action.postAction.type === 'embed_add_poll' && (threadHasPoll(state.thread) || !isPollAllowed(state.thread, undefined)) ) { return state } if ( action.postAction.type === 'embed_add_event' && (threadHasEvent(state.thread) || !isEventAllowed(state.thread, undefined)) ) { return state } let nextPosts = state.thread.posts const postIndex = state.thread.posts.findIndex( p => p.id === action.postId, ) if (postIndex !== -1) { nextPosts = state.thread.posts.slice() nextPosts[postIndex] = postReducer( state.thread.posts[postIndex], action.postAction, ) } return { ...state, isDirty: true, thread: { ...state.thread, posts: nextPosts, }, } } case 'add_post': { const activePostIndex = state.activePostIndex const nextPosts = [...state.thread.posts] nextPosts.splice(activePostIndex + 1, 0, { id: nanoid(), richtext: new RichText({text: ''}), shortenedGraphemeLength: 0, labels: [], embed: { quote: undefined, media: undefined, link: undefined, }, }) return { ...state, isDirty: true, activePostIndex: activePostIndex + 1, mutableNeedsFocusActive: true, thread: { ...state.thread, posts: nextPosts, }, } } case 'remove_post': { if (state.thread.posts.length < 2) { return state } let nextActivePostIndex = state.activePostIndex const indexToRemove = state.thread.posts.findIndex( p => p.id === action.postId, ) let nextPosts = [...state.thread.posts] if (indexToRemove !== -1) { const postToRemove = state.thread.posts[indexToRemove] if (postToRemove.embed.media?.type === 'video') { postToRemove.embed.media.video.abortController.abort() } nextPosts.splice(indexToRemove, 1) nextActivePostIndex = Math.max(0, indexToRemove - 1) } return { ...state, isDirty: true, activePostIndex: nextActivePostIndex, mutableNeedsFocusActive: true, thread: { ...state.thread, posts: nextPosts, }, } } case 'focus_post': { const nextActivePostIndex = state.thread.posts.findIndex( p => p.id === action.postId, ) if (nextActivePostIndex === -1) { return state } return { ...state, activePostIndex: nextActivePostIndex, } } case 'restore_from_draft': { const { draftId, posts, threadgateAllow, postgateEmbeddingRules, loadedMedia, originalLocalRefs, } = action
return { activePostIndex: 0, mutableNeedsFocusActive: true, draftId, isDirty: false, loadedMediaMap: loadedMedia, originalLocalRefs, thread: { posts: keepFirst(keepFirst(posts, 'poll'), 'event'), postgate: createPostgateRecord({ post: '', embeddingRules: postgateEmbeddingRules, }), threadgate: threadgateRecordToAllowUISetting({ $type: 'app.bsky.feed.threadgate', post: '' as AtUriString, createdAt: toDatetimeString(new Date()), allow: threadgateAllow, }), blackskyOnly: false, }, } } case 'clear': { return createComposerState({ initText: undefined, initMention: undefined, initImageUris: [], initQuoteUri: undefined, initInteractionSettings: action.initInteractionSettings, }) } case 'mark_saved': { return { ...state, isDirty: false, draftId: action.draftId, } } }}
function postReducer(state: PostDraft, action: PostAction): PostDraft { switch (action.type) { case 'update_richtext': { return { ...state, richtext: action.richtext, shortenedGraphemeLength: getShortenedLength(action.richtext), } } case 'update_labels': { return { ...state, labels: action.labels, } } case 'embed_add_images': { if (action.images.length === 0 || hasExclusiveEmbed(state.embed)) { return state } const prevMedia = state.embed.media let nextMedia = prevMedia const prevCount = prevMedia?.type === 'images' || prevMedia?.type === 'gallery' ? prevMedia.images.length : 0 const incomingCount = prevCount + action.images.length if (incomingCount > MAX_GALLERY_IMAGES) { // Defense in depth: callers (applyGalleryCap in Composer) should have // already trimmed and surfaced a toast. The hard slice in // imagesToMediaVariant still drops the excess so the cap holds. logger.warn('composer: image add exceeds MAX_GALLERY_IMAGES', { prevCount, incomingCount, dropped: incomingCount - MAX_GALLERY_IMAGES, }) } if (!prevMedia) { nextMedia = imagesToMediaVariant(action.images) } else if (prevMedia.type === 'images' || prevMedia.type === 'gallery') { nextMedia = imagesToMediaVariant([ ...prevMedia.images, ...action.images, ]) } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_update_image': { const prevMedia = state.embed.media if (prevMedia?.type === 'images' || prevMedia?.type === 'gallery') { const updatedImage = action.image const nextMedia = { ...prevMedia, images: prevMedia.images.map(img => { if (img.source.id === updatedImage.source.id) { return updatedImage } return img }), } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } return state } case 'embed_remove_image': { const prevMedia = state.embed.media if (prevMedia?.type === 'images' || prevMedia?.type === 'gallery') { const removedImage = action.image const remainingImages = prevMedia.images.filter(img => { return img.source.id !== removedImage.source.id }) let nextMedia: ImagesMedia | GalleryMedia | undefined if (remainingImages.length === 0) { nextMedia = undefined } else { // Re-pick the variant so a gallery that shrinks to <=4 demotes // back to the legacy `app.bsky.embed.images` shape - keeps old // clients rendering it when possible. nextMedia = imagesToMediaVariant(remainingImages) } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } return state } case 'embed_add_video': { if (hasExclusiveEmbed(state.embed)) { return state } const prevMedia = state.embed.media let nextMedia = prevMedia if (!prevMedia) { nextMedia = { type: 'video', video: createVideoState( action.asset, action.abortController, action.telemetry, ), } } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_update_video': { const videoAction = action.videoAction const prevMedia = state.embed.media let nextMedia = prevMedia if (prevMedia?.type === 'video') { nextMedia = { ...prevMedia, video: videoReducer(prevMedia.video, videoAction), } } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_remove_video': { const prevMedia = state.embed.media let nextMedia = prevMedia if (prevMedia?.type === 'video') { prevMedia.video.abortController.abort() nextMedia = undefined } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_add_uri': { if (hasExclusiveEmbed(state.embed)) { return state } const prevQuote = state.embed.quote const prevLink = state.embed.link let nextQuote = prevQuote let nextLink = prevLink if (isBskyPostUrl(action.uri)) { if (!prevQuote) { nextQuote = { type: 'link', uri: action.uri, } } } else { if (!prevLink) { nextLink = { type: 'link', uri: action.uri, } } } return { ...state, embed: { ...state.embed, quote: nextQuote, link: nextLink, }, } } case 'embed_remove_link': { return { ...state, embed: { ...state.embed, link: undefined, }, } } case 'embed_remove_quote': { return { ...state, embed: { ...state.embed, quote: undefined, }, } } case 'embed_add_gif': { if (hasExclusiveEmbed(state.embed)) { return state } const prevMedia = state.embed.media let nextMedia = prevMedia if (!prevMedia) { nextMedia = { type: 'gif', gif: action.gif, alt: '', } } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_update_gif': { const prevMedia = state.embed.media let nextMedia = prevMedia if (prevMedia?.type === 'gif') { nextMedia = { ...prevMedia, alt: action.alt, } } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_remove_gif': { const prevMedia = state.embed.media let nextMedia = prevMedia if (prevMedia?.type === 'gif') { nextMedia = undefined } return { ...state, embed: { ...state.embed, media: nextMedia, }, } } case 'embed_add_poll': { if (postHasAttachment(state)) { return state } return { ...state, embed: { ...state.embed, poll: {statements: ['']}, }, } } case 'embed_update_poll_statement': { const poll = state.embed.poll if (!poll || action.index < 0 || action.index >= poll.statements.length) { return state } return { ...state, embed: { ...state.embed, poll: { ...poll, statements: poll.statements.map((text, i) => i === action.index ? action.text : text, ), }, }, } } case 'embed_add_poll_statement': { const poll = state.embed.poll if (!poll || poll.statements.length >= POLL_MAX_STATEMENTS) { return state } return { ...state, embed: { ...state.embed, poll: {...poll, statements: [...poll.statements, '']}, }, } } case 'embed_remove_poll_statement': { const poll = state.embed.poll if (!poll || poll.statements.length <= 1) { return state } return { ...state, embed: { ...state.embed, poll: { ...poll, statements: poll.statements.filter((_, i) => i !== action.index), }, }, } } case 'embed_set_poll_assembly': { const poll = state.embed.poll if (!poll) { return state } return { ...state, embed: { ...state.embed, poll: {...poll, assembly: action.assembly}, }, } } case 'embed_remove_poll': { return { ...state, embed: { ...state.embed, poll: undefined, }, } } case 'embed_add_event': { if (postHasAttachment(state)) { return state } return { ...state, embed: {...state.embed, event: createEventDraft()}, } } case 'embed_update_event': { const event = state.embed.event if (!event) { return state } return { ...state, embed: {...state.embed, event: {...event, ...action.fields}}, } } case 'embed_set_event_step': { const event = state.embed.event if (!event) { return state } return { ...state, embed: {...state.embed, event: {...event, step: action.step}}, } } case 'embed_remove_event': { return { ...state, embed: {...state.embed, event: undefined}, } } }}
export function createComposerState({ initText, initMention, initImageUris, initQuoteUri, initInteractionSettings, initBlackskyOnly, initCommunitySpaceUri,}: { initText: string | undefined initMention: string | undefined initImageUris: ComposerOpts['imageUris'] initQuoteUri: string | undefined initInteractionSettings: app.bsky.actor.defs.PostInteractionSettingsPref | undefined initBlackskyOnly?: boolean initCommunitySpaceUri?: string}): ComposerState { let media: ImagesMedia | GalleryMedia | undefined if (initImageUris?.length) { media = imagesToMediaVariant(createInitialImages(initImageUris)) } let quote: Link | undefined if (initQuoteUri) { // TODO: Consider passing the app url directly. const path = postUriToRelativePath(initQuoteUri) if (path) { quote = { type: 'link', uri: toBskyAppUrl(path) as UriString, } } } const initRichText = new RichText({ text: initText ? initText : initMention ? insertMentionAt( `@${initMention}`, initMention.length + 1, `${initMention}`, ) : '', })
let link: Link | undefined
/** * `initText` atm is only used for compose intents, meaning share links from * external sources. If `initText` is defined, we want to extract links/posts * from `initText` and suggest them as embeds. * * This checks for posts separately from other types of links so that posts * can become quotes. The util `suggestLinkCardUri` is then applied to ensure * we suggest at most 1 of each. */ if (initText) { initRichText.detectFacetsWithoutResolution() const detectedExtUris = new Map<string, LinkFacetMatch>() const detectedPostUris = new Map<string, LinkFacetMatch>() if (initRichText.facets) { for (const facet of initRichText.facets) { for (const feature of facet.features) { if (bsky.isType(app.bsky.richtext.facet.link, feature)) { if (isBskyPostUrl(feature.uri)) { detectedPostUris.set(feature.uri, {facet, rt: initRichText}) } else { detectedExtUris.set(feature.uri, {facet, rt: initRichText}) } } } } } const pastSuggestedUris = new Set<string>() const suggestedExtUri = suggestLinkCardUri( true, detectedExtUris, new Map(), pastSuggestedUris, ) if (suggestedExtUri) { link = { type: 'link', uri: suggestedExtUri as UriString, } } const suggestedPostUri = suggestLinkCardUri( true, detectedPostUris, new Map(), pastSuggestedUris, ) if (suggestedPostUri) { /* * `initQuote` is only populated via in-app user action, but we're being * future-defensive here. */ if (!quote) { quote = { type: 'link', uri: suggestedPostUri as UriString, } } } } else if (initMention) { // highlight the mention initRichText.detectFacetsWithoutResolution() }
return { activePostIndex: 0, mutableNeedsFocusActive: false, isDirty: false, thread: { posts: [ { id: nanoid(), richtext: initRichText, shortenedGraphemeLength: getShortenedLength(initRichText), labels: [], embed: { quote, media, link, }, }, ], postgate: createPostgateRecord({ post: '', embeddingRules: initInteractionSettings?.postgateEmbeddingRules || [], }), threadgate: threadgateRecordToAllowUISetting({ $type: 'app.bsky.feed.threadgate', post: '' as AtUriString, createdAt: toDatetimeString(new Date()), allow: initInteractionSettings?.threadgateAllowRules, }), blackskyOnly: !!initBlackskyOnly, communitySpaceUri: initCommunitySpaceUri, }, }}
function getShortenedLength(rt: RichText) { return shortenLinks(rt).graphemeLength}