From 6971629949e466077b02d13a381d5df51f664523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Sun, 14 Apr 2024 20:48:00 +0200 Subject: [PATCH] support images --- main.js | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index fa5f6f2..27e2a1b 100644 --- a/main.js +++ b/main.js @@ -10,6 +10,10 @@ const mastodonUser = process.env.MASTODON_USER; // Bluesky agent const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT }); +await agent.login({ + identifier: process.env.BLUESKY_HANDLE, + password: process.env.BLUESKY_PASSWORD, +}); // File to store the last processed Mastodon post ID const lastProcessedPostIdFile = path.join( @@ -40,17 +44,40 @@ function saveLastProcessedPostId() { } } -async function postToBluesky(text) { - await agent.login({ - identifier: process.env.BLUESKY_HANDLE, - password: process.env.BLUESKY_PASSWORD, - }); - +async function postToBluesky(text, images) { const richText = new RichText({ text }); await richText.detectFacets(agent); + + const blueSkyImages = await images.reduce(async (list, {url, alt}) => { + const imageResponse = await axios.get({ + url, + method: 'GET', + responseType: 'blob', + }); + + const uploadResponse = await agent.uploadBlob(imageResponse.data); + + return [ + ...list, + { + alt, + image: uploadResponse.blob, + } + ]; + }, []); + await agent.post({ text: richText.text, facets: richText.facets, + ...(blueSkyImages.length > 0 + ? { + embed: { + type: 'app.bsky.embed.images', + images: blueSkyImages, + } + } + : {} + ), }); } @@ -87,7 +114,11 @@ async function fetchNewPosts() { try { console.log('📧 posting to BlueSky', currentTimestampId) const text = truncate(removeHtmlTags(item.object.content), currentTimestampId); - postToBluesky(text); + + const images = item.attachment + .filter(attachment => attachment.type === 'Document' && attachment.mediaType.startsWith('image/')) + .map(({name: alt, url}) => ({ alt, url })); + postToBluesky(text, images); if (currentTimestampId > newTimestampId) { newTimestampId = currentTimestampId;