From 1432b4c0f09a9501b4ece4edbf85de07a0104198 Mon Sep 17 00:00:00 2001 From: Brittany Ellich Date: Thu, 24 Sep 2026 10:33:40 -0700 Subject: [PATCH] feat: add Attie link cards (#11756) --- assets/icons/community/README.md | 1 + assets/icons/community/attie-cloud.svg | 1 + oxlint-suppressions.json | 5 - .../Post/Embed/AtCard/attie.test.ts | 24 +++ src/components/Post/Embed/AtCard/attie.ts | 26 +++ src/components/Post/Embed/AtCard/index.tsx | 192 ++++++++++++++++++ src/components/Post/Embed/AtCard/providers.ts | 18 ++ src/components/Post/Embed/index.tsx | 13 +- src/components/icons/Attie.tsx | 29 +++ src/lib/api/index.ts | 18 +- src/lib/api/resolve.ts | 14 ++ src/lib/at-card.test.ts | 69 +++++++ src/lib/at-card.ts | 35 ++++ src/lib/link-meta/link-meta.ts | 16 +- src/view/com/composer/ExternalEmbed.tsx | 12 +- 15 files changed, 456 insertions(+), 17 deletions(-) create mode 100644 assets/icons/community/attie-cloud.svg create mode 100644 src/components/Post/Embed/AtCard/attie.test.ts create mode 100644 src/components/Post/Embed/AtCard/attie.ts create mode 100644 src/components/Post/Embed/AtCard/index.tsx create mode 100644 src/components/Post/Embed/AtCard/providers.ts create mode 100644 src/components/icons/Attie.tsx create mode 100644 src/lib/at-card.test.ts create mode 100644 src/lib/at-card.ts diff --git a/assets/icons/community/README.md b/assets/icons/community/README.md index 299922a5d..235e652b8 100644 --- a/assets/icons/community/README.md +++ b/assets/icons/community/README.md @@ -2,6 +2,7 @@ The icons in this directory are the marks of third-party services that appear in | Icon | Rights holder | |---|---| +| `attie-cloud.svg` | [Attie](https://attie.site) | | `leaflet.svg` | [Leaflet](https://leaflet.pub) | | `offprint.svg` | [Offprint](https://offprint.net) | | `pckt.svg`, `pckt-full.svg` | [pckt](https://pckt.blog) | diff --git a/assets/icons/community/attie-cloud.svg b/assets/icons/community/attie-cloud.svg new file mode 100644 index 000000000..2ee0af8e0 --- /dev/null +++ b/assets/icons/community/attie-cloud.svg @@ -0,0 +1 @@ + diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 46f0e6aaa..e5aaa4a40 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -628,11 +628,6 @@ "count": 1 } }, - "src/lib/link-meta/link-meta.ts": { - "typescript/no-unsafe-member-access": { - "count": 9 - } - }, "src/lib/media/manip.ts": { "typescript/no-explicit-any": { "count": 1 diff --git a/src/components/Post/Embed/AtCard/attie.test.ts b/src/components/Post/Embed/AtCard/attie.test.ts new file mode 100644 index 000000000..3f8f1bd21 --- /dev/null +++ b/src/components/Post/Embed/AtCard/attie.test.ts @@ -0,0 +1,24 @@ +import {isAttieUrl} from './attie' + +describe('isAttieUrl', () => { + it.each([ + 'https://attie.site', + 'https://sowing-dewy-sorbet.attie.site/', + 'https://attie.ai/@brittanyellich.com/pages/abc123', + 'https://attie.ai/@brittanyellich.com/pages/abc123/', + ])('recognizes supported Attie URL %s', uri => { + expect(isAttieUrl(uri)).toBe(true) + }) + + it.each([ + 'http://sowing-dewy-sorbet.attie.site/', + 'https://notattie.site/', + 'https://attie.site.example.com/', + 'https://attie.ai/', + 'https://attie.ai/@brittanyellich.com/', + 'https://attie.ai/@brittanyellich.com/pages/', + 'not a URL', + ])('does not recognize unsupported URL %s', uri => { + expect(isAttieUrl(uri)).toBe(false) + }) +}) diff --git a/src/components/Post/Embed/AtCard/attie.ts b/src/components/Post/Embed/AtCard/attie.ts new file mode 100644 index 000000000..0adceed2a --- /dev/null +++ b/src/components/Post/Embed/AtCard/attie.ts @@ -0,0 +1,26 @@ +/** + * Returns whether a URL is a public Attie site or a public Attie report. + * + * Attie site subdomains use `*.attie.site`. Reports use the explicit + * `attie.ai/@handle/pages/` route, so other Attie pages retain the normal + * external-card treatment. + */ +export function isAttieUrl(uri: string | undefined): boolean { + if (!uri) return false + + try { + const url = new URL(uri) + if (url.protocol !== 'https:') return false + + if (url.hostname === 'attie.site' || url.hostname.endsWith('.attie.site')) { + return true + } + + return ( + url.hostname === 'attie.ai' && + /^\/@[^/]+\/pages\/[^/]+\/?$/.test(url.pathname) + ) + } catch { + return false + } +} diff --git a/src/components/Post/Embed/AtCard/index.tsx b/src/components/Post/Embed/AtCard/index.tsx new file mode 100644 index 000000000..54e374905 --- /dev/null +++ b/src/components/Post/Embed/AtCard/index.tsx @@ -0,0 +1,192 @@ +import {type StyleProp, View, type ViewStyle} from 'react-native' +import {Image} from 'expo-image' +import {useLingui} from '@lingui/react/macro' + +import {useHaptics} from '#/lib/haptics' +import {shareUrl} from '#/lib/sharing' +import {useProfileQuery} from '#/state/queries/profile' +import {UserAvatar} from '#/view/com/util/UserAvatar' +import {atoms as a, useBreakpoints, useTheme} from '#/alf' +import {Divider} from '#/components/Divider' +import {Link} from '#/components/Link' +import {Text} from '#/components/Typography' +import {IS_NATIVE} from '#/env' +import {type app} from '#/lexicons' +import {getAtCardProvider} from './providers' + +const ATTIE_ACCOUNT_DID = 'did:plc:v7lt2fu7igydcjszoexxrvl2' + +/** A whole-card link for AT-aware services, starting with Attie. */ +export function AtCard({ + view, + authorDid, + preview, + onEmbedInteractionCallback, + style, +}: { + view: app.bsky.embed.external.ViewExternal + authorDid?: string + preview?: boolean + onEmbedInteractionCallback?: () => void + style?: StyleProp +}) { + const t = useTheme() + const {t: l} = useLingui() + const {gtPhone} = useBreakpoints() + const playHaptic = useHaptics() + const {data: authorProfile, isError: authorLookupFailed} = useProfileQuery({ + did: authorDid, + }) + const showAttieAccount = !authorDid || (authorLookupFailed && !authorProfile) + const {data: attieProfile} = useProfileQuery({ + did: showAttieAccount ? ATTIE_ACCOUNT_DID : undefined, + }) + const displayedProfile = showAttieAccount ? attieProfile : authorProfile + const displayedName = showAttieAccount + ? displayedProfile?.displayName || 'Attie' + : displayedProfile?.displayName || displayedProfile?.handle || authorDid + const displayedHandle = showAttieAccount + ? displayedProfile?.handle || 'attie.ai' + : displayedProfile?.handle + const provider = getAtCardProvider(view.uri) + if (!provider) return null + + return ( + { + playHaptic('Light') + onEmbedInteractionCallback?.() + }} + onLongPress={ + IS_NATIVE + ? () => { + playHaptic('Heavy') + void shareUrl(view.uri) + return false + } + : undefined + } + style={[ + a.w_full, + a.rounded_lg, + a.border, + t.atoms.border_contrast_low, + preview && a.pointer_events_none, + style, + ]}> + {({hovered, focused, pressed}) => ( + + {view.thumb && ( + + )} + + + {view.title} + + {view.description && ( + + {view.description} + + )} + + + + + + + + {displayedName} + + + {displayedHandle ? l`@${displayedHandle}` : authorDid} + + + + + {/* The CTA is visual; the enclosing link is the single interactive target. */} + + + {l`Visit site`} + + + + )} + + ) +} diff --git a/src/components/Post/Embed/AtCard/providers.ts b/src/components/Post/Embed/AtCard/providers.ts new file mode 100644 index 000000000..9660c1177 --- /dev/null +++ b/src/components/Post/Embed/AtCard/providers.ts @@ -0,0 +1,18 @@ +import {Attie_Filled_Corner0_Rounded as Attie} from '#/components/icons/Attie' +import {isAttieUrl} from './attie' + +/** Provider-specific branding and matching for the shared AT embed card. */ +const providers = [ + { + name: 'Attie', + matches: isAttieUrl, + Icon: Attie, + CtaIcon: Attie, + backgroundColor: '#6338ff', + hoverColor: '#552fe0', + }, +] + +export function getAtCardProvider(uri: string) { + return providers.find(provider => provider.matches(uri)) +} diff --git a/src/components/Post/Embed/index.tsx b/src/components/Post/Embed/index.tsx index 1468f9b3f..aab5a82c6 100644 --- a/src/components/Post/Embed/index.tsx +++ b/src/components/Post/Embed/index.tsx @@ -7,6 +7,7 @@ import {RichText as RichTextAPI} from '@bsky/sdk/richtext' import {Trans} from '@lingui/react/macro' import {useQueryClient} from '@tanstack/react-query' +import {getEmbedCreator} from '#/lib/at-card' import {makeProfileLink} from '#/lib/routes/links' import {getChatInviteCodeFromUrl} from '#/lib/strings/url-helpers' import {useModerationOpts} from '#/state/preferences/moderation-opts' @@ -20,6 +21,8 @@ import {GalleryBleed} from '#/components/images/Gallery' import {ContentHider} from '#/components/moderation/ContentHider' import {PostAlerts} from '#/components/moderation/PostAlerts' import * as ReportDialogMetadataContext from '#/components/moderation/ReportDialog/ReportDialogMetadataContext' +import {AtCard} from '#/components/Post/Embed/AtCard' +import {getAtCardProvider} from '#/components/Post/Embed/AtCard/providers' import {StandardSiteEmbed} from '#/components/Post/Embed/StandardSiteEmbed' import {isStandardSiteEmbed} from '#/components/Post/Embed/StandardSiteEmbed/utils' import {RichText} from '#/components/RichText' @@ -99,13 +102,19 @@ function MediaEmbed({ ) } case 'link': { - if (isStandardSiteEmbed(embed.view.external)) { + const atProvider = getAtCardProvider(embed.view.external.uri) + if (atProvider || isStandardSiteEmbed(embed.view.external)) { + const Card = atProvider ? AtCard : StandardSiteEmbed return ( - diff --git a/src/components/icons/Attie.tsx b/src/components/icons/Attie.tsx new file mode 100644 index 000000000..4925323a5 --- /dev/null +++ b/src/components/icons/Attie.tsx @@ -0,0 +1,29 @@ +import {forwardRef} from 'react' +import Svg, {Path} from 'react-native-svg' + +import {type Props, useCommonSVGProps} from '#/components/icons/common' + +// The eye shapes are cut out of the body with even-odd fill. A single path +// avoids platform differences in SVG mask rendering. +const path = + 'M14.398 2c4.502 0 8.151 3.634 8.151 8.117q0 .678-.108 1.325a5.67 5.67 0 0 1 1.507 3.855c0 8.837-23.948 8.837-23.948 0a5.68 5.68 0 0 1 1.977-4.31 5 5 0 0 1-.153-1.234c0-2.775 2.259-5.025 5.046-5.025q.67.002 1.289.166A8.15 8.15 0 0 1 14.399 2Zm-5.07 9.032a1.6 1.6 0 0 0-.46.008 1.09 1.09 0 0 1-1.628 1.414c-.137.3-.236.637-.284.997-.198 1.484.543 2.807 1.656 2.956 1.112.148 2.174-.934 2.372-2.419s-.544-2.807-1.656-2.956Zm6.008 0a1.6 1.6 0 0 0-.46.008 1.09 1.09 0 0 1-1.628 1.414c-.137.3-.236.637-.284.997-.198 1.484.544 2.807 1.656 2.956 1.112.148 2.174-.935 2.372-2.419s-.544-2.807-1.656-2.956Z' + +export const Attie_Filled_Corner0_Rounded = forwardRef( + function Attie_Filled_Corner0_Rounded(props, ref) { + const {fill, size, style, gradient, ...rest} = useCommonSVGProps(props) + + return ( + + {gradient} + + + ) + }, +) diff --git a/src/lib/api/index.ts b/src/lib/api/index.ts index 55b0fdecb..ad2d49b8b 100644 --- a/src/lib/api/index.ts +++ b/src/lib/api/index.ts @@ -10,6 +10,7 @@ import {t} from '@lingui/core/macro' import {type QueryClient} from '@tanstack/react-query' import {type LinkResolvers} from '#/lib/api/resolve' +import {withCreator} from '#/lib/at-card' import {IMAGE_SIZE_CONFIG_POSTS} from '#/lib/constants' import {isNetworkError} from '#/lib/strings/errors' import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip' @@ -464,13 +465,16 @@ async function resolveMedia( } return { $type: 'app.bsky.embed.external', - external: { - uri: resolvedLink.uri as UriString, - title: resolvedLink.title, - description: resolvedLink.description, - thumb: blob, - associatedRefs: resolvedLink.associatedRefs, - }, + external: withCreator( + { + uri: resolvedLink.uri as UriString, + title: resolvedLink.title, + description: resolvedLink.description, + thumb: blob, + associatedRefs: resolvedLink.associatedRefs, + }, + resolvedLink.authorDid, + ), } } if ( diff --git a/src/lib/api/resolve.ts b/src/lib/api/resolve.ts index 4d9ec3e45..79577a898 100644 --- a/src/lib/api/resolve.ts +++ b/src/lib/api/resolve.ts @@ -33,6 +33,7 @@ type ResolvedExternalLink = { title: string description: string thumb: ComposerImage | undefined + authorDid?: string /** * The AT-URI of the Atmosphere record representing this external content, if * it exists. Example: a site.standard.document record. @@ -272,6 +273,18 @@ function getFileSlug(url: string | undefined): string | undefined { return dotIndex > 0 ? filename.slice(0, dotIndex) : undefined } +function getAuthorDid(author: string | undefined): string | undefined { + if (!author) return + + try { + const uri = new AtUri(author) + if (!uri.host.startsWith('did:') || uri.collection || uri.rkey) return + return uri.host + } catch { + return + } +} + async function resolveExternal(uri: string): Promise { const result = await getLinkMeta(uri) return { @@ -280,6 +293,7 @@ async function resolveExternal(uri: string): Promise { title: result.title ?? '', description: result.description ?? '', thumb: result.image ? await imageToThumb(result.image) : undefined, + authorDid: getAuthorDid(result.author), /* * New fields from Standard Site integration. Other fields are derived from * opengraph/oembed as before. diff --git a/src/lib/at-card.test.ts b/src/lib/at-card.test.ts new file mode 100644 index 000000000..b462a9d6b --- /dev/null +++ b/src/lib/at-card.test.ts @@ -0,0 +1,69 @@ +import {type UriString} from '@atproto/syntax' + +import {app} from '#/lexicons' +import * as bsky from '#/types/bsky' +import {getEmbedCreator, withCreator} from './at-card' + +const uri = 'https://example.attie.site/' as UriString +const did = 'did:plc:4hodhjl2kposuchzvpiviwps' +const external = {uri, title: 'Site', description: ''} +const embed = { + $type: 'app.bsky.embed.external', + external: withCreator(external, did), +} + +it('retains the creator through post validation and JSON round-trip', () => { + const record = bsky.parse(app.bsky.feed.post, { + $type: 'app.bsky.feed.post', + text: uri, + createdAt: '2026-09-23T00:00:00.000Z', + embed, + }) + expect(getEmbedCreator(JSON.parse(JSON.stringify(record)), uri)).toBe(did) +}) + +it('reads the creator from quote-post media rather than the quoted record', () => { + expect( + getEmbedCreator( + { + embed: { + $type: 'app.bsky.embed.recordWithMedia', + record: {creator: 'did:plc:other'}, + media: embed, + }, + }, + uri, + ), + ).toBe(did) +}) + +it('does not borrow attribution from a different URL', () => { + expect(getEmbedCreator({embed}, 'https://other.attie.site/')).toBeUndefined() +}) + +it.each([ + undefined, + null, + {}, + {embed: null}, + {embed: {external}}, + { + embed: { + $type: 'app.bsky.embed.external', + external: {...external, creator: 'not-a-did'}, + }, + }, + {embed: {$type: 'app.bsky.embed.external', external}}, +])( + 'handles old and malformed records without guessing an author: %p', + record => { + expect(getEmbedCreator(record, uri)).toBeUndefined() + }, +) + +it.each([undefined, 'not-a-did', 'at://did:plc:abc'])( + 'does not persist invalid creator %p', + creator => { + expect(withCreator(external, creator)).not.toHaveProperty('creator') + }, +) diff --git a/src/lib/at-card.ts b/src/lib/at-card.ts new file mode 100644 index 000000000..f5b75d525 --- /dev/null +++ b/src/lib/at-card.ts @@ -0,0 +1,35 @@ +import {isValidDid} from '@atproto/syntax' + +import {type app} from '#/lexicons' + +/** + * Optional application extension on an external embed's record. AppView returns + * the original post record even before this field is standardized in the + * external embed lexicon. This is a publisher-supplied attribution claim, not + * proof of ownership. Never infer it from the person sharing the link. + */ +export function withCreator( + external: app.bsky.embed.external.External, + creator: string | undefined, +): app.bsky.embed.external.External & {creator?: string} { + return isValidDid(creator) ? {...external, creator} : external +} + +function isObject(value: unknown): value is Record { + return typeof value === 'object' && value !== null +} + +/** Read attribution only from the record's matching external media. */ +export function getEmbedCreator( + record: unknown, + uri: string, +): string | undefined { + if (!isObject(record)) return + let embed = record.embed + if (!isObject(embed)) return + if (embed.$type === 'app.bsky.embed.recordWithMedia') embed = embed.media + if (!isObject(embed) || embed.$type !== 'app.bsky.embed.external') return + const external = embed.external + if (!isObject(external) || external.uri !== uri) return + return isValidDid(external.creator) ? external.creator : undefined +} diff --git a/src/lib/link-meta/link-meta.ts b/src/lib/link-meta/link-meta.ts index c79b4215a..0672b5f06 100644 --- a/src/lib/link-meta/link-meta.ts +++ b/src/lib/link-meta/link-meta.ts @@ -21,6 +21,7 @@ export interface LinkMeta { title?: string description?: string image?: string + author?: string /** * The AT-URI of the Atmosphere record representing this external content, if * it exists. Example: a site.standard.document record. @@ -29,6 +30,18 @@ export interface LinkMeta { view?: app.bsky.embed.external.View } +type CardybLinkMetaResponse = { + error: string + url: string + title?: string + description?: string + image?: string + author?: string + associated_refs?: LinkMeta['associatedRefs'] + view?: LinkMeta['view'] + external_view?: LinkMeta['view'] +} + export async function getLinkMeta( url: string, timeout = 15e3, @@ -79,7 +92,7 @@ export async function getLinkMeta( {signal: controller.signal}, ) - const body = await response.json() + const body = (await response.json()) as CardybLinkMetaResponse if (body.error !== '') { throw new Error(body.error) @@ -87,6 +100,7 @@ export async function getLinkMeta( meta.description = body.description meta.image = body.image + meta.author = body.author meta.title = body.title meta.associatedRefs = body.associated_refs meta.view = body.view || body.external_view diff --git a/src/view/com/composer/ExternalEmbed.tsx b/src/view/com/composer/ExternalEmbed.tsx index b39e1393f..0397510f0 100644 --- a/src/view/com/composer/ExternalEmbed.tsx +++ b/src/view/com/composer/ExternalEmbed.tsx @@ -10,6 +10,8 @@ import { import {ExternalEmbedRemoveBtn} from '#/view/com/composer/ExternalEmbedRemoveBtn' import {atoms as a, useTheme} from '#/alf' import {Loader} from '#/components/Loader' +import {AtCard} from '#/components/Post/Embed/AtCard' +import {getAtCardProvider} from '#/components/Post/Embed/AtCard/providers' import {ExternalEmbed} from '#/components/Post/Embed/ExternalEmbed' import {ModeratedFeedEmbed} from '#/components/Post/Embed/FeedEmbed' import {JoinRequestEmbed} from '#/components/Post/Embed/JoinRequestEmbed' @@ -90,10 +92,16 @@ export const ExternalEmbedLink = ({ const linkComponent = useMemo(() => { if (data) { if (data.type === 'external') { - if (data.view && isStandardSiteEmbed(data.view.external)) { + const atProvider = getAtCardProvider(uri) + if ( + (data.view && isStandardSiteEmbed(data.view.external)) || + atProvider + ) { + const Card = atProvider ? AtCard : StandardSiteEmbed return ( -