Merge branch 'accounts-user' into 'main'

renderAccount: stop calling findUser, rely on event hydration

Closes #86

See merge request soapbox-pub/ditto!126
This commit is contained in:
Alex Gleason 2024-03-31 04:42:49 +00:00
commit b2f2240d93
3 changed files with 31 additions and 19 deletions

View file

@ -7,9 +7,8 @@ import { Debug, LNURL, type NostrEvent } from '@/deps.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { isEphemeralKind } from '@/kinds.ts'; import { isEphemeralKind } from '@/kinds.ts';
import { DVM } from '@/pipeline/DVM.ts'; import { DVM } from '@/pipeline/DVM.ts';
import { getAuthor } from '@/queries.ts';
import { updateStats } from '@/stats.ts'; import { updateStats } from '@/stats.ts';
import { purifyEvent } from '@/storages/hydrate.ts'; import { hydrateEvents, purifyEvent } from '@/storages/hydrate.ts';
import { cache, client, eventsDB, reqmeister } from '@/storages.ts'; import { cache, client, eventsDB, reqmeister } from '@/storages.ts';
import { Sub } from '@/subs.ts'; import { Sub } from '@/subs.ts';
import { getTagSet } from '@/tags.ts'; import { getTagSet } from '@/tags.ts';
@ -31,7 +30,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
if (!(await verifyEventWorker(event))) return; if (!(await verifyEventWorker(event))) return;
if (await encounterEvent(event, signal)) return; if (await encounterEvent(event, signal)) return;
debug(`NostrEvent<${event.kind}> ${event.id}`); debug(`NostrEvent<${event.kind}> ${event.id}`);
await hydrateEvent(event); await hydrateEvent(event, signal);
await Promise.all([ await Promise.all([
storeEvent(event, signal), storeEvent(event, signal),
@ -57,12 +56,8 @@ async function encounterEvent(event: NostrEvent, signal: AbortSignal): Promise<b
} }
/** Hydrate the event with the user, if applicable. */ /** Hydrate the event with the user, if applicable. */
async function hydrateEvent(event: DittoEvent): Promise<void> { async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> {
const [user] = await eventsDB.query([{ kinds: [30361], authors: [Conf.pubkey], '#d': [event.pubkey], limit: 1 }]); await hydrateEvents({ events: [event], relations: ['author', 'user'], storage: eventsDB, signal });
event.user = user;
const author = await getAuthor(event.pubkey);
event.author = author;
const domain = await db const domain = await db
.selectFrom('pubkey_domains') .selectFrom('pubkey_domains')

View file

@ -1,3 +1,4 @@
import { Conf } from '@/config.ts';
import { db } from '@/db.ts'; import { db } from '@/db.ts';
import { type NostrEvent, type NStore } from '@/deps.ts'; import { type NostrEvent, type NStore } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
@ -29,6 +30,9 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise<DittoEvent[]> {
case 'event_stats': case 'event_stats':
await hydrateEventStats(events); await hydrateEventStats(events);
break; break;
case 'user':
await hydrateUsers({ events, storage, signal });
break;
} }
} }
@ -48,6 +52,23 @@ async function hydrateAuthors(opts: Omit<HydrateEventOpts, 'relations'>): Promis
return events; return events;
} }
async function hydrateUsers(opts: Omit<HydrateEventOpts, 'relations'>): Promise<DittoEvent[]> {
const { events, storage, signal } = opts;
const pubkeys = new Set([...events].map((event) => event.pubkey));
const users = await storage.query(
[{ kinds: [30361], authors: [Conf.pubkey], '#d': [...pubkeys], limit: pubkeys.size }],
{ signal },
);
for (const event of events) {
event.user = users.find((user) => user.tags.find(([name]) => name === 'd')?.[1] === event.pubkey);
}
return events;
}
async function hydrateAuthorStats(events: DittoEvent[]): Promise<DittoEvent[]> { async function hydrateAuthorStats(events: DittoEvent[]): Promise<DittoEvent[]> {
const results = await db const results = await db
.selectFrom('author_stats') .selectFrom('author_stats')

View file

@ -1,5 +1,4 @@
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { findUser } from '@/db/users.ts';
import { lodash, nip19, type UnsignedEvent } from '@/deps.ts'; import { lodash, nip19, type UnsignedEvent } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
@ -30,11 +29,8 @@ async function renderAccount(
} = jsonMetaContentSchema.parse(event.content); } = jsonMetaContentSchema.parse(event.content);
const npub = nip19.npubEncode(pubkey); const npub = nip19.npubEncode(pubkey);
const parsed05 = await parseAndVerifyNip05(nip05, pubkey);
const [user, parsed05] = await Promise.all([ const role = event.user?.tags.find(([name]) => name === 'role')?.[1] ?? 'user';
findUser({ pubkey }),
parseAndVerifyNip05(nip05, pubkey),
]);
return { return {
id: pubkey, id: pubkey,
@ -42,7 +38,7 @@ async function renderAccount(
avatar: picture, avatar: picture,
avatar_static: picture, avatar_static: picture,
bot: false, bot: false,
created_at: user ? user.inserted_at.toISOString() : nostrDate(event.created_at).toISOString(), created_at: nostrDate(event.user?.created_at ?? event.created_at).toISOString(),
discoverable: true, discoverable: true,
display_name: name, display_name: name,
emojis: renderEmojis(event), emojis: renderEmojis(event),
@ -75,11 +71,11 @@ async function renderAccount(
username: parsed05?.nickname || npub.substring(0, 8), username: parsed05?.nickname || npub.substring(0, 8),
ditto: { ditto: {
accepts_zaps: Boolean(getLnurl({ lud06, lud16 })), accepts_zaps: Boolean(getLnurl({ lud06, lud16 })),
is_registered: Boolean(user), is_registered: Boolean(event.user),
}, },
pleroma: { pleroma: {
is_admin: user?.admin || false, is_admin: role === 'admin',
is_moderator: user?.admin || false, is_moderator: ['admin', 'moderator'].includes(role),
}, },
nostr: { nostr: {
pubkey, pubkey,