Skip to content

Commit

Permalink
Distinguish room state and timeline events
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown committed Dec 6, 2024
1 parent bbff7c1 commit dc635a0
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 56 deletions.
302 changes: 258 additions & 44 deletions src/ClientWidgetApi.ts

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions src/driver/WidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export abstract class WidgetDriver {
* the client will return all the events.
* @param eventType The event type to be read.
* @param msgtype The msgtype of the events to be read, if applicable/defined.
* @param stateKey The state key of the events to be read, if applicable/defined.
* @param limit The maximum number of events to retrieve per room. Will be zero to denote "as many
* as possible".
* @param roomIds When null, the user's currently viewed room. Otherwise, the list of room IDs
Expand All @@ -204,6 +205,7 @@ export abstract class WidgetDriver {
* Otherwise, the event ID at which only subsequent events will be returned, as many as specified
* in "limit".
* @returns {Promise<IRoomEvent[]>} Resolves to the room events, or an empty array.
* @deprecated Clients are advised to implement {@link WidgetDriver.readRoomTimeline} instead.
*/
public readRoomEvents(
eventType: string,
Expand All @@ -229,6 +231,7 @@ export abstract class WidgetDriver {
* @param roomIds When null, the user's currently viewed room. Otherwise, the list of room IDs
* to look within, possibly containing Symbols.AnyRoom to denote all known rooms.
* @returns {Promise<IRoomEvent[]>} Resolves to the state events, or an empty array.
* @deprecated Clients are advised to implement {@link WidgetDriver.readRoomTimeline} instead.
*/
public readStateEvents(
eventType: string,
Expand All @@ -239,6 +242,46 @@ export abstract class WidgetDriver {
return Promise.resolve([]);
}

/**
* Reads all events of the given type, and optionally `msgtype` (if applicable/defined),
* the user has access to. The widget API will have already verified that the widget is
* capable of receiving the events. Less events than the limit are allowed to be returned,
* but not more. If `roomIds` is supplied, it may contain `Symbols.AnyRoom` to denote that
* `limit` in each of the client's known rooms should be returned. When `null`, only the
* room the user is currently looking at should be considered. If `since` is specified but
* the event ID isn't present in the number of events fetched by the client due to `limit`,
* the client will return all the events.
* @param roomId The ID of the room to look within.
* @param eventType The event type to be read.
* @param msgtype The msgtype of the events to be read, if applicable/defined.
* @param stateKey The state key of the events to be read, if applicable/defined.
* @param limit The maximum number of events to retrieve per room. Will be zero to denote "as many
* as possible".
* @param since When null, retrieves the number of events specified by the "limit" parameter.
* Otherwise, the event ID at which only subsequent events will be returned, as many as specified
* in "limit".
* @returns {Promise<IRoomEvent[]>} Resolves to the room events, or an empty array.
*/
public readRoomTimeline(
roomId: string,
eventType: string,
msgtype: string | undefined,
stateKey: string | undefined,
limit: number,
since: string | undefined,
): Promise<IRoomEvent[]> {
if (stateKey === undefined) return this.readRoomEvents(eventType, msgtype, limit, [roomId], since);
else return this.readStateEvents(eventType, stateKey, limit, [roomId]);
}

public readRoomState(
roomId: string,
eventType: string,
stateKey: string | undefined,
): Promise<IRoomEvent[]> {
return Promise.resolve([]);
}

/**
* Reads all events that are related to a given event. The widget API will
* have already verified that the widget is capable of receiving the event,
Expand Down Expand Up @@ -360,6 +403,15 @@ export abstract class WidgetDriver {
throw new Error("Download file is not implemented");
}

/**
* Gets the IDs of all joined or invited rooms currently known to the
* client.
* @returns The room IDs.
*/
public getKnownRooms(): string[] {
throw new Error("Querying known rooms is not implemented");
}

/**
* Expresses an error thrown by this driver in a format compatible with the Widget API.
* @param error The error to handle.
Expand Down
28 changes: 28 additions & 0 deletions src/interfaces/RoomStateSyncedAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IWidgetApiRequest, IWidgetApiRequestEmptyData } from "./IWidgetApiRequest";
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
import { IWidgetApiAcknowledgeResponseData } from "./IWidgetApiResponse";

export interface IRoomStateSyncedActionRequest extends IWidgetApiRequest {
action: WidgetApiToWidgetAction.RoomStateSynced;
data: IWidgetApiRequestEmptyData;
}

export interface IRoomStateSyncedActionResponse extends IRoomStateSyncedActionRequest {
response: IWidgetApiAcknowledgeResponseData;
}
1 change: 1 addition & 0 deletions src/interfaces/SendEventAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface ISendEventFromWidgetActionResponse extends ISendEventFromWidget
}

export interface ISendEventToWidgetRequestData extends IWidgetApiRequestData, IRoomEvent {
block: 'timeline' | 'state';
}

export interface ISendEventToWidgetActionRequest extends IWidgetApiRequest {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/WidgetApiAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum WidgetApiToWidgetAction {
ButtonClicked = "button_clicked",
SendEvent = "send_event",
SendToDevice = "send_to_device",
RoomStateSynced = "room_state_synced",
UpdateTurnServers = "update_turn_servers",
}

Expand Down
Loading

0 comments on commit dc635a0

Please sign in to comment.