diff --git a/docs/admin-guide.md b/docs/admin-guide.md index 6dadaf7..2135ad7 100644 --- a/docs/admin-guide.md +++ b/docs/admin-guide.md @@ -62,6 +62,8 @@ read **Putting it behind TLS** below — the quick start above serves plain HTTP | `LASTFM_API_KEY` | api, worker | *(unset)* | Enables importing straight from Last.fm ([get a key](https://www.last.fm/api/account/create)) | | `MUSICBRAINZ_CONTACT` | api, worker | `UI_BASE_URL` | Contact URL/email sent to MusicBrainz for album art lookups (no key needed, but they ask for one) | | `ART_STORAGE_DIR` | api, worker | `var/album-art` | Where downloaded album art is stored; the worker writes it, the api serves it — both need access to the same directory (`docker compose` mounts a shared volume there) | +| `AVATAR_UPLOAD_MAX_BYTES` | api | 2 MB | Largest profile image a user may upload (a reverse proxy needs a body limit at least this large too). Images are stored in `avatars/` inside `ART_STORAGE_DIR` | +| `AVATAR_GRAVATAR_ENABLED` | api | off | Lets users use their Gravatar as their profile image. The api then calls gravatar.com (outbound HTTPS needed), sending it a SHA-256 hash of the user's email address; browsers never contact Gravatar | | `ART_UPLOAD_MAX_BYTES` | api | 5 MB | Largest image a user may upload as their own album art. A reverse proxy in front of the api needs a body limit at least this large | | `IMPORT_MAX_BYTES` | api | 200 MB | Largest export file accepted | | `WORKER_METRICS_PORT` | worker | `9101` | The worker's Prometheus metrics port | @@ -201,6 +203,11 @@ disposable cache — it's simply re-downloaded the next time it's requested. Ima volume and can't be re-created, so include the volume in your backups. The database rows that point at them are in `user_album_art`; restore the two together. +Profile images (uploads, and copies of users' Gravatars) are in `avatars/` in that volume, +one file per user id, and likewise belong in your backups. The `users` table records +which users have one (`avatar_source`); a missing file just shows as no image. Gravatar +copies can be re-fetched by their users, but uploads can't be re-created. + ## Upgrades ```sh diff --git a/docs/user-guide.md b/docs/user-guide.md index 005cea9..7d20ba3 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -65,6 +65,22 @@ flight. Unlike live scrobbles, imports keep plays of any age (still skipping row artist or track, or plays dated in the future). Plays already in your history are skipped, so importing the same export again, or importing overlapping exports, is safe. +## Your profile image + +Click your name in the page header to open **Profile**, where you can give yourself a +picture, shown next to your name. Upload a JPEG, PNG or WebP image, or remove it again. + +If your instance's admin has allowed it, you can also **use your Gravatar**. The server +asks Gravatar for the picture linked to your account email, sending it a hash of that +address (never the address itself), and keeps a copy; your browser doesn't contact +Gravatar. **Refresh Gravatar** fetches it again after you change it there. Choosing an +upload replaces a Gravatar and vice versa. + +Your image is private by default. If your instance shares on the fediverse, **Sharing** +has a **Share my profile image** switch that shows it on your public profile page and +sends it to other servers as your picture. They keep their own copies, so a change or +removal can take a while to show up everywhere. + ## Sharing on the fediverse Only available if your instance's admin has turned this on — ask them, or check whether diff --git a/frontend/account.html b/frontend/account.html new file mode 100644 index 0000000..5393e8e --- /dev/null +++ b/frontend/account.html @@ -0,0 +1,25 @@ + + + + + + Profile · Scrobbled + + + + + + + + + + + +
+

Profile

+

Your profile image, shown next to your name.

+
+
+
+ + diff --git a/frontend/css/style.css b/frontend/css/style.css index 8eab501..94973b2 100644 --- a/frontend/css/style.css +++ b/frontend/css/style.css @@ -209,10 +209,76 @@ code { margin-left: auto; display: flex; gap: 1rem; - align-items: baseline; + align-items: center; color: var(--ink-soft); } +.account-link { + display: flex; + gap: 0.5rem; + align-items: center; + color: inherit; + text-decoration: none; +} + +.account-link:hover .account-name { + color: var(--ink); +} + +.account-avatar { + display: flex; +} + +/* A profile image: round, with the placeholder ring while there's none */ +.avatar { + display: block; + flex: none; + border-radius: 50%; + object-fit: cover; + background: var(--paper-raised); + border: 1px solid var(--rule); +} + +.avatar-sm { + width: 1.75rem; + height: 1.75rem; +} + +.avatar-lg { + width: 7rem; + height: 7rem; +} + +.avatar-empty { + position: relative; +} + +.avatar-empty::after { + content: ""; + position: absolute; + inset: 32%; + border-radius: 50%; + border: 2px solid var(--bar); +} + +.avatar-row { + display: flex; + gap: 1.5rem; + align-items: flex-start; +} + +.avatar-body { + display: grid; + gap: 0.75rem; + max-width: 40rem; +} + +.avatar-xl { + width: 6rem; + height: 6rem; + margin-bottom: 1rem; +} + main { max-width: 1120px; margin: 0 auto; diff --git a/frontend/js/account.js b/frontend/js/account.js new file mode 100644 index 0000000..b12c2f4 --- /dev/null +++ b/frontend/js/account.js @@ -0,0 +1,95 @@ +import { api, describeError, upload } from "./api.js"; +import { avatarImage } from "./avatar.js"; +import { errorBox, h, replace } from "./dom.js"; +import { signedInPage } from "./layout.js"; + +const root = document.getElementById("avatar-panel"); +const messages = document.getElementById("messages"); + +let user = await signedInPage("account.html"); +render(); + +const SOURCES = { + upload: "An image you uploaded.", + gravatar: "Your Gravatar, copied to this server.", + none: "You haven't set a profile image yet.", +}; + +function say(notice) { + replace(messages, notice); +} + +// Runs a change, adopts the user it returns (or refetches), and redraws everything that +// shows the image - this panel and the one in the page header. +async function change(action, done) { + const buttons = root.querySelectorAll("button"); + buttons.forEach((button) => (button.disabled = true)); + try { + user = (await action()) ?? (await api("/auth/me")); + say(h("p", { class: "notice notice-ok" }, done)); + } catch (error) { + say(errorBox(describeError(error))); + } + render(); + const slot = document.querySelector(".account-avatar"); + if (slot) replace(slot, avatarImage(user, "avatar-sm")); +} + +function render() { + const file = h("input", { type: "file", accept: "image/jpeg,image/png,image/webp", hidden: true }); + file.addEventListener("change", () => { + if (!file.files[0]) return; + const data = new FormData(); + data.append("file", file.files[0]); + change(() => upload("/me/avatar", data, undefined, "PUT"), "Profile image updated."); + }); + + const buttons = [ + h("button", { type: "button", class: "button", onclick: () => file.click() }, "Upload image"), + user.gravatar_enabled + ? h( + "button", + { + type: "button", + class: "button button-quiet", + onclick: () => change(() => api("/me/avatar/gravatar", { method: "POST" }), "Now using your Gravatar."), + }, + user.avatar_source === "gravatar" ? "Refresh Gravatar" : "Use my Gravatar", + ) + : null, + user.has_avatar + ? h( + "button", + { + type: "button", + class: "button button-quiet", + onclick: () => change(() => api("/me/avatar", { method: "DELETE" }), "Profile image removed."), + }, + "Remove", + ) + : null, + ]; + + replace( + root, + h( + "div", + { class: "avatar-row" }, + avatarImage(user, "avatar-lg"), + h( + "div", + { class: "avatar-body" }, + h("p", {}, SOURCES[user.avatar_source] ?? SOURCES.none), + h("p", { class: "muted" }, "JPEG, PNG or WebP. Only you see it, unless you choose to share it on your fediverse profile."), + h("div", { class: "button-row" }, buttons, file), + user.gravatar_enabled + ? h( + "p", + { class: "muted" }, + "Using Gravatar makes this server ask Gravatar for the picture linked to your email address, sending it a hash of that address (not the address itself). Your browser never contacts Gravatar.", + ) + : null, + ), + ), + ); +} diff --git a/frontend/js/avatar.js b/frontend/js/avatar.js new file mode 100644 index 0000000..096a6e6 --- /dev/null +++ b/frontend/js/avatar.js @@ -0,0 +1,19 @@ +// A round profile image that starts as a placeholder ring and swaps itself for the real +// picture once (if) it loads. `source` is either a signed-in user (their own image, +// fetched with their token) or { path } for another public image URL. + +import { fetchImage } from "./api.js"; +import { h } from "./dom.js"; + +export function avatarImage(user, extraClass, path = "/me/avatar") { + const classes = extraClass ? `avatar ${extraClass}` : "avatar"; + const el = h("span", { class: `${classes} avatar-empty`, "aria-hidden": "true" }); + if (!user?.has_avatar) return el; + fetchImage(path).then((url) => { + if (!url) return; // miss or failed fetch: leave the placeholder + const img = h("img", { class: classes, alt: "" }); + img.src = url; + el.replaceWith(img); + }); + return el; +} diff --git a/frontend/js/layout.js b/frontend/js/layout.js index b98f55a..f56ad1c 100644 --- a/frontend/js/layout.js +++ b/frontend/js/layout.js @@ -1,6 +1,7 @@ // Shared page shell: site header, navigation, sign-in guard. import { api, getToken, redirectToLogin, setToken } from "./api.js"; +import { avatarImage } from "./avatar.js"; import { h, replace } from "./dom.js"; const NAV = [ @@ -26,7 +27,12 @@ export async function signedInPage(active) { h( "div", { class: "account" }, - h("span", { class: "account-name" }, user.username), + h( + "a", + { class: "account-link", href: "account.html", title: "Your profile image" }, + h("span", { class: "account-avatar" }, avatarImage(user, "avatar-sm")), + h("span", { class: "account-name" }, user.username), + ), h("button", { type: "button", class: "link-button", onclick: signOut }, "Sign out"), ), ); diff --git a/frontend/js/profile.js b/frontend/js/profile.js index 0823439..42764e8 100644 --- a/frontend/js/profile.js +++ b/frontend/js/profile.js @@ -1,4 +1,5 @@ import { api } from "./api.js"; +import { avatarImage } from "./avatar.js"; import { errorBox, h, replace } from "./dom.js"; import { publicPage } from "./layout.js"; @@ -22,6 +23,9 @@ try { }); replace( root, + profile.has_avatar + ? avatarImage(profile, "avatar-xl", `/federation/profiles/${encodeURIComponent(username)}/avatar`) + : null, h("h1", {}, profile.display_name), h("div", { class: "approve-app handle" }, profile.handle), profile.bio ? h("p", { class: "lede profile-bio" }, profile.bio) : null, diff --git a/frontend/js/sharing.js b/frontend/js/sharing.js index fd3f1cf..c1e5fe8 100644 --- a/frontend/js/sharing.js +++ b/frontend/js/sharing.js @@ -16,6 +16,7 @@ const FIELDS = [ "post_milestones", "timezone", "now_playing_mode", + "share_avatar", "display_name", "bio", ]; @@ -155,6 +156,7 @@ async function save(event) { post_milestones: form.elements.post_milestones.checked, timezone: form.elements.timezone.value.trim() || "UTC", now_playing_mode: form.querySelector("input[name=now_playing_mode]:checked").value, + share_avatar: form.elements.share_avatar.checked, display_name: form.elements.display_name.value.trim() || null, bio: form.elements.bio.value.trim() || null, }; diff --git a/frontend/sharing.html b/frontend/sharing.html index eb668c2..e2b4e0e 100644 --- a/frontend/sharing.html +++ b/frontend/sharing.html @@ -114,6 +114,10 @@ Shown on your fediverse profile. Plain text. +
+ + +