diff --git a/src/api/statuses.ts b/src/api/statuses.ts index 7ab44b7e..d472fd31 100644 --- a/src/api/statuses.ts +++ b/src/api/statuses.ts @@ -1,6 +1,6 @@ import { type AppContext } from '@/app.ts'; import { validator, z } from '@/deps.ts'; -import { type Event } from '@/nostr/event.ts'; +import { type Event } from '@/event.ts'; import publish from '../publisher.ts'; import { toStatus } from '../transmute.ts'; diff --git a/src/client.ts b/src/client.ts index eb5b1785..22081a7b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,5 +1,5 @@ import { Author, RelayPool } from '@/deps.ts'; -import { type Event, type SignedEvent } from '@/nostr/event.ts'; +import { type Event, type SignedEvent } from '@/event.ts'; import { poolRelays } from './config.ts'; diff --git a/src/nostr/event.ts b/src/event.ts similarity index 100% rename from src/nostr/event.ts rename to src/event.ts diff --git a/src/handler.ts b/src/handler.ts index 83221931..0cb9631d 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -1,5 +1,5 @@ import { gossipDB } from '@/db.ts'; -import { type Event } from '@/nostr/event.ts'; +import { type Event } from '@/event.ts'; import { getAuthorRelays } from './gossip.ts'; diff --git a/src/nostr/events/kind-0.ts b/src/nostr/events/kind-0.ts deleted file mode 100644 index 2d68abd4..00000000 --- a/src/nostr/events/kind-0.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { z } from '@/deps.ts'; - -import { type Event } from '../event.ts'; - -const optionalString = z.string().optional().catch(undefined); - -const metaContentSchema = z.object({ - name: optionalString, - about: optionalString, - picture: optionalString, - banner: optionalString, - nip05: optionalString, - lud16: optionalString, -}); - -/** Author metadata from Event<0>. */ -type MetaContent = z.infer; - -/** - * Get (and validate) data from a kind 0 event. - * https://github.com/nostr-protocol/nips/blob/master/01.md - */ -function parseContent(event: Event<0>): MetaContent { - try { - const json = JSON.parse(event.content); - return metaContentSchema.parse(json); - } catch (_e) { - return {}; - } -} - -export { type MetaContent, metaContentSchema, parseContent }; diff --git a/src/publisher.ts b/src/publisher.ts index eaad62e9..f9372247 100644 --- a/src/publisher.ts +++ b/src/publisher.ts @@ -1,5 +1,5 @@ import { getEventHash, getSignature } from '@/deps.ts'; -import { type Event } from '@/nostr/event.ts'; +import { type Event } from '@/event.ts'; import { pool } from './client.ts'; import { publishRelays } from './config.ts'; diff --git a/src/schema.ts b/src/schema.ts index c61c4372..e186a945 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1,5 +1,9 @@ import { z } from '@/deps.ts'; +import type { Event } from './event.ts'; + +const optionalString = z.string().optional().catch(undefined); + const jsonSchema = z.string().transform((value, ctx) => { try { return JSON.parse(value); @@ -9,6 +13,33 @@ const jsonSchema = z.string().transform((value, ctx) => { } }); +const metaContentSchema = z.object({ + name: optionalString, + about: optionalString, + picture: optionalString, + banner: optionalString, + nip05: optionalString, + lud16: optionalString, +}); + +/** Author metadata from Event<0>. */ +type MetaContent = z.infer; + +/** + * Get (and validate) data from a kind 0 event. + * https://github.com/nostr-protocol/nips/blob/master/01.md + */ +function parseContent(event: Event<0>): MetaContent { + try { + const json = JSON.parse(event.content); + return metaContentSchema.parse(json); + } catch (_e) { + return {}; + } +} + +export { type MetaContent, metaContentSchema, parseContent }; + /** Alias for `safeParse`, but instead of returning a success object it returns the value (or undefined on fail). */ function parseValue(schema: z.ZodType, value: unknown): T | undefined { const result = schema.safeParse(value); diff --git a/src/transmute.ts b/src/transmute.ts index 7c23437e..d3c5c95e 100644 --- a/src/transmute.ts +++ b/src/transmute.ts @@ -1,6 +1,6 @@ import { nip19 } from '@/deps.ts'; -import { type Event } from '@/nostr/event.ts'; -import { type MetaContent, parseContent } from '@/nostr/events/kind-0.ts'; +import { type Event } from '@/event.ts'; +import { type MetaContent, parseContent } from '@/schema.ts'; import { LOCAL_DOMAIN } from './config.ts'; import { fetchUser } from './client.ts'; diff --git a/src/utils.ts b/src/utils.ts index 3d1aa9dd..00a7ab23 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import { Context, getPublicKey } from '@/deps.ts'; -import { type Event } from '@/nostr/event.ts'; +import { type Event } from '@/event.ts'; /** Get the current time in Nostr format. */ const nostrNow = () => Math.floor(new Date().getTime() / 1000);