diff options
Diffstat (limited to 'app/load.js')
-rw-r--r-- | app/load.js | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/app/load.js b/app/load.js new file mode 100644 index 0000000..9efff3c --- /dev/null +++ b/app/load.js @@ -0,0 +1,224 @@ +// noinspection JSUnresolvedVariable + +async function fetchPlus(resource, options = {}) { + const { timeout = 8000 } = options; + + const controller = new AbortController(); + const id = setTimeout(() => controller.abort(), timeout); + const response = await fetch(resource, { + ...options, + signal: controller.signal + }); + clearTimeout(id); + return response; +} + +window.connected = false; + +let valuesToGet = { + "pluralkit-cloudburst-members": { + url: "/api/data?f=ynmuc-members.json", + limited: false + }, + "pluralkit-cloudburst-fronters": { + url: "/api/data?f=ynmuc-fronters.json", + limited: false + }, + "pluralkit-cloudburst-switches": { + url: "/api/data?f=ynmuc-switches.json", + limited: false + }, + "pluralkit-raindrops-members": { + url: "/api/data?f=gdapd-members.json", + limited: false + }, + "pluralkit-raindrops-fronters": { + url: "/api/data?f=gdapd-fronters.json", + limited: false + }, + "pluralkit-raindrops-switches": { + url: "/api/data?f=gdapd-switches.json", + limited: false + }, + "peh-cloudburst-data": { + url: "/api/cloudburst-data", + limited: false + }, + "peh-raindrops-data": { + url: "/api/raindrops-data", + limited: false + }, + "peh-cloudburst-banners": { + url: "/api/cloudburst-banners", + limited: false + }, + "peh-raindrops-banners": { + url: "/api/raindrops-banners", + limited: false + }, + "identity": { + url: "/api/me", + limited: false + }, + "images": { + url: "/api/app-images", + limited: false + }, + "image-me": { + url: "/api/me-picture", + limited: false + }, + "bits-transactions": { + url: "/bits/Application/TransactionsList/index.php", + limited: false + }, + "bits-goal": { + url: "/bits/Application/GetGoal/index.php", + limited: false + }, + "bits-username": { + url: "/bits/Authentication/Username/index.php", + limited: false + } +} + +let keys = Object.keys(valuesToGet); +let index = 2; + +async function getNewValue() { + if (!keys[0]) { + await postLoad(); + return; + } + + try { + await localforage.setItem(keys[0], (await (await fetchPlus(valuesToGet[keys[0]]["url"], { timeout: 3000 })).text())); + keys.shift(); + + if (!keys[0]) { + await postLoad(); + return; + } + + setTimeout(async () => { + document.getElementById("progress-inner").style.width = ((index / Object.keys(valuesToGet).length) * 100) + "%"; + index++; + await getNewValue(); + }, valuesToGet[keys[0]]["limited"] ? 550 : 0); + } catch (e) { + for (let key of Object.keys(valuesToGet)) { + if (await localforage.getItem(key) === null) { + throw new Error("App requested key '" + key + "' but it can't be retrieved at the moment"); + } + } + + await postLoad(); + } +} + +async function load() { + await localforage.setItem("values-to-get", JSON.stringify(valuesToGet)); + + try { + let online = (await (await fetchPlus("/api/test", { timeout: 3000 })).text()); + if (online === "SUCCESS") window.connected = true; + } catch (e) {} + + await getNewValue(); +} + +async function postLoad() { + for (let key of await localforage.keys()) { + if (key !== "images") localStorage.setItem(key, await localforage.getItem(key)) + } + + localStorage.setItem("pluralkit-0", localStorage.getItem("pluralkit-raindrops-members")); + localStorage.setItem("pluralkit-1", localStorage.getItem("pluralkit-cloudburst-members")); + + window.data = {}; + + for (let value of Object.keys(valuesToGet)) { + try { + window.data[value] = JSON.parse(localStorage.getItem(value)); + } catch (e) { + window.data[value] = localStorage.getItem(value); + } + } + + window.currentFronter = window.data['pluralkit-' + window.data['identity'].id + '-fronters'].members[0] + + try { + let member = window.currentFronter; + await (await window.fetch("/bits/Application/SetCurrentIdentity/index.php?Name=" + btoa(member['display_name'] ?? member['name']).replaceAll("+", "-").replaceAll("/", "_") + "&Picture=" + btoa(member['avatar_url'] ?? "./assets/default.png").replaceAll("+", "-").replaceAll("/", "_"))).text(); + } catch (e) {} + + document.getElementById("home-name").innerText = getMiniName(window.currentFronter['display_name'] ?? window.currentFronter['name']); + document.getElementById("home-avatar").src = window.data['image-me']; + + switch (new Date().getHours()) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + document.getElementById("home-greeting").innerText = "Good night"; + break; + + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + document.getElementById("home-greeting").innerText = "Good morning"; + break; + + case 12: + case 13: + case 14: + case 15: + case 16: + case 17: + document.getElementById("home-greeting").innerText = "Good afternoon"; + break; + + case 18: + case 19: + case 20: + case 21: + case 22: + case 23: + document.getElementById("home-greeting").innerText = "Good evening"; + break; + } + + if (!window.connected) { + document.getElementById("home-app-planner").classList.add("disabled"); + document.getElementById("home-app-emergency").classList.add("disabled"); + } + + if (window.serviceWorkerRegistration.sync) { + window.serviceWorkerRegistration.sync.register('data-sync') + .catch(function(err) { + return err; + }) + .then(() => { + console.log("Scheduled background sync for the next time connection is available"); + }) + } else { + console.log("Background sync not possible on this platform"); + } + + window.currentMemberData = JSON.parse(localStorage.getItem("peh-" + JSON.parse(localStorage.getItem("identity")).id + "-banners"))[JSON.parse(localStorage.getItem("pluralkit-" + JSON.parse(localStorage.getItem("identity")).id + "-fronters")).members[0].name]; + + await refreshBanner(true); refreshTooltips(); + + setInterval(async () => { + Array.from(document.getElementsByClassName("relative-time")).forEach((el) => { + el.innerText = timeAgo(parseInt(el.getAttribute("data-relative-timestamp")) * 1000); + }) + }, 1000) + + document.getElementById("loader").style.display = "none"; +}
\ No newline at end of file |