- Image compression in an encrypted room re-encoded the *plaintext* original, uploaded it unencrypted, and reused the original's encInfo, so the media sat on the server in the clear AND the attachment was undecryptable. The compressed bytes are now run through encryptFile and the synthetic upload item carries the new encInfo (buildCompressedUploadItem, unit-tested; it can never inherit the stale encInfo). - Scheduled messages (MSC4140) are PUT as raw m.room.message, bypassing the SDK encryption pipeline. The Schedule button is now hidden in encrypted rooms, handleScheduleClick no-ops there, and scheduleMessage() itself refuses with a clear error so no caller can regress this. README notes the limitation. - The GIF picker uploaded the Giphy blob unencrypted into E2EE rooms; it now mirrors the voice/attachment path (encryptFile -> upload ciphertext -> content.file). Fixes #6 Fixes #7 Fixes #11 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
import { IContent, MatrixClient, Method } from 'matrix-js-sdk';
|
|
|
|
/**
|
|
* Schedule a message via MSC4140 (delayed messages).
|
|
* Synapse implementation: pass org.matrix.msc4140.delay on the standard
|
|
* /v3/rooms send endpoint. The server stores the event and returns delay_id
|
|
* instead of event_id.
|
|
*/
|
|
export async function scheduleMessage(
|
|
mx: MatrixClient,
|
|
roomId: string,
|
|
content: IContent,
|
|
sendAtMs: number,
|
|
): Promise<string> {
|
|
// MSC4140 delayed events are PUT straight to /send/m.room.message, bypassing
|
|
// the SDK's encryptEventIfNeeded pipeline — the body would land on the server
|
|
// (and later in the timeline) in the clear. Refuse rather than leak; the
|
|
// composer also hides the Schedule button in encrypted rooms.
|
|
if (mx.getRoom?.(roomId)?.hasEncryptionStateEvent()) {
|
|
throw new Error('Scheduled messages are not supported in encrypted rooms.');
|
|
}
|
|
// A past/near target floors at 1000ms (send ~immediately) — an intentional,
|
|
// tested contract; the ScheduleMessageModal already guards ≥60s in the future.
|
|
const delayMs = Math.max(1000, Math.round(sendAtMs - Date.now()));
|
|
const txnId = `sched_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
const path = `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`;
|
|
// Use standard v3 prefix with the MSC4140 delay query param.
|
|
const res = (await mx.http.authedRequest(
|
|
Method.Put,
|
|
path,
|
|
{
|
|
'org.matrix.msc4140.delay': delayMs,
|
|
},
|
|
content,
|
|
)) as { delay_id: string };
|
|
return res.delay_id;
|
|
}
|
|
|
|
/**
|
|
* Cancel a scheduled message via MSC4140.
|
|
*/
|
|
export async function cancelScheduledMessage(mx: MatrixClient, delayId: string): Promise<void> {
|
|
const path = `/delayed_events/${encodeURIComponent(delayId)}`;
|
|
await mx.http.authedRequest(
|
|
Method.Post,
|
|
path,
|
|
undefined,
|
|
{ action: 'cancel' },
|
|
{ prefix: '/_matrix/client/unstable/org.matrix.msc4140' },
|
|
);
|
|
}
|
|
|
|
export async function restartScheduledMessage(mx: MatrixClient, delayId: string): Promise<void> {
|
|
const path = `/delayed_events/${encodeURIComponent(delayId)}`;
|
|
await mx.http.authedRequest(
|
|
Method.Post,
|
|
path,
|
|
undefined,
|
|
{ action: 'restart' },
|
|
{ prefix: '/_matrix/client/unstable/org.matrix.msc4140' },
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Send a scheduled message immediately via MSC4140 (`action: 'send'`) — the
|
|
* server dispatches the stored delayed event now, as a normal timeline event,
|
|
* instead of waiting for its timeout.
|
|
*/
|
|
export async function sendScheduledMessageNow(mx: MatrixClient, delayId: string): Promise<void> {
|
|
const path = `/delayed_events/${encodeURIComponent(delayId)}`;
|
|
await mx.http.authedRequest(
|
|
Method.Post,
|
|
path,
|
|
undefined,
|
|
{ action: 'send' },
|
|
{ prefix: '/_matrix/client/unstable/org.matrix.msc4140' },
|
|
);
|
|
}
|