From 62258f7ee5dbcc539f82e695fe2a608f1fd54bff Mon Sep 17 00:00:00 2001 From: danidfra Date: Tue, 24 Dec 2024 15:33:41 -0300 Subject: [PATCH] Create unfavouriteController --- src/controllers/api/statuses.ts | 48 ++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index ea7a16b2..7ea9fd60 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -316,15 +316,6 @@ const favouriteController: AppController = async (c) => { const target = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] }); if (target) { - await createEvent({ - kind: 7, - content: '+', - tags: [ - ['e', target.id, Conf.relay, '', target.pubkey], - ['p', target.pubkey, Conf.relay], - ], - }, c); - const status = await renderStatus(target, { viewerPubkey: await c.get('signer')?.getPublicKey() }); if (status) { @@ -338,6 +329,44 @@ const favouriteController: AppController = async (c) => { } }; +const unfavouriteController: AppController = async (c) => { + const id = c.req.param('id'); + const signer = c.get('signer')!; + const pubkey = await signer.getPublicKey(); + const store = await Storages.db(); + const { signal } = c.req.raw; + + const [event] = await store.query([{ ids: [id] }], { signal }); + if (!event) { + return c.json({ error: 'Record not found.' }, 404); + } + + const favouriteEvents = await store.query([ + { kinds: [7], authors: [pubkey], '#e': [id] }, + ]); + if (!favouriteEvents.length) { + return c.json({ error: 'Record not found.' }, 404); + } + + favouriteEvents.forEach(async (e) => { + if (e.content === '+') { + await createEvent({ + kind: 5, + tags: [ + ['e', e.id], + ], + content: 'unfavourite', + }, c); + } + }) + + await hydrateEvents({ events: [event], store, signal }) + + const status = await renderStatus(event, { viewerPubkey: pubkey }); + + return c.json(status); +}; + const favouritedByController: AppController = (c) => { const id = c.req.param('id'); const params = c.get('pagination'); @@ -660,6 +689,7 @@ export { reblogStatusController, statusController, unbookmarkController, + unfavouriteController, unpinController, unreblogStatusController, zapController,