diff --git a/frontend/css/style.css b/frontend/css/style.css index be1e45e..4ef8bbc 100644 --- a/frontend/css/style.css +++ b/frontend/css/style.css @@ -844,3 +844,104 @@ input { transition: width 0.4s ease-out; } } + +/* ---- Sharing & profile ---------------------------------------------------- */ +.sharing { + max-width: 44rem; +} + +.share-switch { + display: flex; + align-items: center; + gap: 0.75rem; + font-size: var(--step-1); + font-weight: 650; + margin-bottom: 1rem; +} + +.share-switch input { + width: 1.4rem; + height: 1.4rem; + accent-color: var(--ok); +} + +.handle-panel { + margin-bottom: 2rem; +} + +.handle { + font-size: clamp(1.1rem, 4vw, var(--step-2)); + margin: 0.4rem 0 0.75rem; +} + +.handle-panel .notice { + margin-top: 1rem; +} + +.choices { + border: 0; + border-top: 1px solid var(--rule); + margin: 0 0 1.5rem; + padding: 1.25rem 0 0; + min-inline-size: 0; +} + +.choices legend { + font-weight: 650; + font-size: var(--step-1); + padding: 0 0.5rem 0 0; +} + +.choice { + display: flex; + gap: 0.75rem; + align-items: flex-start; + margin-bottom: 0.9rem; +} + +.choice input { + margin-top: 0.3rem; + accent-color: var(--ink); +} + +.choice label { + display: grid; + gap: 0.1rem; +} + +.choice label .muted { + font-size: var(--step--1); +} + +.choices .hint { + color: var(--ink-soft); + font-size: var(--step--1); + margin-top: 0.5rem; +} + +.field textarea { + min-height: 5rem; + padding: 0.5rem 0.75rem; + border: 1px solid var(--rule); + border-radius: var(--radius-control); + background: var(--paper-raised); + font: inherit; + color: inherit; + resize: vertical; +} + +.field textarea:focus-visible { + border-color: var(--focus); +} + +.profile-bio { + white-space: pre-line; +} + +.profile-follow { + margin: 2rem 0 0.4rem; +} + +.save-row { + align-items: center; +} diff --git a/frontend/js/layout.js b/frontend/js/layout.js index ff2a6e6..feb134d 100644 --- a/frontend/js/layout.js +++ b/frontend/js/layout.js @@ -20,6 +20,7 @@ export async function signedInPage(active) { const header = document.getElementById("site-header"); replace(header, brand(), nav(active)); const user = await api("/auth/me"); + addSharingLink(header.querySelector("nav ul"), active); header.append( h( "div", @@ -61,3 +62,35 @@ async function signOut() { location.href = "login.html"; } } + +// "Sharing" only appears when the server federates. Remembered per tab. +const FEDERATION_KEY = "scrobbler.federation"; + +async function federationAvailable() { + try { + const cached = sessionStorage.getItem(FEDERATION_KEY); + if (cached !== null) return cached === "1"; + } catch { + // Storage unavailable: just ask. + } + let available = false; + try { + await api("/federation/settings"); + available = true; + } catch { + available = false; + } + try { + sessionStorage.setItem(FEDERATION_KEY, available ? "1" : "0"); + } catch { + // Not remembered; asked again next page. + } + return available; +} + +async function addSharingLink(list, active) { + if (!list || !(await federationAvailable())) return; + const href = "sharing.html"; + list.append(h("li", {}, h("a", { href, "aria-current": href === active ? "page" : false }, "Sharing"))); +} + diff --git a/frontend/js/profile.js b/frontend/js/profile.js new file mode 100644 index 0000000..bbf1031 --- /dev/null +++ b/frontend/js/profile.js @@ -0,0 +1,43 @@ +import { api } from "./api.js"; +import { errorBox, h, replace } from "./dom.js"; +import { publicPage } from "./layout.js"; + +const root = document.getElementById("profile"); +const username = new URLSearchParams(location.search).get("u") ?? ""; + +// A public page: never sends visitors to sign in. +publicPage(); + +try { + const profile = await api(`/federation/profiles/${encodeURIComponent(username)}`, { auth: false }); + document.title = `${profile.display_name} · Scrobbler`; + const copy = h("button", { type: "button", class: "button button-quiet" }, "Copy handle"); + copy.addEventListener("click", async () => { + try { + await navigator.clipboard.writeText(profile.handle); + copy.textContent = "Copied"; + } catch { + copy.textContent = "Select and copy it above"; + } + }); + replace( + root, + h("h1", {}, profile.display_name), + h("div", { class: "approve-app handle" }, profile.handle), + profile.bio ? h("p", { class: "lede profile-bio" }, profile.bio) : null, + h("h2", { class: "profile-follow" }, "Follow from the fediverse"), + h( + "p", + { class: "muted" }, + "Search for the handle above in Mastodon or any other fediverse app, then follow.", + ), + h("div", { class: "button-row" }, copy), + ); +} catch (error) { + replace( + root, + error.status === 404 + ? [h("h1", {}, "No profile here"), h("p", { class: "lede" }, "No one on this server shares their listening under that name.")] + : errorBox(error.message), + ); +} diff --git a/frontend/js/sharing.js b/frontend/js/sharing.js new file mode 100644 index 0000000..b9c8757 --- /dev/null +++ b/frontend/js/sharing.js @@ -0,0 +1,82 @@ +import { api, describeError } from "./api.js"; +import { errorBox, h, replace } from "./dom.js"; +import { signedInPage } from "./layout.js"; + +const form = document.getElementById("sharing"); +const messages = document.getElementById("messages"); +const handlePanel = document.getElementById("handle-panel"); +const FIELDS = ["enabled", "visibility", "manually_approves_followers", "discoverable", "indexable", "display_name", "bio"]; + +await signedInPage("sharing.html"); + +let settings; +try { + settings = await api("/federation/settings"); +} catch (error) { + replace( + messages, + error.status === 404 + ? h("p", { class: "notice" }, "This server doesn't share to the fediverse. Ask whoever runs it to turn it on.") + : errorBox(error.message), + ); +} + +if (settings) { + fill(settings); + form.hidden = false; + form.elements.enabled.addEventListener("change", () => showHandle(form.elements.enabled.checked)); + form.addEventListener("submit", save); +} + +function fill(values) { + for (const name of FIELDS) { + const field = form.elements[name]; + if (name === "visibility") { + form.querySelector(`input[name=visibility][value="${values.visibility}"]`).checked = true; + } else if (field.type === "checkbox") { + field.checked = Boolean(values[name]); + } else { + field.value = values[name] ?? ""; + } + } + document.getElementById("handle").textContent = values.handle; + replace( + document.getElementById("profile-link"), + "Your public profile page: ", + h("a", { href: values.profile_url }, values.profile_url.replace(/^https?:\/\//, "")), + ); + showHandle(values.enabled); +} + +function showHandle(on) { + handlePanel.hidden = !on; +} + +async function save(event) { + event.preventDefault(); + const button = form.querySelector("button[type=submit]"); + const changes = { + enabled: form.elements.enabled.checked, + visibility: form.querySelector("input[name=visibility]:checked").value, + manually_approves_followers: form.elements.manually_approves_followers.checked, + discoverable: form.elements.discoverable.checked, + indexable: form.elements.indexable.checked, + display_name: form.elements.display_name.value.trim() || null, + bio: form.elements.bio.value.trim() || null, + }; + const status = document.getElementById("save-status"); + button.disabled = true; + status.textContent = "Saving"; + replace(messages); + try { + const saved = await api("/federation/settings", { method: "PATCH", body: changes }); + fill(saved); + status.textContent = saved.enabled ? `Saved. You're sharing as ${saved.handle}.` : "Saved. Sharing is off."; + } catch (error) { + status.textContent = ""; + replace(messages, errorBox(describeError(error))); + messages.scrollIntoView({ behavior: "smooth", block: "start" }); + } finally { + button.disabled = false; + } +} diff --git a/frontend/profile.html b/frontend/profile.html new file mode 100644 index 0000000..8ebfc48 --- /dev/null +++ b/frontend/profile.html @@ -0,0 +1,18 @@ + + +
+ + +Let people on Mastodon and other fediverse apps follow your listening.
+ + +