Instance software autodetection

This commit is contained in:
Natty 2023-07-20 01:52:39 +02:00
parent d52bf65d07
commit 83f56e7ab5
No known key found for this signature in database
GPG key ID: BF6CB659ADEE60EC
2 changed files with 62 additions and 11 deletions

View file

@ -148,6 +148,11 @@ class ApiClient {
* return {Promise<FediUser[] | null>} * return {Promise<FediUser[] | null>}
*/ */
async getFavs(note) { throw new Error("Not implemented"); } async getFavs(note) { throw new Error("Not implemented"); }
/**
* @return string
*/
getClientName() { throw new Error("Not implemented"); }
} }
class MastodonApiClient extends ApiClient { class MastodonApiClient extends ApiClient {
@ -254,6 +259,10 @@ class MastodonApiClient extends ApiClient {
handle: parseHandle(user["acct"], note.instance) handle: parseHandle(user["acct"], note.instance)
})); }));
} }
getClientName() {
return "mastodon";
}
} }
class MisskeyApiClient extends ApiClient { class MisskeyApiClient extends ApiClient {
@ -391,6 +400,10 @@ class MisskeyApiClient extends ApiClient {
handle: parseHandle(reaction["user"]["username"], reaction["user"]["host"]) handle: parseHandle(reaction["user"]["username"], reaction["user"]["host"])
})); }));
} }
getClientName() {
return "misskey";
}
} }
/** @type Map<string, ApiClient> */ /** @type Map<string, ApiClient> */
@ -420,20 +433,51 @@ function parseHandle(fediHandle, fallbackInstance = "") {
*/ */
async function circleMain() { async function circleMain() {
document.getElementById("btn_create").style.display = "none";
let fediHandle = document.getElementById("txt_mastodon_handle").value;
const selfUser = parseHandle(fediHandle);
const client = await ApiClient.getClient(selfUser.instance);
let progress = document.getElementById("outInfo"); let progress = document.getElementById("outInfo");
const generateBtn = document.getElementById("generateButton");
generateBtn.style.display = "none";
let fediHandle = document.getElementById("txt_mastodon_handle");
const selfUser = parseHandle(fediHandle.value);
let form = document.getElementById("generateForm");
let backend = form.backend;
for (const radio of backend) {
radio.disabled = true;
}
fediHandle.disabled = true;
let client;
switch (backend.value) {
case "mastodon":
client = new MastodonApiClient(selfUser.instance);
break;
case "misskey":
client = new MisskeyApiClient(selfUser.instance);
break;
default:
progress.innerText = "Detecting instance...";
client = await ApiClient.getClient(selfUser.instance);
backend.value = client.getClientName();
break;
}
progress.innerText = "Fetching your user..."; progress.innerText = "Fetching your user...";
const user = await client.getUserIdFromHandle(selfUser); const user = await client.getUserIdFromHandle(selfUser);
if (!user) { if (!user) {
alert("Something went horribly wrong, couldn't fetch your user."); alert("Something went horribly wrong, couldn't fetch your user.");
fediHandle.disabled = false;
for (const radio of backend) {
radio.disabled = false;
}
generateBtn.style.display = "inline";
progress.innerText = "";
return; return;
} }

View file

@ -14,10 +14,17 @@
<h1>Trötpty</h1> <h1>Trötpty</h1>
<h3><span style="text-decoration: line-through" aria-hidden="true">Mastodon</span> Fedi Circle Creator</h3> <h3><span style="text-decoration: line-through" aria-hidden="true">Mastodon</span> Fedi Circle Creator</h3>
<!-- TODO Logo? --> <!-- TODO Logo? -->
<input id="txt_mastodon_handle" type="text" onchange="document.getElementById('btn_create').style = 'display: inline;'; document.getElementById('btn_download').style = 'display: none;'" placeholder="@sonnenbrandi@mieke.club"> <form id="generateForm" onsubmit="(async () => await circleMain())(); return false;">
<br><br> <input id="txt_mastodon_handle" type="text" onchange="document.getElementById('btn_create').style = 'display: inline;'; document.getElementById('btn_download').style = 'display: none;'" placeholder="@sonnenbrandi@mieke.club">
<!-- Buttons --> <br><br>
<button id="btn_create" onclick="(async () => await circleMain())()">Generate circle</button> <span>
<label><input type="radio" name="backend" value="detect" checked> Autodetect</label>
<label><input type="radio" name="backend" value="masto"> Mastodon API</label>
<label><input type="radio" name="backend" value="misskey"> Misskey API</label>
</span>
<br>
<button type="submit" id="generateButton">Generate circle</button>
</form>
<span id="outInfo"></span> <span id="outInfo"></span>
<a href="" id="btn_download" class="button" download="mastodon-circle.png" style="display: none;">DOWNLOAD (klappt wsl nicht)</a> <a href="" id="btn_download" class="button" download="mastodon-circle.png" style="display: none;">DOWNLOAD (klappt wsl nicht)</a>
<!-- <button id="btn_download" onClick="downloadImage()" style="display: none;">DOWNLOAD</button> --> <!-- <button id="btn_download" onClick="downloadImage()" style="display: none;">DOWNLOAD</button> -->