diff --git a/src/app/hooks/useRenderableMediaUrl.ts b/src/app/hooks/useRenderableMediaUrl.ts index 07d445fe3..b5bb9128f 100644 --- a/src/app/hooks/useRenderableMediaUrl.ts +++ b/src/app/hooks/useRenderableMediaUrl.ts @@ -2,7 +2,11 @@ import { useEffect, useState } from 'react'; import { useAtomValue } from 'jotai'; import { isTauri } from '@tauri-apps/api/core'; import { activeSessionIdAtom } from '$state/sessions'; -import { fetchMediaBlob, getCurrentMediaSessionScope } from '$utils/mediaTransport'; +import { + fetchMediaBlob, + getCurrentMediaSessionScope, + getStableMediaCacheKeyFragment, +} from '$utils/mediaTransport'; import { hasControllingServiceWorker, hasServiceWorker } from '$utils/platform'; import { rewriteAuthenticatedMediaUrl } from '$utils/matrix'; @@ -22,7 +26,7 @@ const objectUrlCache = new Map(); const inflightRequests = new Map>(); function getObjectUrlCacheKey(sessionScope: string, url: string): string { - return `${sessionScope}\x00${url}`; + return `${sessionScope}\x00${getStableMediaCacheKeyFragment(url)}`; } function normalizeRenderableMediaUrl(url: string | undefined): string | undefined { diff --git a/src/app/state/sessions.ts b/src/app/state/sessions.ts index c4380d54a..c91a878a6 100644 --- a/src/app/state/sessions.ts +++ b/src/app/state/sessions.ts @@ -10,6 +10,11 @@ import { const log = createLogger('sessions'); +const notifySessionChanged = (): void => { + if (typeof window === 'undefined') return; + window.dispatchEvent(new Event('sable-session-changed')); +}; + export type OidcSessionInfo = { issuer: string; clientId: string; @@ -55,12 +60,14 @@ export function setFallbackSession( localStorage.setItem('cinny_device_id', deviceId); localStorage.setItem('cinny_user_id', userId); localStorage.setItem('cinny_hs_base_url', baseUrl); + notifySessionChanged(); } export const removeFallbackSession = () => { localStorage.removeItem('cinny_hs_base_url'); localStorage.removeItem('cinny_user_id'); localStorage.removeItem('cinny_device_id'); localStorage.removeItem('cinny_access_token'); + notifySessionChanged(); }; export const getFallbackSession = (): Session | undefined => { const baseUrl = localStorage.getItem('cinny_hs_base_url'); @@ -167,6 +174,7 @@ export const sessionsAtom = atom( (session) => session.userId !== action.session.userId ); set(baseSessionsAtom, sessions); + notifySessionChanged(); } } ); @@ -188,6 +196,7 @@ export const updateSessionTokens = ( }; setLocalStorageItem(MATRIX_SESSIONS_KEY, sessions); window.dispatchEvent(new StorageEvent('storage', { key: MATRIX_SESSIONS_KEY })); + notifySessionChanged(); }; export const getStoredSessionRefreshToken = (userId: string): string | undefined => @@ -206,7 +215,9 @@ const baseActiveSessionAtom = atomWithLocalStorage( export const activeSessionIdAtom = atom( (get) => get(baseActiveSessionAtom), (_get, set, value) => { + const previous = _get(baseActiveSessionAtom); set(baseActiveSessionAtom, value); + if (previous !== value) notifySessionChanged(); } ); diff --git a/src/app/styles/overrides/TouchDevices.css b/src/app/styles/overrides/TouchDevices.css index d09fbf7a5..af3f81425 100644 --- a/src/app/styles/overrides/TouchDevices.css +++ b/src/app/styles/overrides/TouchDevices.css @@ -6,14 +6,14 @@ @media (hover: none) { html body button:active, - html body [role="button"]:active, - html body [class*="Button"]:active, - html body [class*="Chip"]:active { + html body [role='button']:active, + html body [class*='Button']:active, + html body [class*='Chip']:active { transform: none !important; } html body button:hover, - html body [role="button"]:hover { + html body [role='button']:hover { transform: none !important; } diff --git a/src/app/utils/mediaTransport.ts b/src/app/utils/mediaTransport.ts index d2668cec3..e694fff6b 100644 --- a/src/app/utils/mediaTransport.ts +++ b/src/app/utils/mediaTransport.ts @@ -133,23 +133,38 @@ export function getActiveMediaSession(): { baseUrl: string; accessToken: string return undefined; } +let cachedSessionScope: string | undefined; + +export const invalidateMediaSessionScope = (): void => { + cachedSessionScope = undefined; +}; + +if (typeof window !== 'undefined') { + window.addEventListener('sable-session-changed', invalidateMediaSessionScope); + window.addEventListener('storage', invalidateMediaSessionScope); +} + export function getCurrentMediaSessionScope(): string { if (typeof localStorage === 'undefined') return 'anonymous'; + if (cachedSessionScope !== undefined) return cachedSessionScope; const activeSession = getActiveStoredSession(); if (activeSession) { - return activeSession.userId; + cachedSessionScope = activeSession.userId; + return cachedSessionScope; } const fallbackBaseUrl = localStorage.getItem(FALLBACK_BASE_URL_KEY); const fallbackUserId = localStorage.getItem(FALLBACK_USER_ID_KEY); if (fallbackBaseUrl && fallbackUserId) { - return fallbackUserId; + cachedSessionScope = fallbackUserId; + return cachedSessionScope; } - return 'anonymous'; + cachedSessionScope = 'anonymous'; + return cachedSessionScope; } function getFetchCacheMode(cacheMode: MediaFetchCacheMode): RequestCache { @@ -159,11 +174,44 @@ function getFetchCacheMode(cacheMode: MediaFetchCacheMode): RequestCache { } function getRequestKey(url: string, cacheMode: MediaFetchCacheMode): string { - return `${cacheMode}:${url}`; + return `${cacheMode}:${getStableMediaCacheKeyFragment(url)}`; } +type MatrixMediaInfo = { + mxcUrl: string; + query: string; +}; + +function getMatrixMediaInfo(url: string): MatrixMediaInfo | undefined { + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return undefined; + } + + const match = parsed.pathname.match( + /^\/_matrix\/(?:media\/[^/]+|client\/v1\/media)\/(?:download|thumbnail)\/([^/]+)\/([^/?#]+)/ + ); + if (!match) return undefined; + + const [, serverName, mediaId] = match; + return { + mxcUrl: `mxc://${serverName}/${mediaId}`, + query: parsed.search, + }; +} + +function getStableMediaCacheKeyFragment(url: string): string { + const info = getMatrixMediaInfo(url); + if (info) return `${info.mxcUrl}${info.query}`; + return url; +} + +export { getStableMediaCacheKeyFragment }; + function getScopedMediaCacheKey(url: string, sessionScope?: string): string { - return `${sessionScope ?? getCurrentMediaSessionScope()}:${url}`; + return `${sessionScope ?? getCurrentMediaSessionScope()}:${getStableMediaCacheKeyFragment(url)}`; } function resolveAccessToken(url: string, options?: MediaTransportOptions): string | undefined {