Add delete reaction controller

This commit is contained in:
Alex Gleason 2024-05-22 18:33:00 -05:00
parent 93922b3f93
commit b0a53f4789
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 49 additions and 2 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

@ -25,7 +25,7 @@ const reactionController: AppController = async (c) => {
await createEvent({
kind: 7,
content: '',
content: emoji,
created_at: Math.floor(Date.now() / 1000),
tags: [['e', id]],
}, c);
@ -35,4 +35,47 @@ const reactionController: AppController = async (c) => {
return c.json(status);
};
export { reactionController };
/**
* 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 };