This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
burgernotes-web/static/js/homeserver.js

113 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2024-06-22 14:58:29 +01:00
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
2024-03-12 18:34:05 +00:00
let homeserverBox = document.getElementById("homeserverBox")
let statusBox = document.getElementById("statusBox")
let changeButton = document.getElementById("changeButton")
2024-06-25 21:03:50 +01:00
let backButton = document.getElementById("backButton")
2024-03-12 18:34:05 +00:00
let remote = localStorage.getItem("SETTING-homeServer")
if (remote == null) {
2024-09-24 19:49:46 +01:00
localStorage.setItem("SETTING-homeServer", "https://notes.ailur.dev")
remote = "https://notes.ailur.dev"
}
document.addEventListener("DOMContentLoaded", function() {
statusBox.innerText = "You are currently connected to: " + remote + ". "
});
2024-03-12 18:34:05 +00:00
function showElements(yesorno) {
if (!yesorno) {
homeserverBox.classList.add("hidden")
changeButton.classList.add("hidden")
inputContainer.classList.add("hidden")
2024-03-12 18:34:05 +00:00
}
else {
homeserverBox.classList.remove("hidden")
changeButton.classList.remove("hidden")
inputContainer.classList.remove("hidden")
2024-03-12 18:34:05 +00:00
}
}
changeButton.addEventListener("click", () => {
2024-03-12 18:34:05 +00:00
async function doStuff() {
let remote = homeserverBox.value
if (remote === "") {
2024-03-12 18:34:05 +00:00
statusBox.innerText = "A homeserver is required!"
return
}
showElements(false)
statusBox.innerText = "Connecting to homeserver..."
fetch(remote + "/api/versionjson")
2024-03-12 18:34:05 +00:00
.then((response) => response)
.then((response) => {
async function doStuff() {
if (response.status === 200) {
let version = await response.json()
let fetchClientVersion = await (await fetch("/static/version.txt")).text()
if (parseInt(version["versionnum"]) < parseInt(fetchClientVersion)) {
localStorage.setItem("legacy", "true")
}
2024-07-30 15:26:47 +01:00
localStorage.setItem("SETTING-homeServer", remote)
2024-03-12 18:34:05 +00:00
2024-03-15 17:01:55 +00:00
if (document.referrer !== "") {
window.location.href = document.referrer;
}
else {
window.location.href = "/login";
2024-03-15 17:01:55 +00:00
}
} else if (response.status === 404) {
let legacyHomeserverCheck = await fetch(remote + "/api/version")
if (legacyHomeserverCheck.status === 200) {
let homeserverText = await legacyHomeserverCheck.text()
let homeserverFloat = homeserverText.split(" ")[2]
let homeserverNameCheck = homeserverText.split(" ")[0]
if (homeserverNameCheck !== "Burgernotes") {
statusBox.innerText = "This homeserver is not compatible with Burgernotes!"
showElements(true)
return
}
let homeserverInt = parseFloat(homeserverFloat) * 100
if (homeserverInt < 200) {
localStorage.setItem("legacy", "true")
2024-07-30 15:26:47 +01:00
localStorage.setItem("SETTING-homeServer", remote)
if (document.referrer !== "") {
window.location.href = document.referrer;
}
else {
window.location.href = "/login";
}
} else if (homeserverInt > 200) {
localStorage.setItem("legacy", "false")
2024-07-30 15:26:47 +01:00
localStorage.setItem("SETTING-homeServer", remote)
if (document.referrer !== "") {
window.location.href = document.referrer;
}
else {
window.location.href = "/login";
}
} else {
statusBox.innerText = "This homeserver is not compatible with Burgernotes!"
showElements(true)
}
}
2024-03-12 18:34:05 +00:00
}
else {
statusBox.innerText = "This homeserver is not compatible with Burgernotes!"
2024-03-12 18:34:05 +00:00
showElements(true)
}
}
doStuff()
});
}
doStuff()
});
2024-06-22 14:58:29 +01:00
backButton.addEventListener("click", () => {
2024-06-25 21:03:50 +01:00
history.back()
});
// @license-end