feat: add error-handling for Bluesky posting; truncate messages with more than 300 chars (#7)
This commit is contained in:
parent
710c8b5089
commit
032677e371
1 changed files with 20 additions and 6 deletions
22
main.js
22
main.js
|
@ -58,6 +58,15 @@ function removeHtmlTags(input) {
|
||||||
return input.replace(/<[^>]*>/g, "");
|
return input.replace(/<[^>]*>/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function truncate(text, timestampId) {
|
||||||
|
if (text.length > 300) {
|
||||||
|
console.warn(`✂ post '${timestampId}' was truncated`)
|
||||||
|
return text.substring(0, 299) + '…'
|
||||||
|
}
|
||||||
|
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
// Function to periodically fetch new Mastodon posts
|
// Function to periodically fetch new Mastodon posts
|
||||||
async function fetchNewPosts() {
|
async function fetchNewPosts() {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
|
@ -74,13 +83,18 @@ async function fetchNewPosts() {
|
||||||
reversed.forEach((item) => {
|
reversed.forEach((item) => {
|
||||||
const currentTimestampId = Date.parse(item.published);
|
const currentTimestampId = Date.parse(item.published);
|
||||||
|
|
||||||
|
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
|
||||||
|
try {
|
||||||
|
console.log('📧 posting to BlueSky', currentTimestampId)
|
||||||
|
const text = truncate(removeHtmlTags(item.object.content), currentTimestampId);
|
||||||
|
postToBluesky(text);
|
||||||
|
|
||||||
if (currentTimestampId > newTimestampId) {
|
if (currentTimestampId > newTimestampId) {
|
||||||
newTimestampId = currentTimestampId;
|
newTimestampId = currentTimestampId;
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
|
console.error('🔥 can\'t post to Bluesky', currentTimestampId, error)
|
||||||
const text = removeHtmlTags(item.object.content);
|
}
|
||||||
postToBluesky(text);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue