From a4819a557370c2b6c2200bfddc13102bb34d350d Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Wed, 21 Jan 2026 16:07:15 -0800 Subject: [PATCH] Feat: Add ntfy alert for upvotes --- functions/api/vote.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/functions/api/vote.js b/functions/api/vote.js index 026f29e..b2f1e07 100644 --- a/functions/api/vote.js +++ b/functions/api/vote.js @@ -3,6 +3,7 @@ const cookie=c=>(c.match(/auth_user=([^;]+)/)?.[1]||null); const hash=c=>(c.match(/auth_hash=([^;]+)/)?.[1]||null); const tsEq=(a,b)=>{if(!a||!b)return!1;let d=a.length^b.length;for(let i=0;i{const c=req.headers.get('Cookie')||'',u=cookie(c),h=hash(c);if(!u||!h)return null;const user=await db.prepare('SELECT id,username,role,pass_hash,banned_until FROM users WHERE username=?').bind(u).first();if(!user||!tsEq(user.pass_hash,h)||(user.banned_until&&new Date(user.banned_until.replace(' ','T')+'Z')>new Date()))return null;return user}; +const notify=(url,msg,prio=3)=>{if(!url)return;const target=url.startsWith('http')?url:`https://${url}`;fetch(target,{method:'POST',body:msg,headers:{'X-Priority':prio.toString()}}).catch(()=>{})}; export async function onRequest({request,env}){ if(request.method==='OPTIONS'){ @@ -46,6 +47,8 @@ export async function onRequestPost({request,env}){ await env.D1_SPCHCAP.prepare(`UPDATE ${isPost?'posts':'comments'} SET score=score+? WHERE id=?`).bind(delta,id).run(); const{score}=await env.D1_SPCHCAP.prepare(`SELECT score FROM ${isPost?'posts':'comments'} WHERE id=?`).bind(id).first(); + if(direction===1) notify(env.NTFY_URL, `Upvote: ${user.username} on ${isPost?'post':'comment'} ${id}`, 2); + return json({score,voted},{},request); }catch(e){return json({error:{message:e.message}},{status:500},request)} }