From 6341987088c0d9637cc4f7f354fc28b42c9bc219 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 29 Apr 2023 15:49:22 -0500 Subject: [PATCH] Delete src/nostr directory --- src/api/statuses.ts | 2 +- src/client.ts | 2 +- src/{nostr => }/event.ts | 0 src/handler.ts | 2 +- src/nostr/events/kind-0.ts | 32 -------------------------------- src/publisher.ts | 2 +- src/schema.ts | 31 +++++++++++++++++++++++++++++++ src/transmute.ts | 4 ++-- src/utils.ts | 2 +- 9 files changed, 38 insertions(+), 39 deletions(-) rename src/{nostr => }/event.ts (100%) delete mode 100644 src/nostr/events/kind-0.ts diff --git a/src/api/statuses.ts b/src/api/statuses.ts index 7ab44b7..d472fd3 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 eb5b178..22081a7 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 8322193..0cb9631 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 2d68abd..0000000 --- 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 eaad62e..f937224 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 c61c437..e186a94 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 7c23437..d3c5c95 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 3d1aa9d..00a7ab2 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);