generated from nhcarrigan/template
feat: chunk long announcements for Discord, Reddit, and Discourse
✨ This commit was made with love from Hikari~ 🌸
This commit is contained in:
@@ -4,10 +4,13 @@
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
/* eslint-disable @typescript-eslint/naming-convention -- we are making raw API calls. */
|
||||
/* eslint-disable no-await-in-loop -- Sequential chunk posting requires awaiting each request. */
|
||||
|
||||
import { chunkContent } from "../utils/chunkContent.js";
|
||||
import type { AnnouncementType } from "../interfaces/announcementType.js";
|
||||
|
||||
const announcementCategoryId = 16;
|
||||
const discourseLimit = 32_000;
|
||||
|
||||
const tags: Record<AnnouncementType, string> = {
|
||||
community: "Community",
|
||||
@@ -17,6 +20,7 @@ const tags: Record<AnnouncementType, string> = {
|
||||
|
||||
/**
|
||||
* Posts an announcement to the NHCarrigan Discourse support forum.
|
||||
* Sends overflow content as sequential replies if it exceeds the 32,000 character limit.
|
||||
* @param title - The title of the announcement.
|
||||
* @param content - The main body of the announcement in markdown.
|
||||
* @param type - Whether the announcement is for a product, community, or company.
|
||||
@@ -31,10 +35,12 @@ export const announceOnDiscourse = async(
|
||||
return "Discourse API key is not set.";
|
||||
}
|
||||
|
||||
const chunks = chunkContent(content, discourseLimit);
|
||||
|
||||
const response = await fetch("https://support.nhcarrigan.com/posts.json", {
|
||||
body: JSON.stringify({
|
||||
category: announcementCategoryId,
|
||||
raw: content,
|
||||
raw: chunks[0],
|
||||
tags: [ tags[type] ],
|
||||
title: title,
|
||||
}),
|
||||
@@ -50,5 +56,34 @@ export const announceOnDiscourse = async(
|
||||
return `Failed to post to Discourse. Status: ${response.status.toString()} ${response.statusText}`;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept generic.
|
||||
const data = (await response.json()) as { topic_id?: number };
|
||||
|
||||
for (const chunk of chunks.slice(1)) {
|
||||
if (data.topic_id === undefined) {
|
||||
return "Failed to retrieve Discourse topic ID for continuation posts.";
|
||||
}
|
||||
|
||||
const replyResponse = await fetch(
|
||||
"https://support.nhcarrigan.com/posts.json",
|
||||
{
|
||||
body: JSON.stringify({
|
||||
raw: chunk,
|
||||
topic_id: data.topic_id,
|
||||
}),
|
||||
headers: {
|
||||
"Api-Key": process.env.FORUM_API_KEY,
|
||||
"Api-Username": "hikari",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
},
|
||||
);
|
||||
|
||||
if (!replyResponse.ok) {
|
||||
return `Failed to post continuation chunk to Discourse. Status: ${replyResponse.status.toString()} ${replyResponse.statusText}`;
|
||||
}
|
||||
}
|
||||
|
||||
return "Successfully posted announcement to Discourse~! ✨";
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user