feat: 좋아요·댓글·SNS 업로드를 콘텐츠 종류 공용으로 통합

This commit is contained in:
skkim committed 2026-08-11 11:03:03 +09:00
1 parent ed1ba71bfe
commit fb8febe462
7 files changed
+318 -109

No files matched your search

+57 -19
View File
@@ -1,5 +1,5 @@
from collections import defaultdict
from typing import List, Optional
from typing import List, Literal, Optional
from fastapi import HTTPException
from sqlalchemy import exists, select
@@ -7,16 +7,54 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.comment.models import Comment
from app.comment.schemas.comment_schema import CommentItem, ReplyItem
from app.ssulbox.models import SsulContent
from app.utils.pagination import PaginatedResponse
from app.video.models import Video
ContentType = Literal["video", "ssul"]
def _target_col(content_type: ContentType):
"""댓글이 달린 대상을 가리키는 컬럼.
`comment` 는 ADO2 영상과 썰박스를 함께 담고 `video_id` / `content_id` 중
정확히 하나만 채운다(CHECK 로 강제). 종류는 어느 컬럼이 채워졌는지가 결정한다.
"""
return Comment.video_id if content_type == "video" else Comment.content_id
async def _ensure_target_exists(
session: AsyncSession,
content_type: ContentType,
target_id: int,
) -> None:
"""댓글 대상(영상 또는 썰박스 콘텐츠)이 존재·공개 상태인지 확인.
id 가 종류별 독립 시퀀스라, 반대쪽 테이블에 같은 id 가 있어도 잡으면 안 된다.
"""
if content_type == "ssul":
q = select(SsulContent.id).where(
SsulContent.id == target_id,
SsulContent.status == "done",
SsulContent.is_deleted.is_(False),
)
else:
q = select(Video.id).where(
Video.id == target_id,
Video.status == "completed",
Video.is_deleted.is_(False),
)
if (await session.execute(q)).scalar_one_or_none() is None:
raise HTTPException(status_code=404, detail="콘텐츠를 찾을 수 없습니다.")
async def _validate_parent(
session: AsyncSession,
parent_id: int,
video_id: int,
content_type: ContentType,
target_id: int,
) -> None:
"""2-depth 제한 + 동일 video 검증."""
"""2-depth 제한 + 동일 대상 검증."""
result = await session.execute(
select(Comment).where(
Comment.id == parent_id,
@@ -27,8 +65,11 @@ async def _validate_parent(
if parent is None:
raise HTTPException(status_code=400, detail="부모 댓글을 찾을 수 없습니다.")
if parent.video_id != video_id:
raise HTTPException(status_code=400, detail="다른 영상의 댓글에는 대댓글을 달 수 없습니다.")
# 부모가 같은 종류의 같은 대상에 달렸는지 본다. video_id 만 비교하면
# 썰박스 댓글(video_id=NULL)에 ADO2 대댓글이 달리는 교차를 못 막는다.
parent_target = parent.video_id if content_type == "video" else parent.content_id
if parent_target != target_id:
raise HTTPException(status_code=400, detail="다른 콘텐츠의 댓글에는 대댓글을 달 수 없습니다.")
if parent.parent_id is not None:
raise HTTPException(status_code=400, detail="대댓글에는 대댓글을 달 수 없습니다. (최대 2-depth)")
@@ -73,24 +114,19 @@ async def create_comment(
nickname: str,
content: str,
parent_id: Optional[int],
content_type: ContentType = "video",
) -> Comment:
# Video 존재 확인
video_result = await session.execute(
select(Video).where(
Video.id == video_id,
Video.status == "completed",
Video.is_deleted == False, # noqa: E712
)
)
if video_result.scalar_one_or_none() is None:
raise HTTPException(status_code=404, detail="영상을 찾을 수 없습니다.")
# 대상 존재 확인
await _ensure_target_exists(session, content_type, video_id)
# parent_id 검증
if parent_id is not None:
await _validate_parent(session, parent_id, video_id)
await _validate_parent(session, parent_id, content_type, video_id)
comment = Comment(
video_id=video_id,
# 종류에 따라 둘 중 하나만 채운다 (CHECK ck_comment_one_target 이 강제)
video_id=video_id if content_type == "video" else None,
content_id=video_id if content_type == "ssul" else None,
user_uuid=user_uuid,
nickname=nickname,
parent_id=parent_id,
@@ -108,6 +144,7 @@ async def list_comments(
page: int,
page_size: int,
current_user_uuid: Optional[str],
content_type: ContentType = "video",
) -> PaginatedResponse[CommentItem]:
offset = (page - 1) * page_size
@@ -121,9 +158,10 @@ async def list_comments(
.correlate(Comment)
)
# 최상위 댓글 필터: 삭제 안 됐거나 살아있는 대댓글이 있는 것
# 최상위 댓글 필터: 삭제 안 됐거나 살아있는 대댓글이 있는 것.
# 종류에 맞는 대상 컬럼으로 걸러야 같은 id 의 반대 종류 댓글이 섞이지 않는다.
parent_where = [
Comment.video_id == video_id,
_target_col(content_type) == video_id,
Comment.parent_id.is_(None),
(Comment.is_deleted == False) | has_live_reply, # noqa: E712
]