Support favourites

This commit is contained in:
Alex Gleason 2023-05-07 12:32:24 -05:00
parent 6e09e6f9e5
commit 4993b4ca52
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 42 additions and 5 deletions

View File

@ -13,7 +13,12 @@ import { emptyArrayController, emptyObjectController } from './controllers/api/f
import { homeController } from './controllers/api/timelines.ts';
import instanceController from './controllers/api/instance.ts';
import { createTokenController, oauthAuthorizeController, oauthController } from './controllers/api/oauth.ts';
import { contextController, createStatusController, statusController } from './controllers/api/statuses.ts';
import {
contextController,
createStatusController,
favouriteController,
statusController,
} from './controllers/api/statuses.ts';
import { requireAuth, setAuth } from './middleware/auth.ts';
import { indexController } from './controllers/site.ts';
@ -51,6 +56,7 @@ app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}', accountController);
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/context', contextController);
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}', statusController);
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/favourite', favouriteController);
app.post('/api/v1/statuses', requireAuth, createStatusController);
app.get('/api/v1/timelines/home', requireAuth, homeController);

View File

@ -1,6 +1,6 @@
import { type AppContext, AppController } from '@/app.ts';
import { getAncestors, getDescendants, getEvent } from '@/client.ts';
import { validator, z } from '@/deps.ts';
import { Kind, validator, z } from '@/deps.ts';
import { type Event } from '@/event.ts';
import publish from '@/publisher.ts';
import { signEvent } from '@/sign.ts';
@ -27,8 +27,8 @@ const createStatusController = validator('json', async (value, c: AppContext) =>
if (result.success) {
const { data } = result;
const event = await signEvent<1>({
kind: 1,
const event = await signEvent({
kind: Kind.Text,
content: data.status,
tags: [],
created_at: Math.floor(new Date().getTime() / 1000),
@ -60,4 +60,34 @@ const contextController: AppController = async (c) => {
return c.json({ error: 'Event not found.' }, 404);
};
export { contextController, createStatusController, statusController };
const favouriteController: AppController = async (c) => {
const id = c.req.param('id');
const target = await getEvent(id, 1);
if (target) {
const event = await signEvent({
kind: Kind.Reaction,
content: '+',
tags: [
['e', target.id],
['p', target.pubkey],
],
created_at: Math.floor(new Date().getTime() / 1000),
}, c);
publish(event);
const status = await toStatus(target);
if (status) {
status.favourited = true;
status.favourites_count++;
}
return c.json(status);
} else {
return c.json({ error: 'Event not found.' }, 404);
}
};
export { contextController, createStatusController, favouriteController, statusController };

View File

@ -14,6 +14,7 @@ export {
type Filter,
getEventHash,
getPublicKey,
Kind,
matchFilter,
nip05,
nip19,