// Profile pictures. A user either has one (the server says so with an // avatar_version, which is also the cache buster in the image URL) or shows // their initial in a coloured circle, as before. const versions = new Map(); // user id -> avatar_version // Learn from a user list (or a single user) which people have a picture. export function noteAvatars(list) { for (const u of list || []) setAvatarVersion(u.id, u.avatar_version); } export function setAvatarVersion(userID, version) { if (version == null) versions.delete(userID); else versions.set(userID, version); } export function clearAvatars() { versions.clear(); } // withAvatar lays the person's picture over el, which keeps its initial // underneath: if the image fails to load, the initial is what shows. export function withAvatar(el, userID) { const v = versions.get(userID); if (v == null) return el; el.classList.add('has-img'); const img = document.createElement('img'); img.className = 'avatar-img'; img.alt = ''; img.decoding = 'async'; img.src = `/api/users/${userID}/avatar?v=${v}`; img.addEventListener('error', () => img.remove()); el.append(img); return el; }