Merge branch 'reactions' into 'main'

Add and delete reactions

See merge request soapbox-pub/ditto!301
This commit is contained in:
Alex Gleason 2024-05-23 00:57:01 +00:00
commit e06dda4014
2 changed files with 85 additions and 0 deletions

View File

@ -43,6 +43,7 @@ import {
updateConfigController,
} from '@/controllers/api/pleroma.ts';
import { preferencesController } from '@/controllers/api/preferences.ts';
import { deleteReactionController, reactionController } from '@/controllers/api/reactions.ts';
import { relayController } from '@/controllers/nostr/relay.ts';
import {
adminReportController,
@ -210,6 +211,9 @@ app.get('/api/v1/mutes', requireSigner, mutesController);
app.get('/api/v1/markers', requireProof(), markersController);
app.post('/api/v1/markers', requireProof(), updateMarkersController);
app.put('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/reactions/:emoji', requireSigner, reactionController);
app.delete('/api/v1/pleroma/statuses/:id{[0-9a-f]{64}}/reactions/:emoji', requireSigner, deleteReactionController);
app.get('/api/v1/admin/accounts', requireRole('admin'), adminAccountsController);
app.get('/api/v1/pleroma/admin/config', requireRole('admin'), configController);
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);

View File

@ -0,0 +1,81 @@
import { AppController } from '@/app.ts';
import { Storages } from '@/storages.ts';
import { createEvent } from '@/utils/api.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
/**
* React to a status.
* https://docs.pleroma.social/backend/development/API/pleroma_api/#put-apiv1pleromastatusesidreactionsemoji
*/
const reactionController: AppController = async (c) => {
const id = c.req.param('id');
const emoji = c.req.param('emoji');
const signer = c.get('signer')!;
if (!/^\p{RGI_Emoji}$/v.test(emoji)) {
return c.json({ error: 'Invalid emoji' }, 400);
}
const store = await Storages.db();
const [event] = await store.query([{ kinds: [1], ids: [id], limit: 1 }]);
if (!event) {
return c.json({ error: 'Status not found' }, 404);
}
await createEvent({
kind: 7,
content: emoji,
created_at: Math.floor(Date.now() / 1000),
tags: [['e', id]],
}, c);
const status = renderStatus(event, { viewerPubkey: await signer.getPublicKey() });
return c.json(status);
};
/**
* Delete reactions to a status.
* https://docs.pleroma.social/backend/development/API/pleroma_api/#delete-apiv1pleromastatusesidreactionsemoji
*/
const deleteReactionController: AppController = async (c) => {
const id = c.req.param('id');
const emoji = c.req.param('emoji');
const signer = c.get('signer')!;
const pubkey = await signer.getPublicKey();
const store = await Storages.db();
if (!/^\p{RGI_Emoji}$/v.test(emoji)) {
return c.json({ error: 'Invalid emoji' }, 400);
}
const [event] = await store.query([
{ kinds: [1], ids: [id], limit: 1 },
]);
if (!event) {
return c.json({ error: 'Status not found' }, 404);
}
const events = await store.query([
{ kinds: [7], authors: [pubkey], '#e': [id] },
]);
const tags = events
.filter((event) => event.content === emoji)
.map((event) => ['e', event.id]);
await createEvent({
kind: 5,
content: '',
created_at: Math.floor(Date.now() / 1000),
tags,
}, c);
const status = renderStatus(event, { viewerPubkey: pubkey });
return c.json(status);
};
export { deleteReactionController, reactionController };