Don't let your memes be dreams

This commit is contained in:
Alex Gleason
2024-05-14 18:23:41 -05:00
parent 08c9ee0670
commit 68b5887ed0
38 changed files with 260 additions and 174 deletions

View File

@@ -42,7 +42,7 @@ async function createEvent(t: EventStub, c: AppContext): Promise<NostrEvent> {
/** Filter for fetching an existing event to update. */
interface UpdateEventFilter extends NostrFilter {
kinds: [number];
limit?: 1;
limit: 1;
}
/** Fetch existing event, update it, then publish the new event. */
@@ -51,7 +51,8 @@ async function updateEvent<E extends EventStub>(
fn: (prev: NostrEvent | undefined) => E,
c: AppContext,
): Promise<NostrEvent> {
const [prev] = await Storages.db.query([filter], { limit: 1, signal: c.req.raw.signal });
const store = await Storages.db();
const [prev] = await store.query([filter], { signal: c.req.raw.signal });
return createEvent(fn(prev), c);
}
@@ -101,7 +102,8 @@ async function updateAdminEvent<E extends EventStub>(
fn: (prev: NostrEvent | undefined) => E,
c: AppContext,
): Promise<NostrEvent> {
const [prev] = await Storages.db.query([filter], { limit: 1, signal: c.req.raw.signal });
const store = await Storages.db();
const [prev] = await store.query([filter], { limit: 1, signal: c.req.raw.signal });
return createAdminEvent(fn(prev), c);
}
@@ -110,7 +112,8 @@ async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEven
debug('EVENT', event);
try {
await pipeline.handleEvent(event, c.req.raw.signal);
await Storages.client.event(event);
const client = await Storages.client();
await client.event(event);
} catch (e) {
if (e instanceof RelayError) {
throw new HTTPException(422, {

View File

@@ -1,4 +1,5 @@
import { Conf } from '@/config.ts';
import { Storages } from '@/storages.ts';
import { getInstanceMetadata } from '@/utils/instance.ts';
/** NIP-46 client-connect metadata. */
@@ -11,7 +12,7 @@ interface ConnectMetadata {
/** Get NIP-46 `nostrconnect://` URI for the Ditto server. */
export async function getClientConnectUri(signal?: AbortSignal): Promise<string> {
const uri = new URL('nostrconnect://');
const { name, tagline } = await getInstanceMetadata(signal);
const { name, tagline } = await getInstanceMetadata(await Storages.db(), signal);
const metadata: ConnectMetadata = {
name,

View File

@@ -1,8 +1,7 @@
import { NostrEvent, NostrMetadata, NSchema as n } from '@nostrify/nostrify';
import { NostrEvent, NostrMetadata, NSchema as n, NStore } from '@nostrify/nostrify';
import { Conf } from '@/config.ts';
import { serverMetaSchema } from '@/schemas/nostr.ts';
import { Storages } from '@/storages.ts';
/** Like NostrMetadata, but some fields are required and also contains some extra fields. */
export interface InstanceMetadata extends NostrMetadata {
@@ -14,8 +13,8 @@ export interface InstanceMetadata extends NostrMetadata {
}
/** Get and parse instance metadata from the kind 0 of the admin user. */
export async function getInstanceMetadata(signal?: AbortSignal): Promise<InstanceMetadata> {
const [event] = await Storages.db.query(
export async function getInstanceMetadata(store: NStore, signal?: AbortSignal): Promise<InstanceMetadata> {
const [event] = await store.query(
[{ kinds: [0], authors: [Conf.pubkey], limit: 1 }],
{ signal },
);

View File

@@ -1,4 +1,4 @@
import { NIP05 } from '@nostrify/nostrify';
import { NIP05, NStore } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug';
import { nip19 } from 'nostr-tools';
@@ -16,7 +16,8 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
const [name, domain] = key.split('@');
try {
if (domain === Conf.url.host) {
const pointer = await localNip05Lookup(name);
const store = await Storages.db();
const pointer = await localNip05Lookup(store, name);
if (pointer) {
debug(`Found: ${key} is ${pointer.pubkey}`);
return pointer;
@@ -36,8 +37,8 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
{ max: 500, ttl: Time.hours(1) },
);
async function localNip05Lookup(name: string): Promise<nip19.ProfilePointer | undefined> {
const [label] = await Storages.db.query([{
async function localNip05Lookup(store: NStore, name: string): Promise<nip19.ProfilePointer | undefined> {
const [label] = await store.query([{
kinds: [1985],
authors: [Conf.pubkey],
'#L': ['nip05'],

View File

@@ -1,10 +1,11 @@
import { Conf } from '@/config.ts';
import { Storages } from '@/storages.ts';
import { NStore } from '@nostrify/nostrify';
export async function getRelays(pubkey: string): Promise<Set<string>> {
import { Conf } from '@/config.ts';
export async function getRelays(store: NStore, pubkey: string): Promise<Set<string>> {
const relays = new Set<`wss://${string}`>();
const events = await Storages.db.query([
const events = await store.query([
{ kinds: [10002], authors: [pubkey, Conf.pubkey], limit: 2 },
]);