From 19361e27e4bf1a7110caa52730dcfd943d3f7fb5 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Sun, 8 Nov 2026 02:28:55 +0000 Subject: [PATCH] guest cancel: self-service end-of-access with the ended state, and the plain OC-payment note MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guests get a one-click-reveal, one-click-confirm Cancel control under the Become-a-member row: it ends the gift via the portal broker (/api/cancel-gift → /broker/cancel-gift), which returns unspent credit to the co-op. An ended guest (cancelled, expired or spent out — via /api/gift-status on the usage 403, or straight from the cancel response) renders the ended state plainly: access ended, unspent credit returned where it was, account and data kept, Become-a-member still present. Members see the new plain line under the balance: membership continues while the Open Collective contribution is active — ending the contribution ends the membership, allowance stops renewing after the current cycle — with the contribute page as the place to manage the payment. Informational only. --- app/main.py | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 8176268..3fcb425 100644 --- a/app/main.py +++ b/app/main.py @@ -218,6 +218,42 @@ async def api_set_matrix_mxid(request: Request): return JSONResponse({"error": e.detail}, status_code=e.status_code) +@app.get("/api/gift-status") +async def api_gift_status(request: Request): + """What became of the caller's gift account (guest view). + + Loaded when the usage view refuses a GUEST (access ended): the portal + answers for ended gift accounts so the dashboard can say plainly that + access ended, the data is kept, and a membership is still available. + Members get guest: false — the caller falls back to the usage error. + """ + identity = get_user_identity(request) + if not identity: + return JSONResponse({"error": "unauthenticated"}, status_code=401) + try: + return await broker_get(identity, "/broker/gift-status") + except HTTPException as e: + return JSONResponse({"error": e.detail}, status_code=e.status_code) + + +@app.post("/api/cancel-gift") +async def api_cancel_gift(request: Request): + """End the caller's own gift account (self-service cancel, guest view). + + One hop to the portal broker, which holds the actual semantics: access + ends immediately, unspent credit returns to the co-op, the ledger keeps + the trail, and a repeat call is a no-op. Gift accounts only — the portal + refuses a paying member with 403. + """ + identity = get_user_identity(request) + if not identity: + return JSONResponse({"error": "unauthenticated"}, status_code=401) + try: + return await broker_post(identity, "/broker/cancel-gift") + except HTTPException as e: + return JSONResponse({"error": e.detail}, status_code=e.status_code) + + # --------------------------------------------------------------------------- # OIDC routes — authorization-code flow against Cloudron's OIDC provider. # Active only when the `oidc` addon is configured (CLOUDRON_OIDC_* present). @@ -393,6 +429,36 @@ PAGE_HTML = """ .buy-hint { color: var(--muted); font-size: 12px; } a.buy-docs { color: var(--green-deep); font-weight: 600; text-decoration: none; white-space: nowrap; } a.buy-docs:hover { text-decoration: underline; } + /* The membership-payment line: how a membership ends, in one plain + sentence under the balance. Members only (a guest has no payment). */ + .oc-note { color: var(--muted); font-size: 12px; margin-top: 12px; } + .oc-note a { color: var(--green-deep); font-weight: 600; text-decoration: none; } + .oc-note a:hover { text-decoration: underline; } + /* Guest self-service cancel: a quiet link that reveals the confirm step, + then ends the gift. Sits under the Become-a-member row. */ + .cancel-row { margin-top: 14px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; } + .cancel-link { + font-family: inherit; cursor: pointer; background: none; border: none; + color: var(--danger); font-size: 12px; font-weight: 600; text-decoration: underline; + padding: 0; + } + .cancel-confirm-note { color: var(--muted); font-size: 12px; } + button.cancel-go { + font-family: inherit; cursor: pointer; background: #fff; color: var(--danger); + border: 1px solid var(--danger); border-radius: 8px; font-size: 13px; + font-weight: 600; padding: 6px 14px; + } + button.cancel-go:hover { background: var(--danger); color: #fff; } + button.cancel-keep { + font-family: inherit; cursor: pointer; background: none; border: none; + color: var(--muted); font-size: 12px; font-weight: 600; text-decoration: underline; + } + /* The ended state: access over, account and data kept, membership one + contribution away. Replaces the usage numbers for an ended guest. */ + .ended { padding: 4px 0 2px; } + .ended h3 { font-size: 17px; font-weight: 700; margin-bottom: 8px; color: var(--ink); } + .ended .hint { color: var(--muted); font-size: 13px; margin-bottom: 10px; } + .ended .buy-btn { margin-top: 4px; } .models { margin-top: 16px; } .model-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; @@ -574,7 +640,7 @@ PAGE_HTML = """
-
+

