generated from nhcarrigan/template
feat: more automated announcements (#8)
### Explanation Makes my life so much easier. ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning Minor - My pull request introduces a new non-breaking feature. Reviewed-on: #8 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #8.
This commit is contained in:
@@ -8,10 +8,14 @@ import { blockedIps } from "../cache/blockedIps.js";
|
||||
import { database } from "../db/database.js";
|
||||
import { announceOnBluesky } from "../modules/announceOnBluesky.js";
|
||||
import { announceOnDiscord } from "../modules/announceOnDiscord.js";
|
||||
import { announceOnFacebook } from "../modules/announceOnFacebook.js";
|
||||
import { announceOnMastodon } from "../modules/announceOnMastodon.js";
|
||||
import { announceOnReddit } from "../modules/announceOnReddit.js";
|
||||
import { announceOnThreads } from "../modules/announceOnThreads.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, isValidString } from "../utils/typeguards.js";
|
||||
import type { FastifyPluginAsync } from "fastify";
|
||||
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
@@ -30,21 +34,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) {
|
||||
@@ -59,53 +68,62 @@ 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.",
|
||||
});
|
||||
}
|
||||
|
||||
if (type !== "products" && type !== "community") {
|
||||
if (!isAnnouncementType(type)) {
|
||||
return await reply.status(400).send({
|
||||
error:
|
||||
"Invalid announcement type. Available types: products, community.",
|
||||
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 {
|
||||
markdown,
|
||||
plaintext,
|
||||
threaded,
|
||||
} = announcement.response;
|
||||
const { title: markdownTitle, content: markdownContent } = markdown;
|
||||
|
||||
await database.getInstance().announcements.create({
|
||||
data: {
|
||||
content,
|
||||
title,
|
||||
type,
|
||||
content: markdownContent,
|
||||
title: markdownTitle,
|
||||
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(
|
||||
markdownTitle,
|
||||
markdownContent,
|
||||
type,
|
||||
);
|
||||
const redditPost = await announceOnReddit(
|
||||
markdownTitle,
|
||||
markdownContent,
|
||||
type,
|
||||
);
|
||||
const blueskyPost = await announceOnBluesky(threaded);
|
||||
const twitterPost = await announceOnTwitter(threaded);
|
||||
const facebookPost = await announceOnFacebook(plaintext);
|
||||
const threadsPost = await announceOnThreads(threaded);
|
||||
const mastodonPost = await announceOnMastodon(threaded);
|
||||
return await reply.status(201).send({
|
||||
message: `Announcement processed. Discord: ${discord}, Reddit: ${reddit}, Bluesky: ${bluesky}, Twitter: ${twitter}`,
|
||||
alert: `Please remember to manually post to: LinkedIn, Peerlist, Ko-fi, and Patreon.`,
|
||||
cost: announcement.cost,
|
||||
message: `Announcement processed. Discord: ${discordPost}, Reddit: ${redditPost}, Bluesky: ${blueskyPost}, Twitter: ${twitterPost}, Facebook: ${facebookPost}, Threads: ${threadsPost}, Mastodon: ${mastodonPost}`,
|
||||
rawPost: announcement.response,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -8,7 +8,7 @@ import { blockedIps } from "../cache/blockedIps.js";
|
||||
import { database } from "../db/database.js";
|
||||
import { getIpFromRequest } from "../modules/getIpFromRequest.js";
|
||||
import { getSanctionComponents } from "../modules/getSanctionComponents.js";
|
||||
import { isValidString } from "../utils/isValidString.js";
|
||||
import { isValidString } from "../utils/typeguards.js";
|
||||
import type { FastifyPluginAsync } from "fastify";
|
||||
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
|
||||
Reference in New Issue
Block a user