diff --git a/backend/routers/channels.py b/backend/routers/channels.py index e202a30..6b06ac0 100644 --- a/backend/routers/channels.py +++ b/backend/routers/channels.py @@ -546,6 +546,7 @@ def bulk_channel_action( @router.get("/{channel_id}", response_model=ChannelOut) def get_channel( channel_id: int, + background_tasks: BackgroundTasks, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): @@ -576,6 +577,16 @@ def get_channel( ).mappings().first() if not row: raise HTTPException(status_code=404, detail="Channel not found") + + # Re-index in the background if stale (not crawled in the last hour) + crawled_at = row.get("crawled_at") + stale = ( + crawled_at is None + or (datetime.utcnow() - crawled_at).total_seconds() > 3600 + ) + if stale: + background_tasks.add_task(_index_channel_task, channel_id, current_user.id) + return ChannelOut(**dict(row)) diff --git a/frontend/src/pages/Channel.jsx b/frontend/src/pages/Channel.jsx index 5b41d50..78d9555 100644 --- a/frontend/src/pages/Channel.jsx +++ b/frontend/src/pages/Channel.jsx @@ -1,4 +1,4 @@ -import { useState, useMemo } from "react"; +import { useState, useMemo, useEffect, useRef } from "react"; import { useParams } from "react-router-dom"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { getChannel, getChannelVideos, followChannel, unfollowChannel, indexChannel, downloadChannel } from "../api"; @@ -41,6 +41,18 @@ export default function ChannelPage() { queryFn: () => getChannelVideos(id).then((r) => r.data), }); + // Refetch after the background re-index has had time to run + const refetchedRef = useRef(false); + useEffect(() => { + if (!id || refetchedRef.current) return; + refetchedRef.current = true; + const t = setTimeout(() => { + qc.invalidateQueries({ queryKey: ["channel", id] }); + qc.invalidateQueries({ queryKey: ["channel-videos", id] }); + }, 8000); + return () => clearTimeout(t); + }, [id, qc]); + const followMut = useMutation({ mutationFn: () => channel?.status === "followed" ? unfollowChannel(id) : followChannel(id),