"""공개된 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) def narration_text_of(ctype: str, row) -> str | None: """공유 설명으로 쓰는 내레이션 한 줄. 무빙 포스터만 지원하고 없으면 None.""" if ctype != JOB_TYPE_VIDEO: return None lines = [str(line).strip() for line in row.narration_lines or [] if line] return " ".join(line for line in lines if line) or None 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