Skip to content

Commit

Permalink
Added Lemmy => Mastodon for posts #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Jul 13, 2023
1 parent 33e4d34 commit b5b4ced
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/pages/lib/lemmy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
})
}

Expand Down

0 comments on commit b5b4ced

Please sign in to comment.