Fix legacy quote posts

This commit is contained in:
Alex Gleason 2024-05-21 13:04:23 -05:00
parent d0ab1d55ce
commit f30aad11a5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 18 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { refreshAuthorStatsDebounced } from '@/stats.ts'; import { refreshAuthorStatsDebounced } from '@/stats.ts';
import { findQuoteTag } from '@/tags.ts';
interface HydrateOpts { interface HydrateOpts {
events: DittoEvent[]; events: DittoEvent[];
@ -81,7 +82,7 @@ function assembleEvents(
event.user = b.find((e) => matchFilter({ kinds: [30361], authors: [admin], '#d': [event.pubkey] }, e)); event.user = b.find((e) => matchFilter({ kinds: [30361], authors: [admin], '#d': [event.pubkey] }, e));
if (event.kind === 1) { if (event.kind === 1) {
const id = event.tags.find(([name]) => name === 'q')?.[1]; const id = findQuoteTag(event.tags)?.[1];
if (id) { if (id) {
event.quote = b.find((e) => matchFilter({ kinds: [1], ids: [id] }, e)); event.quote = b.find((e) => matchFilter({ kinds: [1], ids: [id] }, e));
} }
@ -169,7 +170,7 @@ function gatherQuotes({ events, store, signal }: HydrateOpts): Promise<DittoEven
for (const event of events) { for (const event of events) {
if (event.kind === 1) { if (event.kind === 1) {
const id = event.tags.find(([name]) => name === 'q')?.[1]; const id = findQuoteTag(event.tags)?.[1];
if (id) { if (id) {
ids.add(id); ids.add(id);
} }

View File

@ -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 isRootTag = (tag: string[]) => tag[0] === 'e' && tag[3] === 'root';
const isLegacyReplyTag = (tag: string[]) => tag[0] === 'e' && !tag[3]; 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); 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 };

View File

@ -1,11 +1,10 @@
import { NostrEvent } from '@nostrify/nostrify'; 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 { nip19 } from 'nostr-tools';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { findReplyTag } from '@/tags.ts'; import { findQuoteTag, findReplyTag } from '@/tags.ts';
import { nostrDate } from '@/utils.ts'; import { nostrDate } from '@/utils.ts';
import { getMediaLinks, parseNoteContent, stripimeta } from '@/utils/note.ts'; import { getMediaLinks, parseNoteContent, stripimeta } from '@/utils/note.ts';
import { unfurlCardCached } from '@/utils/unfurl.ts'; import { unfurlCardCached } from '@/utils/unfurl.ts';
@ -30,6 +29,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
: await accountFromPubkey(event.pubkey); : await accountFromPubkey(event.pubkey);
const replyTag = findReplyTag(event.tags); const replyTag = findReplyTag(event.tags);
const quoteTag = findQuoteTag(event.tags);
const mentionedPubkeys = [ const mentionedPubkeys = [
...new Set( ...new Set(
@ -73,8 +73,8 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
const content = buildInlineRecipients(mentions) + html; const content = buildInlineRecipients(mentions) + html;
const cw = event.tags.find(isCWTag); const cw = event.tags.find(([name]) => name === 'content-warning');
const subject = event.tags.find((tag) => tag[0] === 'subject'); const subject = event.tags.find(([name]) => name === 'subject');
const imeta: string[][][] = event.tags const imeta: string[][][] = event.tags
.filter(([name]) => name === 'imeta') .filter(([name]) => name === 'imeta')
@ -88,7 +88,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
card, card,
content, content,
created_at: nostrDate(event.created_at).toISOString(), 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, in_reply_to_account_id: null,
sensitive: !!cw, sensitive: !!cw,
spoiler_text: (cw ? cw[1] : subject?.[1]) || '', spoiler_text: (cw ? cw[1] : subject?.[1]) || '',
@ -110,7 +110,7 @@ async function renderStatus(event: DittoEvent, opts: RenderStatusOpts): Promise<
emojis: renderEmojis(event), emojis: renderEmojis(event),
poll: null, poll: null,
quote: !event.quote ? null : await renderStatus(event.quote, { depth: depth + 1 }), 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), uri: Conf.external(note),
url: Conf.external(note), url: Conf.external(note),
zapped: Boolean(zapEvent), zapped: Boolean(zapEvent),