Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish room state and timeline events #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 266 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export * from "./interfaces/TurnServerActions";
export * from "./interfaces/ReadRelationsAction";
export * from "./interfaces/GetMediaConfigAction";
export * from "./interfaces/UpdateDelayedEventAction";
export * from "./interfaces/UpdateStateAction";
export * from "./interfaces/UploadFileAction";
export * from "./interfaces/DownloadFileAction";

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;
}
36 changes: 36 additions & 0 deletions src/interfaces/UpdateStateAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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, IWidgetApiRequestData } from "./IWidgetApiRequest";
import { WidgetApiToWidgetAction } from "./WidgetApiAction";
import { IWidgetApiResponseData } from "./IWidgetApiResponse";
import { IRoomEvent } from "./IRoomEvent";

export interface IUpdateStateToWidgetRequestData extends IWidgetApiRequestData, IRoomEvent {
}

export interface IUpdateStateToWidgetActionRequest extends IWidgetApiRequest {
action: WidgetApiToWidgetAction.UpdateState;
data: IUpdateStateToWidgetRequestData;
}

export interface IUpdateStateToWidgetResponseData extends IWidgetApiResponseData {
// nothing
}

export interface IUpdateStateToWidgetActionResponse extends IUpdateStateToWidgetActionRequest {
response: IUpdateStateToWidgetResponseData;
}
2 changes: 2 additions & 0 deletions src/interfaces/WidgetApiAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export enum WidgetApiToWidgetAction {
ButtonClicked = "button_clicked",
SendEvent = "send_event",
SendToDevice = "send_to_device",
UpdateState = "update_state",
RoomStateSynced = "room_state_synced",
UpdateTurnServers = "update_turn_servers",
}

Expand Down
Loading
Loading