From 112081e4bb6025419eda23fd84bd8eae8de2d620 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 12 Feb 2025 13:21:04 -0600 Subject: [PATCH] Enable media:true for legacy (non-imeta) URL attachments --- src/storages/EventsDB.ts | 8 ++++++++ src/utils/note.ts | 7 +------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index a96a2ba3..f8e47f2f 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -6,6 +6,7 @@ import { logi } from '@soapbox/logi'; import { JsonValue } from '@std/json'; import { LanguageCode } from 'iso-639-1'; import { Kysely } from 'kysely'; +import linkify from 'linkifyjs'; import { nip27 } from 'nostr-tools'; import { z } from 'zod'; @@ -17,6 +18,7 @@ import { abortError } from '@/utils/abort.ts'; import { purifyEvent } from '@/utils/purify.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { detectLanguage } from '@/utils/language.ts'; +import { getMediaLinks } from '@/utils/note.ts'; /** Function to decide whether or not to index a tag. */ type TagCondition = (opts: TagConditionOpts) => boolean; @@ -97,6 +99,12 @@ class EventsDB extends NPostgres { }) ); + // quirks mode + if (!imeta.length && event.kind === 1) { + const links = linkify.find(event.content).filter(({ type }) => type === 'url'); + imeta.push(...getMediaLinks(links)); + } + if (imeta.length) { ext.media = 'true'; diff --git a/src/utils/note.ts b/src/utils/note.ts index bae371ff..45fcf94a 100644 --- a/src/utils/note.ts +++ b/src/utils/note.ts @@ -22,7 +22,7 @@ interface ParsedNoteContent { /** Convert Nostr content to Mastodon API HTML. Also return parsed data. */ function parseNoteContent(content: string, mentions: MastodonMention[]): ParsedNoteContent { - const links = linkify.find(content).filter(isLinkURL); + const links = linkify.find(content).filter(({ type }) => type === 'url'); const firstUrl = links.find(isNonMediaLink)?.href; const result = linkifyStr(content, { @@ -123,11 +123,6 @@ function isNonMediaLink({ href }: Link): boolean { return /^https?:\/\//.test(href) && !getUrlMediaType(href); } -/** Ensures the Link is a URL so it can be parsed. */ -function isLinkURL(link: Link): boolean { - return link.type === 'url'; -} - /** Get pubkey from decoded bech32 entity, or undefined if not applicable. */ function getDecodedPubkey(decoded: nip19.DecodeResult): string | undefined { switch (decoded.type) {