From f30aad11a5281e7599434486818f62ddc6177ac3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 21 May 2024 13:04:23 -0500 Subject: [PATCH] Fix legacy quote posts --- src/storages/hydrate.ts | 5 +++-- src/tags.ts | 11 +++++++++-- src/views/mastodon/statuses.ts | 12 ++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index e5c488e..f55d741 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -6,6 +6,7 @@ import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { DittoTables } from '@/db/DittoTables.ts'; import { Conf } from '@/config.ts'; import { refreshAuthorStatsDebounced } from '@/stats.ts'; +import { findQuoteTag } from '@/tags.ts'; interface HydrateOpts { events: DittoEvent[]; @@ -81,7 +82,7 @@ function assembleEvents( event.user = b.find((e) => matchFilter({ kinds: [30361], authors: [admin], '#d': [event.pubkey] }, e)); if (event.kind === 1) { - const id = event.tags.find(([name]) => name === 'q')?.[1]; + const id = findQuoteTag(event.tags)?.[1]; if (id) { event.quote = b.find((e) => matchFilter({ kinds: [1], ids: [id] }, e)); } @@ -169,7 +170,7 @@ function gatherQuotes({ events, store, signal }: HydrateOpts): Promise name === 'q')?.[1]; + const id = findQuoteTag(event.tags)?.[1]; if (id) { ids.add(id); } diff --git a/src/tags.ts b/src/tags.ts index a683393..ecddaf4 100644 --- a/src/tags.ts +++ b/src/tags.ts @@ -35,8 +35,15 @@ const isReplyTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'reply'; const isRootTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'root'; const isLegacyReplyTag = (tag: string[]) => tag[0] === 'e' && !tag[3]; -function findReplyTag(tags: string[][]) { +const isQuoteTag = (tag: string[]) => tag[0] === 'q'; +const isLegacyQuoteTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'mention'; + +function findReplyTag(tags: string[][]): string[] | undefined { return tags.find(isReplyTag) || tags.find(isRootTag) || tags.findLast(isLegacyReplyTag); } -export { addTag, deleteTag, findReplyTag, getTagSet, hasTag }; +function findQuoteTag(tags: string[][]): string[] | undefined { + return tags.find(isQuoteTag) || tags.find(isLegacyQuoteTag); +} + +export { addTag, deleteTag, findQuoteTag, findReplyTag, getTagSet, hasTag }; diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index a06aac2..1571977 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -1,11 +1,10 @@ import { NostrEvent } from '@nostrify/nostrify'; -import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; import { nip19 } from 'nostr-tools'; import { Conf } from '@/config.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { Storages } from '@/storages.ts'; -import { findReplyTag } from '@/tags.ts'; +import { findQuoteTag, findReplyTag } from '@/tags.ts'; import { nostrDate } from '@/utils.ts'; import { getMediaLinks, parseNoteContent, stripimeta } from '@/utils/note.ts'; import { unfurlCardCached } from '@/utils/unfurl.ts'; @@ -30,6 +29,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise< : await accountFromPubkey(event.pubkey); const replyTag = findReplyTag(event.tags); + const quoteTag = findQuoteTag(event.tags); const mentionedPubkeys = [ ...new Set( @@ -73,8 +73,8 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise< const content = buildInlineRecipients(mentions) + html; - const cw = event.tags.find(isCWTag); - const subject = event.tags.find((tag) => tag[0] === 'subject'); + const cw = event.tags.find(([name]) => name === 'content-warning'); + const subject = event.tags.find(([name]) => name === 'subject'); const imeta: string[][][] = event.tags .filter(([name]) => name === 'imeta') @@ -88,7 +88,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise< card, content, created_at: nostrDate(event.created_at).toISOString(), - in_reply_to_id: replyTag ? replyTag[1] : null, + in_reply_to_id: replyTag?.[1] ?? null, in_reply_to_account_id: null, sensitive: !!cw, spoiler_text: (cw ? cw[1] : subject?.[1]) || '', @@ -110,7 +110,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise< emojis: renderEmojis(event), poll: null, quote: !event.quote ? null : await renderStatus(event.quote, { depth: depth + 1 }), - quote_id: event.tags.find(([name]) => name === 'q')?.[1] ?? null, + quote_id: quoteTag?.[1] ?? null, uri: Conf.external(note), url: Conf.external(note), zapped: Boolean(zapEvent),