Add preliminary nameRequestsController

This commit is contained in:
Alex Gleason
2024-06-08 22:16:34 -05:00
parent a14515bbe0
commit bd6424acf5
6 changed files with 89 additions and 19 deletions

View File

@@ -3,9 +3,11 @@ import { z } from 'zod';
import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { Storages } from '@/storages.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { Storages } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { createEvent } from '@/utils/api.ts';
import { renderNameRequest } from '@/views/ditto.ts';
const markerSchema = z.enum(['read', 'write']);
@@ -60,15 +62,15 @@ function renderRelays(event: NostrEvent): RelayEntity[] {
}, [] as RelayEntity[]);
}
const inviteRequestSchema = z.object({
const nameRequestSchema = z.object({
nip05: z.string().email(),
reason: z.string().max(500).optional(),
});
export const inviteRequestController: AppController = async (c) => {
const { nip05, reason } = inviteRequestSchema.parse(await c.req.json());
export const nameRequestController: AppController = async (c) => {
const { nip05, reason } = nameRequestSchema.parse(await c.req.json());
await createEvent({
const event = await createEvent({
kind: 3036,
content: reason,
tags: [
@@ -79,5 +81,20 @@ export const inviteRequestController: AppController = async (c) => {
],
}, c);
return new Response(null, { status: 204 });
await hydrateEvents({ events: [event], store: await Storages.db() });
const nameRequest = await renderNameRequest(event);
return c.json(nameRequest);
};
export const nameRequestsController: AppController = async (c) => {
const store = await Storages.db();
const signer = c.get('signer')!;
const pubkey = await signer.getPublicKey();
const events = await store.query([{ kinds: [3036], authors: [pubkey], limit: 20 }])
.then((events) => hydrateEvents({ events, store }));
const nameRequests = await Promise.all(events.map(renderNameRequest));
return c.json(nameRequests);
};