Add some very basic fediverse integration
All checks were successful
/ weblog.lol (push) Successful in 12s

It doesn't go as far as I wanted, because I don't
know how to get the counts I need as yet.
This commit is contained in:
Gordon Pedersen 2024-05-11 12:43:50 +10:00
parent 3005376521
commit d929f8935b
4 changed files with 97 additions and 36 deletions

View file

@ -29,6 +29,10 @@
<a class="status_url" href="{status_url}"><i class="omg-icon omg-prami"></i> via status.lol</a> <a class="status_url" href="{status_url}"><i class="omg-icon omg-prami"></i> via status.lol</a>
<a class="external_url" href="{external_url}"><i class="omg-icon omg-fediverse"></i> Interact on the Fediverse</a> <a class="external_url" href="{external_url}"><i class="omg-icon omg-fediverse"></i> Interact on the Fediverse</a>
</aside> </aside>
<aside class="post-info">
<a class="status_url" href="https://deathau.status.lol/663cb4853797d"><i class="omg-icon omg-prami"></i> via status.lol</a>
<a class="external_url" href="https://monrepos.casa/objects/0e03068e-3466-3cb4-8207-08c267219144"><i class="omg-icon omg-fediverse"></i> Reply on the Fediverse</a>
</aside>
<aside class="post-tags"> <aside class="post-tags">
{tags} {tags}
</aside> </aside>

1
fediverse.js Symbolic link
View file

@ -0,0 +1 @@
./weblog/B0. Template/fediverse.js.md

View file

@ -6,14 +6,9 @@ Location: /fediverse.js
/**/ /**/
const SUBSCRIBE_LINK_REL = 'http://ostatus.org/schema/1.0/subscribe' const SUBSCRIBE_LINK_REL = 'http://ostatus.org/schema/1.0/subscribe'
function share(uri, handle) { async function getSubscribeTemplate() {
if(!handle){ const input = prompt("Please enter your fediverse / mastodon handle (e.g. '@user@domain.social')", "@")
handle = prompt("Please enter your fediverse / mastodon handle (e.g. '@user@domain.social')\n(remember to look for blocked pop-ups)", "@") let handle = input.trim().replace(/^@/,'')
}
if(handle) {
const input = handle
handle = handle.trim().replace(/^@/,'')
const split = handle.split('@') const split = handle.split('@')
if(split.length == 2) { if(split.length == 2) {
const resource = `acct:${handle}` const resource = `acct:${handle}`
@ -21,31 +16,92 @@ function share(uri, handle) {
// look up remote user via webfinger // look up remote user via webfinger
const url = `https://${domain}/.well-known/webfinger?resource=${resource}` const url = `https://${domain}/.well-known/webfinger?resource=${resource}`
fetch(url, {headers: { return fetch(url, {headers: {
'Content-Type': 'application/activity+json' 'Content-Type': 'application/json'
}}).then(async result => { }}).then(async result => {
const json = await result.json() const json = await result.json()
const subscribe = json.links.find(link => link.rel && link.rel == SUBSCRIBE_LINK_REL) const subscribe = json.links.find(link => link.rel && link.rel == SUBSCRIBE_LINK_REL)
let template = subscribe.template let template = subscribe.template
window.open(template.replace("{uri}", uri), '_blank').focus() return template
}) })
.catch(e => { .catch(e => {
console.error(e) console.error(e)
throw `Sorry, we couldn't find an uri for ${input}.\n\nTry searching for "${uri}" on ${domain} (or in your fediverse client of choice)` alert(`Sorry, we couldn't find an uri for ${input}.\n\nTry searching for "${uri}" on ${domain} (or in your fediverse client of choice)`)
return null
}) })
} }
else { else {
throw 'Please enter your fediverse address in @user@domain.social format' alert('Please enter your fediverse address in @user@domain.social format')
return null
}
return null
}
function linkToUrl(uri) {
// window.open(uri, '_blank').focus()
const link = document.createElement('a')
link.href = uri
link.target = "_blank"
link.click()
}
function noDataCick(href) {
return async (e) => {
e.preventDefault()
let subscribe_template = await getSubscribeTemplate()
if(subscribe_template) {
localStorage.setItem('subscribe_template', subscribe_template)
const url = subscribe_template.replace("{uri}", href)
linkToUrl(url)
addSocialButtons(this, url)
}
else {
linkToUrl(href)
} }
} }
} }
function addSocialButtons(el, href) {
// TODO: fetch the post from the fediverse to add in comment, like and retweet count
// the following fetches the activitypub object but has no reference to the likes, etc
// if I had the local id, I could use the mastodon API to get it, but friendica uses the diaspora:guid in the link
// const url = new URL(href).searchParams.get("uri")
// fetch(url, {
// headers: {
// 'Accept': 'application/json'
// }
// }).then(async result => {
// const json = await result.text()
// console.log(json)
// })
// .catch(e => {
// console.error(e)
// })
const innerHTML = `
<a href='${href}' target="_blank"><i class="fa fa-comment"></i></a>
<a href='${href}' target="_blank"><i class="fa fa-star"></i></a>
<a href='${href}' target="_blank"><i class="fa fa-retweet"></i></a>
`
const span = document.createElement('span')
span.classList.add('fediverse')
span.innerHTML = innerHTML
el.insertAdjacentElement('afterend', span)
el.remove()
}
function fediverse() { function fediverse() {
let subscribe_template = localStorage.getItem('subscribe_template');
document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => { document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => {
el.addEventListener('click', e => { if(subscribe_template) {
e.preventDefault() console.log(subscribe_template.replace("{uri}", el.href))
share(el.href) addSocialButtons(el, subscribe_template.replace("{uri}", el.href))
}) }
else {
el.addEventListener('click', noDataCick(el.href))
}
}) })
} }

View file

@ -93,7 +93,7 @@ main.page {
margin-left: .75em; margin-left: .75em;
} }
.post-info>a { .post-info a {
margin-left: 1em; margin-left: 1em;
background: none; background: none;
} }