Files
o2o-castad-backend/app/p2v/services/public_content.py
T
skkim b16d4a8f1b feat: P2V 파이프라인 연동 및 좋아요·댓글·공유 지원
- 무빙 포스터(p2v_video)·롱컷(p2v_playreel) 파이프라인과 워커, 갤러리·내 콘텐츠 노출
- 좋아요·댓글: comment/video_reaction에 p2v_playreel_id 추가, 종류별 CHECK 확장
- P2V 소프트 삭제(is_deleted), queued/running 삭제 차단
- 공개 상세·공유 OG 페이지 추가
- 삭제된 부모 댓글이 목록에서 사라지던 has_live_reply 서브쿼리 버그 수정
- 마이그레이션: migration_2026-10-15_p2v_reactions.sql
2026-10-15 10:42:30 +09:00

91 lines
3.2 KiB
Python

"""공개된 P2V 콘텐츠 조회 — 좋아요·댓글·공유·공개 상세가 같은 기준을 쓴다.
공개 = 완료(done) + 삭제되지 않음 + 영상 URL 있음. 이 조건이 갈라지면
목록에는 보이는데 좋아요·공유는 404 인 콘텐츠가 생긴다.
"""
from sqlalchemy import Select, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.database.like_cache import (
get_like_count,
is_user_liked,
set_like_count,
)
from app.p2v.constants import JOB_TYPE_PLAYREEL, JOB_TYPE_VIDEO
from app.p2v.models import PlayreelTask, PosterAliveTask
from app.p2v.worker.playreel import thumbnail_url as playreel_thumbnail_url
from app.video.models import VideoReaction
#: 콘텐츠 종류 → 잡 테이블 모델. 종류 문자열은 like_cache 의 ctype 과 같다
MODELS = {
JOB_TYPE_VIDEO: PosterAliveTask,
JOB_TYPE_PLAYREEL: PlayreelTask,
}
P2V_TYPES = tuple(MODELS)
#: 종류 → video_reaction 에서 이 종류가 쓰는 대상 컬럼
REACTION_COLUMN = {
JOB_TYPE_VIDEO: VideoReaction.p2v_video_id,
JOB_TYPE_PLAYREEL: VideoReaction.p2v_playreel_id,
}
def _public_conditions(model, content_id: int) -> list:
return [
model.id == content_id,
model.status == "done",
model.is_deleted.is_(False),
model.video_url.is_not(None),
model.video_url != "",
]
def public_exists_query(ctype: str, content_id: int) -> Select:
"""이 종류의 공개 콘텐츠 한 건을 id 로 찾는 쿼리(존재 확인용)."""
model = MODELS[ctype]
return select(model.id).where(*_public_conditions(model, content_id))
async def fetch_public(session: AsyncSession, ctype: str, content_id: int):
"""공개 콘텐츠 행. 없거나 비공개면 None."""
model = MODELS[ctype]
return (
await session.execute(select(model).where(*_public_conditions(model, content_id)))
).scalar_one_or_none()
def poster_of(ctype: str, row) -> str | None:
"""무빙 포스터는 컬럼에, 롱컷은 정해진 경로에 썸네일이 있다."""
return row.thumbnail_url if ctype == JOB_TYPE_VIDEO else playreel_thumbnail_url(row)
async def like_state(
session: AsyncSession, ctype: str, content_id: int, user_uuid: str | None
) -> tuple[int, bool]:
"""(좋아요 수, 내 좋아요 여부). Redis 우선, 미스면 DB 로 보정하고 캐시를 채운다."""
column = REACTION_COLUMN[ctype]
like_count = await get_like_count(content_id, ctype=ctype)
if like_count is None:
like_count = (
await session.execute(
select(func.count(VideoReaction.id)).where(column == content_id)
)
).scalar() or 0
await set_like_count(content_id, like_count, ctype=ctype)
if not user_uuid:
return like_count, False
cached = await is_user_liked(content_id, user_uuid, ctype=ctype)
if cached is not None:
return like_count, cached
# user-set 콜드 미스 — 응답 정확성만 필요하므로 DB 존재 확인으로 대체한다
liked = (
await session.execute(
select(VideoReaction.id).where(
column == content_id, VideoReaction.user_uuid == user_uuid
)
)
).scalar_one_or_none() is not None
return like_count, liked