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

111 lines
4.6 KiB
JavaScript
Raw 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("homeserverURL")
if (remote == null) {
localStorage.setItem("homeserverURL", "https://notes.hectabit.org")
remote = "https://notes.hectabit.org"
}
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")
}
else {
homeserverBox.classList.remove("hidden")
changeButton.classList.remove("hidden")
}
}
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-03-12 18:34:05 +00:00
localStorage.setItem("homeserverURL", remote)
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")
localStorage.setItem("homeserverURL", remote)
if (document.referrer !== "") {
window.location.href = document.referrer;
}
else {
window.location.href = "/login";
}
} else if (homeserverInt > 200) {
localStorage.setItem("legacy", "false")
localStorage.setItem("homeserverURL", 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