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
This commit is contained in:
1 parent
bb1ea12958
commit
b16d4a8f1b
126 files changed
+6921
-62
No files matched your search
@@ -27,7 +27,7 @@ from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Literal, Optional
|
||||
|
||||
from sqlalchemy import Select, func, literal, or_, select, union_all
|
||||
from sqlalchemy import Select, String, false, func, literal, or_, select, union_all
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.comment.models import Comment
|
||||
@@ -40,11 +40,21 @@ from app.database.like_cache import (
|
||||
mset_like_counts,
|
||||
)
|
||||
from app.home.models import Project
|
||||
from app.p2v.constants import JOB_TYPE_PLAYREEL, JOB_TYPE_VIDEO
|
||||
from app.p2v.models import PlayreelTask, PosterAliveTask
|
||||
from app.p2v.worker.artifact import THUMBNAIL, artifact_url
|
||||
from app.p2v.worker.playreel import PIPELINE as PLAYREEL_PIPELINE
|
||||
from app.ssulbox.models import SsulContent
|
||||
from app.utils.address_parser import SIDO_CITIES, SIDO_SEARCH_ALIASES
|
||||
from app.video.models import Video, VideoReaction
|
||||
from config import p2v_settings
|
||||
|
||||
ContentType = Literal["video", "ssul"]
|
||||
ContentType = Literal["video", "ssul", "p2v_video", "p2v_playreel"]
|
||||
|
||||
#: P2V 종류. 좋아요·댓글은 `video_reaction.p2v_*_id` / `comment.p2v_*_id` 에 쌓인다.
|
||||
#: 종류 문자열은 like_cache 의 ctype 과 같다.
|
||||
CT_P2V_VIDEO = JOB_TYPE_VIDEO
|
||||
CT_P2V_PLAYREEL = JOB_TYPE_PLAYREEL
|
||||
|
||||
#: 정렬 가능한 키. 그 외 값은 created_at 으로 떨어진다.
|
||||
SORT_CREATED = "created_at"
|
||||
@@ -124,6 +134,24 @@ def _ssul_comment_subq():
|
||||
)
|
||||
|
||||
|
||||
def _p2v_like_subq(ctype: str, model):
|
||||
return (
|
||||
select(func.count(VideoReaction.id))
|
||||
.where(_reaction_target(ctype) == model.id)
|
||||
.correlate(model)
|
||||
.scalar_subquery()
|
||||
)
|
||||
|
||||
|
||||
def _p2v_comment_subq(ctype: str, model):
|
||||
return (
|
||||
select(func.count(Comment.id))
|
||||
.where(_comment_target(ctype) == model.id, Comment.is_deleted.is_(False))
|
||||
.correlate(model)
|
||||
.scalar_subquery()
|
||||
)
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────
|
||||
# 필터
|
||||
# ──────────────────────────────────────────────
|
||||
@@ -181,6 +209,19 @@ def _ssul_where(store_name: Optional[str], region: Optional[str]) -> list:
|
||||
return clauses
|
||||
|
||||
|
||||
def _p2v_where(model, video_col, store_name: Optional[str], region: Optional[str],
|
||||
user_uuid: Optional[str] = None) -> list:
|
||||
clauses = [model.status == "done", model.is_deleted.is_(False), video_col.is_not(None)]
|
||||
if user_uuid:
|
||||
clauses.append(model.user_id == user_uuid)
|
||||
if store_name:
|
||||
clauses.append(model.name.ilike(f"%{store_name}%"))
|
||||
if region:
|
||||
# P2V 는 지역 정보가 없다. 지역 검색에는 걸리지 않는다
|
||||
clauses.append(false())
|
||||
return clauses
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────
|
||||
# 브랜치
|
||||
# ──────────────────────────────────────────────
|
||||
@@ -228,6 +269,38 @@ def _ssul_branch(where: list, sort_by: str) -> Select:
|
||||
return select(*cols).where(*where)
|
||||
|
||||
|
||||
def _p2v_branch(ctype: str, model, video_col, poster_col, sort_by: str) -> Select:
|
||||
"""무빙 포스터·롱컷 공용. 포스터 컬럼이 없는 종류(롱컷)는 NULL 로 두고 `_to_items` 가 채운다."""
|
||||
cols = [
|
||||
literal(ctype).label("ctype"),
|
||||
model.id.label("cid"),
|
||||
model.name.label("store_name"),
|
||||
literal(None, String).label("region"),
|
||||
video_col.label("movie_url"),
|
||||
model.created_at.label("created_at"),
|
||||
literal("").label("task_id"),
|
||||
(poster_col if poster_col is not None else literal(None, String)).label("poster_url"),
|
||||
model.name.label("title"),
|
||||
literal(None, String).label("description"),
|
||||
literal(None, String).label("hashtags"),
|
||||
]
|
||||
if sort_by == SORT_LIKE:
|
||||
cols.append(_p2v_like_subq(ctype, model).label("sort_value"))
|
||||
elif sort_by == SORT_COMMENT:
|
||||
cols.append(_p2v_comment_subq(ctype, model).label("sort_value"))
|
||||
return select(*cols)
|
||||
|
||||
|
||||
def _p2v_video_branch(where: list, sort_by: str) -> Select:
|
||||
return _p2v_branch(CT_P2V_VIDEO, PosterAliveTask, PosterAliveTask.video_url,
|
||||
PosterAliveTask.thumbnail_url, sort_by).where(*where)
|
||||
|
||||
|
||||
def _p2v_playreel_branch(where: list, sort_by: str) -> Select:
|
||||
return _p2v_branch(CT_P2V_PLAYREEL, PlayreelTask, PlayreelTask.video_url,
|
||||
None, sort_by).where(*where)
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────
|
||||
# 페이지 확정 후 집계
|
||||
# ──────────────────────────────────────────────
|
||||
@@ -235,14 +308,24 @@ def _ssul_branch(where: list, sort_by: str) -> Select:
|
||||
def _reaction_target(ctype: str):
|
||||
"""반응 테이블에서 이 종류가 쓰는 대상 컬럼.
|
||||
|
||||
`comment` / `video_reaction` 은 두 종류를 함께 담고 `video_id` 또는 `content_id`
|
||||
중 하나만 채운다(CHECK 로 강제). 어느 컬럼을 볼지만 갈아끼우면 된다.
|
||||
`comment` / `video_reaction` 은 모든 종류를 함께 담고 종류별 컬럼 중 하나만
|
||||
채운다(CHECK 로 강제). 어느 컬럼을 볼지만 갈아끼우면 된다.
|
||||
"""
|
||||
return VideoReaction.video_id if ctype == CT_VIDEO else VideoReaction.content_id
|
||||
return {
|
||||
CT_VIDEO: VideoReaction.video_id,
|
||||
CT_SSUL: VideoReaction.content_id,
|
||||
CT_P2V_VIDEO: VideoReaction.p2v_video_id,
|
||||
CT_P2V_PLAYREEL: VideoReaction.p2v_playreel_id,
|
||||
}[ctype]
|
||||
|
||||
|
||||
def _comment_target(ctype: str):
|
||||
return Comment.video_id if ctype == CT_VIDEO else Comment.content_id
|
||||
return {
|
||||
CT_VIDEO: Comment.video_id,
|
||||
CT_SSUL: Comment.content_id,
|
||||
CT_P2V_VIDEO: Comment.p2v_video_id,
|
||||
CT_P2V_PLAYREEL: Comment.p2v_playreel_id,
|
||||
}[ctype]
|
||||
|
||||
|
||||
async def _like_counts_for(
|
||||
@@ -319,7 +402,7 @@ async def enrich(
|
||||
user_uuid: Optional[str],
|
||||
) -> list[UnifiedItem]:
|
||||
"""페이지에 실린 항목에만 좋아요·댓글 수와 내 좋아요 여부를 채운다."""
|
||||
for ctype in (CT_VIDEO, CT_SSUL):
|
||||
for ctype in (CT_VIDEO, CT_SSUL, CT_P2V_VIDEO, CT_P2V_PLAYREEL):
|
||||
ids = [it.id for it in items if it.ctype == ctype]
|
||||
if not ids:
|
||||
continue
|
||||
@@ -358,6 +441,8 @@ async def fetch_gallery(
|
||||
"""전체 갤러리(`/video/all`). (항목, 전체 개수) 반환."""
|
||||
v_where = _video_where(store_name, region)
|
||||
s_where = _ssul_where(store_name, region)
|
||||
p_video_where = _p2v_where(PosterAliveTask, PosterAliveTask.video_url, store_name, region)
|
||||
p_reel_where = _p2v_where(PlayreelTask, PlayreelTask.video_url, store_name, region)
|
||||
|
||||
# 전체 개수는 브랜치별로 센다 — UNION 을 만들어 세는 것보다 싸다.
|
||||
total = (
|
||||
@@ -374,9 +459,24 @@ async def fetch_gallery(
|
||||
)
|
||||
).scalar() or 0
|
||||
|
||||
if p2v_settings.P2V_ENABLED:
|
||||
total += (
|
||||
await session.execute(
|
||||
select(func.count(PosterAliveTask.id)).where(*p_video_where)
|
||||
)
|
||||
).scalar() or 0
|
||||
total += (
|
||||
await session.execute(
|
||||
select(func.count(PlayreelTask.id)).where(*p_reel_where)
|
||||
)
|
||||
).scalar() or 0
|
||||
|
||||
branches = [_video_branch(v_where, sort_by)]
|
||||
if include_ssul:
|
||||
branches.append(_ssul_branch(s_where, sort_by))
|
||||
if p2v_settings.P2V_ENABLED:
|
||||
branches.append(_p2v_video_branch(p_video_where, sort_by))
|
||||
branches.append(_p2v_playreel_branch(p_reel_where, sort_by))
|
||||
|
||||
u = union_all(*branches).subquery() if len(branches) > 1 else branches[0].subquery()
|
||||
|
||||
@@ -411,7 +511,9 @@ def _to_items(rows) -> list[UnifiedItem]:
|
||||
movie_url=r.movie_url,
|
||||
created_at=r.created_at,
|
||||
task_id=r.task_id or "",
|
||||
poster_url=r.poster_url,
|
||||
# 롱컷의 정지컷은 컬럼이 아니라 정해진 경로에 있다
|
||||
poster_url=(artifact_url(PLAYREEL_PIPELINE, r.cid, THUMBNAIL)
|
||||
if r.ctype == CT_P2V_PLAYREEL else r.poster_url),
|
||||
title=r.title,
|
||||
description=r.description,
|
||||
hashtags=list(r.hashtags) if r.hashtags else None,
|
||||
@@ -459,16 +561,31 @@ async def fetch_my_contents(
|
||||
SsulContent.video_url.is_not(None),
|
||||
]
|
||||
|
||||
p_video_where = _p2v_where(PosterAliveTask, PosterAliveTask.video_url, None, None, user_uuid)
|
||||
p_reel_where = _p2v_where(PlayreelTask, PlayreelTask.video_url, None, None, user_uuid)
|
||||
|
||||
total = ((await session.execute(
|
||||
select(func.count(Video.id)).where(*v_where)
|
||||
)).scalar() or 0) + ((await session.execute(
|
||||
select(func.count(SsulContent.id)).where(*s_where)
|
||||
)).scalar() or 0)
|
||||
|
||||
u = union_all(
|
||||
branches = [
|
||||
_video_branch(v_where, SORT_CREATED),
|
||||
_ssul_branch(s_where, SORT_CREATED),
|
||||
).subquery()
|
||||
]
|
||||
if p2v_settings.P2V_ENABLED:
|
||||
total += ((await session.execute(
|
||||
select(func.count(PosterAliveTask.id)).where(*p_video_where)
|
||||
)).scalar() or 0) + ((await session.execute(
|
||||
select(func.count(PlayreelTask.id)).where(*p_reel_where)
|
||||
)).scalar() or 0)
|
||||
branches += [
|
||||
_p2v_video_branch(p_video_where, SORT_CREATED),
|
||||
_p2v_playreel_branch(p_reel_where, SORT_CREATED),
|
||||
]
|
||||
|
||||
u = union_all(*branches).subquery()
|
||||
|
||||
rows = (
|
||||
await session.execute(
|
||||
|
||||
Reference in new issue
Block a user