Don't resolve facets twice

This commit is contained in:
Eric Bailey
2024-08-29 12:51:11 -05:00
parent 97fcc42c57
commit 9fcd83e06f
5 changed files with 20 additions and 45 deletions
+14 -8
View File
@@ -12,6 +12,7 @@ import {
moderateFeedGenerator, moderateFeedGenerator,
moderateUserList, moderateUserList,
ModerationDecision, ModerationDecision,
RichText as RichTextApi,
} from '@atproto/api' } from '@atproto/api'
import {ModeratorData} from '../data/getModeratorData.js' import {ModeratorData} from '../data/getModeratorData.js'
@@ -50,8 +51,12 @@ export function Post({
}) { }) {
if (AppBskyFeedPost.isRecord(post.record)) { if (AppBskyFeedPost.isRecord(post.record)) {
const avatar = data.images.get(post.author.avatar) const avatar = data.images.get(post.author.avatar)
const text = post.record.text const rt = post.record.text
const rt = data.texts.get(text) ? new RichTextApi({
text: post.record.text,
facets: post.record.facets,
})
: undefined
const hasInteractions = post.likeCount > 0 || post.repostCount > 0 const hasInteractions = post.likeCount > 0 || post.repostCount > 0
const moderation = moderatePost(post, moderatorData.moderationOptions) const moderation = moderatePost(post, moderatorData.moderationOptions)
@@ -551,7 +556,12 @@ export function QuoteEmbed({
) { ) {
const {author, value: post, embeds} = embed.record const {author, value: post, embeds} = embed.record
const avatar = data.images.get(author.avatar) const avatar = data.images.get(author.avatar)
const rt = data.texts.get(post.text) const rt = post.text
? new RichTextApi({
text: post.text,
facets: post.facets,
})
: undefined
const postView = viewRecordToPostView(embed.record) const postView = viewRecordToPostView(embed.record)
const moderation = moderatePost(postView, moderatorData.moderationOptions) const moderation = moderatePost(postView, moderatorData.moderationOptions)
@@ -591,11 +601,7 @@ export function QuoteEmbed({
</Box> </Box>
</Box> </Box>
{rt && ( {rt && <RichText value={rt} cx={[a.text_sm]} />}
<Box>
<RichText value={rt} cx={[a.text_sm]} />
</Box>
)}
{Boolean(embeds && embeds.length) && ( {Boolean(embeds && embeds.length) && (
<Box cx={[a.pt_sm]}> <Box cx={[a.pt_sm]}>
+3 -3
View File
@@ -18,7 +18,7 @@ export function RichText({
const baseStyles = [ const baseStyles = [
a.leading_snug, a.leading_snug,
{ {
whiteSpace: 'pre-wrap', // whiteSpace: 'wrap',
}, },
cx ? s(cx) : {}, cx ? s(cx) : {},
] ]
@@ -63,7 +63,7 @@ export function RichText({
} else if (link && AppBskyRichtextFacet.validateLink(link).success) { } else if (link && AppBskyRichtextFacet.validateLink(link).success) {
const url = toShortUrl(text) const url = toShortUrl(text)
if (disableLinks) { if (disableLinks) {
els.push(url) els.push(<span key={key}>{url}</span>)
} else { } else {
els.push( els.push(
<span key={key} style={s(linkStyles)}> <span key={key} style={s(linkStyles)}>
@@ -82,7 +82,7 @@ export function RichText({
</span>, </span>,
) )
} else { } else {
els.push(segment.text) els.push(<span key={key}>{segment.text}</span>)
} }
key++ key++
} }
+2 -1
View File
@@ -13,7 +13,8 @@ export function Text({
<div <div
style={s([ style={s([
a.flex, a.flex,
a.flex_col, a.flex_wrap,
a.gap_xs,
a.font_normal, a.font_normal,
a.leading_tight, a.leading_tight,
a.tracking_wide, a.tracking_wide,
-32
View File
@@ -4,10 +4,7 @@ import {
AppBskyEmbedRecord, AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia, AppBskyEmbedRecordWithMedia,
AppBskyFeedDefs, AppBskyFeedDefs,
AppBskyFeedPost,
AppBskyGraphDefs, AppBskyGraphDefs,
AtpAgent,
RichText,
} from '@atproto/api' } from '@atproto/api'
import {httpLogger} from '../logger.js' import {httpLogger} from '../logger.js'
@@ -28,7 +25,6 @@ export type Image = Metadata & {
export type PostData = { export type PostData = {
images: Map<string, Image> images: Map<string, Image>
texts: Map<string, RichText>
} }
function normalizeAspectRatio(aspectRatio?: { function normalizeAspectRatio(aspectRatio?: {
@@ -45,10 +41,8 @@ function normalizeAspectRatio(aspectRatio?: {
export async function getPostData( export async function getPostData(
post: AppBskyFeedDefs.PostView, post: AppBskyFeedDefs.PostView,
agent: AtpAgent,
): Promise<PostData> { ): Promise<PostData> {
const images: Map<string, Metadata> = new Map() const images: Map<string, Metadata> = new Map()
const texts: Map<string, RichText> = new Map()
// console.log(JSON.stringify(post, null, 2)) // console.log(JSON.stringify(post, null, 2))
@@ -61,10 +55,6 @@ export async function getPostData(
}) })
} }
if (AppBskyFeedPost.isRecord(post.record) && post.record.text) {
texts.set(post.record.text, new RichText({text: post.record.text}))
}
if (post.embed) { if (post.embed) {
if (AppBskyEmbedImages.isView(post.embed)) { if (AppBskyEmbedImages.isView(post.embed)) {
// get OPs media // get OPs media
@@ -166,16 +156,6 @@ export async function getPostData(
}) })
} }
} }
if (
AppBskyFeedPost.isRecord(post.embed.record.value) &&
post.embed.record.value.text
) {
texts.set(
post.embed.record.value.text,
new RichText({text: post.embed.record.value.text}),
)
}
} }
if (AppBskyGraphDefs.isListView(post.embed.record)) { if (AppBskyGraphDefs.isListView(post.embed.record)) {
@@ -271,16 +251,6 @@ export async function getPostData(
} }
} }
} }
if (
AppBskyFeedPost.isRecord(post.embed.record.record.value) &&
post.embed.record.record.value.text
) {
texts.set(
post.embed.record.record.value.text,
new RichText({text: post.embed.record.record.value.text}),
)
}
} }
} }
} }
@@ -309,7 +279,6 @@ export async function getPostData(
} }
}), }),
) )
await Promise.all(Array.from(texts.values()).map(r => r.detectFacets(agent)))
const extracted = resolved.filter(([, i]) => i.image !== null) as [ const extracted = resolved.filter(([, i]) => i.image !== null) as [
string, string,
Image, Image,
@@ -317,6 +286,5 @@ export async function getPostData(
return { return {
images: new Map(extracted), images: new Map(extracted),
texts,
} }
} }
+1 -1
View File
@@ -40,7 +40,7 @@ export default function (ctx: AppContext, app: Express) {
} }
const [postData, moderatorData] = await Promise.all([ const [postData, moderatorData] = await Promise.all([
getPostData(post, ctx.appviewAgent), getPostData(post),
getModeratorData(ctx.appviewAgent), getModeratorData(ctx.appviewAgent),
]) ])