Add reactionsController

This commit is contained in:
Alex Gleason 2024-05-23 13:16:24 -05:00
parent f6973e11fa
commit 86a931d725
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 42 additions and 2 deletions

View File

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

View File

@ -2,6 +2,9 @@ import { AppController } from '@/app.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { createEvent } from '@/utils/api.ts'; import { createEvent } from '@/utils/api.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
/** /**
* React to a status. * React to a status.
@ -78,4 +81,40 @@ const deleteReactionController: AppController = async (c) => {
return c.json(status); return c.json(status);
}; };
export { deleteReactionController, reactionController }; /**
* Get an object of emoji to account mappings with accounts that reacted to the post.
* https://docs.pleroma.social/backend/development/API/pleroma_api/#get-apiv1pleromastatusesidreactions
*/
const reactionsController: AppController = async (c) => {
const id = c.req.param('id');
const store = await Storages.db();
const pubkey = await c.get('signer')?.getPublicKey();
const events = await store.query([{ kinds: [7], '#e': [id], limit: 100 }])
.then((events) => hydrateEvents({ events, store }));
/** Events grouped by emoji. */
const byEmoji = events.reduce((acc, event) => {
const emoji = event.content;
acc[emoji] = acc[emoji] || [];
acc[emoji].push(event);
return acc;
}, {} as Record<string, DittoEvent[]>);
const results = await Promise.all(
Object.entries(byEmoji).map(async ([name, events]) => {
return {
name,
count: events.length,
me: pubkey && events.some((event) => event.pubkey === pubkey),
accounts: await Promise.all(
events.map((event) => event.author ? renderAccount(event.author) : accountFromPubkey(event.pubkey)),
),
};
}),
);
return c.json(results);
};
export { deleteReactionController, reactionController, reactionsController };