From c796a544ab90f6bf24cb6a5fcaf4eca4d5a8fd98 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Sat, 29 Mar 2025 15:18:38 +0000 Subject: [PATCH] Add functionality to delete comment from notification --- app/actions/comments.ts | 12 ++++++++++-- app/api/comments/moderate/[id]/route.ts | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/actions/comments.ts b/app/actions/comments.ts index bb893d2..7180af7 100644 --- a/app/actions/comments.ts +++ b/app/actions/comments.ts @@ -21,8 +21,16 @@ const notificationBody = (comment: { id: number, content: string }, user: { name method: 'PATCH', headers: { Authorization: `Bearer ${process.env.NTFY_TOKEN}` - }, - clear: true + } + }, + { + action: 'http', + label: 'Delete comment', + url: `${process.env.NEXT_PUBLIC_SITE_URL}/api/comments/moderate/${comment.id}`, + method: 'DELETE', + headers: { + Authorization: `Bearer ${process.env.NTFY_TOKEN}` + } } ] } diff --git a/app/api/comments/moderate/[id]/route.ts b/app/api/comments/moderate/[id]/route.ts index 955126d..e0c724c 100644 --- a/app/api/comments/moderate/[id]/route.ts +++ b/app/api/comments/moderate/[id]/route.ts @@ -14,7 +14,28 @@ export async function PATCH(request: Request, {params}: { params: Promise<{ id: .update({published: true}) .eq('id', id) - return new Response(JSON.stringify({}), {status: 200}) + return new Response(null, {status: 204}) + } + return new Response(JSON.stringify({status: 'Not Found'}), {status: 404}) + } + + return new Response(JSON.stringify({status: 'Unauthorized'}), {status: 401}) +} + +export async function DELETE(request: Request, {params}: { params: Promise<{ id: number }> }) { + const {id} = await params + const headersList = await headers() + const authorizationHeader = headersList.get('authorization') + + if (authorizationHeader === `Bearer ${process.env.NTFY_TOKEN}`) { + if (typeof id !== 'undefined') { + const supabase = await createClient() + + await supabase.from('comments') + .delete() + .eq('id', id) + + return new Response(null, {status: 204}) } return new Response(JSON.stringify({status: 'Not Found'}), {status: 404}) }