mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-25 19:41:17 -08:00
fix media on secondary accounts not loading (#1521)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes media not loading for any account except the first one #### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
7 files changed
+84
-8
No files matched your search
@@ -125,7 +125,7 @@ function KatexRenderer({
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
void Promise.all([import('katex'), import('katex/dist/katex.min.css')]).then(([katex]) => {
|
||||
void import('katex').then((katex) => {
|
||||
if (mounted) {
|
||||
setHtml(katex.default.renderToString(math, { throwOnError: false, displayMode }));
|
||||
}
|
||||
|
||||
@@ -114,6 +114,36 @@ describe('fetchMediaBlob', () => {
|
||||
TEST_TIMEOUT
|
||||
);
|
||||
|
||||
it('selects the JSON-encoded active session instead of falling back to the first account', async () => {
|
||||
const { getActiveMediaSession, getCurrentMediaSessionScope } = await import('./mediaTransport');
|
||||
|
||||
localStorage.setItem(
|
||||
'matrixSessions',
|
||||
JSON.stringify([
|
||||
{
|
||||
baseUrl: 'https://matrix.example.org',
|
||||
userId: '@alice:example.org',
|
||||
deviceId: 'ALICE',
|
||||
accessToken: 'alice-token',
|
||||
},
|
||||
{
|
||||
baseUrl: 'https://other.example.org',
|
||||
userId: '@bob:example.org',
|
||||
deviceId: 'BOB',
|
||||
accessToken: 'bob-token',
|
||||
},
|
||||
])
|
||||
);
|
||||
localStorage.setItem('matrixActiveSession', JSON.stringify('@bob:example.org'));
|
||||
|
||||
expect(getActiveMediaSession()).toEqual({
|
||||
baseUrl: 'https://other.example.org',
|
||||
accessToken: 'bob-token',
|
||||
userId: '@bob:example.org',
|
||||
});
|
||||
expect(getCurrentMediaSessionScope()).toBe('@bob:example.org');
|
||||
});
|
||||
|
||||
it(
|
||||
'uses caller-provided auth and cache scope when present',
|
||||
async () => {
|
||||
|
||||
@@ -65,11 +65,24 @@ function parseStoredSessions(): StoredSession[] {
|
||||
}
|
||||
}
|
||||
|
||||
function getStoredActiveSessionId(): string | undefined {
|
||||
const raw = localStorage.getItem(ACTIVE_SESSION_KEY);
|
||||
if (!raw) return undefined;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return typeof parsed === 'string' ? parsed : undefined;
|
||||
} catch {
|
||||
// Keep reading pre-multi-account values written without JSON encoding.
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
function getActiveStoredSession(): StoredSession | undefined {
|
||||
if (typeof localStorage === 'undefined') return undefined;
|
||||
|
||||
const sessions = parseStoredSessions();
|
||||
const activeSessionId = localStorage.getItem(ACTIVE_SESSION_KEY);
|
||||
const activeSessionId = getStoredActiveSessionId();
|
||||
return (
|
||||
(activeSessionId
|
||||
? sessions.find((session) => session.userId === activeSessionId)
|
||||
|
||||
@@ -112,4 +112,30 @@ describe('Tauri media session coordinator', () => {
|
||||
const scopes = commands.setMediaSession.mock.calls.map(([params]) => params.scope);
|
||||
expect(scopes).toEqual(['@a:matrix.example', '@a:matrix.example']);
|
||||
});
|
||||
|
||||
it('syncs the native session when the active account changes in this window', async () => {
|
||||
mediaTransport.getActiveMediaSession
|
||||
.mockReturnValueOnce({
|
||||
baseUrl: 'https://one.example',
|
||||
accessToken: 'one',
|
||||
userId: '@a:one.example',
|
||||
})
|
||||
.mockReturnValue({
|
||||
baseUrl: 'https://two.example',
|
||||
accessToken: 'two',
|
||||
userId: '@b:two.example',
|
||||
});
|
||||
|
||||
await initTauriMediaSession();
|
||||
commands.setMediaSession.mockClear();
|
||||
|
||||
window.dispatchEvent(new Event('sable-session-changed'));
|
||||
await vi.waitFor(() => expect(commands.setMediaSession).toHaveBeenCalledTimes(1));
|
||||
|
||||
expect(commands.setMediaSession).toHaveBeenCalledWith({
|
||||
baseUrl: 'https://two.example',
|
||||
token: 'two',
|
||||
scope: '@b:two.example',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import { getActiveMediaSession } from './mediaTransport';
|
||||
const log = createLogger('tauri-media-auth');
|
||||
|
||||
let pendingNativeWrite: Promise<void> = Promise.resolve();
|
||||
let tauriMediaSessionListenersInstalled = false;
|
||||
|
||||
export const updateTauriMediaSession = (
|
||||
baseUrl?: string,
|
||||
@@ -41,9 +42,14 @@ const syncTauriMediaSession = (): Promise<void> => {
|
||||
export const initTauriMediaSession = (): Promise<void> => {
|
||||
if (!isTauri()) return Promise.resolve();
|
||||
|
||||
const initialSync = syncTauriMediaSession();
|
||||
window.addEventListener('storage', () => {
|
||||
void syncTauriMediaSession();
|
||||
});
|
||||
return initialSync;
|
||||
if (!tauriMediaSessionListenersInstalled) {
|
||||
const sync = () => {
|
||||
void syncTauriMediaSession();
|
||||
};
|
||||
window.addEventListener('storage', sync);
|
||||
window.addEventListener('sable-session-changed', sync);
|
||||
tauriMediaSessionListenersInstalled = true;
|
||||
}
|
||||
|
||||
return syncTauriMediaSession();
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import '@fontsource/space-mono/400.css';
|
||||
import '@fontsource/space-mono/700.css';
|
||||
import '@fontsource/space-mono/400-italic.css';
|
||||
import '@fontsource/space-mono/700-italic.css';
|
||||
import 'katex/dist/katex.min.css';
|
||||
import 'folds/dist/style.css';
|
||||
import { configClass, varsClass } from 'folds';
|
||||
import App from './app/pages/App';
|
||||
|
||||
+1
-1
@@ -143,7 +143,7 @@ export default defineConfig(({ command }) => {
|
||||
clearScreen: false,
|
||||
appType: 'spa',
|
||||
publicDir: false,
|
||||
base: buildConfig.base,
|
||||
base: command === 'build' && isTauriBuild ? './' : buildConfig.base,
|
||||
envPrefix: ['VITE_', 'TAURI_ENV_*'],
|
||||
define: {
|
||||
APP_VERSION: JSON.stringify(appVersion),
|
||||
|
||||
Reference in new issue
Block a user