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
|
2024-06-13 08:58:14 +00:00
|
|
|
if (req.query.hub.challenge) {
|
2024-06-11 19:11:18 +00:00
|
|
|
// Respond with the challenge to verify the subscription
|
2024-06-13 08:58:14 +00:00
|
|
|
return reply.send(req.query.hub.challenge);
|
2024-06-11 19:11:18 +00:00
|
|
|
} else {
|
|
|
|
// Handle other cases or errors
|
|
|
|
return reply.code(404).send("Not found");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fastify.post("/youtube", async (req, reply) => {
|
2024-06-13 08:58:14 +00:00
|
|
|
const { headers, payload } = req;
|
2024-06-11 19:11:18 +00:00
|
|
|
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();
|
|
|
|
};
|