Merge branch 'mentions-perf' into 'main'

Improve mentions performance

See merge request soapbox-pub/ditto!190
This commit is contained in:
Alex Gleason 2024-04-28 17:16:01 +00:00
commit 61e648ff31
1 changed files with 11 additions and 6 deletions

View File

@ -1,12 +1,12 @@
import { NostrEvent } from '@nostrify/nostrify';
import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { nip19 } from '@/deps.ts'; import { nip19 } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getMediaLinks, parseNoteContent } from '@/note.ts'; import { getMediaLinks, parseNoteContent } from '@/note.ts';
import { getAuthor } from '@/queries.ts';
import { jsonMediaDataSchema } from '@/schemas/nostr.ts'; import { jsonMediaDataSchema } from '@/schemas/nostr.ts';
import { eventsDB } from '@/storages.ts'; import { eventsDB, optimizer } from '@/storages.ts';
import { findReplyTag } from '@/tags.ts'; import { findReplyTag } from '@/tags.ts';
import { nostrDate } from '@/utils.ts'; import { nostrDate } from '@/utils.ts';
import { unfurlCardCached } from '@/utils/unfurl.ts'; import { unfurlCardCached } from '@/utils/unfurl.ts';
@ -40,11 +40,17 @@ async function renderStatus(event: DittoEvent, opts: statusOpts): Promise<any> {
), ),
]; ];
const mentionedProfiles = await optimizer.query(
[{ authors: mentionedPubkeys, limit: mentionedPubkeys.length }],
);
const { html, links, firstUrl } = parseNoteContent(event.content); const { html, links, firstUrl } = parseNoteContent(event.content);
const [mentions, card, relatedEvents] = await Promise const [mentions, card, relatedEvents] = await Promise
.all([ .all([
Promise.all(mentionedPubkeys.map(toMention)), Promise.all(
mentionedPubkeys.map((pubkey) => toMention(pubkey, mentionedProfiles.find((event) => event.pubkey === pubkey))),
),
firstUrl ? unfurlCardCached(firstUrl) : null, firstUrl ? unfurlCardCached(firstUrl) : null,
viewerPubkey viewerPubkey
? await eventsDB.query([ ? await eventsDB.query([
@ -131,9 +137,8 @@ async function renderReblog(event: DittoEvent, opts: statusOpts) {
}; };
} }
async function toMention(pubkey: string) { async function toMention(pubkey: string, event?: NostrEvent) {
const author = await getAuthor(pubkey); const account = event ? await renderAccount(event) : undefined;
const account = author ? await renderAccount(author) : undefined;
if (account) { if (account) {
return { return {