nostrController: serve names from labels

This commit is contained in:
Alex Gleason 2024-03-27 15:10:13 -05:00
parent 1e73f55c8c
commit 8baa9a16db
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 22 additions and 8 deletions

View File

@ -1,8 +1,7 @@
import { AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { findUser } from '@/db/users.ts';
import { z } from '@/deps.ts';
import type { AppController } from '@/app.ts';
import { eventsDB } from '@/storages.ts';
const nameSchema = z.string().min(1).regex(/^\w+$/);
@ -11,17 +10,32 @@ const nameSchema = z.string().min(1).regex(/^\w+$/);
* https://github.com/nostr-protocol/nips/blob/master/05.md
*/
const nostrController: AppController = async (c) => {
const name = nameSchema.safeParse(c.req.query('name'));
const user = name.success ? await findUser({ username: name.data }) : null;
const result = nameSchema.safeParse(c.req.query('name'));
const name = result.success ? result.data : undefined;
if (!user) return c.json({ names: {}, relays: {} });
if (!name) {
return c.json({ names: {}, relays: {} });
}
const [label] = await eventsDB.query([{
kinds: [1985],
authors: [Conf.pubkey],
'#L': ['nip05'],
'#l': [`${name}@${Conf.url.host}`],
}]);
const pubkey = label?.tags.find(([name]) => name === 'p')?.[1];
if (!label || !pubkey) {
return c.json({ names: {}, relays: {} });
}
return c.json({
names: {
[user.username]: user.pubkey,
[name]: pubkey,
},
relays: {
[user.pubkey]: [Conf.relay],
[pubkey]: [Conf.relay],
},
});
};