From 17f06d4a606d798849ba6aca9cfc779957e4448d Mon Sep 17 00:00:00 2001 From: Elizabeth Cray Date: Thu, 6 Feb 2025 22:20:03 -0500 Subject: [PATCH] Create embeded link if no media --- README.md | 4 ++++ index.mjs | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1e1ae39..a78b8fa 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,7 @@ Don't forget to install dependencies with `npm i` and adjust the config by copyi Get the mastodon credentials from going to user preferences > development > and creating a new application. For bluesky credentials you have to make a new app password under settings > privacy and security > app passwords. This should ideally be run from a cron job (specifically `run.sh`), but can be run with just `node index.js`. + +## ToDo List + +* Cleanup code diff --git a/index.mjs b/index.mjs index 7c16c42..5062cb2 100644 --- a/index.mjs +++ b/index.mjs @@ -88,16 +88,12 @@ if (existsSync(historyFile)){ history += readFileSync(historyFile, 'UTF-8').split('\n'); } -const bskyPost = async (text, media = []) => { +const bskyPost = async (text, postUrl, media = []) => { let uploadedMedia = []; for (let m of media){ - // let mime = `${m.url}`.split('.').pop(); - // mime = mime.toLowerCase(); - // mime = mime === 'jpg'?'jpeg':mime; const fileBuffer = Buffer.from(readFileSync(m.data)); const { ext, mimeT } = await fileTypeFromBuffer(fileBuffer); let uploadResult = await bsky.uploadBlob(fileBuffer, { - // encoding: `image/${mime}` encoding: mimeT }); if (uploadResult.success){ @@ -124,6 +120,16 @@ const bskyPost = async (text, media = []) => { "$type": "app.bsky.embed.images", images: uploadedMedia } + } else { + // set embed to mastodon post + post.embed = { + "$type": "app.bsky.embed.external", + external: { + uri: postUrl, + title: `Post from @${process.env.MASTODON_USER}@${process.env.MASTODON_INSTANCE.substring(8)}`, + description: text + } + } } console.log(JSON.stringify(post)); let bskyPostData = await bsky.post(post); @@ -186,7 +192,7 @@ client.get(`/accounts/${process.env.MASTODON_ID}/statuses`, { }); } } - bskyPost(text, medias); + bskyPost(text, status.url, medias); appendFileSync(historyFile, `\n${status.id}`); } else { console.log(`ERROR: ${status.url} is too long and unable to be reposted`); @@ -194,7 +200,7 @@ client.get(`/accounts/${process.env.MASTODON_ID}/statuses`, { }else{ // is boosted post let text = status.reblog.url; - bskyPost(text, []); + bskyPost(text, status.url, []); appendFileSync(historyFile, `\n${status.id}`); } }