mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-19 18:13:41 -05:00
web/media: handle unicode correctly for fallback avatars
This commit is contained in:
parent
d573f2fa58
commit
f68070807c
1 changed files with 9 additions and 1 deletions
|
@ -70,8 +70,16 @@ function escapeHTMLChar(char: string): string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getFallbackCharacter(from: unknown, idx: number): string {
|
||||||
|
if (!from || typeof from !== "string" || from.length <= idx) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
// Array.from appears to be the only way to handle Unicode correctly
|
||||||
|
return Array.from(from.slice(0, (idx + 1) * 2))[idx]?.toUpperCase().toWellFormed() ?? ""
|
||||||
|
}
|
||||||
|
|
||||||
export const getAvatarURL = (userID: UserID, content?: Partial<MemberEventContent>): string | undefined => {
|
export const getAvatarURL = (userID: UserID, content?: Partial<MemberEventContent>): string | undefined => {
|
||||||
const fallbackCharacter = (content?.displayname?.[0]?.toUpperCase() ?? userID[1].toUpperCase()).toWellFormed()
|
const fallbackCharacter = getFallbackCharacter(content?.displayname, 0) || getFallbackCharacter(userID, 1)
|
||||||
const backgroundColor = getUserColor(userID)
|
const backgroundColor = getUserColor(userID)
|
||||||
const [server, mediaID] = parseMXC(content?.avatar_url)
|
const [server, mediaID] = parseMXC(content?.avatar_url)
|
||||||
if (!mediaID) {
|
if (!mediaID) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue