|
| 1 | +""" |
| 2 | +Feedback routes - handles user feedback and waitlist signups |
| 3 | +Posts to Discord webhook server-side to keep webhook URL secret |
| 4 | +""" |
| 5 | +import os |
| 6 | +import httpx |
| 7 | +from datetime import datetime |
| 8 | +from fastapi import APIRouter, HTTPException, Request |
| 9 | +from pydantic import BaseModel, EmailStr |
| 10 | +from typing import Optional |
| 11 | +from services.rate_limiter import rate_limit |
| 12 | + |
| 13 | +router = APIRouter(prefix="/feedback", tags=["feedback"]) |
| 14 | + |
| 15 | +DISCORD_WEBHOOK_URL = os.getenv("DISCORD_FEEDBACK_WEBHOOK") |
| 16 | + |
| 17 | +MOOD_CONFIG = { |
| 18 | + "frustrated": {"emoji": "😠", "color": 0xEF4444, "label": "Frustrated"}, |
| 19 | + "meh": {"emoji": "😐", "color": 0xEAB308, "label": "Meh"}, |
| 20 | + "good": {"emoji": "😊", "color": 0x22C55E, "label": "Good"}, |
| 21 | + "love": {"emoji": "🤩", "color": 0x8B5CF6, "label": "Love it!"}, |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +class FeedbackRequest(BaseModel): |
| 26 | + mood: str |
| 27 | + message: Optional[str] = None |
| 28 | + email: Optional[EmailStr] = None |
| 29 | + |
| 30 | + |
| 31 | +class WaitlistRequest(BaseModel): |
| 32 | + email: EmailStr |
| 33 | + plan: str # "pro" or "enterprise" |
| 34 | + |
| 35 | + |
| 36 | +async def post_to_discord(embed: dict) -> bool: |
| 37 | + """Post an embed to Discord webhook.""" |
| 38 | + if not DISCORD_WEBHOOK_URL: |
| 39 | + return False |
| 40 | + |
| 41 | + try: |
| 42 | + async with httpx.AsyncClient() as client: |
| 43 | + response = await client.post( |
| 44 | + DISCORD_WEBHOOK_URL, |
| 45 | + json={"embeds": [embed]}, |
| 46 | + timeout=10.0 |
| 47 | + ) |
| 48 | + return response.status_code == 204 |
| 49 | + except Exception: |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | +@router.post("") |
| 54 | +@rate_limit(requests_per_minute=5) |
| 55 | +async def submit_feedback(request: Request, body: FeedbackRequest): |
| 56 | + """Submit user feedback - posts to Discord.""" |
| 57 | + if not DISCORD_WEBHOOK_URL: |
| 58 | + raise HTTPException(status_code=503, detail="Feedback service unavailable") |
| 59 | + |
| 60 | + mood_info = MOOD_CONFIG.get(body.mood, MOOD_CONFIG["good"]) |
| 61 | + |
| 62 | + embed = { |
| 63 | + "title": "💬 New Feedback", |
| 64 | + "color": mood_info["color"], |
| 65 | + "fields": [ |
| 66 | + {"name": "Mood", "value": f"{mood_info['emoji']} {mood_info['label']}", "inline": True}, |
| 67 | + ], |
| 68 | + "footer": {"text": "OpenCodeIntel Feedback"}, |
| 69 | + "timestamp": datetime.utcnow().isoformat(), |
| 70 | + } |
| 71 | + |
| 72 | + if body.email: |
| 73 | + embed["fields"].append({"name": "User", "value": body.email, "inline": True}) |
| 74 | + |
| 75 | + if body.message: |
| 76 | + embed["fields"].append({"name": "Message", "value": body.message[:1000], "inline": False}) |
| 77 | + |
| 78 | + success = await post_to_discord(embed) |
| 79 | + if not success: |
| 80 | + raise HTTPException(status_code=500, detail="Failed to submit feedback") |
| 81 | + |
| 82 | + return {"success": True} |
| 83 | + |
| 84 | + |
| 85 | +@router.post("/waitlist") |
| 86 | +@rate_limit(requests_per_minute=3) |
| 87 | +async def join_waitlist(request: Request, body: WaitlistRequest): |
| 88 | + """Join waitlist for Pro or Enterprise plan.""" |
| 89 | + if not DISCORD_WEBHOOK_URL: |
| 90 | + raise HTTPException(status_code=503, detail="Waitlist service unavailable") |
| 91 | + |
| 92 | + is_enterprise = body.plan.lower() == "enterprise" |
| 93 | + |
| 94 | + if is_enterprise: |
| 95 | + embed = { |
| 96 | + "title": "🏢 Enterprise Inquiry", |
| 97 | + "color": 0x8B5CF6, |
| 98 | + "fields": [ |
| 99 | + {"name": "Email", "value": body.email, "inline": True}, |
| 100 | + {"name": "Plan", "value": "Enterprise (Custom)", "inline": True}, |
| 101 | + ], |
| 102 | + "footer": {"text": "OpenCodeIntel Enterprise"}, |
| 103 | + "timestamp": datetime.utcnow().isoformat(), |
| 104 | + } |
| 105 | + else: |
| 106 | + embed = { |
| 107 | + "title": "🚀 New Waitlist Signup", |
| 108 | + "color": 0x3B82F6, |
| 109 | + "fields": [ |
| 110 | + {"name": "Email", "value": body.email, "inline": True}, |
| 111 | + {"name": "Plan Interest", "value": "Pro ($19/month)", "inline": True}, |
| 112 | + ], |
| 113 | + "footer": {"text": "OpenCodeIntel Waitlist"}, |
| 114 | + "timestamp": datetime.utcnow().isoformat(), |
| 115 | + } |
| 116 | + |
| 117 | + success = await post_to_discord(embed) |
| 118 | + if not success: |
| 119 | + raise HTTPException(status_code=500, detail="Failed to join waitlist") |
| 120 | + |
| 121 | + return {"success": True} |
0 commit comments