fix(push): resolve the account for pushes that omit user_id

This commit is contained in:
Erwan Leboucher committed 2026-08-07 16:35:00 +02:00
1 parent 39e0ef2cb4
commit 20ade55752
2 files changed
+57 -4

No files matched your search

+44
View File
@@ -82,6 +82,50 @@ describe('service worker media auth recovery', () => {
expect(fetch).not.toHaveBeenCalled();
});
it('resolves a push without user_id against the only known account', async () => {
await swTestHooks.setSession(
'alice-window',
'alice-token',
'https://matrix.example.org',
'@alice:example.org'
);
await expect(swTestHooks.getSessionForPush(undefined)).resolves.toMatchObject({
accessToken: 'alice-token',
});
});
it('resolves a push without user_id from cache after an sw restart', async () => {
persistedSessions = JSON.stringify({
'@alice:example.org': {
accessToken: 'alice-token',
baseUrl: 'https://matrix.example.org',
userId: '@alice:example.org',
},
});
await expect(swTestHooks.getSessionForPush(undefined)).resolves.toMatchObject({
accessToken: 'alice-token',
});
});
it('refuses to guess an account for a push without user_id when several are signed in', async () => {
await swTestHooks.setSession(
'alice-window',
'alice-token',
'https://matrix.example.org',
'@alice:example.org'
);
await swTestHooks.setSession(
'bob-window',
'bob-token',
'https://matrix.example.org',
'@bob:example.org'
);
await expect(swTestHooks.getSessionForPush(undefined)).resolves.toBeUndefined();
});
it('removes a stale persisted account when a client switches users', async () => {
await swTestHooks.setSession(
'client-a',
+13 -4
View File
@@ -382,9 +382,17 @@ function mxcToNotificationUrl(mxcUrl: string, baseUrl: string): string | undefin
return `${baseUrl}/_matrix/media/v3/thumbnail/${encodeURIComponent(server)}/${encodeURIComponent(mediaId)}?width=96&height=96&method=crop`;
}
async function getSessionForPush(userId: string): Promise<SessionInfo | undefined> {
const liveSession = [...sessions.values()].find((session) => session.userId === userId);
return liveSession ?? loadPersistedSession(userId);
async function getSessionForPush(userId?: string): Promise<SessionInfo | undefined> {
if (userId) {
const liveSession = [...sessions.values()].find((session) => session.userId === userId);
return liveSession ?? loadPersistedSession(userId);
}
const liveAccounts = new Set([...sessions.values()].map((session) => session.userId));
if (liveAccounts.size === 1) return sessions.values().next().value;
const persisted = Object.values(await loadPersistedSessions());
return persisted.length === 1 ? persisted[0] : undefined;
}
/**
@@ -450,7 +458,7 @@ async function handleMinimalPushPayload(
userId: string | undefined,
windowClients: readonly Client[]
): Promise<void> {
const session = userId ? await getSessionForPush(userId) : undefined;
const session = await getSessionForPush(userId);
if (!session) {
// No session anywhere — app was never opened since install, or the user logged out.
@@ -859,6 +867,7 @@ function unavailableAuthenticatedMediaResponse(): Response {
}
export const swTestHooks = {
getSessionForPush,
requestSessionWithTimeout,
respondWithMediaAuthRecovery,
setSession,