youtuber-blog/src/routes/webhook.js
2024-06-13 10:58:14 +02:00

64 lines
1.7 KiB
JavaScript

/** @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";
/**
*
* @param {FastifyInstance} fastify
* @param {unknown} _
* @param {() => void} done
*/
export const webhookRoutes = (fastify, _, done) => {
fastify.get("/", async (request, response) => {
try {
} catch (e) {
response.status(400).send({
success: false,
message: "User not found",
log: e.message
});
return;
}
});
fastify.get("/youtube", async (req, reply) => {
// Check if the request contains the 'hub.challenge' query parameter
if (req.query.hub.challenge) {
// Respond with the challenge to verify the subscription
return reply.send(req.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 } = req;
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();
};