diff --git a/src/routes/webhook.js b/src/routes/webhook.js index 059b35d..0a2add3 100644 --- a/src/routes/webhook.js +++ b/src/routes/webhook.js @@ -14,7 +14,7 @@ import { authMiddleware } from "../modules/middleware.js"; export const webhookRoutes = (fastify, _, done) => { fastify.get("/", async (request, response) => { try { - + } catch (e) { response.status(400).send({ success: false, @@ -25,5 +25,39 @@ export const webhookRoutes = (fastify, _, done) => { } }); + fastify.get("/youtube", async (req, reply) => { + // Check if the request contains the 'hub.challenge' query parameter + if (request.query.hub.challenge) { + // Respond with the challenge to verify the subscription + return reply.send(request.query.hub.challenge); + } else { + // Handle other cases or errors + return reply.code(404).send("Not found"); + } + }); + + fastify.post("/youtube", async (req, reply) => { + const { headers, payload } = request; + const contentType = headers['content-type']; + + // Check if the content type is 'application/atom+xml' + if (contentType === 'application/atom+xml') { + // Parse the XML payload + const { entries } = payload; + + // Example processing: log the video IDs of new videos + entries.forEach(entry => { + const videoId = entry.find('yt:videoId').value; + console.log(`New video uploaded: ${videoId}`); + }); + + // Respond with a success status + return reply.code(200).send(); + } else { + // Respond with an error status if the content type is not expected + return reply.code(400).send("Bad Request"); + } + }) + done(); };