2026-06-04 10:26:08 -04:00
|
|
|
import { IContent, MatrixClient, Method } from 'matrix-js-sdk';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Schedule a message via MSC4140 (delayed messages).
|
2026-06-04 17:59:39 -04:00
|
|
|
* 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.
|
2026-06-04 10:26:08 -04:00
|
|
|
*/
|
|
|
|
|
export async function scheduleMessage(
|
|
|
|
|
mx: MatrixClient,
|
|
|
|
|
roomId: string,
|
|
|
|
|
content: IContent,
|
|
|
|
|
sendAtMs: number,
|
|
|
|
|
): Promise<string> {
|
2026-06-04 15:05:09 -04:00
|
|
|
const delayMs = Math.max(1000, Math.round(sendAtMs - Date.now()));
|
2026-06-04 10:26:08 -04:00
|
|
|
const txnId = `sched_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
2026-06-04 15:05:09 -04:00
|
|
|
const path = `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`;
|
2026-06-04 17:59:39 -04:00
|
|
|
// 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 };
|
2026-06-04 10:26:08 -04:00
|
|
|
return res.delay_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cancel a scheduled message via MSC4140.
|
|
|
|
|
*/
|
|
|
|
|
export async function cancelScheduledMessage(mx: MatrixClient, delayId: string): Promise<void> {
|
2026-06-04 15:05:09 -04:00
|
|
|
const path = `/delayed_events/${encodeURIComponent(delayId)}`;
|
|
|
|
|
await mx.http.authedRequest(
|
|
|
|
|
Method.Post,
|
|
|
|
|
path,
|
|
|
|
|
undefined,
|
|
|
|
|
{ action: 'cancel' },
|
2026-06-04 17:59:39 -04:00
|
|
|
{ prefix: '/_matrix/client/unstable/org.matrix.msc4140' },
|
2026-06-04 15:05:09 -04:00
|
|
|
);
|
2026-06-04 10:26:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function restartScheduledMessage(mx: MatrixClient, delayId: string): Promise<void> {
|
2026-06-04 15:05:09 -04:00
|
|
|
const path = `/delayed_events/${encodeURIComponent(delayId)}`;
|
|
|
|
|
await mx.http.authedRequest(
|
|
|
|
|
Method.Post,
|
|
|
|
|
path,
|
|
|
|
|
undefined,
|
|
|
|
|
{ action: 'restart' },
|
2026-06-04 17:59:39 -04:00
|
|
|
{ prefix: '/_matrix/client/unstable/org.matrix.msc4140' },
|
2026-06-04 15:05:09 -04:00
|
|
|
);
|
2026-06-04 10:26:08 -04:00
|
|
|
}
|