Cache profile fields and assets behind one shared store.
Trigger cinny-mobile / dispatch (push) Successful in 1s
Trigger cinny-mobile / dispatch (push) Successful in 1s
Colour, banner, collectibles, and profile each read the same extended profile endpoint through separate caches with mismatched TTLs, so a single profile view fired three round trips. They now read through one cache with a shared TTL and in-flight dedupe, and profile media is kept as blob URLs instead of being re-downloaded per mount. useUserProfile now prefers the SDK's sync-cached user and only fetches when both the SDK and the cache are cold.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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';
|
||||
@@ -120,26 +121,8 @@ export function extractCollectiblesFromProfile(profile: Record<string, unknown>)
|
||||
};
|
||||
}
|
||||
|
||||
async function loadCollectiblesFromProfile(mx: MatrixClient, userId: string): Promise<CollectibleProfileFields> {
|
||||
if (await mx.doesServerSupportExtendedProfiles()) {
|
||||
try {
|
||||
const profile = await mx.getExtendedProfile(userId);
|
||||
return extractCollectiblesFromProfile(profile);
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = (await mx.getProfileInfo(userId)) as Record<string, unknown>;
|
||||
return extractCollectiblesFromProfile(profile);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadUserCollectibles(mx: MatrixClient, userId: string): Promise<CollectibleProfileFields> {
|
||||
return loadCollectiblesFromProfile(mx, userId);
|
||||
return extractCollectiblesFromProfile(await getProfileFields(mx, userId));
|
||||
}
|
||||
|
||||
export async function saveCollectible(
|
||||
@@ -179,21 +162,7 @@ export async function getBannerUrlProfileKey(mx: MatrixClient): Promise<string>
|
||||
}
|
||||
|
||||
export async function loadBannerUrl(mx: MatrixClient, userId: string): Promise<string | undefined> {
|
||||
if (await mx.doesServerSupportExtendedProfiles()) {
|
||||
try {
|
||||
const profile = await mx.getExtendedProfile(userId);
|
||||
return extractBannerUrlFromProfile(profile);
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = (await mx.getProfileInfo(userId)) as Record<string, unknown>;
|
||||
return extractBannerUrlFromProfile(profile);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return extractBannerUrlFromProfile(await getProfileFields(mx, userId));
|
||||
}
|
||||
|
||||
export async function saveBannerUrl(mx: MatrixClient, bannerMxc: string): Promise<void> {
|
||||
@@ -204,7 +173,7 @@ export async function saveBannerUrl(mx: MatrixClient, bannerMxc: string): Promis
|
||||
const key = await getBannerUrlProfileKey(mx);
|
||||
await mx.setExtendedProfileProperty(key, bannerMxc);
|
||||
const userId = mx.getUserId();
|
||||
if (userId) clearBannerCache(userId);
|
||||
if (userId) clearProfileCache(userId);
|
||||
}
|
||||
|
||||
export async function deleteBannerUrl(mx: MatrixClient): Promise<void> {
|
||||
@@ -215,17 +184,7 @@ export async function deleteBannerUrl(mx: MatrixClient): Promise<void> {
|
||||
const key = await getBannerUrlProfileKey(mx);
|
||||
await mx.deleteExtendedProfileProperty(key);
|
||||
const userId = mx.getUserId();
|
||||
if (userId) clearBannerCache(userId);
|
||||
}
|
||||
|
||||
let bannerCacheClearImpl: (userId: string) => void = () => {};
|
||||
|
||||
export function registerBannerCacheClear(fn: (userId: string) => void): void {
|
||||
bannerCacheClearImpl = fn;
|
||||
}
|
||||
|
||||
function clearBannerCache(userId: string): void {
|
||||
bannerCacheClearImpl(userId);
|
||||
if (userId) clearProfileCache(userId);
|
||||
}
|
||||
|
||||
export async function getColorPreferenceProfileKey(mx: MatrixClient): Promise<string> {
|
||||
@@ -239,21 +198,7 @@ export async function loadColorPreference(
|
||||
mx: MatrixClient,
|
||||
userId: string
|
||||
): Promise<ColorPreference | undefined> {
|
||||
if (await mx.doesServerSupportExtendedProfiles()) {
|
||||
try {
|
||||
const profile = await mx.getExtendedProfile(userId);
|
||||
return extractColorPreferenceFromProfile(profile);
|
||||
} catch {
|
||||
// fall through to standard profile endpoint
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = (await mx.getProfileInfo(userId)) as Record<string, unknown>;
|
||||
return extractColorPreferenceFromProfile(profile);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return extractColorPreferenceFromProfile(await getProfileFields(mx, userId));
|
||||
}
|
||||
|
||||
export function extractMemberColorPreference(room: Room | undefined, userId: string): ColorPreference | undefined {
|
||||
@@ -272,17 +217,7 @@ export async function saveColorPreference(mx: MatrixClient, preference: ColorPre
|
||||
const key = await getColorPreferenceProfileKey(mx);
|
||||
await mx.setExtendedProfileProperty(key, preference);
|
||||
const userId = mx.getUserId();
|
||||
if (userId) clearPreferenceCache(userId);
|
||||
}
|
||||
|
||||
let preferenceCacheClearImpl: (userId: string) => void = () => {};
|
||||
|
||||
export function registerPreferenceCacheClear(fn: (userId: string) => void): void {
|
||||
preferenceCacheClearImpl = fn;
|
||||
}
|
||||
|
||||
function clearPreferenceCache(userId: string): void {
|
||||
preferenceCacheClearImpl(userId);
|
||||
if (userId) clearProfileCache(userId);
|
||||
}
|
||||
|
||||
export async function deleteColorPreference(mx: MatrixClient): Promise<void> {
|
||||
@@ -293,5 +228,5 @@ export async function deleteColorPreference(mx: MatrixClient): Promise<void> {
|
||||
const key = await getColorPreferenceProfileKey(mx);
|
||||
await mx.deleteExtendedProfileProperty(key);
|
||||
const userId = mx.getUserId();
|
||||
if (userId) clearPreferenceCache(userId);
|
||||
if (userId) clearProfileCache(userId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user