Merge branch 'perf-hydrate-repost' into 'main'

Perf hydrate repost

See merge request soapbox-pub/ditto!154
This commit is contained in:
Alex Gleason 2024-04-13 21:37:54 +00:00
commit fe6b42211c
7 changed files with 62 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import { getLnurl } from '@/utils/lnurl.ts';
import { nip05Cache } from '@/utils/nip05.ts';
import { asyncReplaceAll } from '@/utils/text.ts';
import { eventsDB } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
const createStatusSchema = z.object({
in_reply_to_id: z.string().regex(/[0-9a-f]{64}/).nullish(),
@ -212,6 +213,7 @@ const favouritedByController: AppController = (c) => {
/** https://docs.joinmastodon.org/methods/statuses/#boost */
const reblogStatusController: AppController = async (c) => {
const eventId = c.req.param('id');
const { signal } = c.req.raw;
const event = await getEvent(eventId, {
kind: 1,
@ -226,6 +228,13 @@ const reblogStatusController: AppController = async (c) => {
tags: [['e', event.id], ['p', event.pubkey]],
}, c);
await hydrateEvents({
events: [reblogEvent],
relations: ['repost', 'author'],
storage: eventsDB,
signal: signal,
});
const status = await renderReblog(reblogEvent);
return c.json(status);

View File

@ -6,6 +6,8 @@ import { getFeedPubkeys } from '@/queries.ts';
import { Sub } from '@/subs.ts';
import { bech32ToPubkey } from '@/utils.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { eventsDB } from '@/storages.ts';
const debug = Debug('ditto:streaming');
@ -64,6 +66,13 @@ const streamingController: AppController = (c) => {
if (filter) {
for await (const event of Sub.sub(socket, '1', [filter])) {
if (event.kind === 6) {
await hydrateEvents({
events: [event],
relations: ['repost', 'author'],
storage: eventsDB,
signal: AbortSignal.timeout(1000),
});
const status = await renderReblog(event);
if (status) {
send('update', status);

View File

@ -49,7 +49,12 @@ async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
const events = await eventsDB
.query(filters, { signal })
.then((events) =>
hydrateEvents({ events, relations: ['author', 'author_stats', 'event_stats'], storage: eventsDB, signal })
hydrateEvents({
events,
relations: ['author', 'author_stats', 'event_stats', 'repost'],
storage: eventsDB,
signal,
})
);
if (!events.length) {

View File

@ -1,5 +1,5 @@
import './precheck.ts';
import './sentry.ts';
import app from './app.ts';
import '@/precheck.ts';
import '@/sentry.ts';
import app from '@/app.ts';
Deno.serve(app.fetch);

View File

@ -33,6 +33,9 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise<DittoEvent[]> {
case 'user':
await hydrateUsers({ events, storage, signal });
break;
case 'repost':
await hydrateRepostEvents({ events, storage, signal });
break;
}
}
@ -111,6 +114,36 @@ async function hydrateEventStats(events: DittoEvent[]): Promise<DittoEvent[]> {
return events;
}
async function hydrateRepostEvents(opts: Omit<HydrateEventOpts, 'relations'>): Promise<DittoEvent[]> {
const { events, storage, signal } = opts;
const results = await storage.query([{
kinds: [1],
ids: events.map((event) => {
if (event.kind === 6) {
const originalPostId = event.tags.find(([name]) => name === 'e')?.[1];
if (!originalPostId) return event.id;
else return originalPostId;
}
return event.id;
}),
}]);
for (const event of events) {
if (event.kind === 6) {
const originalPostId = event.tags.find(([name]) => name === 'e')?.[1];
if (!originalPostId) continue;
const originalPostEvent = results.find((event) => event.id === originalPostId);
if (!originalPostEvent) continue;
await hydrateAuthors({ events: [originalPostEvent], storage: storage, signal: signal });
event.repost = originalPostEvent;
}
}
return events;
}
/** Return a normalized event without any non-standard keys. */
function purifyEvent(event: NostrEvent): NostrEvent {
return {

View File

@ -4,7 +4,7 @@ import { Conf } from '@/config.ts';
import { nip19 } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getMediaLinks, parseNoteContent } from '@/note.ts';
import { getAuthor, getEvent } from '@/queries.ts';
import { getAuthor } from '@/queries.ts';
import { jsonMediaDataSchema } from '@/schemas/nostr.ts';
import { eventsDB } from '@/storages.ts';
import { findReplyTag } from '@/tags.ts';
@ -106,7 +106,6 @@ async function renderReblog(event: DittoEvent) {
const repostId = event.tags.find(([name]) => name === 'e')?.[1];
if (!repostId) return;
event.repost = await getEvent(repostId, { kind: 1 });
if (!event.repost) return;
const reblog = await renderStatus(event.repost);

View File

@ -1,6 +1,6 @@
import { assertEquals, assertRejects } from '@/deps-test.ts';
import { fetchWorker } from './fetch.ts';
import { fetchWorker } from '@/workers/fetch.ts';
Deno.test({
name: 'fetchWorker',