diff --git a/.changeset/fresh-peaches-smell.md b/.changeset/fresh-peaches-smell.md new file mode 100644 index 00000000..ccf81acd --- /dev/null +++ b/.changeset/fresh-peaches-smell.md @@ -0,0 +1,5 @@ +--- +'@matrix-widget-toolkit/api': minor +--- + +The api package now exposes some utility functions via the `utils` module diff --git a/packages/api/api-report.api.md b/packages/api/api-report.api.md index f116b3b2..50fdbc3e 100644 --- a/packages/api/api-report.api.md +++ b/packages/api/api-report.api.md @@ -92,6 +92,9 @@ export function isValidRedactionEvent(event: RoomEvent): event is Redac // @public export function isValidRoomMemberStateEvent(event: StateEvent): event is StateEvent; +// @public +export function makeEventFromSendStateEventResult(type: string, stateKey: string, content: T, sender: string, sendResult: ISendEventFromWidgetResponseData): StateEvent; + // @public export type MembershipState = 'join' | 'invite' | 'leave' | 'ban' | 'knock'; @@ -172,6 +175,9 @@ export type RoomMemberStateEventContent = { avatar_url?: string | null; }; +// @public +export function sendStateEventWithEventResult(widgetApi: WidgetApi, type: string, stateKey: string, content: T): Promise>; + // @public export const STATE_EVENT_POWER_LEVELS = "m.room.power_levels"; diff --git a/packages/api/src/api/index.ts b/packages/api/src/api/index.ts index 78507434..4d4cbd19 100644 --- a/packages/api/src/api/index.ts +++ b/packages/api/src/api/index.ts @@ -37,5 +37,9 @@ export type { WidgetParameters, WidgetRegistration, } from './types'; +export { + makeEventFromSendStateEventResult, + sendStateEventWithEventResult, +} from './utils'; export { WidgetApiImpl } from './WidgetApiImpl'; export type { WidgetApiOptions } from './WidgetApiImpl'; diff --git a/packages/api/src/api/utils.ts b/packages/api/src/api/utils.ts index a09f282d..8744b825 100644 --- a/packages/api/src/api/utils.ts +++ b/packages/api/src/api/utils.ts @@ -17,9 +17,11 @@ import { Capability, IRoomEvent, + ISendEventFromWidgetResponseData, Symbols, WidgetEventCapability, } from 'matrix-widget-api'; +import { StateEvent, WidgetApi } from './types'; export function convertToRawCapabilities( rawCapabilities: Array, @@ -72,3 +74,56 @@ export function isInRoom( return roomIds.includes(matrixEvent.room_id); } + +/** + * Create a state event from the arguments. + * + * @returns A state event with current timestamp origin_server_ts. + */ +export function makeEventFromSendStateEventResult( + type: string, + stateKey: string, + content: T, + sender: string, + sendResult: ISendEventFromWidgetResponseData, +): StateEvent { + if (sendResult.event_id === undefined) { + throw new Error('Send state event did not return an event ID'); + } + + return { + content, + event_id: sendResult.event_id, + origin_server_ts: Date.now(), + room_id: sendResult.room_id, + sender, + state_key: stateKey, + type, + }; +} + +/** + * Send a state event and resolve to a "virtual" state event. + * + * @returns Promise, that resolves to a state event with current timestamp origin_server_ts. + */ +export async function sendStateEventWithEventResult( + widgetApi: WidgetApi, + type: string, + stateKey: string, + content: T, +): Promise> { + if (widgetApi.widgetParameters.userId === undefined) { + throw new Error('Own user ID is undefined'); + } + + const response = await widgetApi.sendStateEvent(type, content, { stateKey }); + + return makeEventFromSendStateEventResult( + type, + stateKey, + content, + widgetApi.widgetParameters.userId, + response, + ); +}