Skip to content

Commit 8bd25d6

Browse files
committed
fix(ui): retain avatar images across profile updates
1 parent 7efb3f7 commit 8bd25d6

6 files changed

Lines changed: 61 additions & 57 deletions

File tree

‎ui/src/components/viewer-facepile.ts‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { html, nothing } from "lit";
22
import { property } from "lit/decorators.js";
3-
import { keyed } from "lit/directives/keyed.js";
3+
import { live } from "lit/directives/live.js";
44
import { until } from "lit/directives/until.js";
55
import type { PresenceEntry } from "../api/types.ts";
66
import { CONTROL_UI_BUILD_INFO, type ControlUiBuildInfo } from "../build-info.ts";
@@ -199,18 +199,15 @@ class ViewerAvatar extends OpenClawLightDomContentsElement {
199199
});
200200
const imageUrl = avatar.kind === "initials" ? null : resolveAvatarImageUrl(avatar.url);
201201
const pending = imageUrl !== null && typeof imageUrl !== "string";
202-
// A changed route or request must own fresh DOM so an older image's load
203-
// callback cannot suppress the next avatar's pending initials.
204-
return html`${keyed(
205-
imageUrl ?? `${user.id}:${this.variant}`,
206-
html`<span
207-
class=${`viewer-avatar viewer-avatar--${this.variant}${pending ? " is-fallback" : ""}`}
208-
data-viewer-id=${user.id}
209-
aria-label=${label}
210-
>
211-
${renderViewerAvatar(user, avatar, imageUrl)}
212-
</span>`,
213-
)}`;
202+
// Image events toggle fallback imperatively; live() compares actual DOM so
203+
// a changed avatar restores initials without replacing its existing image.
204+
return html`<span
205+
class=${live(`viewer-avatar viewer-avatar--${this.variant}${pending ? " is-fallback" : ""}`)}
206+
data-viewer-id=${user.id}
207+
aria-label=${label}
208+
>
209+
${renderViewerAvatar(user, avatar, imageUrl)}
210+
</span>`;
214211
}
215212
}
216213

‎ui/src/lib/identity-avatar.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,22 @@ describe("authenticated profile avatar cache", () => {
327327
);
328328
});
329329

330-
it("never fetches a sender-controlled URL outside the trusted avatar route", () => {
330+
it("never forwards gateway credentials to a sender-controlled avatar origin", () => {
331331
setAvatarGatewayOrigin("https://gateway.example.test", "Bearer profile-token");
332332
const fetchAvatar = vi.spyOn(globalThis, "fetch");
333333

334-
expect(resolveAvatarImageUrl("https://evil.example/api/users/profile-ada/avatar")).toBeNull();
335-
expect(resolveAvatarImageUrl("/api/secrets")).toBeNull();
334+
for (const avatarUrl of [
335+
"https://evil.example/api/users/profile-ada/avatar?v=7",
336+
"https://gateway.example.test.evil.example/api/users/profile-ada/avatar?v=7",
337+
"https://[email protected]/api/users/profile-ada/avatar?v=7",
338+
"//evil.example/api/users/profile-ada/avatar?v=7",
339+
"/api/secrets",
340+
]) {
341+
expect(resolveAvatarImageUrl(avatarUrl)).toBeNull();
342+
expect(resolveAvatar({ id: "profile-ada", profileAvatarUrl: avatarUrl })).toMatchObject({
343+
kind: "initials",
344+
});
345+
}
336346
expect(fetchAvatar).not.toHaveBeenCalled();
337347
});
338348
});

‎ui/src/lib/identity-avatar.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ function toTrustedAvatarUrl(value: string, gatewayOrigin: string | null): string
111111
}
112112
}
113113

