diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index afd3673..7e4a4c3 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -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); diff --git a/src/controllers/api/streaming.ts b/src/controllers/api/streaming.ts index ed6ccdd..643ac01 100644 --- a/src/controllers/api/streaming.ts +++ b/src/controllers/api/streaming.ts @@ -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); diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 677bc60..c54abd4 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -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) { diff --git a/src/server.ts b/src/server.ts index 2a1906b..f433cde 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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); diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index e56ebe6..0876f90 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -33,6 +33,9 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise { 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 { return events; } +async function hydrateRepostEvents(opts: Omit): Promise { + 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 { diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index 6762e82..19ce381 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -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); diff --git a/src/workers/fetch.test.ts b/src/workers/fetch.test.ts index d9a6102..e7283b7 100644 --- a/src/workers/fetch.test.ts +++ b/src/workers/fetch.test.ts @@ -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',