Skip to content
Open
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
50 changes: 34 additions & 16 deletions src/stores/widgets/ElementWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
type ISearchUserDirectoryResult,
type IGetMediaConfigResult,
} from "matrix-widget-api";
import {

Check failure on line 30 in src/stores/widgets/ElementWidgetDriver.ts

View workflow job for this annotation

GitHub Actions / ESLint

Imports "Room" are only used as type
ClientEvent,
type ITurnServer as IClientTurnServer,
EventType,
type IContent,
MatrixError,
type MatrixEvent,
Direction,
THREAD_RELATION_TYPE,
type SendDelayedEventResponse,
type StateEvents,
type TimelineEvents,
Room,
} from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger";
import {
Expand Down Expand Up @@ -85,17 +85,17 @@
* authorized actions on the widget's behalf). Essentially this is a glorified
* set of callbacks.
*/
// TODO: Consider alternative designs for matrix-widget-api?

Check warning on line 88 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aOy&open=AZsDXCqo-D1nUVyk0aOy&pullRequest=31465
// Replace with matrix-rust-sdk?
export class ElementWidgetDriver extends WidgetDriver {
private allowedCapabilities: Set<Capability>;

Check warning on line 91 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Member 'allowedCapabilities' is never reassigned; mark it as `readonly`.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aOz&open=AZsDXCqo-D1nUVyk0aOz&pullRequest=31465

// TODO: Refactor widgetKind into the Widget class

Check warning on line 93 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO0&open=AZsDXCqo-D1nUVyk0aO0&pullRequest=31465
public constructor(
private forWidget: Widget,

Check warning on line 95 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Member 'forWidget: Widget' is never reassigned; mark it as `readonly`.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO1&open=AZsDXCqo-D1nUVyk0aO1&pullRequest=31465
private forWidgetKind: WidgetKind,

Check warning on line 96 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Member 'forWidgetKind: WidgetKind' is never reassigned; mark it as `readonly`.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO2&open=AZsDXCqo-D1nUVyk0aO2&pullRequest=31465
virtual: boolean,
private inRoomId?: string,

Check warning on line 98 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Member 'inRoomId?: string' is never reassigned; mark it as `readonly`.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO3&open=AZsDXCqo-D1nUVyk0aO3&pullRequest=31465
) {
super();

Expand Down Expand Up @@ -258,7 +258,7 @@
});
}

// TODO: Do something when the widget requests new capabilities not yet asked for

Check warning on line 261 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO4&open=AZsDXCqo-D1nUVyk0aO4&pullRequest=31465
let rememberApproved = false;
if (missing.size > 0) {
try {
Expand All @@ -275,7 +275,7 @@
}

// discard all previously allowed capabilities if they are not requested
// TODO: this results in an unexpected behavior when this function is called during the capabilities renegotiation of MSC2974 that will be resolved later.

Check warning on line 278 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO5&open=AZsDXCqo-D1nUVyk0aO5&pullRequest=31465
const allAllowed = new Set(iterableIntersection(allowedSoFar, requested));

if (rememberApproved) {
Expand Down Expand Up @@ -314,7 +314,7 @@
r = await client.sendStateEvent(
roomId,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],

Check warning on line 317 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This assertion is unnecessary since it does not change the type of the expression.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO6&open=AZsDXCqo-D1nUVyk0aO6&pullRequest=31465
stateKey,
);
} else if (eventType === EventType.RoomRedaction) {
Expand All @@ -325,7 +325,7 @@
r = await client.sendEvent(
roomId,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents],

Check warning on line 328 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This assertion is unnecessary since it does not change the type of the expression.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO7&open=AZsDXCqo-D1nUVyk0aO7&pullRequest=31465
);

if (eventType === EventType.RoomMessage) {
Expand Down Expand Up @@ -387,7 +387,7 @@
delay,
...(parentDelayId !== null && { parent_delay_id: parentDelayId }),
};
} else if (parentDelayId !== null) {

Check warning on line 390 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO8&open=AZsDXCqo-D1nUVyk0aO8&pullRequest=31465
delayOpts = {
parent_delay_id: parentDelayId,
};
Expand All @@ -396,13 +396,13 @@
}

