From 7ba14d560fbca1f2178cd3068a3071eb3997c553 Mon Sep 17 00:00:00 2001 From: Nic Lake Date: Fri, 24 May 2024 20:30:26 -0500 Subject: [PATCH] Utilizing Webfinger to pull avatars from Mastodon --- src/components/Person.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/Person.js b/src/components/Person.js index 457b71a4..0df7718c 100644 --- a/src/components/Person.js +++ b/src/components/Person.js @@ -9,15 +9,27 @@ export default function Person({ person }) { const twitter = person.twitter ? `https://unavatar.io/${person.twitter.replace('@', '')}` : null; - const mastodonArr = person.mastodon - ? person.mastodon.replace('@', '').split('@') - : null; const website = `https://unavatar.io/${url.host}`; const unavatar = person.twitter ? `${twitter}?fallback=${website}&ttl=28d` : website; // const img = `https://images.weserv.nl/?url=${unavatar}&w=100&l=9&af&il&n=-1`; - const img = unavatar; + const mastodonArr = person.mastodon + ? person.mastodon.replace('@', '').split('@') + : null; + const webfinger = + person.mastodon + ? JSON.parse(`https://${mastodonArr[0]}/.well-known/webfinger?resource=https://${mastodonArr[0]}/@${mastodonArr[1]}`) + : null; + const wfIndex = + person.mastodon && webfinger + ? webfinger.links.findIndex((link) => link.rel === 'http://webfinger.net/rel/avatar') + : null; + const wfAvatar = + person.mastodon && wfIndex + ? webfinger.links[wfIndex].href + : website; + const img = person.twitter ? unavatar : (person.mastodon ? wfAvatar : website); const { tag: currentTag } = useParams(); return (