Spaces:
Runtime error
Runtime error
| "use server"; | |
| import { auth } from "@/lib/auth"; | |
| import { headers } from "next/headers"; | |
| import { db } from "@/lib/db"; | |
| import { youtubeVideos, youtubeChannels } from "@/lib/db/schema"; | |
| import { eq, and } from "drizzle-orm"; | |
| import { inngest } from "@/inngest/client"; | |
| export interface TriggerObjectDetectionResult { | |
| success: boolean; | |
| error?: string; | |
| } | |
| /** | |
| * Server action to manually trigger object detection for a specific video. | |
| * | |
| * @param videoId - Internal database video ID | |
| * @returns Result object with success status or error message | |
| */ | |
| export async function triggerObjectDetectionAction( | |
| videoId: string | |
| ): Promise<TriggerObjectDetectionResult> { | |
| // 1. Authenticate user | |
| const session = await auth.api.getSession({ | |
| headers: await headers(), | |
| }); | |
| if (!session || !session.user) { | |
| return { | |
| success: false, | |
| error: "Unauthorized", | |
| }; | |
| } | |
| try { | |
| // 2. Verify video exists and user owns the channel | |
| const [videoWithChannel] = await db | |
| .select({ | |
| id: youtubeVideos.id, | |
| ytVideoId: youtubeVideos.videoId, | |
| creatorId: youtubeChannels.creatorId, | |
| }) | |
| .from(youtubeVideos) | |
| .innerJoin(youtubeChannels, eq(youtubeVideos.channelId, youtubeChannels.id)) | |
| .where( | |
| and( | |
| eq(youtubeVideos.id, videoId), | |
| eq(youtubeChannels.creatorId, session.user.id) | |
| ) | |
| ) | |
| .limit(1); | |
| if (!videoWithChannel) { | |
| return { | |
| success: false, | |
| error: "Video not found or permission denied", | |
| }; | |
| } | |
| // 3. Trigger Inngest workflow | |
| await inngest.send({ | |
| name: 'youtube/video.detect-objects', | |
| data: { | |
| videoId: videoWithChannel.id, | |
| videoUrl: `https://www.youtube.com/watch?v=${videoWithChannel.ytVideoId}`, | |
| }, | |
| }); | |
| return { | |
| success: true, | |
| }; | |
| } catch (error) { | |
| console.error("Failed to trigger object detection:", error); | |
| return { | |
| success: false, | |
| error: "An unexpected error occurred while triggering object detection.", | |
| }; | |
| } | |
| } | |