Skip to content

Commit

Permalink
Support future sending via Widget API (MSC4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Jul 17, 2024
1 parent 5d4e05b commit 7be6b9e
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {

import { MatrixEvent, IEvent, IContent, EventStatus } from "./models/event";
import { ISendEventResponse, SendFutureRequestOpts, SendFutureResponse } from "./@types/requests";
import { EventType } from "./@types/event";
import { EventType, StateEvents } from "./@types/event";
import { logger } from "./logger";
import {
MatrixClient,
Expand Down Expand Up @@ -260,8 +260,20 @@ export class RoomWidgetClient extends MatrixClient {
futureOpts?: SendFutureRequestOpts,
): Promise<ISendEventResponse | SendFutureResponse> {
if (futureOpts) {
throw new Error("Future sending via widgets is not implemented");
// TODO: updatePendingEvent for futures?
const response = await this.widgetApi.sendRoomEvent(
event.getType(),
event.getContent(),
room.roomId,
"future_timeout" in futureOpts ? futureOpts.future_timeout : undefined,
"parent_future_id" in futureOpts ? futureOpts.parent_future_id : undefined,
);
if (!response.future_id) {
throw new Error("'future_id' absent from response to a futures request");
}
return { future_id: response.future_id };
}

let response: ISendEventFromWidgetResponseData;
try {
response = await this.widgetApi.sendRoomEvent(event.getType(), event.getContent(), room.roomId);
Expand All @@ -271,6 +283,9 @@ export class RoomWidgetClient extends MatrixClient {
}

room.updatePendingEvent(event, EventStatus.SENT, response.event_id);
if (!response.event_id) {
throw new Error("'event_id' absent from response to an event request");
}
return { event_id: response.event_id };
}

Expand All @@ -280,7 +295,33 @@ export class RoomWidgetClient extends MatrixClient {
content: any,
stateKey = "",
): Promise<ISendEventResponse> {
return await this.widgetApi.sendStateEvent(eventType, stateKey, content, roomId);
const response = await this.widgetApi.sendStateEvent(eventType, stateKey, content, roomId);
if (response.event_id === undefined) {
throw new Error("'event_id' absent from response to an event request");
}
return { event_id: response.event_id };
}

/**
* @experimental This currently relies on an unstable MSC (MSC4140).
*/
// eslint-disable-next-line
public async _unstable_sendStateFuture<K extends keyof StateEvents>(
roomId: string,
futureOpts: SendFutureRequestOpts,
eventType: K,
content: StateEvents[K],
stateKey = "",
): Promise<SendFutureResponse> {
// TODO: better type checking
return (await this.widgetApi.sendStateEvent(
eventType,
stateKey,
content,
roomId,
"future_timeout" in futureOpts ? futureOpts.future_timeout : undefined,
"parent_future_id" in futureOpts ? futureOpts.parent_future_id : undefined,
)) as SendFutureResponse;
}

public async sendToDevice(eventType: string, contentMap: SendToDeviceContentMap): Promise<{}> {
Expand Down

0 comments on commit 7be6b9e

Please sign in to comment.