We do a little refactoring
This commit is contained in:
parent
c26a424430
commit
53655f99bf
|
@ -1,11 +1,10 @@
|
||||||
import { validator, z } from '@/deps.ts';
|
import { validator, z } from '@/deps.ts';
|
||||||
|
import { type Event } from '@/nostr/event.ts';
|
||||||
|
|
||||||
import publish from '../publisher.ts';
|
import publish from '../publisher.ts';
|
||||||
import { toStatus } from '../transmute.ts';
|
import { toStatus } from '../transmute.ts';
|
||||||
import { getKeys } from '../utils.ts';
|
import { getKeys } from '../utils.ts';
|
||||||
|
|
||||||
import type { Event } from '../event.ts';
|
|
||||||
|
|
||||||
const createStatusSchema = z.object({
|
const createStatusSchema = z.object({
|
||||||
status: z.string(),
|
status: z.string(),
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { Author, RelayPool } from '@/deps.ts';
|
import { Author, RelayPool } from '@/deps.ts';
|
||||||
|
import { type Event, type SignedEvent } from '@/nostr/event.ts';
|
||||||
|
|
||||||
import { poolRelays } from './config.ts';
|
import { poolRelays } from './config.ts';
|
||||||
|
|
||||||
import type { Event, SignedEvent } from './event.ts';
|
|
||||||
import { eventDateComparator, nostrNow } from './utils.ts';
|
import { eventDateComparator, nostrNow } from './utils.ts';
|
||||||
|
|
||||||
const pool = new RelayPool(poolRelays);
|
const pool = new RelayPool(poolRelays);
|
||||||
|
|
|
@ -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. */
|
/** Gets relays which pertain to the author from the event. */
|
||||||
function getAuthorRelays(event: Event): URL[] {
|
function getAuthorRelays(event: Event): URL[] {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { gossipDB } from '@/db.ts';
|
import { gossipDB } from '@/db.ts';
|
||||||
|
import { type Event } from '@/nostr/event.ts';
|
||||||
|
|
||||||
import { getAuthorRelays } from './gossip.ts';
|
import { getAuthorRelays } from './gossip.ts';
|
||||||
|
|
||||||
import type { Event } from './event.ts';
|
|
||||||
|
|
||||||
function handleEvent(event: Event): void {
|
function handleEvent(event: Event): void {
|
||||||
handleRelays(event);
|
handleRelays(event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 };
|
|
@ -1,10 +1,9 @@
|
||||||
import { getEventHash, signEvent } from '@/deps.ts';
|
import { getEventHash, signEvent } from '@/deps.ts';
|
||||||
|
import { type Event } from '@/nostr/event.ts';
|
||||||
|
|
||||||
import { pool } from './client.ts';
|
import { pool } from './client.ts';
|
||||||
import { publishRelays } from './config.ts';
|
import { publishRelays } from './config.ts';
|
||||||
|
|
||||||
import type { Event } from './event.ts';
|
|
||||||
|
|
||||||
/** Publish an event to the Nostr relay. */
|
/** Publish an event to the Nostr relay. */
|
||||||
function publish(event: Event, privatekey: string, relays = publishRelays): void {
|
function publish(event: Event, privatekey: string, relays = publishRelays): void {
|
||||||
event.id = getEventHash(event);
|
event.id = getEventHash(event);
|
||||||
|
|
|
@ -9,26 +9,13 @@ const jsonSchema = z.string().transform((value, ctx) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const optionalString = z.string().optional().catch(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 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 {
|
|
||||||
const result = schema.safeParse(value);
|
const result = schema.safeParse(value);
|
||||||
return result.success ? result.data : undefined;
|
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) => {
|
const relaySchema = z.custom<URL>((relay) => {
|
||||||
if (typeof relay !== 'string') return false;
|
if (typeof relay !== 'string') return false;
|
||||||
|
@ -40,5 +27,4 @@ const relaySchema = z.custom<URL>((relay) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export { jsonSchema, metaContentSchema, parseRelay, relaySchema };
|
export { jsonSchema, parseRelay, relaySchema };
|
||||||
export type { MetaContent };
|
|
||||||
|
|
|
@ -1,19 +1,12 @@
|
||||||
import { nip19 } from '@/deps.ts';
|
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 { LOCAL_DOMAIN } from './config.ts';
|
||||||
import { fetchUser } from './client.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';
|
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>) {
|
function toAccount(event: Event<0>) {
|
||||||
const { pubkey } = event;
|
const { pubkey } = event;
|
||||||
const { name, nip05, picture, banner, about }: MetaContent = parseContent(event);
|
const { name, nip05, picture, banner, about }: MetaContent = parseContent(event);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Context, getPublicKey } from '@/deps.ts';
|
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. */
|
/** Get the current time in Nostr format. */
|
||||||
const nostrNow = () => Math.floor(new Date().getTime() / 1000);
|
const nostrNow = () => Math.floor(new Date().getTime() / 1000);
|
||||||
|
|
Loading…
Reference in New Issue