114-
function loadIdentityAvatar(url: string): Promise<string | null> {
114+
function loadIdentityAvatar(url: string): string | Promise<string | null> {
115115
const cacheKey = url;
116116
const cached = identityAvatarCache.get(cacheKey);
117117
if (cached) {
118118
// Map order is the LRU order; concurrent roster, profile, and chat views
119119
// must share both the authenticated request and its resulting blob.
120120
identityAvatarCache.delete(cacheKey);
121121
identityAvatarCache.set(cacheKey, cached);
122-
return cached.promise;
122+
return cached.loaded && cached.blobUrl ? cached.blobUrl : cached.promise;
123123
}
124124

125125
const entry: CachedIdentityAvatar = {

‎ui/src/pages/chat/chat-avatar.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ describe("attributed sender avatars", () => {
194194
expect(image?.getAttribute("src")).toBe("blob:second-sender");
195195
return image!;
196196
});
197+
expect(secondImage).toBe(firstImage);
197198
secondImage.dispatchEvent(new Event("load"));
198199
expect(container.querySelector(".chat-avatar-slot")?.classList.contains("is-fallback")).toBe(
199200
false,

‎ui/src/pages/chat/chat-avatar.ts‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Control UI chat module implements chat avatar behavior.
22
import { html, nothing } from "lit";
3-
import { keyed } from "lit/directives/keyed.js";
3+
import { live } from "lit/directives/live.js";
44
import { until } from "lit/directives/until.js";
55
import type { GatewayHelloOk } from "../../api/gateway.ts";
66
import { normalizeBasePath } from "../../app-route-paths.ts";
@@ -65,32 +65,31 @@ export function renderChatAvatar(
6565
// The derived route may 404 (no upload, no Gravatar); swap to initials
6666
// instead of a broken image. Lit reuses DOM parts, so a load must clear a
6767
// prior sender's error state.
68-
return html`${keyed(
69-
imageUrl,
70-
html`<span class=${`chat-avatar-slot${typeof imageUrl === "string" ? "" : " is-fallback"}`}>
71-
<img
72-
class="chat-avatar user"
73-
src=${typeof imageUrl === "string"
74-
? imageUrl
75-
: until(
76-
imageUrl.then((url) => url ?? nothing),
77-
nothing,
78-
)}
79-
alt="${label}"
80-
@error=${(event: Event) => {
81-
const image = event.currentTarget as HTMLImageElement;
82-
settleAvatarImageUrl(image.getAttribute("src"));
83-
image.closest(".chat-avatar-slot")?.classList.add("is-fallback");
84-
}}
85-
@load=${(event: Event) => {
86-
const image = event.currentTarget as HTMLImageElement;
87-
settleAvatarImageUrl(image.getAttribute("src"));
88-
image.closest(".chat-avatar-slot")?.classList.remove("is-fallback");
89-
}}
90-
/>
91-
${initialsAvatar}
92-
</span>`,
93-
)}`;
68+
return html`<span
69+
class=${live(`chat-avatar-slot${typeof imageUrl === "string" ? "" : " is-fallback"}`)}
70+
>
71+
<img
72+
class="chat-avatar user"
73+
src=${typeof imageUrl === "string"
74+
? imageUrl
75+
: until(
76+
imageUrl.then((url) => url ?? nothing),
77+
nothing,
78+
)}
79+
alt="${label}"
80+
@error=${(event: Event) => {
81+
const image = event.currentTarget as HTMLImageElement;
82+
settleAvatarImageUrl(image.getAttribute("src"));
83+
image.closest(".chat-avatar-slot")?.classList.add("is-fallback");
84+
}}
85+
@load=${(event: Event) => {
86+
const image = event.currentTarget as HTMLImageElement;
87+
settleAvatarImageUrl(image.getAttribute("src"));
88+
image.closest(".chat-avatar-slot")?.classList.remove("is-fallback");
89+
}}
90+
/>
91+
${initialsAvatar}
92+
</span>`;
9493
}
9594
const assistantName = assistant?.name?.trim() || "Assistant";
9695
const assistantAvatar = assistant?.avatar?.trim() || "";

‎ui/src/pages/chat/components/chat-author-avatar.ts‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { html, nothing, type TemplateResult } from "lit";
2-
import { keyed } from "lit/directives/keyed.js";
2+
import { live } from "lit/directives/live.js";
33
import { until } from "lit/directives/until.js";
44
import { formatSenderLabel } from "../../../lib/chat/sender-label.ts";
55
import {
@@ -82,15 +82,12 @@ export function renderChatAuthorAvatar(
8282
avatar.kind === "initials"
8383
? renderInitialsAvatar(avatar)
8484
: renderResolvedAvatar(fallback, imageUrl);
85-
return html`${keyed(
86-
imageUrl ?? `${sender.id ?? ""}:${label}`,
87-
html`<span
88-
class=${`chat-author-avatar${pending ? " is-fallback" : ""}`}
89-
role="img"
90-
aria-label=${label}
91-
title=${label}
92-
>
93-
${resolved}
94-
</span>`,
95-
)}`;
85+
return html`<span
86+
class=${live(`chat-author-avatar${pending ? " is-fallback" : ""}`)}
87+
role="img"
88+
aria-label=${label}
89+
title=${label}
90+
>
91+
${resolved}
92+
</span>`;
9693
}

0 commit comments

Comments
 (0)