Preliminary sqlite db setup

This commit is contained in:
Alex Gleason
2023-08-06 12:54:00 -05:00
parent 00fa037aaf
commit 91eac97d5c
4 changed files with 107 additions and 51 deletions

View File

@@ -7,7 +7,9 @@ import type { AppContext, AppController } from '@/app.ts';
const actorController: AppController = async (c) => {
const username = c.req.param('username');
const user = await db.users.findFirst({ where: { username } });
const user = db.getUserByUsername(username);
if (!user) return notFound(c);
const event = await getAuthor(user.pubkey);
if (!event) return notFound(c);

View File

@@ -10,25 +10,20 @@ const nameSchema = z.string().min(1).regex(/^\w+$/);
* Serves NIP-05's nostr.json.
* https://github.com/nostr-protocol/nips/blob/master/05.md
*/
const nostrController: AppController = async (c) => {
try {
const name = nameSchema.parse(c.req.query('name'));
const user = await db.users.findFirst({ where: { username: name } });
const relay = Conf.relay;
const nostrController: AppController = (c) => {
const name = nameSchema.safeParse(c.req.query('name'));
const user = name.success ? db.getUserByUsername(name.data) : null;
return c.json({
names: {
[user.username]: user.pubkey,
},
relays: relay
? {
[user.pubkey]: [relay],
}
: {},
});
} catch (_e) {
return c.json({ names: {}, relays: {} });
}
if (!user) return c.json({ names: {}, relays: {} });
return c.json({
names: {
[user.username]: user.pubkey,
},
relays: {
[user.pubkey]: [Conf.relay],
},
});
};
export { nostrController };

View File

@@ -36,26 +36,27 @@ const acctSchema = z.custom<URL>((value) => value instanceof URL)
path: ['resource', 'acct'],
});
async function handleAcct(c: AppContext, resource: URL): Promise<Response> {
try {
const [username, host] = acctSchema.parse(resource);
const user = await db.users.findFirst({ where: { username } });
const json = renderWebfinger({
pubkey: user.pubkey,
username: user.username,
subject: `acct:${username}@${host}`,
});
c.header('content-type', 'application/jrd+json');
return c.body(JSON.stringify(json));
} catch (e) {
if (e instanceof z.ZodError) {
return c.json({ error: 'Invalid acct URI', schema: e }, 400);
} else {
return c.json({ error: 'Not found' }, 404);
}
function handleAcct(c: AppContext, resource: URL): Response {
const result = acctSchema.safeParse(resource);
if (!result.success) {
return c.json({ error: 'Invalid acct URI', schema: result.error }, 400);
}
const [username, host] = result.data;
const user = db.getUserByUsername(username);
if (!user) {
return c.json({ error: 'Not found' }, 404);
}
const json = renderWebfinger({
pubkey: user.pubkey,
username: user.username,
subject: `acct:${username}@${host}`,
});
c.header('content-type', 'application/jrd+json');
return c.body(JSON.stringify(json));
}
interface RenderWebfingerOpts {