nostrController: serve names from labels
This commit is contained in:
parent
1e73f55c8c
commit
8baa9a16db
|
@ -1,8 +1,7 @@
|
||||||
|
import { AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { findUser } from '@/db/users.ts';
|
|
||||||
import { z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
|
import { eventsDB } from '@/storages.ts';
|
||||||
import type { AppController } from '@/app.ts';
|
|
||||||
|
|
||||||
const nameSchema = z.string().min(1).regex(/^\w+$/);
|
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
|
* https://github.com/nostr-protocol/nips/blob/master/05.md
|
||||||
*/
|
*/
|
||||||
const nostrController: AppController = async (c) => {
|
const nostrController: AppController = async (c) => {
|
||||||
const name = nameSchema.safeParse(c.req.query('name'));
|
const result = nameSchema.safeParse(c.req.query('name'));
|
||||||
const user = name.success ? await findUser({ username: name.data }) : null;
|
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({
|
return c.json({
|
||||||
names: {
|
names: {
|
||||||
[user.username]: user.pubkey,
|
[name]: pubkey,
|
||||||
},
|
},
|
||||||
relays: {
|
relays: {
|
||||||
[user.pubkey]: [Conf.relay],
|
[pubkey]: [Conf.relay],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue