From 7f9d042bbe03ff193133f0d698a5b0bc38caa5ad Mon Sep 17 00:00:00 2001 From: 7w1 Date: Wed, 5 Aug 2026 21:21:13 -0500 Subject: [PATCH] fix: scope by account --- .../ScheduledMessagesList.test.tsx | 10 +++-- .../schedule-send/ScheduledMessagesList.tsx | 2 +- .../room/roomScheduleCoordinator.test.ts | 41 ++++++++++++++++--- src/app/state/room/roomScheduleCoordinator.ts | 15 ++++++- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/app/features/room/schedule-send/ScheduledMessagesList.test.tsx b/src/app/features/room/schedule-send/ScheduledMessagesList.test.tsx index 2137f9248..d5d20be54 100644 --- a/src/app/features/room/schedule-send/ScheduledMessagesList.test.tsx +++ b/src/app/features/room/schedule-send/ScheduledMessagesList.test.tsx @@ -144,8 +144,8 @@ describe('ScheduledMessagesList cancellation', () => { testState.cancelDelayedEvent.mockReset(); testState.coordinatorRun.mockReset(); testState.invalidateQueries.mockReset(); - testState.coordinatorRun.mockImplementation((_roomId: string, operation: () => unknown) => - operation() + testState.coordinatorRun.mockImplementation( + (_mx: unknown, _roomId: string, operation: () => unknown) => operation() ); }); @@ -157,7 +157,11 @@ describe('ScheduledMessagesList cancellation', () => { fireEvent.click(cancel); fireEvent.click(cancel); - expect(testState.coordinatorRun).toHaveBeenCalledWith(room.roomId, expect.any(Function)); + expect(testState.coordinatorRun).toHaveBeenCalledWith( + testState.matrix, + room.roomId, + expect.any(Function) + ); expect(testState.cancelDelayedEvent).toHaveBeenCalledOnce(); expect(cancel).toBeDisabled(); expect(cancel).toHaveAttribute('aria-busy', 'true'); diff --git a/src/app/features/room/schedule-send/ScheduledMessagesList.tsx b/src/app/features/room/schedule-send/ScheduledMessagesList.tsx index c9aafa05a..98f19beee 100644 --- a/src/app/features/room/schedule-send/ScheduledMessagesList.tsx +++ b/src/app/features/room/schedule-send/ScheduledMessagesList.tsx @@ -199,7 +199,7 @@ export function ScheduledMessagesList({ room, onEditMessage }: ScheduledMessages })); try { - await roomScheduleCoordinator.run(room.roomId, () => cancelDelayedEvent(mx, delayId)); + await roomScheduleCoordinator.run(mx, room.roomId, () => cancelDelayedEvent(mx, delayId)); invalidateEvents(); setCancellationStates((states) => { const next = { ...states }; diff --git a/src/app/state/room/roomScheduleCoordinator.test.ts b/src/app/state/room/roomScheduleCoordinator.test.ts index 98f67e548..758f0ae1f 100644 --- a/src/app/state/room/roomScheduleCoordinator.test.ts +++ b/src/app/state/room/roomScheduleCoordinator.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it, vi } from 'vitest'; import { roomScheduleCoordinator } from './roomScheduleCoordinator'; +const account = (userId: string) => ({ getSafeUserId: () => userId }); +const alice = account('@alice:example.org'); + function deferred() { let resolve!: () => void; const promise = new Promise((resolvePromise) => { @@ -14,12 +17,12 @@ describe('roomScheduleCoordinator', () => { const first = deferred(); const order: string[] = []; - const firstOperation = roomScheduleCoordinator.run('!room:example.org', async () => { + const firstOperation = roomScheduleCoordinator.run(alice, '!room:example.org', async () => { order.push('first-start'); await first.promise; order.push('first-end'); }); - const secondOperation = roomScheduleCoordinator.run('!room:example.org', async () => { + const secondOperation = roomScheduleCoordinator.run(alice, '!room:example.org', async () => { order.push('second'); }); @@ -35,11 +38,11 @@ describe('roomScheduleCoordinator', () => { const second = vi.fn<() => Promise>(async () => 'completed'); await expect( - roomScheduleCoordinator.run('!room:example.org', async () => { + roomScheduleCoordinator.run(alice, '!room:example.org', async () => { throw new Error('cancel failed'); }) ).rejects.toThrow('cancel failed'); - await expect(roomScheduleCoordinator.run('!room:example.org', second)).resolves.toBe( + await expect(roomScheduleCoordinator.run(alice, '!room:example.org', second)).resolves.toBe( 'completed' ); @@ -50,8 +53,34 @@ describe('roomScheduleCoordinator', () => { const first = deferred(); const second = vi.fn<() => Promise>(async () => 'completed'); - const firstOperation = roomScheduleCoordinator.run('!first:example.org', () => first.promise); - const secondOperation = roomScheduleCoordinator.run('!second:example.org', second); + const firstOperation = roomScheduleCoordinator.run( + alice, + '!first:example.org', + () => first.promise + ); + const secondOperation = roomScheduleCoordinator.run(alice, '!second:example.org', second); + + await expect(secondOperation).resolves.toBe('completed'); + expect(second).toHaveBeenCalledOnce(); + + first.resolve(); + await firstOperation; + }); + + it('does not serialize the same room ID across accounts', async () => { + const first = deferred(); + const second = vi.fn<() => Promise>(async () => 'completed'); + + const firstOperation = roomScheduleCoordinator.run( + alice, + '!room:example.org', + () => first.promise + ); + const secondOperation = roomScheduleCoordinator.run( + account('@bob:example.org'), + '!room:example.org', + second + ); await expect(secondOperation).resolves.toBe('completed'); expect(second).toHaveBeenCalledOnce(); diff --git a/src/app/state/room/roomScheduleCoordinator.ts b/src/app/state/room/roomScheduleCoordinator.ts index ddfa595ed..80182b760 100644 --- a/src/app/state/room/roomScheduleCoordinator.ts +++ b/src/app/state/room/roomScheduleCoordinator.ts @@ -1,4 +1,15 @@ import { createKeyedQueue } from '$utils/keyedQueue'; +import type { MatrixClient } from '$types/matrix-sdk'; -/** Serializes delayed-event mutations for each room without blocking other rooms. */ -export const roomScheduleCoordinator = { run: createKeyedQueue() }; +const runQueued = createKeyedQueue(); + +/** Serializes delayed-event mutations per account and room. */ +export const roomScheduleCoordinator = { + run( + mx: Pick, + roomId: string, + operation: () => T | PromiseLike + ): Promise { + return runQueued(`${mx.getSafeUserId()}\0${roomId}`, operation); + }, +};