Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/api/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,55 @@ async def subscribe(
return out


@router.post("/{channel_id}/refresh-images", response_model=ChannelDetail)
async def refresh_channel_images(
channel_id: str,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
) -> ChannelDetail:
"""Fetch fresh avatar and banner images from YouTube and update the channel."""
result = await db.execute(select(Channel).where(Channel.id == channel_id))
channel = result.scalar_one_or_none()
if not channel:
raise HTTPException(status_code=404, detail="Channel not found")

images = await _resolve_channel_images(channel.youtube_channel_id)
if images.get("avatar_url"):
channel.avatar_url = images["avatar_url"]
if images.get("banner_url"):
channel.banner_url = images["banner_url"]
await db.commit()
await db.refresh(channel)

sub_count_result = await db.execute(
select(func.count())
.select_from(UserSubscription)
.where(UserSubscription.channel_id == channel_id)
)
subscriber_count = sub_count_result.scalar() or 0

video_count_result = await db.execute(
select(func.count()).select_from(Video).where(Video.channel_id == channel_id)
)
video_count = video_count_result.scalar() or 0

sub_result = await db.execute(
select(UserSubscription).where(
UserSubscription.user_id == user.id,
UserSubscription.channel_id == channel_id,
)
)
sub = sub_result.scalar_one_or_none()

detail = ChannelDetail.model_validate(channel)
detail.subscriber_count = subscriber_count
detail.video_count = video_count
detail.is_subscribed = sub is not None
if sub:
detail.tracking_mode = sub.tracking_mode
return detail


@router.delete("/{channel_id}/unsubscribe")
async def unsubscribe(
channel_id: str,
Expand Down
Loading