let r: SendDelayedEventResponse | null;
if (stateKey !== null) {

Check warning on line 399 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO9&open=AZsDXCqo-D1nUVyk0aO9&pullRequest=31465
// state event
r = await client._unstable_sendDelayedStateEvent(
roomId,
delayOpts,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],

Check warning on line 405 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This assertion is unnecessary since it does not change the type of the expression.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO-&open=AZsDXCqo-D1nUVyk0aO-&pullRequest=31465
stateKey,
);
} else {
Expand All @@ -412,7 +412,7 @@
delayOpts,
null,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents],

Check warning on line 415 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This assertion is unnecessary since it does not change the type of the expression.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aO_&open=AZsDXCqo-D1nUVyk0aO_&pullRequest=31465
);
}

Expand Down Expand Up @@ -507,6 +507,38 @@
}
}

/**
* Generator function that retrieves events for readRoomTimeline
* @param room The room to check the timeline of.
* @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. 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 A generator that emits events.
*/
private *readRoomTimelineIterator(
room: Room,
eventType: string,
msgtype: string | undefined,
stateKey: string | undefined,
limit: number,
since: string | undefined,
): Generator<IRoomEvent, void, void> {
let resultCount: number = 0;
const events = [...room.getLiveTimeline().getEvents()]; // timelines are most recent last
for (let ev = events.pop(); ev && resultCount < limit && ev.getId() !== since; ev = events.pop()) {
if (ev.getType() !== eventType) continue;
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent()["msgtype"]) continue;
if (stateKey !== undefined && ev.getStateKey() !== stateKey) continue;
yield ev.getEffectiveEvent() as IRoomEvent;
resultCount++;
}
}

/**
* 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
Expand All @@ -532,23 +564,9 @@
since: string | undefined,
): Promise<IRoomEvent[]> {
limit = limit > 0 ? Math.min(limit, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary

const room = MatrixClientPeg.safeGet().getRoom(roomId);
if (room === null) return [];
const results: MatrixEvent[] = [];
const events = room.getLiveTimeline().getEvents(); // timelines are most recent last
for (let i = events.length - 1; i >= 0; i--) {
const ev = events[i];
if (results.length >= limit) break;
if (since !== undefined && ev.getId() === since) break;

if (ev.getType() !== eventType) continue;
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent()["msgtype"]) continue;
if (stateKey !== undefined && ev.getStateKey() !== stateKey) continue;
results.push(ev);
}

return results.map((e) => e.getEffectiveEvent() as IRoomEvent);
return [...this.readRoomTimelineIterator(room, eventType, msgtype, stateKey, limit, since)];
}

/**
Expand Down Expand Up @@ -607,7 +625,7 @@
inRoomId: this.inRoomId,
});
const [confirm] = await finished;
if (!confirm) {

Check warning on line 628 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aPA&open=AZsDXCqo-D1nUVyk0aPA&pullRequest=31465
observer.update({ state: OpenIDRequestState.Blocked });
} else {
observer.update({ state: OpenIDRequestState.Allowed, token: await getToken() });
Expand Down Expand Up @@ -652,7 +670,7 @@
}
}

public async readEventRelations(

Check warning on line 673 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Async method 'readEventRelations' has too many parameters (8). Maximum allowed is 7.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aPB&open=AZsDXCqo-D1nUVyk0aPB&pullRequest=31465
eventId: string,
roomId?: string,
relationType?: string,
Expand All @@ -667,7 +685,7 @@
roomId = roomId ?? SdkContextClass.instance.roomViewStore.getRoomId() ?? undefined;

if (typeof roomId !== "string") {
throw new Error("Error while reading the current room");

Check warning on line 688 in src/stores/widgets/ElementWidgetDriver.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

`new Error()` is too unspecific for a type check. Use `new TypeError()` instead.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZsDXCqo-D1nUVyk0aPC&open=AZsDXCqo-D1nUVyk0aPC&pullRequest=31465
}

const { events, nextBatch, prevBatch } = await client.relations(
Expand Down
Loading