diff --git a/views/projects/item/fediverse-madness/index.html b/views/projects/item/fediverse-madness/index.html index 6114c94..4716764 100644 --- a/views/projects/item/fediverse-madness/index.html +++ b/views/projects/item/fediverse-madness/index.html @@ -130,8 +130,8 @@ diff --git a/views/projects/item/fediverse-madness/scripts/game.js b/views/projects/item/fediverse-madness/scripts/game.js index 580ac23..0c054e9 100644 --- a/views/projects/item/fediverse-madness/scripts/game.js +++ b/views/projects/item/fediverse-madness/scripts/game.js @@ -10,6 +10,8 @@ function escapeHtml(unsafe) .replace(/<\/?object\/?>/g, ""); } +const n = ()=>null; + const el_id_user = document.querySelector("#user"); const el_id_instance = document.querySelector("#instance"); const el_id_whoami = document.querySelector("#whoami"); @@ -33,29 +35,31 @@ async function verify() { const instance = el_id_instance.value.replace(/@/gim, ""); el_id_user.value = username; el_id_instance.value = instance; - el_id_submitwhoamiFollowers.disabled = true; - el_id_submitwhoamiFollowing.disabled = true; if (username.length < 1) return null; if (instance.length < 1) return null; - let user_req; - try { - user_req = await fetch(`https://${instance}/api/v1/accounts/lookup?acct=${username}`); - } catch (err) { - return null; - } + const webf_req = await fetch(`https://${instance}/.well-known/webfinger?resource=acct:${username}@${instance}`) + .catch(n); // who cares + if (webf_req == null) return null; + const webf_json = await webf_req.json(); + const user_req = await fetch(webf_json.links.find(x => x.type == "application/activity+json").href, { + headers: {"Accept": "application/activity+json"} + }) + .catch(n); // who cares + if (user_req == null) return null; const user_json = await user_req.json(); - el_id_submitwhoamiFollowers.disabled = false; - el_id_submitwhoamiFollowing.disabled = false; - if (user_req.ok) return { - USER_ID: user_json.id, - INSTANCE: instance - }; - else return null; + return { + FOLLOWERS_LINK: user_json.followers, + FOLLOWING_LINK: user_json.following + } } el_id_submitwhoamiFollowers.addEventListener("click", async (e) => { el_id_errorwhoami.innerHTML = ""; + el_id_submitwhoamiFollowers.disabled = true; + el_id_submitwhoamiFollowing.disabled = true; const result = await verify(); + el_id_submitwhoamiFollowers.disabled = false; + el_id_submitwhoamiFollowing.disabled = false; if (result == null) { el_id_errorwhoami.innerHTML = "Invalid user!"; return false; @@ -76,10 +80,7 @@ el_id_submitwhoamiFollowing.addEventListener("click", async (e) => { el_id_errorwhoami.innerHTML = "Invalid user!"; return false; } - game = { - ...game, - ...result - }; + game = result; gamemodeFollowers = false; getFollowing(); @@ -103,10 +104,10 @@ let lastId = ""; let selectedUsers = []; function renderNameHTML(name, user) { - return escapeHtml(name).replace(/:([a-z0-9_-]+?):/gim, (m, p1) => { - const emoji = user.emojis.find(x => x.shortcode == p1); + return escapeHtml(name).replace(/(:[a-z0-9_-]+?:)/gim, (m, p1) => { + const emoji = user.emojis.find(x => x.name == p1); if (emoji == null) return null; - return ``; + return ``; }); } @@ -119,16 +120,32 @@ async function getFollowers(dontLoadNew = false) { let out; if (!dontLoadNew) { el_id_loadingfollowers.innerHTML = `Retrieving followers... Please wait.`; - res = await fetch(`https://${game.INSTANCE}/api/v1/accounts/${game.USER_ID}/followers${lastId.length > 0 ? `?max_id=${lastId}` : ""}`); + res = await fetch(game.FOLLOWERS_LINK); out = await res.json(); } el_id_submitfollowers.disabled = false; el_id_morefollowers.disabled = false; if (dontLoadNew) return false; - if (out.length < 1) { + if (typeof out.first === "object" && Array.isArray(out.first.orderedItems) && out.first.orderedItems.length < 1) { el_id_loadingfollowers.innerHTML = `No followers found.`; return false; } + const unsanitizedUsers = await Promise.all(out.first.orderedItems.map(async (x) => { + const fetcher = await fetch(x, { + headers: {"Accept": "application/activity+json"} + }).catch(n); + return + })); + console.log(unsanitizedUsers); + userList.push(...unsanitizedUsers.map(user => ({ + fqn: user.preferredUsername + "@" + new URL(user.url).hostname, + avatar: user.icon.url, + display_name: user.name, + emojis: user.tag.filter(x => x.type == "Emoji"), + fields: user.attachment, + note: user.summary, + url: user.url + }))); el_id_listfollowers.hidden = false; el_id_listfollowers.innerHTML += out.reduce((pv, cuser) => { return pv + ` @@ -142,19 +159,6 @@ async function getFollowers(dontLoadNew = false) { @${cuser.fqn || cuser.acct} ` }, ""); - userList.push(...out.map(user => ({ - fqn: user.fqn || user.acct, - avatar: user.avatar, - bot: user.bot, - created_at: user.created_at, - display_name: user.display_name, - emojis: user.emojis, - fields: user.fields, - id: user.id, - note: user.note, - username: user.url, - username: user.username - }))); lastId = userList.at(-1).id; selectboxes = Array.from(el_id_listfollowers.querySelectorAll(".follower_checkbox")); selectboxes.forEach(x => x.addEventListener("change", checkSelectedAmtFollowers));