fix(e2ee): Prepare room encryption before sending (#1643)

<!-- 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. -->

We need to be sure that the key exchanges was done before we can start
sending the first message

#### Type of change

- [x] 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:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] 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:
Erwan Leboucher authored and GitHub committed 2026-08-05 08:59:52 +00:00
commit 518b4d9edd
2 files changed
+19 -1

No files matched your search

+10 -1
View File
@@ -274,6 +274,7 @@ export const getReplyContent = (
const log = createLogger('RoomInput');
const debugLog = createDebugLogger('RoomInput');
const ENCRYPTION_PREPARATION_INTERVAL_MS = 60_000;
interface ReplyEventContent {
'm.relates_to'?: IEventRelation;
}
@@ -497,9 +498,17 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const handlePaste = useFilePasteHandler(handleFiles);
const dropZoneVisible = useFileDropZone(fileDropContainerRef, handleFiles);
const [hasText, setHasText] = useState(false);
const lastEncryptionPreparationAt = useRef(0);
const handleEditorChange = useCallback(() => {
setHasText(!isEmptyEditor(editor));
}, [editor]);
if (!room.hasEncryptionStateEvent()) return;
const now = Date.now();
if (now - lastEncryptionPreparationAt.current < ENCRYPTION_PREPARATION_INTERVAL_MS) return;
lastEncryptionPreparationAt.current = now;
mx.getCrypto()?.prepareToEncrypt(room);
}, [editor, mx, room]);
const hasContent = hasText || selectedFiles.length > 0;
const isComposing = useComposingCheck();
@@ -5,8 +5,11 @@ import { useEffect, useState } from 'react';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { scheduleDecrypt } from '$utils/decryptScheduler';
import { createDebugLogger } from '$utils/debugLogger';
import * as Sentry from '@sentry/react';
const debugLog = createDebugLogger('EncryptedContent');
type EncryptedContentProps = {
mEvent: MatrixEvent;
children: () => ReactNode;
@@ -39,6 +42,12 @@ export function EncryptedContent({ mEvent, children }: EncryptedContentProps) {
toggleEncrypted(mEvent.getType() === (EventType.RoomMessageEncrypted as string));
const handleDecrypted: MatrixEventHandlerMap[MatrixEventEvent.Decrypted] = (event) => {
if (event.isDecryptionFailure()) {
debugLog.error('error', 'Failed to decrypt room event', {
eventId: event.getId(),
roomId: event.getRoomId(),
sender: event.getSender(),
reason: event.decryptionFailureReason ?? 'UNKNOWN_ERROR',
});
Sentry.metrics.count('sable.decryption.failure', 1, {
attributes: { reason: event.decryptionFailureReason ?? 'UNKNOWN_ERROR' },
});