mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Hydrate null authors
This commit is contained in:
parent
96416747c2
commit
33e5b20159
3 changed files with 41 additions and 8 deletions
|
|
@ -6,6 +6,7 @@ import { Storages } from '@/storages.ts';
|
||||||
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||||
import { type DittoRelation } from '@/interfaces/DittoFilter.ts';
|
import { type DittoRelation } from '@/interfaces/DittoFilter.ts';
|
||||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||||
|
import { fallbackAuthor } from '@/utils.ts';
|
||||||
import { findReplyTag, getTagSet } from '@/utils/tags.ts';
|
import { findReplyTag, getTagSet } from '@/utils/tags.ts';
|
||||||
|
|
||||||
const debug = Debug('ditto:queries');
|
const debug = Debug('ditto:queries');
|
||||||
|
|
@ -38,15 +39,21 @@ const getEvent = async (
|
||||||
.then(([event]) => event);
|
.then(([event]) => event);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
|
/**
|
||||||
const getAuthor = async (pubkey: string, opts: GetEventOpts = {}): Promise<NostrEvent | undefined> => {
|
* Get a Nostr `set_medatadata` event for a user's pubkey.
|
||||||
|
* @deprecated Use `store.query` directly.
|
||||||
|
*/
|
||||||
|
async function getAuthor(pubkey: string, opts: GetEventOpts = {}): Promise<NostrEvent | undefined> {
|
||||||
const store = await Storages.db();
|
const store = await Storages.db();
|
||||||
const { signal = AbortSignal.timeout(1000) } = opts;
|
const { signal = AbortSignal.timeout(1000) } = opts;
|
||||||
|
|
||||||
return await store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal })
|
const events = await store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal });
|
||||||
.then((events) => hydrateEvents({ events, store, signal }))
|
const event = events[0] ?? fallbackAuthor(pubkey);
|
||||||
.then(([event]) => event);
|
|
||||||
};
|
await hydrateEvents({ events: [event], store, signal });
|
||||||
|
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
/** Get users the given pubkey follows. */
|
/** Get users the given pubkey follows. */
|
||||||
const getFollows = async (pubkey: string, signal?: AbortSignal): Promise<NostrEvent | undefined> => {
|
const getFollows = async (pubkey: string, signal?: AbortSignal): Promise<NostrEvent | undefined> => {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import { z } from 'zod';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
|
||||||
|
import { fallbackAuthor } from '@/utils.ts';
|
||||||
import { findQuoteTag } from '@/utils/tags.ts';
|
import { findQuoteTag } from '@/utils/tags.ts';
|
||||||
import { findQuoteInContent } from '@/utils/note.ts';
|
import { findQuoteInContent } from '@/utils/note.ts';
|
||||||
import { getAmount } from '@/utils/bolt11.ts';
|
import { getAmount } from '@/utils/bolt11.ts';
|
||||||
|
|
@ -98,7 +99,8 @@ export function assembleEvents(
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (const event of a) {
|
for (const event of a) {
|
||||||
event.author = b.find((e) => matchFilter({ kinds: [0], authors: [event.pubkey] }, e));
|
event.author = b.find((e) => matchFilter({ kinds: [0], authors: [event.pubkey] }, e)) ??
|
||||||
|
fallbackAuthor(event.pubkey);
|
||||||
event.user = b.find((e) => matchFilter({ kinds: [30382], authors: [admin], '#d': [event.pubkey] }, e));
|
event.user = b.find((e) => matchFilter({ kinds: [30382], authors: [admin], '#d': [event.pubkey] }, e));
|
||||||
event.info = b.find((e) => matchFilter({ kinds: [30383], authors: [admin], '#d': [event.id] }, e));
|
event.info = b.find((e) => matchFilter({ kinds: [30383], authors: [admin], '#d': [event.id] }, e));
|
||||||
|
|
||||||
|
|
|
||||||
26
src/utils.ts
26
src/utils.ts
|
|
@ -74,6 +74,30 @@ function isURL(value: unknown): boolean {
|
||||||
return z.string().url().safeParse(value).success;
|
return z.string().url().safeParse(value).success;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { bech32ToPubkey, eventAge, findTag, isNostrId, isURL, type Nip05, nostrDate, nostrNow, parseNip05 };
|
/** Render an empty author event so other things can stick to it. */
|
||||||
|
function fallbackAuthor(pubkey: string): NostrEvent {
|
||||||
|
return {
|
||||||
|
kind: 0,
|
||||||
|
pubkey,
|
||||||
|
content: '',
|
||||||
|
tags: [],
|
||||||
|
created_at: nostrNow(),
|
||||||
|
id: '',
|
||||||
|
sig: '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
bech32ToPubkey,
|
||||||
|
eventAge,
|
||||||
|
fallbackAuthor,
|
||||||
|
findTag,
|
||||||
|
isNostrId,
|
||||||
|
isURL,
|
||||||
|
type Nip05,
|
||||||
|
nostrDate,
|
||||||
|
nostrNow,
|
||||||
|
parseNip05,
|
||||||
|
};
|
||||||
|
|
||||||
export { Time } from '@/utils/time.ts';
|
export { Time } from '@/utils/time.ts';
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue