Clean up embed replacement

This commit is contained in:
Eric Bailey
2024-08-07 10:55:10 -05:00
parent a61bef8716
commit f576db3023
3 changed files with 88 additions and 49 deletions
+37 -21
View File
@@ -1,11 +1,16 @@
import {AppBskyFeedPostgate, AtUri, BskyAgent} from '@atproto/api' import {
AppBskyFeedDefs,
AppBskyFeedPostgate,
AtUri,
BskyAgent,
} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query' import {useMutation, useQueryClient} from '@tanstack/react-query'
import {networkRetry} from '#/lib/async/retry' import {networkRetry} from '#/lib/async/retry'
import {updatePostShadow} from '#/state/cache/post-shadow' import {updatePostShadow} from '#/state/cache/post-shadow'
import {useGetPosts} from '#/state/queries/post' import {useGetPosts} from '#/state/queries/post'
import { import {
createEmbed, createMaybeDetachedQuoteEmbed,
createPostgateRecord, createPostgateRecord,
mergePostgateRecords, mergePostgateRecords,
POSTGATE_COLLECTION, POSTGATE_COLLECTION,
@@ -109,47 +114,58 @@ export function useToggleQuoteDetachmentMutation() {
return useMutation({ return useMutation({
mutationFn: async ({ mutationFn: async ({
postUri, post,
quotedUri, quoteUri,
action, action,
}: { }: {
postUri: string post: AppBskyFeedDefs.PostView
quotedUri: string quoteUri: string
action: 'detach' | 'reattach' action: 'detach' | 'reattach'
}) => { }) => {
await upsertPostgate({agent, postUri: quotedUri}, async prev => { await upsertPostgate({agent, postUri: quoteUri}, async prev => {
if (prev) { if (prev) {
if (action === 'detach') { if (action === 'detach') {
return mergePostgateRecords(prev, { return mergePostgateRecords(prev, {
detachedQuotes: [postUri], detachedQuotes: [post.uri],
}) })
} else if (action === 'reattach') { } else if (action === 'reattach') {
return { return {
...prev, ...prev,
detachedQuotes: detachedQuotes:
prev.detachedQuotes?.filter(uri => uri !== postUri) || [], prev.detachedQuotes?.filter(uri => uri !== post.uri) || [],
} }
} }
} else { } else {
if (action === 'detach') { if (action === 'detach') {
return createPostgateRecord({ return createPostgateRecord({
post: quotedUri, post: quoteUri,
detachedQuotes: [postUri], detachedQuotes: [post.uri],
}) })
} }
} }
}) })
}, },
async onSuccess(_data, {postUri, quotedUri, action}) { async onSuccess(_data, {post, quoteUri, action}) {
const [post, quotedPost] = await getPosts({uris: [postUri, quotedUri]}) if (action === 'detach') {
updatePostShadow(queryClient, post.uri, {
updatePostShadow(queryClient, postUri, { embed: createMaybeDetachedQuoteEmbed({
embed: createEmbed({ post,
post, quote: undefined,
embeddedPost: quotedPost, quoteUri,
detached: action === 'detach', detached: true,
}), }),
}) })
} else if (action === 'reattach') {
const [quote] = await getPosts({uris: [quoteUri]})
updatePostShadow(queryClient, post.uri, {
embed: createMaybeDetachedQuoteEmbed({
post,
quote,
quoteUri: undefined,
detached: false,
}),
})
}
}, },
}) })
} }
+48 -25
View File
@@ -49,25 +49,56 @@ export function createEmbedViewRemovedRecord({uri}: {uri: string}) {
} }
} }
export function createEmbed({ export function createMaybeDetachedQuoteEmbed({
post, post,
embeddedPost, quote,
quoteUri,
detached, detached,
}: { }:
post: AppBskyFeedDefs.PostView | {
embeddedPost: AppBskyFeedDefs.PostView post: AppBskyFeedDefs.PostView
detached: boolean quote: AppBskyFeedDefs.PostView
}) { quoteUri: undefined
detached: false
}
| {
post: AppBskyFeedDefs.PostView
quote: undefined
quoteUri: string
detached: true
}): AppBskyEmbedRecord.View | AppBskyEmbedRecordWithMedia.View | undefined {
if (AppBskyEmbedRecord.isView(post.embed)) { if (AppBskyEmbedRecord.isView(post.embed)) {
if (detached) return createEmbedViewRemovedRecord({uri: embeddedPost.uri}) if (detached) {
return createEmbedRecordView({post: embeddedPost}) return createEmbedViewRemovedRecord({uri: quoteUri})
} else {
return createEmbedRecordView({post: quote})
}
} else if (AppBskyEmbedRecordWithMedia.isView(post.embed)) { } else if (AppBskyEmbedRecordWithMedia.isView(post.embed)) {
if (detached) if (detached) {
return { return {
...post.embed, ...post.embed,
record: createEmbedViewRemovedRecord({uri: embeddedPost.uri}), record: createEmbedViewRemovedRecord({uri: quoteUri}),
} }
return createEmbedRecordWithmediaView({post, embeddedPost}) } else {
return createEmbedRecordWithMediaView({post, quote})
}
}
}
export function createEmbedViewRecordFromPost(
post: AppBskyFeedDefs.PostView,
): AppBskyEmbedRecord.ViewRecord {
return {
$type: 'app.bsky.embed.record#viewRecord',
uri: post.uri,
cid: post.cid,
author: post.author,
value: post.record,
labels: post.labels,
replyCount: post.replyCount,
repostCount: post.repostCount,
likeCount: post.likeCount,
indexedAt: post.indexedAt,
} }
} }
@@ -78,30 +109,22 @@ export function createEmbedRecordView({
}): AppBskyEmbedRecord.View { }): AppBskyEmbedRecord.View {
return { return {
$type: 'app.bsky.embed.record#view', $type: 'app.bsky.embed.record#view',
record: { record: createEmbedViewRecordFromPost(post),
$type: 'app.bsky.embed.record#viewRecord',
...post,
value: post.record,
},
} }
} }
export function createEmbedRecordWithmediaView({ export function createEmbedRecordWithMediaView({
post, post,
embeddedPost, quote,
}: { }: {
post: AppBskyFeedDefs.PostView post: AppBskyFeedDefs.PostView
embeddedPost: AppBskyFeedDefs.PostView quote: AppBskyFeedDefs.PostView
}): AppBskyEmbedRecordWithMedia.View | undefined { }): AppBskyEmbedRecordWithMedia.View | undefined {
if (!AppBskyEmbedRecordWithMedia.isView(post.embed)) return if (!AppBskyEmbedRecordWithMedia.isView(post.embed)) return
return { return {
...(post.embed || {}), ...(post.embed || {}),
record: { record: {
record: { record: createEmbedViewRecordFromPost(quote),
$type: 'app.bsky.embed.record#viewRecord',
...embeddedPost,
value: embeddedPost.record,
},
}, },
} }
} }
+3 -3
View File
@@ -283,11 +283,11 @@ let PostDropdownBtn = ({
// TODO handle failure // TODO handle failure
toggleQuoteDetachment({ toggleQuoteDetachment({
postUri, post,
quotedUri: quoteEmbed.uri, quoteUri: quoteEmbed.uri,
action: quoteEmbed.isDetached ? 'reattach' : 'detach', action: quoteEmbed.isDetached ? 'reattach' : 'detach',
}) })
}, [quoteEmbed, postUri, toggleQuoteDetachment]) }, [quoteEmbed, post, toggleQuoteDetachment])
const canEmbed = isWeb && gtMobile && !hideInPWI const canEmbed = isWeb && gtMobile && !hideInPWI