RADICAL
A petition you backed just hit ${milestone.toLocaleString()} signatures!
"${proposal.text}"
What happens next: ${action}
View the petition →
You're receiving this because you signed this petition on RADICAL. Unsubscribe
`
})
}).catch(e => console.error(`Email to ${email} failed:`, e));
}
} catch (error) {
console.error('sendMilestoneEmails error:', error);
}
}
async function sendWeeklyDigest(env) {
if (!env.RESEND_API_KEY) return;
try {
const weekAgo = Date.now() - 7 * 24 * 60 * 60 * 1000;
const top5 = await env.DB.prepare(`
SELECT p.id, p.text,
(SELECT COUNT(*) FROM votes WHERE proposal_id = p.id AND vote_type = 'upvote') as upvotes
FROM proposals p
WHERE (p.hidden = 0 OR p.hidden IS NULL)
ORDER BY upvotes DESC LIMIT 5
`).all();
const stats = await env.DB.prepare(`
SELECT COUNT(*) as new_votes FROM votes WHERE timestamp >= ?
`).bind(weekAgo).first();
const subscribers = await env.DB.prepare(`
SELECT DISTINCT email FROM users WHERE email IS NOT NULL AND notify_milestones = 1
`).all();
const proposalRows = top5.results.map((p, i) =>
`
RADICAL — Weekly Digest
${stats.new_votes} votes cast this week
🏆 Top Proposals This Week
→ Vote now at theradicalparty.com
You're receiving this weekly digest from RADICAL.
`;
for (const { email } of subscribers.results) {
await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: { 'Authorization': `Bearer ${env.RESEND_API_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
from: 'RADICAL