Make use of applyWrites when publishing posts (#5809)
* make use of applywrites * Explicit $type --------- Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
This commit is contained in:
+67
-54
@@ -4,13 +4,17 @@ import {
|
|||||||
AppBskyEmbedRecord,
|
AppBskyEmbedRecord,
|
||||||
AppBskyEmbedRecordWithMedia,
|
AppBskyEmbedRecordWithMedia,
|
||||||
AppBskyEmbedVideo,
|
AppBskyEmbedVideo,
|
||||||
|
AppBskyFeedPost,
|
||||||
|
AppBskyFeedPostgate,
|
||||||
AtUri,
|
AtUri,
|
||||||
BlobRef,
|
BlobRef,
|
||||||
BskyAgent,
|
BskyAgent,
|
||||||
ComAtprotoLabelDefs,
|
ComAtprotoLabelDefs,
|
||||||
|
ComAtprotoRepoApplyWrites,
|
||||||
ComAtprotoRepoStrongRef,
|
ComAtprotoRepoStrongRef,
|
||||||
RichText,
|
RichText,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {TID} from '@atproto/common-web'
|
||||||
import {t} from '@lingui/macro'
|
import {t} from '@lingui/macro'
|
||||||
import {QueryClient} from '@tanstack/react-query'
|
import {QueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
@@ -18,7 +22,6 @@ import {isNetworkError} from '#/lib/strings/errors'
|
|||||||
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
|
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {compressImage} from '#/state/gallery'
|
import {compressImage} from '#/state/gallery'
|
||||||
import {writePostgateRecord} from '#/state/queries/postgate'
|
|
||||||
import {
|
import {
|
||||||
fetchResolveGifQuery,
|
fetchResolveGifQuery,
|
||||||
fetchResolveLinkQuery,
|
fetchResolveLinkQuery,
|
||||||
@@ -26,7 +29,6 @@ import {
|
|||||||
import {
|
import {
|
||||||
createThreadgateRecord,
|
createThreadgateRecord,
|
||||||
threadgateAllowUISettingToAllowRecordValue,
|
threadgateAllowUISettingToAllowRecordValue,
|
||||||
writeThreadgateRecord,
|
|
||||||
} from '#/state/queries/threadgate'
|
} from '#/state/queries/threadgate'
|
||||||
import {ComposerDraft, EmbedDraft} from '#/view/com/composer/state/composer'
|
import {ComposerDraft, EmbedDraft} from '#/view/com/composer/state/composer'
|
||||||
import {createGIFDescription} from '../gif-alt-text'
|
import {createGIFDescription} from '../gif-alt-text'
|
||||||
@@ -101,16 +103,74 @@ export async function post(
|
|||||||
langs = opts.langs.slice(0, 3)
|
langs = opts.langs.slice(0, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
let res
|
const rkey = TID.nextStr()
|
||||||
try {
|
const uri = `at://${agent.assertDid}/app.bsky.feed.post/${rkey}`
|
||||||
opts.onStateChange?.(t`Posting...`)
|
const date = new Date().toISOString()
|
||||||
res = await agent.post({
|
|
||||||
|
const writes: ComAtprotoRepoApplyWrites.Create[] = []
|
||||||
|
|
||||||
|
// Create post record
|
||||||
|
{
|
||||||
|
const record: AppBskyFeedPost.Record = {
|
||||||
|
$type: 'app.bsky.feed.post',
|
||||||
|
createdAt: date,
|
||||||
text: rt.text,
|
text: rt.text,
|
||||||
facets: rt.facets,
|
facets: rt.facets,
|
||||||
reply,
|
reply,
|
||||||
embed,
|
embed,
|
||||||
langs,
|
langs,
|
||||||
labels,
|
labels,
|
||||||
|
}
|
||||||
|
|
||||||
|
writes.push({
|
||||||
|
$type: 'com.atproto.repo.applyWrites#create',
|
||||||
|
collection: 'app.bsky.feed.post',
|
||||||
|
rkey: rkey,
|
||||||
|
value: record,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create threadgate record
|
||||||
|
if (draft.threadgate.some(tg => tg.type !== 'everybody')) {
|
||||||
|
const record = createThreadgateRecord({
|
||||||
|
createdAt: date,
|
||||||
|
post: uri,
|
||||||
|
allow: threadgateAllowUISettingToAllowRecordValue(draft.threadgate),
|
||||||
|
})
|
||||||
|
|
||||||
|
writes.push({
|
||||||
|
$type: 'com.atproto.repo.applyWrites#create',
|
||||||
|
collection: 'app.bsky.feed.threadgate',
|
||||||
|
rkey: rkey,
|
||||||
|
value: record,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create postgate record
|
||||||
|
if (
|
||||||
|
draft.postgate.embeddingRules?.length ||
|
||||||
|
draft.postgate.detachedEmbeddingUris?.length
|
||||||
|
) {
|
||||||
|
const record: AppBskyFeedPostgate.Record = {
|
||||||
|
...draft.postgate,
|
||||||
|
$type: 'app.bsky.feed.postgate',
|
||||||
|
createdAt: date,
|
||||||
|
post: uri,
|
||||||
|
}
|
||||||
|
|
||||||
|
writes.push({
|
||||||
|
$type: 'com.atproto.repo.applyWrites#create',
|
||||||
|
collection: 'app.bsky.feed.postgate',
|
||||||
|
rkey: rkey,
|
||||||
|
value: record,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await agent.com.atproto.repo.applyWrites({
|
||||||
|
repo: agent.assertDid,
|
||||||
|
writes: writes,
|
||||||
|
validate: true,
|
||||||
})
|
})
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error(`Failed to create post`, {
|
logger.error(`Failed to create post`, {
|
||||||
@@ -125,54 +185,7 @@ export async function post(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (draft.threadgate.some(tg => tg.type !== 'everybody')) {
|
return {uri}
|
||||||
try {
|
|
||||||
// TODO: this needs to be batch-created with the post!
|
|
||||||
await writeThreadgateRecord({
|
|
||||||
agent,
|
|
||||||
postUri: res.uri,
|
|
||||||
threadgate: createThreadgateRecord({
|
|
||||||
post: res.uri,
|
|
||||||
allow: threadgateAllowUISettingToAllowRecordValue(draft.threadgate),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
} catch (e: any) {
|
|
||||||
logger.error(`Failed to create threadgate`, {
|
|
||||||
context: 'composer',
|
|
||||||
safeMessage: e.message,
|
|
||||||
})
|
|
||||||
throw new Error(
|
|
||||||
t`Failed to save post interaction settings. Your post was created but users may be able to interact with it.`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
draft.postgate.embeddingRules?.length ||
|
|
||||||
draft.postgate.detachedEmbeddingUris?.length
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
// TODO: this needs to be batch-created with the post!
|
|
||||||
await writePostgateRecord({
|
|
||||||
agent,
|
|
||||||
postUri: res.uri,
|
|
||||||
postgate: {
|
|
||||||
...draft.postgate,
|
|
||||||
post: res.uri,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} catch (e: any) {
|
|
||||||
logger.error(`Failed to create postgate`, {
|
|
||||||
context: 'composer',
|
|
||||||
safeMessage: e.message,
|
|
||||||
})
|
|
||||||
throw new Error(
|
|
||||||
t`Failed to save post interaction settings. Your post was created but users may be able to interact with it.`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveEmbed(
|
async function resolveEmbed(
|
||||||
|
|||||||
Reference in New Issue
Block a user