From b5b4ced7ce92c6d0e0c5dd56c5a4802287034325 Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Thu, 13 Jul 2023 19:24:47 +0300 Subject: [PATCH] Added Lemmy => Mastodon for posts https://github.com/ManeraKai/fediredirect/issues/4 --- src/pages/lib/lemmy.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pages/lib/lemmy.js b/src/pages/lib/lemmy.js index b39ec69..9eeb24e 100644 --- a/src/pages/lib/lemmy.js +++ b/src/pages/lib/lemmy.js @@ -110,8 +110,7 @@ function lemmy_to_lemmy(url, options) { } function can_lemmy_to_mastodon(url, options) { - - for (const regexItem of [regex.userFederated, regex.userLocal, regex.communityFederated, regex.communityLocal]) { + for (const regexItem of [regex.userFederated, regex.userLocal, regex.communityFederated, regex.communityLocal, regex.post]) { if (url.pathname.match(regexItem)) { if (!options.mastodon) return 'credentials' return true @@ -143,6 +142,21 @@ function lemmy_to_mastodon(url, options) { resolve(`${utils.protocolHost(options.mastodon.instance)}/@${communityLocalRegex[1]}@${url.hostname}`) return } + + const localPostRegex = url.pathname.match(regex.post) + if (localPostRegex) { + const req = new XMLHttpRequest(); + req.open("GET", `${options.mastodon.instance}/api/v2/search?q=${encodeURIComponent(url.href)}&resolve=true&limit=1`, false); + req.setRequestHeader('Authorization', `Bearer ${options.mastodon.access_token}`) + req.onload = async () => { + const data = JSON.parse(req.responseText)['statuses'][0] + const post_id = data['id'] + const username = data['account']['username'] + resolve(`${utils.protocolHost(options.mastodon.instance)}/@${username}@${url.hostname}/${post_id}`) + } + req.send(); + return + } }) }