Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import {type AppBskyFeedPost, type AtpAgent} from '@atproto/api'import {ensureValidRecordKey} from '@atproto/syntax'import {t} from '@lingui/core/macro'import {type QueryClient} from '@tanstack/react-query'
import { type PostOpts, quotedSpace, resolveEmbed, resolveReply, resolveRT,} from '#/lib/api/index'import {POST_COLLECTION, spaceCreateRecord} from '#/lib/api/space-write'
/** * Post a thread into a permissioned space. * * Deliberately not a branch inside `postCommunity`: that function implements * the Blacksky community feed's three-step write (submit to the appview, write * a public stub, admit to the feed generator), and none of those steps exist * here. A space post is one ordinary `app.bsky.feed.post` written into the * author's own permissioned repo; the syncer materialises it into the feed. * * Records are built as raw JSON rather than through typed builders, because a * reply within a space refers to space URIs, which are not valid at-uris and * would fail SDK-side validation. */export async function postToSpace( agent: AtpAgent, queryClient: QueryClient, space: string, opts: PostOpts,): Promise<{uris: string[]}> { const thread = opts.thread const langs = opts.langs?.slice(0, 3) const uris: string[] = []
// Read access is uniform within a space but not across spaces, so quoting // another space's post here would name a private post to people who cannot // see it. Same space is fine — everyone reading this record can already read // the quoted one. const foreign = thread.posts .map(p => quotedSpace(p.embed.quote?.uri)) .find(quoted => quoted && quoted !== space) if (foreign) { throw new Error( t`This is a private post from another space. You can only quote it in a post to that space.`, ) }
// Posts are written one at a time, so an unsupported draft has to be refused // before the first write rather than part-way through the thread. if (thread.posts.some(p => p.embed.poll)) { throw new Error( t`Discussions are not available in private community feeds.`, ) }
// Threads are written in order: each reply refers to the space URI the host // returned for the post before it, so they cannot be batched. let root: {uri: string; cid: string} | undefined let parent: {uri: string; cid: string} | undefined
// Replying into the space: the first draft hangs off the post being replied // to, and everything after it hangs off its predecessor as usual. if (opts.replyTo) { const replyRef = await resolveReply(agent, opts.replyTo) if (replyRef) { root = replyRef.root parent = replyRef.parent } }
for (const [index, draft] of thread.posts.entries()) { const rt = await resolveRT(agent, draft.richtext) const embed = await resolveEmbed( agent, queryClient, draft, opts.onStateChange, )
const record: Record<string, unknown> = { $type: POST_COLLECTION, text: rt.text, createdAt: new Date().toISOString(), } if (rt.facets?.length) record.facets = rt.facets if (langs?.length) record.langs = langs if (embed) record.embed = embed if (draft.labels.length) { record.labels = { $type: 'com.atproto.label.defs#selfLabels', values: draft.labels.map(val => ({val})), } } if (root && parent) { record.reply = {root, parent} }
opts.onStateChange?.(t`Posting to the private feed...`) const rkey = opts.draftId ? `${opts.draftId}-${index}` : draft.id ensureValidRecordKey(rkey) const written = await spaceCreateRecord( agent, space, POST_COLLECTION, record, rkey, )
uris.push(written.uri) root ??= written parent = written }
return {uris}}
/** * Threadgates and postgates are not written into a space. * * The public gate records address posts by at-uri and are enforced where * public content is hydrated, neither of which applies here. The composer * hides the controls; this exists so a caller that still holds gate state * knows it is intentionally dropped rather than silently lost. */export function spacePostsIgnoreGates(): true { return true}
export type {AppBskyFeedPost}