post split text as reply

This commit is contained in:
René Schimmelpfennig 2024-04-15 20:46:53 +02:00
parent 6971629949
commit 3efbd21985
No known key found for this signature in database
GPG key ID: EABA8F8CCF842F0B

59
main.js
View file

@ -44,10 +44,17 @@ function saveLastProcessedPostId() {
} }
} }
async function postToBluesky(text, images) { async function createBlueskyMessage(text) {
const richText = new RichText({ text }); const richText = new RichText({ text });
await richText.detectFacets(agent); await richText.detectFacets(agent);
return {
text: richText.text,
facets: richText.facets
};
}
async function postToBluesky(textParts, images) {
const blueSkyImages = await images.reduce(async (list, {url, alt}) => { const blueSkyImages = await images.reduce(async (list, {url, alt}) => {
const imageResponse = await axios.get({ const imageResponse = await axios.get({
url, url,
@ -66,9 +73,8 @@ async function postToBluesky(text, images) {
]; ];
}, []); }, []);
await agent.post({ const rootMessageResponse = await agent.post({
text: richText.text, ...(await createBlueskyMessage(textParts[0])),
facets: richText.facets,
...(blueSkyImages.length > 0 ...(blueSkyImages.length > 0
? { ? {
embed: { embed: {
@ -79,19 +85,50 @@ async function postToBluesky(text, images) {
: {} : {}
), ),
}); });
if (textParts.length === 1) return;
let replyMessageResponse = null
for (let index = 1; index < textParts.length; index++) {
replyMessageResponse = await agent.post({
...(await createBlueskyMessage(textParts[index])),
reply: {
root: rootMessageResponse,
parent: replyMessageResponse ?? rootMessageResponse,
}
});
}
} }
function removeHtmlTags(input) { function removeHtmlTags(input) {
return input.replace(/<[^>]*>/g, ""); return input.replace(/<[^>]*>/g, "");
} }
function truncate(text, timestampId) { function splitText(text, maxLength) {
if (text.length > 300) { // Split the text by spaces
console.warn(`✂ post '${timestampId}' was truncated`) const words = text.split(" ");
return text.substring(0, 299) + '…'
let result = [];
let currentChunk = "";
for (const word of words) {
// Add the current word to the current chunk
const potentialChunk = `${currentChunk} ${word}`.trim();
if (potentialChunk.length <= maxLength) {
// If the current chunk is still under max length, add the word
currentChunk = potentialChunk;
} else {
// Otherwise, add the current chunk to the result and start a new chunk
result.push(currentChunk);
currentChunk = word;
}
} }
return text // Add the last chunk to the result
result.push(currentChunk);
return result;
} }
// Function to periodically fetch new Mastodon posts // Function to periodically fetch new Mastodon posts
@ -113,12 +150,12 @@ async function fetchNewPosts() {
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) { if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
try { try {
console.log('📧 posting to BlueSky', currentTimestampId) console.log('📧 posting to BlueSky', currentTimestampId)
const text = truncate(removeHtmlTags(item.object.content), currentTimestampId); const textParts = splitText(removeHtmlTags(item.object.content), 300);
const images = item.attachment const images = item.attachment
.filter(attachment => attachment.type === 'Document' && attachment.mediaType.startsWith('image/')) .filter(attachment => attachment.type === 'Document' && attachment.mediaType.startsWith('image/'))
.map(({name: alt, url}) => ({ alt, url })); .map(({name: alt, url}) => ({ alt, url }));
postToBluesky(text, images); postToBluesky(textParts, images);
if (currentTimestampId > newTimestampId) { if (currentTimestampId > newTimestampId) {
newTimestampId = currentTimestampId; newTimestampId = currentTimestampId;