feat(ssulbox): 업장 검색을 ADO2 방식으로 전환하고 URL 해석을 워커로 이관
This commit is contained in:
1 parent
50dc7af5bc
commit
459099998e
5 files changed
+296
-198
No files matched your search
@@ -5,10 +5,11 @@ castad 는 `/api/*` prefix 를 쓰지 않고 도메인별 prefix 를 쓰므로 `
|
||||
인증은 castad `get_current_user` 를 그대로 쓴다(원본의 auth 라우터·JWT 는 폐기).
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy import select
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from app.credit.exceptions import InsufficientCreditError
|
||||
from app.database.like_cache import get_like_count, is_user_liked, set_like_count
|
||||
from app.database.session import AsyncSessionLocal, get_session
|
||||
from app.ssulbox.constants import ORPHAN_STATUSES, is_generation_available
|
||||
from app.ssulbox.exceptions import GenerationUnavailableError, TaskNotFoundError
|
||||
@@ -17,13 +18,16 @@ from app.ssulbox.schemas.ssulbox_schema import (
|
||||
SsulActiveTasksResponse,
|
||||
SsulCreateRequest,
|
||||
SsulCreateResponse,
|
||||
SsulPlaceSearchResponse,
|
||||
SsulDeleteResponse,
|
||||
SsulDetailResponse,
|
||||
SsulTaskStatus,
|
||||
)
|
||||
from app.ssulbox.services import place_service, task_service
|
||||
from app.ssulbox.services import task_service
|
||||
from app.ssulbox.worker import job_manager
|
||||
from app.user.dependencies.auth import get_current_user
|
||||
from app.user.dependencies.auth import get_current_user, get_current_user_optional
|
||||
from app.user.models import User
|
||||
# 좋아요는 castad video_reaction 에 병합돼 있다 (썰박스 행은 content_id 가 채워짐)
|
||||
from app.video.models import VideoReaction
|
||||
from app.utils.logger import get_logger
|
||||
from config import ssulbox_settings
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -33,33 +37,6 @@ logger = get_logger("ssulbox")
|
||||
router = APIRouter(prefix="/ssul", tags=["Ssulbox"])
|
||||
|
||||
|
||||
@router.get(
|
||||
"/search/place",
|
||||
response_model=SsulPlaceSearchResponse,
|
||||
summary="업장 검색 (네이버 지도)",
|
||||
description="""
|
||||
업장명으로 네이버 지도 후보를 검색합니다.
|
||||
|
||||
castad `/search/accommodation`(네이버 **검색 API**)과 달리 **`place_url` 을 포함**합니다.
|
||||
생성 파이프라인이 네이버 지도 place 페이지를 크롤링하므로 이 URL 이 필요합니다.
|
||||
|
||||
- 동명 업장은 주소로 구분됩니다.
|
||||
- 검색 실패·타임아웃 시 빈 목록을 반환합니다(사용자는 네이버 링크를 직접 붙여넣을 수 있습니다).
|
||||
- Playwright 크롤링이라 수 초 걸립니다.
|
||||
""",
|
||||
responses={
|
||||
200: {"description": "검색 성공 (결과 없음도 200)"},
|
||||
401: {"description": "인증 실패"},
|
||||
},
|
||||
)
|
||||
async def search_place(
|
||||
query: str = Query(..., min_length=2, description="업장명"),
|
||||
limit: int = Query(default=8, ge=1, le=20),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> SsulPlaceSearchResponse:
|
||||
items = await place_service.search_places(query, limit=limit)
|
||||
return SsulPlaceSearchResponse(query=query, count=len(items), items=items)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/create",
|
||||
@@ -120,8 +97,16 @@ async def create_ssul(
|
||||
|
||||
# 커밋이 끝난 뒤에 큐잉한다 — 워커가 아직 없는 행을 조회하면 안 된다.
|
||||
# create_job 은 앱 이벤트 루프를 캡처하므로 반드시 요청 핸들러에서 호출한다.
|
||||
# place URL 해석은 워커가 한다 — 10초 이상 걸려 요청을 막으면 안 된다.
|
||||
job_manager.create_job(
|
||||
content_id, body.scenario, body.input, body.scenes, body.seconds
|
||||
content_id,
|
||||
body.scenario,
|
||||
body.input,
|
||||
body.scenes,
|
||||
body.seconds,
|
||||
store_name=body.store_name,
|
||||
road_address=body.road_address,
|
||||
address=body.address,
|
||||
)
|
||||
|
||||
return SsulCreateResponse(
|
||||
@@ -192,3 +177,124 @@ async def get_task(
|
||||
if row is None or row.user_uuid != current_user.user_uuid:
|
||||
raise TaskNotFoundError()
|
||||
return SsulTaskStatus.model_validate(row)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{content_id}",
|
||||
response_model=SsulDetailResponse,
|
||||
summary="썰박스 콘텐츠 공개 상세",
|
||||
description="""
|
||||
완성된 썰박스 콘텐츠의 상세 정보를 반환합니다. **비로그인도 접근 가능**합니다
|
||||
(공유 링크 `/ssul/{content_id}` 가 이 API 를 씁니다 — castad `/video/{video_id}` 와 동일한 패턴).
|
||||
|
||||
- `status=done` 이고 삭제되지 않은 콘텐츠만 조회됩니다.
|
||||
- `is_liked_by_me` 는 비로그인이면 항상 false 입니다.
|
||||
- 댓글은 `GET /comment/video/{content_id}?type=ssul` 로 따로 조회합니다.
|
||||
""",
|
||||
responses={
|
||||
200: {"description": "조회 성공"},
|
||||
404: {"description": "콘텐츠를 찾을 수 없음"},
|
||||
},
|
||||
)
|
||||
async def get_content_detail(
|
||||
content_id: int,
|
||||
current_user: User | None = Depends(get_current_user_optional),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> SsulDetailResponse:
|
||||
row = await session.get(SsulContent, content_id)
|
||||
if (
|
||||
row is None
|
||||
or row.is_deleted
|
||||
or row.status != "done"
|
||||
or not row.video_url
|
||||
):
|
||||
raise TaskNotFoundError()
|
||||
|
||||
# 좋아요 수: Redis 우선, 미스면 DB 집계 후 캐시에 채운다 (castad 상세와 동일)
|
||||
like_count = await get_like_count(content_id, ctype="ssul")
|
||||
if like_count is None:
|
||||
like_count = (
|
||||
await session.execute(
|
||||
select(func.count(VideoReaction.id)).where(
|
||||
VideoReaction.content_id == content_id
|
||||
)
|
||||
)
|
||||
).scalar() or 0
|
||||
await set_like_count(content_id, like_count, ctype="ssul")
|
||||
|
||||
is_liked = False
|
||||
if current_user:
|
||||
cached = await is_user_liked(
|
||||
content_id, current_user.user_uuid, ctype="ssul"
|
||||
)
|
||||
if cached is None:
|
||||
# user-set 콜드 미스 — 응답 정확성만 필요하므로 DB 존재 확인으로 대체
|
||||
is_liked = (
|
||||
await session.execute(
|
||||
select(VideoReaction.id).where(
|
||||
VideoReaction.content_id == content_id,
|
||||
VideoReaction.user_uuid == current_user.user_uuid,
|
||||
)
|
||||
)
|
||||
).scalar_one_or_none() is not None
|
||||
else:
|
||||
is_liked = cached
|
||||
|
||||
return SsulDetailResponse(
|
||||
content_id=row.id,
|
||||
scenario=row.scenario,
|
||||
video_url=row.video_url,
|
||||
store_name=row.store_name or None,
|
||||
region=row.region,
|
||||
created_at=row.created_at,
|
||||
like_count=like_count,
|
||||
is_liked_by_me=is_liked,
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/{content_id}",
|
||||
response_model=SsulDeleteResponse,
|
||||
summary="썰박스 콘텐츠 소프트 삭제",
|
||||
description="""
|
||||
본인 소유의 썰박스 콘텐츠를 소프트 삭제합니다 (`is_deleted=True`, 데이터는 유지).
|
||||
castad `DELETE /archive/videos/{video_id}` 와 동일한 정책입니다.
|
||||
|
||||
- 진행 중(queued/running)인 잡은 삭제할 수 없습니다 — 완료·실패 후에 지워야
|
||||
워커·크레딧 환불 경로와 충돌하지 않습니다.
|
||||
""",
|
||||
responses={
|
||||
200: {"description": "삭제 성공"},
|
||||
401: {"description": "인증 실패"},
|
||||
404: {"description": "콘텐츠를 찾을 수 없음 (남의 것 포함)"},
|
||||
409: {"description": "진행 중인 잡은 삭제 불가"},
|
||||
},
|
||||
)
|
||||
async def delete_content(
|
||||
content_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
) -> SsulDeleteResponse:
|
||||
row = await session.get(SsulContent, content_id)
|
||||
# 남의 것이면 존재 여부를 노출하지 않고 동일하게 404
|
||||
if row is None or row.is_deleted or row.user_uuid != current_user.user_uuid:
|
||||
raise TaskNotFoundError()
|
||||
|
||||
# 진행 중인 잡을 지우면 워커가 완료 시점에 삭제된 행을 done 으로 되살리거나,
|
||||
# 실패 환불 경로와 꼬인다. 터미널 상태에서만 허용한다.
|
||||
if row.status in ("queued", "running"):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="생성이 진행 중입니다. 완료된 뒤에 삭제해주세요.",
|
||||
)
|
||||
|
||||
row.is_deleted = True
|
||||
await session.commit()
|
||||
logger.info(
|
||||
f"[delete_content] id={content_id} user={current_user.user_uuid}"
|
||||
)
|
||||
return SsulDeleteResponse(
|
||||
success=True,
|
||||
content_id=content_id,
|
||||
message="콘텐츠가 삭제되었습니다.",
|
||||
)
|
||||
Reference in new issue
Block a user