fix: prevent duplicate media and reactions

This commit is contained in:
Erwan Leboucher committed 2026-08-13 12:15:13 +02:00
1 parent aecb2a16d7
commit 851e8d91c2
6 files changed
+88 -6

No files matched your search

+22
View File
@@ -781,6 +781,28 @@ describe('RoomInput submit regressions', () => {
);
});
it('sends a replied attachment once when a touch produces pointerup and click', async () => {
const send = deferred<{ event_id: string }>();
const file = new File(['attachment'], 'attachment.txt', { type: 'text/plain' });
testState.isMobile = true;
testState.sendIndividualAttachmentAsCaption = true;
testState.pendingUploads = [{ status: 'success', file, mxc: 'mxc://example/attachment' }];
testState.matrix.sendMessage.mockReturnValue(send.promise);
render(<RoomInputHarness initialReply />);
fireEvent.click(screen.getByRole('button', { name: 'Prepare attachment' }));
const submit = sendButton();
fireEvent.pointerDown(submit, { pointerType: 'touch' });
fireEvent.pointerUp(submit, { pointerType: 'touch' });
fireEvent.click(submit);
await waitFor(() => expect(testState.matrix.sendMessage).toHaveBeenCalledOnce());
expect(testState.matrix.sendMessage.mock.calls[0]?.[2]?.['m.relates_to']).toEqual(
expect.objectContaining({ 'm.in_reply_to': expect.anything() })
);
send.resolve({ event_id: '$event' });
});
it('sends on touch pointerup when the tap never produces a click', async () => {
render(<RoomInputHarness />);
fireEvent.click(screen.getByRole('button', { name: 'Compose text' }));
+8 -3
View File
@@ -393,6 +393,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const [isSending, setIsSending] = useState(false);
const [ingestingFiles, setIngestingFiles] = useState(false);
const fileIngestionCountRef = useRef(0);
const submissionInFlightRef = useRef(false);
const composerControllerRef = useRef<ComposerController>();
composerControllerRef.current ??= createComposerController();
// Bumped when this composer goes away, so async work started for a previous
@@ -1714,16 +1715,20 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
);
const submit = useCallback(() => {
if (submissionInFlightRef.current) return Promise.resolve(undefined);
submissionInFlightRef.current = true;
// A mobile edit replaces an existing event, so it owns neither the draft nor the reply.
const isMobileEdit = Boolean(editingEvent && isMobile);
const submission = takeSubmission({
clearEditor: !isMobileEdit,
claimReplyDraft: !isMobileEdit,
});
return (
const queued =
composerControllerRef.current?.enqueue((isLive) => executeSubmit(submission, isLive)) ??
Promise.resolve(undefined)
);
Promise.resolve(undefined);
return queued.finally(() => {
submissionInFlightRef.current = false;
});
}, [editingEvent, executeSubmit, isMobile, takeSubmission]);
const handleKeyDown: KeyboardEventHandler = useCallback(
@@ -170,6 +170,24 @@ describe('pickNativeFile', () => {
]);
});
it('does not return the same gallery URI twice', async () => {
const uri = androidUri('content://media/external/images/media/1000000034');
mocks.androidFs.showOpenFilePicker.mockResolvedValue([uri, uri]);
mocks.androidFs.getMetadata.mockResolvedValue({
type: 'File',
name: '1000000034.png',
lastModified: new Date(1700000000000),
byteLength: 3,
mimeType: 'image/png',
});
const files = await pickNativeFile('media');
expect(files).toHaveLength(1);
expect(mocks.androidFs.getMetadata).toHaveBeenCalledOnce();
expect(mocks.androidFs.readFile).toHaveBeenCalledOnce();
});
it('picks documents through the file picker', async () => {
mocks.androidFs.showOpenFilePicker.mockResolvedValue([androidUri('content://docs/1')]);
mocks.androidFs.getMetadata.mockResolvedValue({
+7 -1
View File
@@ -88,9 +88,15 @@ const pickAndroidFiles = async (
mimeTypes: pickerMode === 'media' ? MEDIA_MIME_TYPES : [],
multiple: true,
});
const seenUris = new Set<string>();
const uniqueUris = uris.filter(({ uri }) => {
if (seenUris.has(uri)) return false;
seenUris.add(uri);
return true;
});
const files = await Promise.all(
uris.map(async (uri, index): Promise<File | undefined> => {
uniqueUris.map(async (uri, index): Promise<File | undefined> => {
try {
const metadata = await AndroidFs.getMetadata(uri);
if (metadata.type !== 'File') return undefined;
+32 -1
View File
@@ -13,10 +13,15 @@ const mediaTransport = vi.hoisted(() => ({
getCurrentMediaSessionScope: vi.fn<() => string>(() => '@user:example.com'),
}));
const reactions = vi.hoisted(() => ({
getEventReactions: vi.fn<() => unknown>(),
}));
vi.mock('@tauri-apps/api/core', () => tauriApi);
vi.mock('./mediaTransport', () => mediaTransport);
vi.mock('./room/relations', () => reactions);
const { mxcUrlToHttp, rewriteAuthenticatedMediaUrl } = await import('./matrix');
const { mxcUrlToHttp, rewriteAuthenticatedMediaUrl, toggleReaction } = await import('./matrix');
describe('rewriteAuthenticatedMediaUrl', () => {
beforeEach(() => {
@@ -101,6 +106,32 @@ describe('rewriteAuthenticatedMediaUrl', () => {
});
});
describe('toggleReaction', () => {
it('redacts the existing reaction from the current user', () => {
const reaction = {
getId: () => '$reaction',
getSender: () => '@me:example.org',
};
reactions.getEventReactions.mockReturnValue({
getSortedAnnotationsByKey: () => [['👍', new Set([reaction])]],
});
const mx = {
getUserId: () => '@me:example.org',
redactEvent: vi.fn<(roomId: string, eventId: string) => void>(),
sendEvent: vi.fn<(...args: unknown[]) => void>(),
} as unknown as MatrixClient;
const room = {
roomId: '!room:example.org',
getUnfilteredTimelineSet: vi.fn<() => unknown>(),
};
toggleReaction(mx, room as never, '$message', '👍');
expect(mx.redactEvent).toHaveBeenCalledWith('!room:example.org', '$reaction');
expect(mx.sendEvent).not.toHaveBeenCalled();
});
});
describe('mxcUrlToHttp', () => {
it('rewrites SDK legacy media URLs under Tauri without useAuthentication', () => {
tauriApi.isTauri.mockReturnValue(true);
+1 -1
View File
@@ -544,7 +544,7 @@ export const toggleReaction = (
const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : [];
const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!));
if (myReaction && myReaction.isRelation?.()) {
if (myReaction) {
const eventId = myReaction.getId();
if (eventId) mx.redactEvent(room.roomId, eventId);
return;