youtuber-blog/src/routes/webhook.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-05-29 19:35:18 +00:00
/** @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 {
2024-06-11 19:11:18 +00:00
2024-05-29 19:35:18 +00:00
} catch (e) {
response.status(400).send({
success: false,
message: "User not found",
log: e.message
});
return;
}
});
2024-06-11 19:11:18 +00:00
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");
}
})
2024-05-29 19:35:18 +00:00
done();
};