remove unneeded imports

This commit is contained in:
Gigiaj 2025-04-14 09:49:53 -05:00
parent c413396562
commit e2c97f81a7

View file

@ -1,16 +1,11 @@
import {
type Capability,
EventDirection,
type IOpenIDCredentials,
type IOpenIDUpdate,
type ISendDelayedEventDetails,
type ISendEventDetails,
type ITurnServer,
type IReadEventRelationsResult,
type IRoomEvent,
MatrixCapabilities,
OpenIDRequestState,
type SimpleObservable,
type Widget,
WidgetDriver,
WidgetEventCapability,
@ -19,33 +14,22 @@ import {
type ISearchUserDirectoryResult,
type IGetMediaConfigResult,
type UpdateDelayedEventAction,
} from "matrix-widget-api";
} from 'matrix-widget-api';
import {
ClientEvent,
type ITurnServer as IClientTurnServer,
EventType,
type IContent,
MatrixError,
type MatrixEvent,
Direction,
THREAD_RELATION_TYPE,
type SendDelayedEventResponse,
type StateEvents,
type TimelineEvents,
MatrixClient,
Room, // Import Room
Timeline, // Import Timeline
} from "matrix-js-sdk"; // Assuming matrix-js-sdk is correctly imported
import {
type ApprovalOpts,
type CapabilitiesOpts,
WidgetLifecycle,
} from "@matrix-org/react-sdk-module-api/lib/lifecycles/WidgetLifecycle"; // Assuming this path is correct
import { logger } from "matrix-js-sdk/lib/logger"; // Assuming logger is correctly imported
} from 'matrix-js-sdk';
export class SmallWidgetDriver extends WidgetDriver {
private allowedCapabilities: Set<Capability>;
private readonly mxClient: MatrixClient; // Store the client instance
public constructor(
@ -54,7 +38,7 @@ export class SmallWidgetDriver extends WidgetDriver {
private forWidget: Widget,
private forWidgetKind: WidgetKind,
virtual: boolean, // Assuming 'virtual' might be needed later, kept for consistency
private inRoomId?: string,
private inRoomId?: string
) {
super();
this.mxClient = mx; // Store the passed instance
@ -76,19 +60,22 @@ export class SmallWidgetDriver extends WidgetDriver {
// Capability to read room state (MSC2762)
this.allowedCapabilities.add(`org.matrix.msc2762.state:${inRoomId}`);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomMember).raw,
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomMember).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, "org.matrix.msc3401.call").raw,
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call').raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomEncryption).raw,
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomEncryption).raw
);
const clientUserId = this.mxClient.getSafeUserId();
// For the legacy membership type
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Send, "org.matrix.msc3401.call.member", clientUserId)
.raw,
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
clientUserId
).raw
);
const clientDeviceId = this.mxClient.getDeviceId();
if (clientDeviceId !== null) {
@ -96,37 +83,42 @@ export class SmallWidgetDriver extends WidgetDriver {
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
"org.matrix.msc3401.call.member",
`_${clientUserId}_${clientDeviceId}`,
).raw,
'org.matrix.msc3401.call.member',
`_${clientUserId}_${clientDeviceId}`
).raw
);
// Version with no leading underscore, for room versions whose auth rules allow it
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
"org.matrix.msc3401.call.member",
`${clientUserId}_${clientDeviceId}`,
).raw,
'org.matrix.msc3401.call.member',
`${clientUserId}_${clientDeviceId}`
).raw
);
}
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, "org.matrix.msc3401.call.member").raw,
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call.member')
.raw
);
// for determining auth rules specific to the room version
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomCreate).raw,
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomCreate).raw
);
const sendRecvRoomEvents = [
"io.element.call.encryption_keys",
"org.matrix.rageshake_request",
'io.element.call.encryption_keys',
'org.matrix.rageshake_request',
EventType.Reaction,
EventType.RoomRedaction,
"io.element.call.reaction",
'io.element.call.reaction',
];
for (const eventType of sendRecvRoomEvents) {
this.allowedCapabilities.add(WidgetEventCapability.forRoomEvent(EventDirection.Send, eventType).raw);
this.allowedCapabilities.add(WidgetEventCapability.forRoomEvent(EventDirection.Receive, eventType).raw);
this.allowedCapabilities.add(
WidgetEventCapability.forRoomEvent(EventDirection.Send, eventType).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forRoomEvent(EventDirection.Receive, eventType).raw
);
}
const sendRecvToDevice = [
@ -144,16 +136,14 @@ export class SmallWidgetDriver extends WidgetDriver {
];
for (const eventType of sendRecvToDevice) {
this.allowedCapabilities.add(
WidgetEventCapability.forToDeviceEvent(EventDirection.Send, eventType).raw,
WidgetEventCapability.forToDeviceEvent(EventDirection.Send, eventType).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forToDeviceEvent(EventDirection.Receive, eventType).raw,
WidgetEventCapability.forToDeviceEvent(EventDirection.Receive, eventType).raw
);
}
}
public async validateCapabilities(requested: Set<Capability>): Promise<Set<Capability>> {
// Stubbed under the assumption voice calls will be valid thru element-call
return requested;
@ -163,24 +153,24 @@ export class SmallWidgetDriver extends WidgetDriver {
eventType: K,
content: StateEvents[K],
stateKey: string | null,
targetRoomId: string | null,
targetRoomId: string | null
): Promise<ISendEventDetails>;
public async sendEvent<K extends keyof TimelineEvents>(
eventType: K,
content: TimelineEvents[K],
stateKey: null,
targetRoomId: string | null,
targetRoomId: string | null
): Promise<ISendEventDetails>;
public async sendEvent(
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null,
targetRoomId: string | null = null
): Promise<ISendEventDetails> {
const client = this.mxClient;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error("Not in a room or not attached to a client");
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let r: { event_id: string } | null;
if (stateKey !== null) {
@ -189,17 +179,17 @@ export class SmallWidgetDriver extends WidgetDriver {
roomId,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey,
stateKey
);
} else if (eventType === EventType.RoomRedaction) {
// special case: extract the `redacts` property and call redact
r = await client.redactEvent(roomId, content["redacts"]);
r = await client.redactEvent(roomId, content['redacts']);
} else {
// message event
r = await client.sendEvent(
roomId,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents],
content as TimelineEvents[keyof TimelineEvents]
);
}
@ -216,7 +206,7 @@ export class SmallWidgetDriver extends WidgetDriver {
eventType: K,
content: StateEvents[K],
stateKey: string | null,
targetRoomId: string | null,
targetRoomId: string | null
): Promise<ISendDelayedEventDetails>;
/**
* @experimental Part of MSC4140 & MSC4157
@ -227,7 +217,7 @@ export class SmallWidgetDriver extends WidgetDriver {
eventType: K,
content: TimelineEvents[K],
stateKey: null,
targetRoomId: string | null,
targetRoomId: string | null
): Promise<ISendDelayedEventDetails>;
public async sendDelayedEvent(
delay: number | null,
@ -235,12 +225,12 @@ export class SmallWidgetDriver extends WidgetDriver {
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null,
targetRoomId: string | null = null
): Promise<ISendDelayedEventDetails> {
const client = this.mxClient;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error("Not in a room or not attached to a client");
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let delayOpts;
if (delay !== null) {
@ -253,7 +243,7 @@ export class SmallWidgetDriver extends WidgetDriver {
parent_delay_id: parentDelayId,
};
} else {
throw new Error("Must provide at least one of delay or parentDelayId");
throw new Error('Must provide at least one of delay or parentDelayId');
}
let r: SendDelayedEventResponse | null;
@ -264,7 +254,7 @@ export class SmallWidgetDriver extends WidgetDriver {
delayOpts,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey,
stateKey
);
} else {
// message event
@ -273,7 +263,7 @@ export class SmallWidgetDriver extends WidgetDriver {
delayOpts,
null,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents],
content as TimelineEvents[keyof TimelineEvents]
);
}
@ -286,10 +276,13 @@ export class SmallWidgetDriver extends WidgetDriver {
/**
* @experimental Part of MSC4140 & MSC4157
*/
public async updateDelayedEvent(delayId: string, action: UpdateDelayedEventAction): Promise<void> {
public async updateDelayedEvent(
delayId: string,
action: UpdateDelayedEventAction
): Promise<void> {
const client = this.mxClient;
if (!client) throw new Error("Not in a room or not attached to a client");
if (!client) throw new Error('Not in a room or not attached to a client');
await client._unstable_updateDelayedEvent(delayId, action);
}
@ -300,13 +293,13 @@ export class SmallWidgetDriver extends WidgetDriver {
public async sendToDevice(
eventType: string,
encrypted: boolean,
contentMap: { [userId: string]: { [deviceId: string]: object } },
contentMap: { [userId: string]: { [deviceId: string]: object } }
): Promise<void> {
const client = this.mxClient;
if (encrypted) {
const crypto = client.getCrypto();
if (!crypto) throw new Error("E2EE not enabled");
if (!crypto) throw new Error('E2EE not enabled');
// attempt to re-batch these up into a single request
const invertedContentMap: { [content: string]: { userId: string; deviceId: string }[] } = {};
@ -326,11 +319,11 @@ export class SmallWidgetDriver extends WidgetDriver {
const batch = await crypto.encryptToDeviceMessages(
eventType,
recipients,
JSON.parse(stringifiedContent),
JSON.parse(stringifiedContent)
);
await client.queueToDevice(batch);
}),
})
);
} else {
await client.queueToDevice({
@ -340,7 +333,7 @@ export class SmallWidgetDriver extends WidgetDriver {
userId,
deviceId,
payload: content,
})),
}))
),
});
}
@ -368,7 +361,7 @@ export class SmallWidgetDriver extends WidgetDriver {
msgtype: string | undefined,
stateKey: string | undefined,
limit: number,
since: string | undefined,
since: string | undefined
): Promise<IRoomEvent[]> {
limit = limit > 0 ? Math.min(limit, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary
@ -382,8 +375,10 @@ export class SmallWidgetDriver extends WidgetDriver {
if (since !== undefined && ev.getId() === since) break;
if (ev.getType() !== eventType || ev.isState()) continue;
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent()["msgtype"]) continue;
if (ev.getStateKey() !== undefined && stateKey !== undefined && ev.getStateKey() !== stateKey) continue;
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent()['msgtype'])
continue;
if (ev.getStateKey() !== undefined && stateKey !== undefined && ev.getStateKey() !== stateKey)
continue;
results.push(ev);
}
@ -399,7 +394,11 @@ export class SmallWidgetDriver extends WidgetDriver {
* @returns {Promise<IRoomEvent[]>} Resolves to the events representing the
* current values of the room state entries.
*/
public async readRoomState(roomId: string, eventType: string, stateKey: string | undefined): Promise<IRoomEvent[]> {
public async readRoomState(
roomId: string,
eventType: string,
stateKey: string | undefined
): Promise<IRoomEvent[]> {
const room = this.mxClient.getRoom(roomId);
if (room === null) return [];
const state = room.getLiveTimeline().getState(Direction.Forward);
@ -425,14 +424,14 @@ export class SmallWidgetDriver extends WidgetDriver {
from?: string,
to?: string,
limit?: number,
direction?: "f" | "b",
direction?: 'f' | 'b'
): Promise<IReadEventRelationsResult> {
const client = this.mxClient;
const dir = direction as Direction;
roomId = roomId ?? this.inRoomId ?? undefined;
if (typeof roomId !== "string") {
throw new Error("Error while reading the current room");
if (typeof roomId !== 'string') {
throw new Error('Error while reading the current room');
}
const { events, nextBatch, prevBatch } = await client.relations(
@ -440,7 +439,7 @@ export class SmallWidgetDriver extends WidgetDriver {
eventId,
relationType ?? null,
eventType ?? null,
{ from, to, limit, dir },
{ from, to, limit, dir }
);
return {
@ -450,7 +449,10 @@ export class SmallWidgetDriver extends WidgetDriver {
};
}
public async searchUserDirectory(searchTerm: string, limit?: number): Promise<ISearchUserDirectoryResult> {
public async searchUserDirectory(
searchTerm: string,
limit?: number
): Promise<ISearchUserDirectoryResult> {
const client = this.mxClient;
const { limited, results } = await client.searchUserDirectory({ term: searchTerm, limit });
@ -501,9 +503,7 @@ export class SmallWidgetDriver extends WidgetDriver {
* @returns The room IDs.
*/
public getKnownRooms(): string[] {
return this.mxClient
.getVisibleRooms()
.map((r) => r.roomId);
return this.mxClient.getVisibleRooms().map((r) => r.roomId);
}
/**
@ -514,6 +514,8 @@ export class SmallWidgetDriver extends WidgetDriver {
* or undefined if it is not a {@link MatrixError}.
*/
public processError(error: unknown): IWidgetApiErrorResponseDataDetails | undefined {
return error instanceof MatrixError ? { matrix_api_error: error.asWidgetApiErrorData() } : undefined;
return error instanceof MatrixError
? { matrix_api_error: error.asWidgetApiErrorData() }
: undefined;
}
}