|
import logging
|
|
from fastapi import APIRouter, Request
|
|
from fastapi.responses import HTMLResponse
|
|
from devplacepy.database import get_table, get_daily_topic, get_users_by_uids, get_comment_counts_by_post_uids, get_recent_comments_by_post_uids, get_site_stats, get_top_authors, paginate
|
|
from devplacepy.attachments import get_attachments_batch
|
|
from devplacepy.content import enrich_items
|
|
from devplacepy.templating import templates
|
|
from devplacepy.utils import get_current_user
|
|
from devplacepy.seo import list_page_seo
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
|
|
def get_feed_posts(user, tab: str = "all", topic: str = None, before: str = None):
|
|
posts_table = get_table("posts")
|
|
order = ["-stars", "-created_at"] if tab == "trending" else ["-created_at"]
|
|
|
|
if tab == "following":
|
|
if not user:
|
|
return [], None
|
|
follows = get_table("follows")
|
|
following = [f["following_uid"] for f in follows.find(follower_uid=user["uid"])]
|
|
if not following:
|
|
return [], None
|
|
posts, next_cursor = paginate(posts_table, posts_table.table.columns.user_uid.in_(following), before=before, order=order)
|
|
else:
|
|
filters = {"topic": topic} if topic else {}
|
|
posts, next_cursor = paginate(posts_table, before=before, order=order, **filters)
|
|
|
|
if not posts:
|
|
return [], next_cursor
|
|
|
|
authors = get_users_by_uids([p["user_uid"] for p in posts])
|
|
counts = get_comment_counts_by_post_uids([p["uid"] for p in posts])
|
|
|
|
result = enrich_items(posts, "post", authors, {"comment_count": counts}, user=user)
|
|
return result, next_cursor
|
|
|
|
|
|
@router.get("", response_class=HTMLResponse)
|
|
async def feed_page(request: Request, tab: str = "all", topic: str = None, before: str = None):
|
|
user = get_current_user(request)
|
|
posts, next_cursor = get_feed_posts(user, tab, topic, before)
|
|
stats = get_site_stats()
|
|
top_authors = get_top_authors(5)
|
|
daily_topic = get_daily_topic()
|
|
|
|
post_uids_list = [item["post"]["uid"] for item in posts]
|
|
attachments_map = get_attachments_batch("post", post_uids_list)
|
|
recent_comments = get_recent_comments_by_post_uids(post_uids_list, 3, user)
|
|
for item in posts:
|
|
uid = item["post"]["uid"]
|
|
item["attachments"] = attachments_map.get(uid, [])
|
|
item["recent_comments"] = recent_comments.get(uid, [])
|
|
|
|
seo_ctx = list_page_seo(
|
|
request,
|
|
title="Feed",
|
|
description="Discover the latest developer discussions, projects, and community activity on DevPlace.",
|
|
breadcrumbs=[{"name": "Home", "url": "/feed"}, {"name": "Feed", "url": "/feed"}],
|
|
)
|
|
return templates.TemplateResponse(request, "feed.html", {
|
|
**seo_ctx,
|
|
"request": request,
|
|
"user": user,
|
|
"posts": posts,
|
|
"current_tab": tab,
|
|
"current_topic": topic,
|
|
"total_members": stats["total_members"],
|
|
"posts_today": stats["posts_today"],
|
|
"total_projects": stats["total_projects"],
|
|
"total_gists": stats["total_gists"],
|
|
"top_authors": top_authors,
|
|
"daily_topic": daily_topic,
|
|
"next_cursor": next_cursor,
|
|
})
|