async function like(id) { const formData = new FormData(); formData.append('post', id); const result = await fetch('/like', { method: 'POST', body: formData }); if (result.status === 401) { window.location.href = "/login"; return; } if (result.status < 200 || result.status >= 400) { alert("your post didnt get the like because the server thought your opinion was invalid"); return; } } async function post() { const postbox = document.getElementById('newpost'); const formData = new FormData(); formData.append('post', postbox.value); const result = await fetch('/post', { method: 'POST', body: formData }); if (result.status < 200 || result.status >= 400) { alert("you have been silenced by the server and your post did not go through"); return; } window.location.href = "/"; } async function register() { const usernameBox = document.getElementById('username'); const passwordBox = document.getElementById('password'); const formData = new FormData(); formData.append('username', usernameBox.value); formData.append('password', passwordBox.value); const result = await fetch('/register', { method: 'POST', body: formData }); if (result.status < 200 || result.status >= 400) { alert(await result.text()); return; } window.location.href = "/"; } async function login() { const usernameBox = document.getElementById('username'); const passwordBox = document.getElementById('password'); const formData = new FormData(); formData.append('username', usernameBox.value); formData.append('password', passwordBox.value); const result = await fetch('/login', { method: 'POST', body: formData }); if (result.status < 200 || result.status >= 400) { alert("for some reason the server hates you and didn't let you log into your account"); return; } window.location.href = "/"; } async function checkLoginStatus() { const result = await fetch('/me'); if (result.headers.get('X-Logged-In') !== 'true') { return { username: "", bio: "" }; } const obj = { username: result.headers.get('X-Username'), bio: result.headers.get('X-Bio') }; return obj; } document.addEventListener('DOMContentLoaded', async function() { if (window.fetch) { const userStatus = await checkLoginStatus(); // update nav link / redirect regardless of whether a postbox exists const loginLink = document.getElementById("loginLink"); if (userStatus.username === "") { if (window.location.pathname === "/settings") { window.location.href = "/login"; return; } } else if (loginLink) { loginLink.textContent = "hello, " + userStatus.username + "!"; loginLink.href = "/profile/" + userStatus.username; } if (userStatus.username !== "" && window.location.pathname === "/settings") { // fill bio box with the bio const textarea = document.getElementById("change-bio"); textarea.value = userStatus.bio; } const postbox = document.getElementById("postbox"); if (!postbox) { return; } // postbox-specific setup stays here if (userStatus.username === "") { const newElement = document.createElement("p"); newElement.textContent = "Welcome to Chookchat!\nTo get posting, click 'login' to log in or create an account.\nEnjoy your stay!"; postbox.appendChild(newElement); } else { const helperText = document.createElement("p"); helperText.textContent = "You're posting as " + userStatus.username; postbox.appendChild(helperText); const textArea = document.createElement("textarea"); textArea.id = "newpost"; postbox.appendChild(textArea); const postButton = document.createElement("button"); postButton.textContent = "Post"; postButton.id = "postbutton"; postButton.onclick = post; postbox.appendChild(postButton); } const form = document.getElementById("postbox-form"); if (!form) { return; } form.style.display = 'none'; } }); async function settingsChangePassword() { const oldpassword = document.getElementById("change-password-oldpassword"); const newpassword = document.getElementById("change-password"); const formData = new FormData(); formData.append('oldpassword', oldpassword.value); formData.append('newpassword', newpassword.value); const result = await fetch('/settings/changePassword', { method: "POST", body: formData }); if (result.status < 200 || result.status >= 400) { alert("for whatever reason your password wasn't changed"); } else { alert("your password was changed!"); } } async function settingsChangeUsername() { const password = document.getElementById("change-username-password"); const newusername = document.getElementById("change-username"); const formData = new FormData(); formData.append('password', password.value); formData.append('newusername', newusername.value); const result = await fetch('/settings/changeUsername', { method: "POST", body: formData }); if (result.status < 200 || result.status >= 400) { alert("for whatever reason your username wasn't changed"); } else { alert("your username was changed!"); } } async function settingsChangeBio() { const bio = document.getElementById("change-bio"); const formData = new FormData(); formData.append('bio', bio.value); const result = await fetch('/settings/changeBio', { method: "POST", body: formData }); if (result.status < 200 || result.status >= 400) { alert("for whatever reason your bio wasn't changed"); } else { alert("your bio was changed!"); } } async function invalidateAllSessions() { const formData = new FormData(); formData.append('a', 'a'); const result = await fetch('/settings/invalidateAllSessions', { method: "POST", body: formData }); if (result.status < 200 || result.status >= 400) { alert("for whatever reason your sessions weren't invalidated"); } else { alert("your sessions were invalidated!"); } }