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 11, 2024
1 parent cac6c79 commit bfc5d3c
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ import {
} from "matrix-widget-api";

import { MatrixEvent, IEvent, IContent, EventStatus } from "./models/event";
import { ISendEventResponse, ISendFutureRequestOpts, ISendFutureResponse } from "./@types/requests";
import { EventType } from "./@types/event";
import {
ISendActionFutureResponse,
ISendEventResponse,
ISendFutureRequestOpts,
ISendFutureResponse,
ISendTimeoutFutureResponse,
} from "./@types/requests";
import { EventType, StateEvents } from "./@types/event";
import { logger } from "./logger";
import {
MatrixClient,
Expand Down Expand Up @@ -254,14 +260,37 @@ export class RoomWidgetClient extends MatrixClient {
event: MatrixEvent,
futureOpts: F,
): Promise<ISendFutureResponse<F>>;
protected async encryptAndSendEvent<F extends ISendFutureRequestOpts>(
protected async encryptAndSendEvent(
room: Room,
event: MatrixEvent,
futureOpts?: F,
): Promise<ISendEventResponse | ISendFutureResponse<F>> {
futureOpts?: ISendFutureRequestOpts,
): Promise<ISendEventResponse | ISendTimeoutFutureResponse | ISendActionFutureResponse> {
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,

Check failure on line 274 in src/embedded.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Expected 2-3 arguments, but got 5.
"future_group_id" in futureOpts ? futureOpts.future_group_id : undefined,
);
if (!response.future_group_id) {
throw new Error("'future_group_id' absent from response to a futures request");
}
if (!response.send_token) {
throw new Error("'send_token' absent from response to a futures request");
}
if (!response.cancel_token) {
throw new Error("'cancel_token' absent from response to a futures request");
}
return {
future_group_id: response.future_group_id,
send_token: response.send_token,
cancel_token: response.cancel_token,
...(response.refresh_token && { refresh_token: response.refresh_token }),

Check failure on line 290 in src/embedded.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Spread types may only be created from object types.
};
}

let response: ISendEventFromWidgetResponseData;
try {
response = await this.widgetApi.sendRoomEvent(event.getType(), event.getContent(), room.roomId);
Expand All @@ -271,6 +300,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 +312,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) {

Check failure on line 316 in src/embedded.ts

View workflow job for this annotation

GitHub Actions / Jest [unit] (Node lts/*)

RoomWidgetClient › state events › sends

TypeError: Cannot read properties of undefined (reading 'event_id') at event_id (src/embedded.ts:316:22) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)

Check failure on line 316 in src/embedded.ts

View workflow job for this annotation

GitHub Actions / Jest [unit] (Node 22)

RoomWidgetClient › state events › sends

TypeError: Cannot read properties of undefined (reading 'event_id') at event_id (src/embedded.ts:316:22) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
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, F extends ISendFutureRequestOpts>(
roomId: string,
futureOpts: F,
eventType: K,
content: StateEvents[K],
stateKey = "",
): Promise<ISendFutureResponse<F>> {
// TODO: better type checking
return (await this.widgetApi.sendStateEvent(
eventType,
stateKey,
content,
roomId,
"future_timeout" in futureOpts ? futureOpts.future_timeout : undefined,

Check failure on line 339 in src/embedded.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Expected 3-4 arguments, but got 6.
"future_group_id" in futureOpts ? futureOpts.future_group_id : undefined,
)) as unknown as ISendFutureResponse<F>;
}

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

0 comments on commit bfc5d3c

Please sign in to comment.