perf: hydrate repost events in reblog endpoint & streaming
This commit is contained in:
parent
24efca5ea0
commit
37bee709cd
|
@ -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,7 +228,14 @@ const reblogStatusController: AppController = async (c) => {
|
|||
tags: [['e', event.id], ['p', event.pubkey]],
|
||||
}, c);
|
||||
|
||||
const status = await renderReblog(reblogEvent, { loadOriginalPostEvent: true });
|
||||
await hydrateEvents({
|
||||
events: [reblogEvent, event],
|
||||
relations: ['repost', 'author'],
|
||||
storage: eventsDB,
|
||||
signal: signal,
|
||||
});
|
||||
|
||||
const status = await renderReblog(reblogEvent);
|
||||
|
||||
return c.json(status);
|
||||
};
|
||||
|
|
|
@ -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,7 +66,14 @@ const streamingController: AppController = (c) => {
|
|||
if (filter) {
|
||||
for await (const event of Sub.sub(socket, '1', [filter])) {
|
||||
if (event.kind === 6) {
|
||||
const status = await renderReblog(event, { loadOriginalPostEvent: true });
|
||||
await hydrateEvents({
|
||||
events: [event],
|
||||
relations: ['repost', 'author'],
|
||||
storage: eventsDB,
|
||||
signal: AbortSignal.timeout(1000),
|
||||
});
|
||||
|
||||
const status = await renderReblog(event);
|
||||
if (status) {
|
||||
send('update', status);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
|
|||
|
||||
const statuses = (await Promise.all(events.map((event) => {
|
||||
if (event.kind === 6) {
|
||||
return renderReblog(event, {});
|
||||
return renderReblog(event);
|
||||
}
|
||||
return renderStatus(event, c.get('pubkey'));
|
||||
}))).filter((boolean) => boolean);
|
||||
|
|
|
@ -3,7 +3,6 @@ import { db } from '@/db.ts';
|
|||
import { type NostrEvent, type NStore } from '@/deps.ts';
|
||||
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||
import { type DittoRelation } from '@/interfaces/DittoFilter.ts';
|
||||
import { eventsDB } from '@/storages.ts';
|
||||
|
||||
interface HydrateEventOpts {
|
||||
events: DittoEvent[];
|
||||
|
@ -35,7 +34,7 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise<DittoEvent[]> {
|
|||
await hydrateUsers({ events, storage, signal });
|
||||
break;
|
||||
case 'repost':
|
||||
await hydrateRepostEvents(events);
|
||||
await hydrateRepostEvents({ events, storage, signal });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -115,8 +114,19 @@ async function hydrateEventStats(events: DittoEvent[]): Promise<DittoEvent[]> {
|
|||
return events;
|
||||
}
|
||||
|
||||
async function hydrateRepostEvents(events: DittoEvent[]): Promise<DittoEvent[]> {
|
||||
const results = await eventsDB.query([{ kinds: [1], ids: events.map((event) => event.id) }]);
|
||||
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) {
|
||||
|
@ -126,6 +136,7 @@ async function hydrateRepostEvents(events: DittoEvent[]): Promise<DittoEvent[]>
|
|||
const originalPostEvent = results.find((event) => event.id === originalPostId);
|
||||
if (!originalPostEvent) continue;
|
||||
|
||||
await hydrateAuthors({ events: [originalPostEvent], storage: storage, signal: signal });
|
||||
event.repost = originalPostEvent;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
@ -100,21 +100,12 @@ async function renderStatus(event: DittoEvent, viewerPubkey?: string) {
|
|||
};
|
||||
}
|
||||
|
||||
type reblogOpts = {
|
||||
loadOriginalPostEvent?: boolean;
|
||||
};
|
||||
|
||||
async function renderReblog(event: DittoEvent, opts: reblogOpts) {
|
||||
const { loadOriginalPostEvent } = opts;
|
||||
|
||||
async function renderReblog(event: DittoEvent) {
|
||||
if (!event.author) return;
|
||||
|
||||
const repostId = event.tags.find(([name]) => name === 'e')?.[1];
|
||||
if (!repostId) return;
|
||||
|
||||
if (loadOriginalPostEvent) {
|
||||
event.repost = await getEvent(repostId, { kind: 1 });
|
||||
}
|
||||
if (!event.repost) return;
|
||||
|
||||
const reblog = await renderStatus(event.repost);
|
||||
|
|
Loading…
Reference in New Issue