feat(archive): 영상 목록 조회에 내 좋아요 여부(is_liked_by_me) 포함
This commit is contained in:
parent
11c358da3c
commit
1507a0f859
@ -17,7 +17,11 @@ from app.user.models import User
|
|||||||
from app.utils.logger import get_logger
|
from app.utils.logger import get_logger
|
||||||
from app.utils.pagination import PaginatedResponse
|
from app.utils.pagination import PaginatedResponse
|
||||||
from app.comment.models import Comment
|
from app.comment.models import Comment
|
||||||
from app.database.like_cache import get_like_counts, mset_like_counts
|
from app.database.like_cache import (
|
||||||
|
bulk_is_user_liked,
|
||||||
|
get_like_counts,
|
||||||
|
mset_like_counts,
|
||||||
|
)
|
||||||
from app.video.models import Video, VideoReaction
|
from app.video.models import Video, VideoReaction
|
||||||
from app.video.schemas.video_schema import VideoListItem
|
from app.video.schemas.video_schema import VideoListItem
|
||||||
|
|
||||||
@ -149,6 +153,24 @@ async def get_videos(
|
|||||||
if vid not in db_found_ids:
|
if vid not in db_found_ids:
|
||||||
like_count_map[vid] = 0
|
like_count_map[vid] = 0
|
||||||
|
|
||||||
|
# is_liked_by_me: Redis user-set 기준, 캐시 미스 시 현재 사용자 상태만 DB 조회
|
||||||
|
raw_liked = await bulk_is_user_liked(video_ids, current_user.user_uuid)
|
||||||
|
needs_db_lookup = [
|
||||||
|
vid for vid, liked in raw_liked.items()
|
||||||
|
if liked is None and like_count_map.get(vid, 0) > 0
|
||||||
|
]
|
||||||
|
if needs_db_lookup:
|
||||||
|
liked_video_ids = set((await session.execute(
|
||||||
|
select(VideoReaction.video_id).where(
|
||||||
|
VideoReaction.video_id.in_(needs_db_lookup),
|
||||||
|
VideoReaction.user_uuid == current_user.user_uuid,
|
||||||
|
)
|
||||||
|
)).scalars().all())
|
||||||
|
for vid in needs_db_lookup:
|
||||||
|
raw_liked[vid] = vid in liked_video_ids
|
||||||
|
|
||||||
|
liked_map = {vid: bool(liked) for vid, liked in raw_liked.items()}
|
||||||
|
|
||||||
# VideoListItem으로 변환
|
# VideoListItem으로 변환
|
||||||
items = [
|
items = [
|
||||||
VideoListItem(
|
VideoListItem(
|
||||||
@ -161,6 +183,7 @@ async def get_videos(
|
|||||||
created_at=video.created_at,
|
created_at=video.created_at,
|
||||||
like_count=like_count_map.get(video.id) or 0,
|
like_count=like_count_map.get(video.id) or 0,
|
||||||
comment_count=comment_count or 0,
|
comment_count=comment_count or 0,
|
||||||
|
is_liked_by_me=liked_map.get(video.id, False),
|
||||||
)
|
)
|
||||||
for video, project, comment_count in rows
|
for video, project, comment_count in rows
|
||||||
]
|
]
|
||||||
|
|||||||
@ -162,6 +162,10 @@ class VideoListItem(BaseModel):
|
|||||||
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
created_at: Optional[datetime] = Field(None, description="생성 일시")
|
||||||
like_count: int = Field(0, description="좋아요 수")
|
like_count: int = Field(0, description="좋아요 수")
|
||||||
comment_count: int = Field(0, description="댓글 수 (대댓글 포함)")
|
comment_count: int = Field(0, description="댓글 수 (대댓글 포함)")
|
||||||
|
is_liked_by_me: bool = Field(
|
||||||
|
False,
|
||||||
|
description="현재 로그인 사용자가 좋아요를 눌렀는지",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class VideoThumbnailItem(BaseModel):
|
class VideoThumbnailItem(BaseModel):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user