"""Public, signed, product-update-only opt-outs. GET never changes preferences.""" from html import escape from urllib.parse import urlencode from fastapi import APIRouter, Depends from fastapi.responses import HTMLResponse from sqlalchemy.ext.asyncio import AsyncSession from ...database import get_db from ...services.product_update_email_service import ( InvalidUnsubscribeToken, ProductUpdateEmailService, ProductUpdateEmailsNotConfigured, UNSUBSCRIBE_PATH, ) router = APIRouter(tags=["email preferences"]) HEADERS = { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer", "X-Robots-Tag": "noindex, nofollow", "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'", } def _page(title: str, body: str, status_code: int = 200) -> HTMLResponse: return HTMLResponse(f'''
Open the unsubscribe link in your latest SupoClip product email, or contact support@supoclip.com for help.
", 400) def _unavailable() -> HTMLResponse: return _page("We couldn’t update your preference", "Please try again later, or contact support@supoclip.com.
", 503) @router.get(UNSUBSCRIBE_PATH, response_class=HTMLResponse) async def confirm_unsubscribe(token: str = "", db: AsyncSession = Depends(get_db)): service = ProductUpdateEmailService(db) try: recipient = await service.validate_token(token) except InvalidUnsubscribeToken: return _invalid() except ProductUpdateEmailsNotConfigured: return _unavailable() if recipient["product_updates_unsubscribed_at"] is not None: return _success() action = escape(f"{UNSUBSCRIBE_PATH}?{urlencode({'token': token})}", quote=True) return _page("Unsubscribe from product updates?", f'''You’ll stop receiving feature announcements and other SupoClip product updates.
This won’t change your account, security, billing, or clip-ready email preferences.
''') def _success() -> HTMLResponse: return _page("You’re unsubscribed", "You won’t receive further SupoClip product updates.
Your other email preferences are unchanged.
") @router.post(UNSUBSCRIBE_PATH, response_class=HTMLResponse) async def unsubscribe(token: str = "", db: AsyncSession = Depends(get_db)): # Both the confirmation form and RFC 8058 one-click clients use POST. # No session, cookies, redirect, or extra request fields are required. try: await ProductUpdateEmailService(db).unsubscribe(token) except InvalidUnsubscribeToken: return _invalid() except ProductUpdateEmailsNotConfigured: return _unavailable() return _success()