""" The public side of unsubscribing: the page an email's Unsubscribe link opens, and the one-click POST that mail apps send on the reader's behalf. Nothing here needs a sign-in; the signed link is the proof. The page asks for a click (GET never unsubscribes) so link scanners that open every link in a message do not remove people by accident. """ from __future__ import annotations import html from fastapi import APIRouter from fastapi.responses import HTMLResponse from backend import unsubscribe router = APIRouter() @router.get("/u/{token}", response_class=HTMLResponse) def unsubscribe_page(token: str): try: _, email = unsubscribe.parse_token(token) except ValueError as exc: return HTMLResponse(unsubscribe.page("Link not valid", f"
{html.escape(str(exc))}.
"), status_code=400) return unsubscribe.page( "Unsubscribe", f"Stop emails to {html.escape(email)}?
" f'', ) @router.post("/u/{token}", response_class=HTMLResponse) def unsubscribe_now(token: str): try: email = unsubscribe.unsubscribe_with_token(token) except ValueError as exc: return HTMLResponse(unsubscribe.page("Link not valid", f"{html.escape(str(exc))}.
"), status_code=400) return unsubscribe.page("You are unsubscribed", f"{html.escape(email)} will not get more emails from this sender.
")