/** @typedef {import("fastify").FastifyInstance} FastifyInstance */ import { eq } from "drizzle-orm"; import { db } from "../db/index.js"; import { users } from "../db/schemas.js"; import { authMiddleware } from "../modules/middleware.js"; import { getAccessToken, getCaptionText, getVideoCaptions, getVideosFromPlaylist } from "../utils/youtube.js"; /** * * @param {FastifyInstance} fastify * @param {unknown} _ * @param {() => void} done */ export const videoRoutes = (fastify, _, done) => { fastify.register(authMiddleware); fastify.get("/", async (request, response) => { try { const token = await getAccessToken(fastify, request); const [user] = await db.select().from(users).where(eq(users.id, request.session.user_id)); const videos = await getVideosFromPlaylist(token, user.uploads_playlist_id); response.send({ success: true, videos }); } catch (e) { console.log(e); } }); fastify.get("/captions/:video_id", async (request, response) => { try { const token = await getAccessToken(fastify, request); const captions_list = await getVideoCaptions(token.access_token, request.params.video_id); const caption = captions_list.filter(x => x.snippet.language === "en"); if (caption.length === 0) { response.send({ success: false, message: "Couldn't find caption" }); return; } const caption_text = await getCaptionText(token.access_token, caption[0].id); response.send({ captions_info: caption, captions: caption_text }); } catch (e) { console.log(e); } }); done(); };