We do a little refactoring

This commit is contained in:
Alex Gleason 2023-04-10 19:34:00 -05:00
parent c26a424430
commit 53655f99bf
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
10 changed files with 45 additions and 38 deletions

View File

@ -1,11 +1,10 @@
import { validator, z } from '@/deps.ts';
import { type Event } from '@/nostr/event.ts';
import publish from '../publisher.ts';
import { toStatus } from '../transmute.ts';
import { getKeys } from '../utils.ts';
import type { Event } from '../event.ts';
const createStatusSchema = z.object({
status: z.string(),
});

View File

@ -1,8 +1,8 @@
import { Author, RelayPool } from '@/deps.ts';
import { type Event, type SignedEvent } from '@/nostr/event.ts';
import { poolRelays } from './config.ts';
import type { Event, SignedEvent } from './event.ts';
import { eventDateComparator, nostrNow } from './utils.ts';
const pool = new RelayPool(poolRelays);

View File

@ -1,6 +1,6 @@
import { parseRelay } from './schema.ts';
import { type Event } from '@/nostr/event.ts';
import type { Event } from './event.ts';
import { parseRelay } from './schema.ts';
/** Gets relays which pertain to the author from the event. */
function getAuthorRelays(event: Event): URL[] {

View File

@ -1,9 +1,8 @@
import { gossipDB } from '@/db.ts';
import { type Event } from '@/nostr/event.ts';
import { getAuthorRelays } from './gossip.ts';
import type { Event } from './event.ts';
function handleEvent(event: Event): void {
handleRelays(event);
}

View File

@ -0,0 +1,32 @@
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<typeof metaContentSchema>;
/**
* 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 };

View File

@ -1,10 +1,9 @@
import { getEventHash, signEvent } from '@/deps.ts';
import { type Event } from '@/nostr/event.ts';
import { pool } from './client.ts';
import { publishRelays } from './config.ts';
import type { Event } from './event.ts';
/** Publish an event to the Nostr relay. */
function publish(event: Event, privatekey: string, relays = publishRelays): void {
event.id = getEventHash(event);

View File

@ -9,26 +9,13 @@ const jsonSchema = z.string().transform((value, ctx) => {
}
});
const optionalString = z.string().optional().catch(undefined);
const metaContentSchema = z.object({
name: optionalString,
about: optionalString,
picture: optionalString,
banner: optionalString,
nip05: optionalString,
lud16: optionalString,
});
type MetaContent = z.infer<typeof metaContentSchema>;
/** It's like `safeParse` except it returns `T` on success and `undefined` on fail. */
function parseish<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
/** Alias for `safeParse`, but instead of returning a success object it returns the value (or undefined on fail). */
function parseValue<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
const result = schema.safeParse(value);
return result.success ? result.data : undefined;
}
const parseRelay = (relay: string | URL) => parseish(relaySchema, relay);
const parseRelay = (relay: string | URL) => parseValue(relaySchema, relay);
const relaySchema = z.custom<URL>((relay) => {
if (typeof relay !== 'string') return false;
@ -40,5 +27,4 @@ const relaySchema = z.custom<URL>((relay) => {
}
});
export { jsonSchema, metaContentSchema, parseRelay, relaySchema };
export type { MetaContent };
export { jsonSchema, parseRelay, relaySchema };

View File

@ -1,19 +1,12 @@
import { nip19 } from '@/deps.ts';
import { type Event } from '@/nostr/event.ts';
import { type MetaContent, parseContent } from '@/nostr/events/kind-0.ts';
import { LOCAL_DOMAIN } from './config.ts';
import { fetchUser } from './client.ts';
import { jsonSchema, MetaContent, metaContentSchema } from './schema.ts';
import type { Event } from './event.ts';
const DEFAULT_AVATAR = 'https://gleasonator.com/images/avi.png';
function parseContent(event: Event<0>): MetaContent {
const json = jsonSchema.parse(event.content);
const result = metaContentSchema.safeParse(json);
return result.success ? result.data : {};
}
function toAccount(event: Event<0>) {
const { pubkey } = event;
const { name, nip05, picture, banner, about }: MetaContent = parseContent(event);

View File

@ -1,6 +1,5 @@
import { Context, getPublicKey } from '@/deps.ts';
import type { Event } from './event.ts';
import { type Event } from '@/nostr/event.ts';
/** Get the current time in Nostr format. */
const nostrNow = () => Math.floor(new Date().getTime() / 1000);