import { MatrixClient, Room } from 'matrix-js-sdk'; import { ThemeKind } from '../hooks/useTheme'; import { clearProfileCache, getProfileFields } from './profileCache'; /** MSC4522 stable profile field for username colors */ export const PROFILE_KEY_COLOR_PREFERENCE_STABLE = 'm.color_preference'; /** MSC4522 unstable profile field for username colors */ export const PROFILE_KEY_COLOR_PREFERENCE_UNSTABLE = 'eu.she-a.color'; /** MSC4427 unstable profile field for banner */ export const PROFILE_KEY_BANNER_URL_UNSTABLE = 'chat.commet.profile_banner'; /** MSC4427 stable profile field for banner */ export const PROFILE_KEY_BANNER_URL_STABLE = 'm.banner_url'; /** Paarrot profile effect (animated profile background) */ export const PROFILE_KEY_PROFILE_EFFECT = 'im.paarrot.profile_effect'; /** Paarrot nameplate (username bar background) */ export const PROFILE_KEY_NAMEPLATE = 'im.paarrot.nameplate'; /** Paarrot avatar decoration (avatar frame) */ export const PROFILE_KEY_AVATAR_DECORATION = 'im.paarrot.avatar_decoration'; export type StoredCollectible = { sku_id: string; name: string; assets: Record; effect?: unknown; }; export type CollectibleProfileFields = { profile_effect?: StoredCollectible; nameplate?: StoredCollectible; avatar_decoration?: StoredCollectible; }; export type ColorPreference = { on_dark?: string; on_light?: string; }; const HEX_COLOR_RE = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; export function isValidHexColor(color: string): boolean { return HEX_COLOR_RE.test(color); } export function parseColorPreference(value: unknown): ColorPreference | undefined { if (!value || typeof value !== 'object') return undefined; const obj = value as Record; const on_dark = typeof obj.on_dark === 'string' && isValidHexColor(obj.on_dark) ? obj.on_dark : undefined; const on_light = typeof obj.on_light === 'string' && isValidHexColor(obj.on_light) ? obj.on_light : undefined; if (!on_dark && !on_light) return undefined; return { on_dark, on_light }; } export function extractColorPreferenceFromProfile(profile: Record): ColorPreference | undefined { const stable = parseColorPreference(profile[PROFILE_KEY_COLOR_PREFERENCE_STABLE]); if (stable) return stable; return parseColorPreference(profile[PROFILE_KEY_COLOR_PREFERENCE_UNSTABLE]); } export function resolveColorForTheme( preference: ColorPreference | undefined, themeKind: ThemeKind ): string | undefined { if (!preference) return undefined; if (themeKind === ThemeKind.Dark) { return preference.on_dark ?? preference.on_light; } return preference.on_light ?? preference.on_dark; } export function hasColorPreference(preference: ColorPreference | undefined): boolean { return Boolean(preference?.on_dark || preference?.on_light); } export function extractBannerUrlFromProfile(profile: Record): string | undefined { const stable = profile[PROFILE_KEY_BANNER_URL_STABLE]; if (typeof stable === 'string' && stable.startsWith('mxc://')) return stable; const unstable = profile[PROFILE_KEY_BANNER_URL_UNSTABLE]; if (typeof unstable === 'string' && unstable.startsWith('mxc://')) return unstable; return undefined; } function parseStoredCollectible(value: unknown): StoredCollectible | undefined { if (!value || typeof value !== 'object') return undefined; const obj = value as Record; const sku_id = typeof obj.sku_id === 'string' ? obj.sku_id : undefined; const name = typeof obj.name === 'string' ? obj.name : undefined; const assets = obj.assets; if (!sku_id || !name || !assets || typeof assets !== 'object') return undefined; const parsedAssets: Record = {}; for (const [key, mxc] of Object.entries(assets as Record)) { if (typeof mxc === 'string' && mxc.startsWith('mxc://')) { parsedAssets[key] = mxc; } } if (Object.keys(parsedAssets).length === 0) return undefined; return { sku_id, name, assets: parsedAssets, effect: obj.effect, }; } export function extractCollectiblesFromProfile(profile: Record): CollectibleProfileFields { return { profile_effect: parseStoredCollectible(profile[PROFILE_KEY_PROFILE_EFFECT]), nameplate: parseStoredCollectible(profile[PROFILE_KEY_NAMEPLATE]), avatar_decoration: parseStoredCollectible(profile[PROFILE_KEY_AVATAR_DECORATION]), }; } export async function loadUserCollectibles(mx: MatrixClient, userId: string): Promise { return extractCollectiblesFromProfile(await getProfileFields(mx, userId)); } export async function saveCollectible( mx: MatrixClient, key: string, collectible: StoredCollectible | undefined ): Promise { if (!(await mx.doesServerSupportExtendedProfiles())) { throw new Error('Server does not support extended profile fields (MSC4133)'); } if (!collectible) { await mx.deleteExtendedProfileProperty(key); return; } await mx.setExtendedProfileProperty(key, collectible); } export async function saveProfileEffect(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise { await saveCollectible(mx, PROFILE_KEY_PROFILE_EFFECT, collectible); } export async function saveNameplate(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise { await saveCollectible(mx, PROFILE_KEY_NAMEPLATE, collectible); } export async function saveAvatarDecoration(mx: MatrixClient, collectible: StoredCollectible | undefined): Promise { await saveCollectible(mx, PROFILE_KEY_AVATAR_DECORATION, collectible); } export async function getBannerUrlProfileKey(mx: MatrixClient): Promise { if (await mx.isVersionSupported('v1.16')) { return PROFILE_KEY_BANNER_URL_STABLE; } return PROFILE_KEY_BANNER_URL_UNSTABLE; } export async function loadBannerUrl(mx: MatrixClient, userId: string): Promise { return extractBannerUrlFromProfile(await getProfileFields(mx, userId)); } export async function saveBannerUrl(mx: MatrixClient, bannerMxc: string): Promise { if (!(await mx.doesServerSupportExtendedProfiles())) { throw new Error('Server does not support extended profile fields (MSC4133)'); } const key = await getBannerUrlProfileKey(mx); await mx.setExtendedProfileProperty(key, bannerMxc); const userId = mx.getUserId(); if (userId) clearProfileCache(userId); } export async function deleteBannerUrl(mx: MatrixClient): Promise { if (!(await mx.doesServerSupportExtendedProfiles())) { throw new Error('Server does not support extended profile fields (MSC4133)'); } const key = await getBannerUrlProfileKey(mx); await mx.deleteExtendedProfileProperty(key); const userId = mx.getUserId(); if (userId) clearProfileCache(userId); } export async function getColorPreferenceProfileKey(mx: MatrixClient): Promise { if (await mx.isVersionSupported('v1.16')) { return PROFILE_KEY_COLOR_PREFERENCE_STABLE; } return PROFILE_KEY_COLOR_PREFERENCE_UNSTABLE; } export async function loadColorPreference( mx: MatrixClient, userId: string ): Promise { return extractColorPreferenceFromProfile(await getProfileFields(mx, userId)); } export function extractMemberColorPreference(room: Room | undefined, userId: string): ColorPreference | undefined { if (!room || typeof (room as Room).getMember !== 'function' || !userId) return undefined; const member = room.getMember(userId); const content = member?.events.member?.getContent(); if (!content) return undefined; return extractColorPreferenceFromProfile(content as Record); } export async function saveColorPreference(mx: MatrixClient, preference: ColorPreference): Promise { if (!(await mx.doesServerSupportExtendedProfiles())) { throw new Error('Server does not support extended profile fields (MSC4133)'); } const key = await getColorPreferenceProfileKey(mx); await mx.setExtendedProfileProperty(key, preference); const userId = mx.getUserId(); if (userId) clearProfileCache(userId); } export async function deleteColorPreference(mx: MatrixClient): Promise { if (!(await mx.doesServerSupportExtendedProfiles())) { throw new Error('Server does not support extended profile fields (MSC4133)'); } const key = await getColorPreferenceProfileKey(mx); await mx.deleteExtendedProfileProperty(key); const userId = mx.getUserId(); if (userId) clearProfileCache(userId); }