Add the profile image UI and docs (SCR-1)
CI / validate-monitoring (push) Successful in 10s
CI / lint (push) Successful in 14s
CI / federation-interop (push) Successful in 1m15s
CI / test (push) Successful in 7m2s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 10s
CI / lint (push) Successful in 14s
CI / federation-interop (push) Successful in 1m15s
CI / test (push) Successful in 7m2s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
A Profile page to upload, use Gravatar or remove the image, the avatar in the page header and on the public profile, a Sharing switch for it, and user/admin guide notes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b878523d51
commit
b388e7112d
10 files changed
+246
-2
No files matched your search
@@ -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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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"),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user