-
-
Notifications
You must be signed in to change notification settings - Fork 353
feat(ui): New user donations list in donor section! #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| // placeholders - for now anyway - when real data loads, these going to disappear | ||
| // Monthly donors appear first; within each group sorted by recency | ||
|
|
||
| const PLACEHOLDERS_DONORS = [ | ||
| /* so many to test if: | ||
| - It works | ||
| - How it behaves if there are 5< users | ||
| - If code breaks when it runs out of space (this is unlikely since there is so many lines) [auto suggested by VsCode auto completion yay] | ||
| - if monthly users are correctly placed before one-time donors | ||
| - to write useless memes | ||
| */ | ||
| { name: 'Samidy', type: 'monthly', timestamp: '2026-04-29T10:00:00Z' }, | ||
| { name: 'Binimum', type: 'monthly', timestamp: '2026-04-20T09:00:00Z' }, | ||
| { name: 'John Monochrome', type: 'once', timestamp: '2026-04-27T15:00:00Z' }, | ||
| { name: 'Chroma', type: 'monthly', timestamp: '2026-04-25T12:00:00Z' }, | ||
| { name: 'Israel', type: 'once', timestamp: '2026-04-18T08:00:00Z' }, | ||
| { name: 'Tidal', type: 'once', timestamp: '2026-04-18T08:00:00Z' }, | ||
| { name: 'Kasane Teto (i think thats how you write her name)', type: 'monthly', timestamp: '2026-04-18T08:00:00Z' }, | ||
| ]; | ||
|
|
||
| const CORS_HEADERS = { | ||
| 'Access-Control-Allow-Origin': '*', | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
|
|
||
| export async function onRequestOptions() { | ||
| return new Response(null, { status: 204, headers: CORS_HEADERS }); | ||
| } | ||
|
|
||
| export async function onRequestGet(context) { | ||
| const { env } = context; | ||
|
|
||
| let donors = PLACEHOLDERS_DONORS; | ||
|
|
||
| if (env.DONORS_KV) { | ||
| const stored = await env.DONORS_KV.get('donors').catch(() => null); | ||
| if (stored) donors = JSON.parse(stored); | ||
| } | ||
|
|
||
| donors.sort((a, b) => { | ||
| if (a.type === 'monthly' && b.type !== 'monthly') return -1; | ||
| if (a.type !== 'monthly' && b.type === 'monthly') return 1; | ||
| return new Date(b.timestamp) - new Date(a.timestamp); | ||
| }); | ||
|
|
||
| return new Response(JSON.stringify(donors), { | ||
| status: 200, | ||
| headers: { ...CORS_HEADERS, 'Cache-Control': 'public, max-age=60' }, | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||||||||||||||||||||||||||||||||||
| // Ko-fi webhook handler | by uzif (God i fucking love claude) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // how to run this stupid shit: | ||||||||||||||||||||||||||||||||||||||
| // Configure the webhook URL in your Ko-fi settings: https://ko-fi.com/manage/webhooks | ||||||||||||||||||||||||||||||||||||||
| // Set the URL to: https://monochrome.tf/api/kofi-webhook | ||||||||||||||||||||||||||||||||||||||
| // Set KOFI_VERIFICATION_TOKEN in your Cloudflare Pages environment variables | ||||||||||||||||||||||||||||||||||||||
| // Bind a KV namespace named DONORS_KV in your Cloudflare Pages settings | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const CORS_HEADERS = { | ||||||||||||||||||||||||||||||||||||||
| 'Access-Control-Allow-Origin': '*', | ||||||||||||||||||||||||||||||||||||||
| 'Content-Type': 'application/json', | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export async function onRequestOptions() { | ||||||||||||||||||||||||||||||||||||||
| return new Response(null, { status: 204, headers: CORS_HEADERS }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| export async function onRequestPost(context) { | ||||||||||||||||||||||||||||||||||||||
| const { request, env } = context; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| const formData = await request.formData(); | ||||||||||||||||||||||||||||||||||||||
| const raw = formData.get('data'); | ||||||||||||||||||||||||||||||||||||||
| if (!raw) { | ||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ error: 'Missing data' }), { | ||||||||||||||||||||||||||||||||||||||
| status: 400, | ||||||||||||||||||||||||||||||||||||||
| headers: CORS_HEADERS, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const donation = JSON.parse(raw); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (env.KOFI_VERIFICATION_TOKEN && donation.verification_token !== env.KOFI_VERIFICATION_TOKEN) { | ||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ error: 'Invalid verification token' }), { | ||||||||||||||||||||||||||||||||||||||
| status: 401, | ||||||||||||||||||||||||||||||||||||||
| headers: CORS_HEADERS, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+35
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Webhook auth is fail-open when If the env var is missing, any caller can write donor records. This should fail closed for production safety. Suggested fix+ if (!env.KOFI_VERIFICATION_TOKEN) {
+ return new Response(JSON.stringify({ error: 'Webhook verification token is not configured' }), {
+ status: 503,
+ headers: CORS_HEADERS,
+ });
+ }
- if (env.KOFI_VERIFICATION_TOKEN && donation.verification_token !== env.KOFI_VERIFICATION_TOKEN) {
+ if (donation.verification_token !== env.KOFI_VERIFICATION_TOKEN) {
return new Response(JSON.stringify({ error: 'Invalid verification token' }), {
status: 401,
headers: CORS_HEADERS,
});
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (!donation.is_public) { | ||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ ok: true, skipped: 'private' }), { | ||||||||||||||||||||||||||||||||||||||
| status: 200, | ||||||||||||||||||||||||||||||||||||||
| headers: CORS_HEADERS, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const donor = { | ||||||||||||||||||||||||||||||||||||||
| name: donation.from_name || 'Anonymous', | ||||||||||||||||||||||||||||||||||||||
| type: donation.is_subscription_payment ? 'monthly' : 'once', | ||||||||||||||||||||||||||||||||||||||
| timestamp: donation.timestamp || new Date().toISOString(), | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (!env.DONORS_KV) { | ||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ ok: true, stored: false, reason: 'KV not configured' }), { | ||||||||||||||||||||||||||||||||||||||
| status: 200, | ||||||||||||||||||||||||||||||||||||||
| headers: CORS_HEADERS, | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const existing = JSON.parse((await env.DONORS_KV.get('donors').catch(() => null)) || '[]'); | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protect against malformed KV payloads when parsing existing donors. This parse can throw and drop valid webhook events with a 500 response. Suggested fix- const existing = JSON.parse((await env.DONORS_KV.get('donors').catch(() => null)) || '[]');
+ const rawExisting = (await env.DONORS_KV.get('donors').catch(() => null)) || '[]';
+ let existing = [];
+ try {
+ const parsed = JSON.parse(rawExisting);
+ if (Array.isArray(parsed)) existing = parsed;
+ } catch {
+ existing = [];
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const idx = existing.findIndex((d) => d.name === donor.name); | ||||||||||||||||||||||||||||||||||||||
| if (idx >= 0) { | ||||||||||||||||||||||||||||||||||||||
| if (donor.type === 'monthly') existing[idx].type = 'monthly'; | ||||||||||||||||||||||||||||||||||||||
| existing[idx].timestamp = donor.timestamp; | ||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||
| existing.unshift(donor); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| //modify the values to show more or less ocntributors (0, 100) | ||||||||||||||||||||||||||||||||||||||
| await env.DONORS_KV.put('donors', JSON.stringify(existing.slice(0, 100))); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+62
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. KV read-modify-write can lose updates under concurrent webhook deliveries. Two near-simultaneous requests can read the same old list and overwrite each other. This is a data-loss risk for donor history. Consider moving this path to a single-writer primitive (e.g., Durable Object) or a transactional store so updates are serialized. 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ ok: true }), { status: 200, headers: CORS_HEADERS }); | ||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||
| return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: CORS_HEADERS }); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -105,6 +105,38 @@ async function loadDownloadsModule() { | |||||
| return downloadsModule; | ||||||
| } | ||||||
|
|
||||||
| async function fetchDonors() { | ||||||
| try { | ||||||
| const response = await fetch('/api/donors'); | ||||||
| if (!response.ok) return; | ||||||
| const donors = await response.json(); | ||||||
| if (!Array.isArray(donors)) return; | ||||||
|
|
||||||
| const con = document.querySelector('.donate-donors-list'); | ||||||
| if (!con) return; | ||||||
|
|
||||||
| donors.forEach((donor) => { | ||||||
| const div = document.createElement('div'); | ||||||
| const icon = donor.type === 'monthly' ? '♥' : '☕'; | ||||||
| const label = donor.type === 'monthly' ? 'Monthly Supporter' : 'One-time Donor'; | ||||||
| div.innerHTML = ` | ||||||
| <a href="https://ko-fi.com/monochrometf" target="_blank" rel="noopener noreferrer"> | ||||||
| <span class="donor-icon">${icon}</span> | ||||||
| <span>${donor.name}</span> | ||||||
| <span class="contrib">${label}</span> | ||||||
|
Comment on lines
+122
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Untrusted donor name is rendered via
Suggested fix- donors.forEach((donor) => {
- const div = document.createElement('div');
- const icon = donor.type === 'monthly' ? '♥' : '☕';
- const label = donor.type === 'monthly' ? 'Monthly Supporter' : 'One-time Donor';
- div.innerHTML = `
- <a href="https://ko-fi.com/monochrometf" target="_blank" rel="noopener noreferrer">
- <span class="donor-icon">${icon}</span>
- <span>${donor.name}</span>
- <span class="contrib">${label}</span>
- </a>
- `;
- con.appendChild(div);
- });
+ donors.forEach((donor) => {
+ const div = document.createElement('div');
+ const icon = donor.type === 'monthly' ? '♥' : '☕';
+ const label = donor.type === 'monthly' ? 'Monthly Supporter' : 'One-time Donor';
+
+ const a = document.createElement('a');
+ a.href = 'https://ko-fi.com/monochrometf';
+ a.target = '_blank';
+ a.rel = 'noopener noreferrer';
+
+ const iconSpan = document.createElement('span');
+ iconSpan.className = 'donor-icon';
+ iconSpan.textContent = icon;
+
+ const nameSpan = document.createElement('span');
+ nameSpan.textContent = donor.name || 'Anonymous';
+
+ const labelSpan = document.createElement('span');
+ labelSpan.className = 'contrib';
+ labelSpan.textContent = label;
+
+ a.append(iconSpan, nameSpan, labelSpan);
+ div.appendChild(a);
+ con.appendChild(div);
+ });🤖 Prompt for AI Agents |
||||||
| </a> | ||||||
| `; | ||||||
| con.appendChild(div); | ||||||
| }); | ||||||
| } catch (e) { | ||||||
| const con = document.querySelector('.donate-donors-failed'); | ||||||
| if (!con) return; | ||||||
| const div = document.createElement('div'); | ||||||
| div.innerHTML = `<h4 style="text-align: center; color: var(--muted-foreground);">Failed to Fetch Donors List</h4>`; | ||||||
| con.appendChild(div); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| async function fetchcontributors() { | ||||||
| try { | ||||||
| const response = await fetch('https://api.samidy.com/api/contributors'); | ||||||
|
|
@@ -482,6 +514,7 @@ document.addEventListener('DOMContentLoaded', async () => { | |||||
| initTracker().catch(console.error); | ||||||
|
|
||||||
| await fetchcontributors(); | ||||||
| fetchDonors(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle the This line currently triggers Suggested fix- fetchDonors();
+ void fetchDonors();📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: Lint Codebase[error] 517-517: ESLint: Promises must be awaited / handled with .catch or .then with rejection handler (or marked with void) ( 🤖 Prompt for AI Agents |
||||||
| const castBtn = document.getElementById('cast-btn'); | ||||||
| initializeCasting(audioPlayer, castBtn); | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard KV JSON parsing to prevent endpoint-wide failure on malformed data.
JSON.parse(stored)can throw and currently bubbles out ofonRequestGet, which turns donor list fetches into 500s.Suggested fix
🤖 Prompt for AI Agents