Add lazy comment fetching to watch page

- VideoComment model (video_id, author, text, likes, is_pinned, published_at)
- fetch_video_comments() in ytdlp.py: top 20 comments, no reply threads,
  sorted pinned-first then by likes
- GET /videos/by-yt/{id}/comments — returns cached comments instantly
- POST /videos/by-yt/{id}/comments/refresh — fetches from YouTube, stores, returns
- Watch page: CommentsSection shows "Load comments" button when uncached,
  renders comments with author/likes once loaded; Refresh link to re-fetch

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mattias Tall
2026-05-26 11:15:41 +02:00
parent d6dd07e0bd
commit cdf6520fd8
5 changed files with 201 additions and 0 deletions

View File

@@ -638,6 +638,72 @@ def delete_bookmark(
db.commit()
@router.get("/by-yt/{youtube_video_id}/comments")
def get_comments(
youtube_video_id: str,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
from ..models import VideoComment
video = db.query(Video).filter_by(youtube_video_id=youtube_video_id).first()
if not video:
return []
comments = (
db.query(VideoComment)
.filter_by(video_id=video.id)
.order_by(VideoComment.is_pinned.desc(), VideoComment.likes.desc())
.all()
)
return [
{
"author": c.author,
"text": c.text,
"likes": c.likes,
"is_pinned": c.is_pinned,
"published_at": c.published_at,
}
for c in comments
]
@router.post("/by-yt/{youtube_video_id}/comments/refresh")
def refresh_comments(
youtube_video_id: str,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
):
from ..models import VideoComment
video = db.query(Video).filter_by(youtube_video_id=youtube_video_id).first()
if not video:
raise HTTPException(status_code=404, detail="Video not found")
# Clear existing and re-fetch
db.query(VideoComment).filter_by(video_id=video.id).delete()
db.commit()
fetched = ytdlp.fetch_video_comments(youtube_video_id)
for c in fetched:
db.add(VideoComment(video_id=video.id, **c))
db.commit()
comments = (
db.query(VideoComment)
.filter_by(video_id=video.id)
.order_by(VideoComment.is_pinned.desc(), VideoComment.likes.desc())
.all()
)
return [
{
"author": c.author,
"text": c.text,
"likes": c.likes,
"is_pinned": c.is_pinned,
"published_at": c.published_at,
}
for c in comments
]
@router.get("/queue", response_model=list[VideoDetail])
def queued_videos(
db: Session = Depends(get_db),