generated from nhcarrigan/template
feat: use json schema to get all announcements
This commit is contained in:
@@ -10,9 +10,9 @@ import { announceOnBluesky } from "../modules/announceOnBluesky.js";
|
||||
import { announceOnDiscord } from "../modules/announceOnDiscord.js";
|
||||
import { announceOnReddit } from "../modules/announceOnReddit.js";
|
||||
import { announceOnTwitter } from "../modules/announceOnTwitter.js";
|
||||
import { generateAnnouncements } from "../modules/generateAnnouncements.js";
|
||||
import { getIpFromRequest } from "../modules/getIpFromRequest.js";
|
||||
import { summarisePost } from "../modules/summarisePost.js";
|
||||
import { isAnnouncementType } from "../utils/typeguards.js";
|
||||
import { isAnnouncementType, isValidString } from "../utils/typeguards.js";
|
||||
import type { FastifyPluginAsync } from "fastify";
|
||||
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
@@ -31,21 +31,26 @@ export const announcementRoutes: FastifyPluginAsync = async(server) => {
|
||||
},
|
||||
take: 10,
|
||||
});
|
||||
return await reply.status(200).type("application/json").
|
||||
send(announcements.map((announcement) => {
|
||||
return {
|
||||
content: announcement.content,
|
||||
createdAt: announcement.createdAt,
|
||||
title: announcement.title,
|
||||
type: announcement.type,
|
||||
};
|
||||
}));
|
||||
return await reply.
|
||||
status(200).
|
||||
type("application/json").
|
||||
send(
|
||||
announcements.map((announcement) => {
|
||||
return {
|
||||
content: announcement.content,
|
||||
createdAt: announcement.createdAt,
|
||||
title: announcement.title,
|
||||
type: announcement.type,
|
||||
};
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Fastify requires Body instead of body.
|
||||
server.post<{ Body: { title: string; content: string; type: string } }>(
|
||||
server.post<{ Body: { content: string; type: string } }>(
|
||||
"/announcement",
|
||||
// eslint-disable-next-line complexity -- This is a complex route, but it is necessary to validate the announcement.
|
||||
|
||||
// eslint-disable-next-line max-statements -- This is a long function.
|
||||
async(request, reply) => {
|
||||
const token = request.headers.authorization;
|
||||
if (token === undefined || token !== process.env.ANNOUNCEMENT_TOKEN) {
|
||||
@@ -60,15 +65,8 @@ export const announcementRoutes: FastifyPluginAsync = async(server) => {
|
||||
});
|
||||
}
|
||||
|
||||
const { title, content, type } = request.body;
|
||||
if (
|
||||
typeof title !== "string"
|
||||
|| typeof content !== "string"
|
||||
|| typeof type !== "string"
|
||||
|| title.length === 0
|
||||
|| content.length === 0
|
||||
|| type.length === 0
|
||||
) {
|
||||
const { content, type } = request.body;
|
||||
if (!isValidString(content) || !isValidString(type)) {
|
||||
return await reply.status(400).send({
|
||||
error: "Missing required fields.",
|
||||
});
|
||||
@@ -76,37 +74,45 @@ export const announcementRoutes: FastifyPluginAsync = async(server) => {
|
||||
|
||||
if (!isAnnouncementType(type)) {
|
||||
return await reply.status(400).send({
|
||||
error:
|
||||
`Invalid announcement type. Available types: products, community, company.`,
|
||||
error: `Invalid announcement type. Available types: products, community, company.`,
|
||||
});
|
||||
}
|
||||
|
||||
const announcement = await generateAnnouncements(content);
|
||||
|
||||
if (announcement === null) {
|
||||
return await reply.status(201).send({
|
||||
message: `Failed to generate announcements.`,
|
||||
});
|
||||
}
|
||||
|
||||
const { bluesky, discord, reddit, twitter } = announcement;
|
||||
const { title: discordTitle, content: discordContent } = discord;
|
||||
const { title: redditTitle, content: redditContent } = reddit;
|
||||
|
||||
await database.getInstance().announcements.create({
|
||||
data: {
|
||||
content,
|
||||
title,
|
||||
type,
|
||||
content: discordContent,
|
||||
title: discordTitle,
|
||||
type: type,
|
||||
},
|
||||
});
|
||||
|
||||
const discord = await announceOnDiscord(title, content, type);
|
||||
const reddit = await announceOnReddit(title, content, type);
|
||||
const summary = await summarisePost(title, content);
|
||||
if (summary === null) {
|
||||
return await reply.status(201).send({
|
||||
message: `Announcement processed. Discord: ${discord}, Reddit: ${reddit}, Bluesky: Skipped (AI summarisation failed), Twitter: Skipped (AI summarisation failed).`,
|
||||
});
|
||||
}
|
||||
if (summary.length > 280) {
|
||||
return await reply.status(201).send({
|
||||
message: `Announcement processed. Discord: ${discord}, Reddit: ${reddit}, Bluesky: Skipped (AI summary too long), Twitter: Skipped (AI summary too long).`,
|
||||
});
|
||||
}
|
||||
|
||||
const bluesky = await announceOnBluesky(summary);
|
||||
const twitter = await announceOnTwitter(summary);
|
||||
const discordPost = await announceOnDiscord(
|
||||
discordTitle,
|
||||
discordContent,
|
||||
type,
|
||||
);
|
||||
const redditPost = await announceOnReddit(
|
||||
redditTitle,
|
||||
redditContent,
|
||||
type,
|
||||
);
|
||||
const blueskyPost = await announceOnBluesky(bluesky);
|
||||
const twitterPost = await announceOnTwitter(twitter);
|
||||
return await reply.status(201).send({
|
||||
message: `Announcement processed. Discord: ${discord}, Reddit: ${reddit}, Bluesky: ${bluesky}, Twitter: ${twitter}`,
|
||||
message: `Announcement processed. Discord: ${discordPost}, Reddit: ${redditPost}, Bluesky: ${blueskyPost}, Twitter: ${twitterPost}`,
|
||||
rawPost: announcement,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user