From 4f0e84e9aaa87943c86f7e729ef797f1158d1f3c Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Thu, 2 Oct 2025 15:02:32 -0700 Subject: [PATCH] Feat: Add Gemini moderation for new posts --- functions/api/posts.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/api/posts.js b/functions/api/posts.js index 565213c..c26b2cb 100644 --- a/functions/api/posts.js +++ b/functions/api/posts.js @@ -46,6 +46,10 @@ export async function onRequestPost({request,env}){ const{sub,title,link,content}=body; if(!sub||!title)return json({error:'Missing fields'},{status:400},request); + const mod=await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=${env.GOOGLE_KEY}`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({contents:[{parts:[{text:`Is this appropriate for a public forum? Respond ONLY "yes" or "no".\n\n${title}\n\n${content||''}`}]}]})}); + if(!mod.ok){const err=await mod.text();return json({error:{message:`Moderation failed: ${err}`}},{status:500},request)} + if((await mod.json()).candidates?.[0]?.content.parts[0].text.trim().toLowerCase()!=='yes')return json({error:{message:'Post rejected by Gemini 2.5 Flash Lite.'}},{status:400},request); + let sub_row=await env.D1_SPCHCAP.prepare('SELECT id FROM subs WHERE name=?').bind(sub).first(); if(!sub_row){ await env.D1_SPCHCAP.prepare('INSERT INTO subs(name)VALUES(?)').bind(sub).run();