Add GET /api/v1/statuses/:id

This commit is contained in:
Alex Gleason 2023-04-29 17:26:56 -05:00
parent f87a17e609
commit d84e593e53
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import { type AppContext } from '@/app.ts';
import { type AppContext, AppController } from '@/app.ts';
import { validator, z } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { getEvent } from '../client.ts';
import publish from '../publisher.ts';
import { toStatus } from '../transmute.ts';
@ -9,6 +10,18 @@ const createStatusSchema = z.object({
status: z.string(),
});
const statusController: AppController = async (c) => {
const id = c.req.param('id');
const event = await getEvent(id);
if (event && event.kind === 1) {
return c.json(await toStatus(event as Event<1>));
}
return c.json({ error: 'Event not found.' }, 404);
};
const createStatusController = validator('json', async (value, c: AppContext) => {
const pubkey = c.get('pubkey')!;
const seckey = c.get('seckey');
@ -33,4 +46,4 @@ const createStatusController = validator('json', async (value, c: AppContext) =>
}
});
export { createStatusController };
export { createStatusController, statusController };

View File

@ -11,7 +11,7 @@ import { emptyArrayController, emptyObjectController } from './api/fallback.ts';
import homeController from './api/home.ts';
import instanceController from './api/instance.ts';
import { createTokenController } from './api/oauth.ts';
import { createStatusController } from './api/statuses.ts';
import { createStatusController, statusController } from './api/statuses.ts';
import { requireAuth, setAuth } from './middleware/auth.ts';
interface AppEnv extends HonoEnv {
@ -42,6 +42,7 @@ app.get('/api/v1/accounts/search', accountSearchController);
app.get('/api/v1/accounts/lookup', accountLookupController);
app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}', accountController);
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}', statusController);
app.post('/api/v1/statuses', requireAuth, createStatusController);
app.get('/api/v1/timelines/home', requireAuth, homeController);