guest cancel: self-service end-of-access with the ended state, and the plain OC-payment note

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.
This commit is contained in:
inference-bot committed 2026-11-08 02:28:55 +00:00
1 parent 67c10bae25
commit 19361e27e4
1 file changed
+163 -3
+163 -3
View File
@@ -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 = """<!DOCTYPE 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 = """<!DOCTYPE html>
</header>
<div class="card">
<div class="usage-header">
<div class="usage-header" id="usage-header">
<h2>Usage</h2>
<span class="mode-switch">
<button class="mode-btn active" id="mode-mine" onclick="setUsageMode('mine')">My usage</button>
@@ -602,6 +668,18 @@ PAGE_HTML = """<!DOCTYPE html>
<a class="buy-docs" href="https://docs.inference.coop/getting-started/#need-more-than-the-allowance" target="_blank" rel="noopener">More about allowance &amp; credits →</a>
</span>
</div>
<p class="oc-note" id="oc-note" style="display:none">
Your membership continues while your Open Collective contribution is active — ending the contribution ends your membership, and your allowance stops renewing after the current cycle.
<a href="@@BECOME_MEMBER_URL@@" target="_blank" rel="noopener">Manage your payment on Open Collective →</a>
</p>
<div class="cancel-row" id="cancel-row" style="display:none">
<button class="cancel-link" id="cancel-reveal" onclick="revealCancelGift()">Cancel gift access</button>
<span id="cancel-confirm" style="display:none;">
<span class="cancel-confirm-note">This ends your access now. Unspent gift credit returns to the co-op; your account and data are kept.</span>
<button class="cancel-go" id="cancel-go" onclick="doCancelGift()">Confirm — end my access</button>
<button class="cancel-keep" onclick="hideCancelGift()">Keep it</button>
</span>
</div>
<div class="models" id="models"></div>
</div>
@@ -645,7 +723,7 @@ PAGE_HTML = """<!DOCTYPE html>
<a class="docs-link" href="https://docs.inference.coop/energy/" target="_blank">How we estimate energy and carbon →</a>
</div>
<div class="card">
<div class="card" id="keys-card">
<h2>API keys</h2>
<p class="hint">Keys let you use the co-op's AI from your own tools. They draw from the same balance as chat.</p>
<div class="keys" id="keys"><div class="empty">No API keys yet.</div></div>
@@ -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 =
'<div class="ended">' +
'<h3>Your gift access has ended</h3>' +
'<p class="hint">' + line + '</p>' +
'<p class="hint">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.</p>' +
'<a class="buy-btn" href="@@BECOME_MEMBER_URL@@" target="_blank" rel="noopener">Become a member</a>' +
'</div>';
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;