Spaces:
Running
Running
Kethan Dosapati
Enhance static UI and authentication: improve sidebar auth visibility, add mobile responsiveness, and refine error messages; update `/report` endpoint to use `ArticleOut` model; redesign styles for a Reddit-like layout with theming and responsive tweaks.
363bda3 | """GET/POST comments for an article.""" | |
| from typing import Optional | |
| from fastapi import APIRouter, Header, HTTPException | |
| from auth import get_author_from_bearer | |
| from database import supabase | |
| from models import Comment | |
| router = APIRouter(tags=["comments"]) | |
| def list_comments(article_id: str): | |
| """List comments for an article, oldest first.""" | |
| result = ( | |
| supabase.table("comments") | |
| .select("id, article_id, body, author, created_at") | |
| .eq("article_id", article_id) | |
| .order("created_at", desc=False) | |
| .execute() | |
| ) | |
| return result.data | |
| def create_comment( | |
| article_id: str, | |
| payload: dict, | |
| authorization: Optional[str] = Header(None), | |
| ): | |
| """Add a comment. Requires an account: sign in with Hugging Face (send author in body) or with app account (Authorization: Bearer <token>).""" | |
| body = (payload.get("body") or "").strip() | |
| author = get_author_from_bearer(authorization) | |
| if not author: | |
| author = (payload.get("author") or "").strip() or None | |
| if payload.get("username"): | |
| author = author or (payload.get("username") or "").strip() | |
| if not body: | |
| raise HTTPException(status_code=400, detail="body required") | |
| if len(body) > 2000: | |
| raise HTTPException(status_code=400, detail="body max 2000 characters") | |
| if not author: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Sign in to comment. Use Hugging Face (when on HF) or create an account / sign in.", | |
| ) | |
| user_id = (payload.get("user_id") or "").strip() or None | |
| avatar_url = (payload.get("avatar_url") or "").strip() or None | |
| row = {"article_id": article_id, "body": body, "author": author} | |
| if user_id: | |
| row["user_id"] = user_id | |
| if avatar_url: | |
| row["avatar_url"] = avatar_url | |
| try: | |
| result = supabase.table("comments").insert(row).execute() | |
| if not result.data: | |
| raise HTTPException(status_code=500, detail="Failed to insert comment") | |
| return result.data[0] | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |