Download app/forums/router.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 5.81 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/app/forums/router.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/app/forums/router.py
-
curl -L -o router.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/app/forums/router.py
5.81 kB
| """ | |
| ALLI Community Forums Domain - FastAPI Router Endpoints | |
| """ | |
| import os | |
| from typing import Optional, List | |
| from fastapi import APIRouter, Request, HTTPException, Query, status | |
| from .schemas import ( | |
| TopicCreateRequest, | |
| TopicUpdateRequest, | |
| TopicResponse, | |
| ReplyCreateRequest, | |
| ReplyUpdateRequest, | |
| ReplyResponse, | |
| TopicDetailResponse, | |
| TopicListResponse, | |
| ) | |
| from .service import ForumService | |
| router = APIRouter(prefix="/forums", tags=["Community Forums"]) | |
| forum_service = ForumService() | |
| def extract_user_context(request: Request) -> tuple[str, str, Optional[str], bool]: | |
| """ | |
| Extracts author_id, author_name, avatar_url, and is_admin from request state or fallback headers. | |
| """ | |
| user = getattr(request.state, "user", None) | |
| if user and isinstance(user, dict): | |
| user_id = user.get("id") or "anonymous_user" | |
| name = user.get("email", "").split("@")[0] or user.get("user_name") or "ALLI Developer" | |
| avatar = user.get("avatar_url") | |
| is_admin = user.get("role") in ("admin", "service_role") or user_id == "service_account" | |
| return user_id, name, avatar, is_admin | |
| user_id = request.headers.get("X-User-Id", "anonymous_user") | |
| name = request.headers.get("X-User-Name", "Community Member") | |
| avatar = request.headers.get("X-User-Avatar", None) | |
| master_key = os.environ.get("MASTER_API_KEY", "") | |
| api_key = request.headers.get("X-API-Key", "") | |
| is_admin = ( | |
| request.headers.get("X-Admin-Role", "").lower() in ("true", "1") | |
| or (bool(master_key) and api_key == master_key) | |
| ) | |
| return user_id, name, avatar, is_admin | |
| async def list_topics_endpoint( | |
| category: Optional[str] = Query(None, description="Category filter (e.g., general, tutorials, showcase)"), | |
| search: Optional[str] = Query(None, description="Text search in title or content"), | |
| tag: Optional[str] = Query(None, description="Filter by tag"), | |
| sort_by: str = Query("latest", description="Sort order: latest, popular, views"), | |
| page: int = Query(1, ge=1), | |
| limit: int = Query(20, ge=1, le=50), | |
| ): | |
| """List topics with pagination and filters.""" | |
| return forum_service.list_topics( | |
| category=category, | |
| search=search, | |
| tag=tag, | |
| page=page, | |
| limit=limit, | |
| sort_by=sort_by, | |
| ) | |
| async def create_topic_endpoint( | |
| payload: TopicCreateRequest, | |
| request: Request, | |
| ): | |
| """Create a new discussion topic.""" | |
| user_id, name, avatar, _ = extract_user_context(request) | |
| return forum_service.create_topic( | |
| request=payload, | |
| author_id=user_id, | |
| author_name=name, | |
| author_avatar=avatar, | |
| ) | |
| async def get_topic_endpoint( | |
| topic_id: str, | |
| ): | |
| """Get topic details along with its threaded replies.""" | |
| return forum_service.get_topic_detail(topic_id) | |
| async def update_topic_endpoint( | |
| topic_id: str, | |
| payload: TopicUpdateRequest, | |
| request: Request, | |
| ): | |
| """Update topic content, title, tags, or moderation status.""" | |
| user_id, _, _, is_admin = extract_user_context(request) | |
| return forum_service.update_topic( | |
| topic_id=topic_id, | |
| updates=payload, | |
| user_id=user_id, | |
| is_admin=is_admin, | |
| ) | |
| async def delete_topic_endpoint( | |
| topic_id: str, | |
| request: Request, | |
| ): | |
| """Delete a topic and its associated replies.""" | |
| user_id, _, _, is_admin = extract_user_context(request) | |
| success = forum_service.delete_topic( | |
| topic_id=topic_id, | |
| user_id=user_id, | |
| is_admin=is_admin, | |
| ) | |
| if not success: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Topic could not be deleted.") | |
| return {"message": "Topic deleted successfully", "topic_id": topic_id} | |
| # ─── REPLIES ───────────────────────────────────────────────────────────────── | |
| async def create_reply_endpoint( | |
| topic_id: str, | |
| payload: ReplyCreateRequest, | |
| request: Request, | |
| ): | |
| """Post a reply to an active topic.""" | |
| user_id, name, avatar, _ = extract_user_context(request) | |
| return forum_service.create_reply( | |
| topic_id=topic_id, | |
| request=payload, | |
| author_id=user_id, | |
| author_name=name, | |
| author_avatar=avatar, | |
| ) | |
| async def update_reply_endpoint( | |
| reply_id: str, | |
| payload: ReplyUpdateRequest, | |
| request: Request, | |
| ): | |
| """Edit a previously posted reply.""" | |
| user_id, _, _, is_admin = extract_user_context(request) | |
| return forum_service.update_reply( | |
| reply_id=reply_id, | |
| request=payload, | |
| user_id=user_id, | |
| is_admin=is_admin, | |
| ) | |
| async def delete_reply_endpoint( | |
| reply_id: str, | |
| request: Request, | |
| ): | |
| """Delete a reply.""" | |
| user_id, _, _, is_admin = extract_user_context(request) | |
| success = forum_service.delete_reply( | |
| reply_id=reply_id, | |
| user_id=user_id, | |
| is_admin=is_admin, | |
| ) | |
| if not success: | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Reply could not be deleted.") | |
| return {"message": "Reply deleted successfully", "reply_id": reply_id} | |