From af5420222bb56b7fac2d9f25b9dda208f12525ab Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 8 May 2023 18:17:37 -0500 Subject: [PATCH] Add recepients-inline for better Mastodon compatibility --- src/transmute.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/transmute.ts b/src/transmute.ts index bb4a120..51f8995 100644 --- a/src/transmute.ts +++ b/src/transmute.ts @@ -1,4 +1,4 @@ -import { findReplyTag, lodash, nip19, TTLCache, unfurl, z } from '@/deps.ts'; +import { findReplyTag, lodash, nip19, nip21, TTLCache, unfurl, z } from '@/deps.ts'; import { type Event } from '@/event.ts'; import { type MetaContent, parseMetaContent } from '@/schema.ts'; @@ -107,11 +107,18 @@ async function toStatus(event: Event<1>) { const { html, links, firstUrl } = parseNoteContent(event.content); const mediaLinks = getMediaLinks(links); + const [mentions, card] = await Promise.all([ + Promise.all(mentionedPubkeys.map(toMention)), + firstUrl ? await unfurlCardCached(firstUrl) : null, + ]); + + const content = buildInlineRecipients(mentions) + html; + return { id: event.id, account, - card: firstUrl ? await unfurlCardCached(firstUrl) : null, - content: html, + card, + content, created_at: new Date(event.created_at * 1000).toISOString(), in_reply_to_id: replyTag ? replyTag[1] : null, in_reply_to_account_id: null, @@ -129,7 +136,7 @@ async function toStatus(event: Event<1>) { reblog: null, application: null, media_attachments: mediaLinks.map(renderAttachment), - mentions: await Promise.all(mentionedPubkeys.map(toMention)), + mentions, tags: [], emojis: [], poll: null, @@ -138,6 +145,18 @@ async function toStatus(event: Event<1>) { }; } +type Mention = Awaited>; + +function buildInlineRecipients(mentions: Mention[]): string { + const elements = mentions.reduce((acc, { url, username }) => { + const name = nip21.BECH32_REGEX.test(username) ? username.substring(0, 8) : username; + acc.push(`@${name}`); + return acc; + }, []); + + return `${elements.join(' ')} `; +} + const attachmentTypeSchema = z.enum(['image', 'video', 'gifv', 'audio', 'unknown']).catch('unknown'); function renderAttachment({ url, mimeType }: MediaLink) {