Usage

@@ -602,6 +668,18 @@ PAGE_HTML = """ More about allowance & credits →
+ +
@@ -645,7 +723,7 @@ PAGE_HTML = """ How we estimate energy and carbon →
-
+

API keys

Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.

No API keys yet.
@@ -702,24 +780,106 @@ function flash(msg, ok) { function fmtMoney(n) { const v = Math.abs(Number(n)) < 0.005 ? 0 : (Number(n) || 0); return '$' + v.toFixed(2); } +// --- Guest self-service cancel (reveal → confirm → ended state) ----------- + +function revealCancelGift() { + $('cancel-reveal').style.display = 'none'; + $('cancel-confirm').style.display = ''; +} + +function hideCancelGift() { + $('cancel-confirm').style.display = 'none'; + $('cancel-reveal').style.display = ''; +} + +async function doCancelGift() { + const btn = $('cancel-go'); + btn.disabled = true; + btn.textContent = 'Ending…'; + try { + const r = await fetch('/api/cancel-gift', { method: 'POST' }); + const d = await r.json().catch(() => ({})); + if (!r.ok || d.error) { + flash(d.error || 'Cancelling failed — try again', false); + btn.disabled = false; + btn.textContent = 'Confirm — end my access'; + return; + } + renderGiftEnded({ + gift_status: d.gift_status || 'cancelled by recipient', + credits_returned: d.credits_returned || 0 + }); + } catch (e) { + flash('Cancelling failed — try again', false); + btn.disabled = false; + btn.textContent = 'Confirm — end my access'; + } +} + +// The ended state: access over, account and data kept, membership one +// contribution away. Replaces the usage card content. +function renderGiftEnded(g) { + const st = String(g.gift_status || ''); + let line; + if (st === 'cancelled by recipient') { + line = 'You cancelled your gift access.'; + if (Number(g.credits_returned) > 0) { + line += ' Unspent credit of ' + fmtMoney(g.credits_returned) + ' was returned to the co-op.'; + } + } else if (st === 'expired') { + line = 'The gift ran out of time.'; + } else if (st === 'depleted') { + line = 'The gift credit was spent.'; + } else { + line = 'Your gift access has ended.'; + } + $('usage-header').style.display = 'none'; + $('view-coop').style.display = 'none'; + $('cancel-row').style.display = 'none'; + $('view-mine').innerHTML = + '
' + + '

Your gift access has ended

' + + '

' + line + '

' + + '

Your account and data are kept. Become a member anytime with the same login — your account resumes as a full membership, and a member can re-invite you.

' + + 'Become a member' + + '
'; + const kc = document.getElementById('keys-card'); + if (kc) kc.style.display = 'none'; +} + async function load() { try { const r = await fetch('/api/usage'); if (r.status === 401) { location.reload(); return; } const d = await r.json(); - if (d.error) { flash(d.error, false); return; } + if (d.error) { + // A 403 can be an ENDED GUEST (cancelled, expired or spent out): the + // portal answers /api/gift-status for those, so the dashboard says + // plainly that access ended instead of flashing an error. + if (r.status === 403) { + try { + const gs = await fetch('/api/gift-status'); + const g = await gs.json(); + if (g && g.guest && g.ended) { renderGiftEnded(g); return; } + } catch (e) { /* fall through to the error flash */ } + } + flash(d.error, false); + return; + } // Gift accounts (portal flag): no Matrix section — the card is hidden in // the markup and only shown to members — and a Become-a-member button in // place of the credit-pack buy path. Everything else stays. const isGuest = !!d.guest; $('matrix-card').style.display = isGuest ? 'none' : ''; + $('oc-note').style.display = isGuest ? 'none' : ''; if (isGuest) { const b = $('buy-btn'); b.href = '@@BECOME_MEMBER_URL@@'; b.textContent = 'Become a member'; $('buy-credits').textContent = 'Your gift credit never expires · members get a monthly allowance'; $('meta').textContent = 'A gift from a member — $5 of AI usage across chat and the API.'; + $('cancel-row').style.display = ''; } const balance = d.balance ?? 0;