Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Implement MSC3869: Read event relations with the Widget API #9210

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"matrix-encrypt-attachment": "^1.0.3",
"matrix-events-sdk": "^0.0.1-beta.7",
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop",
"matrix-widget-api": "^1.0.0",
"matrix-widget-api": "^1.1.0",
"minimist": "^1.2.5",
"opus-recorder": "^8.0.3",
"pako": "^2.0.3",
Expand Down
48 changes: 48 additions & 0 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
IOpenIDUpdate,
ISendEventDetails,
ITurnServer,
IReadEventRelationsResult,
IRoomEvent,
MatrixCapabilities,
OpenIDRequestState,
Expand All @@ -37,6 +38,7 @@ import { IContent, IEvent, MatrixEvent } from "matrix-js-sdk/src/models/event";
import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
import { Direction } from "matrix-js-sdk/src/matrix";

import { iterableDiff, iterableIntersection } from "../../utils/iterables";
import { MatrixClientPeg } from "../../MatrixClientPeg";
Expand Down Expand Up @@ -366,4 +368,50 @@ export class StopGapWidgetDriver extends WidgetDriver {
client.off(ClientEvent.TurnServersError, onTurnServersError);
}
}

public async readEventRelations(
eventId: string,
roomId?: string,
relationType?: string,
eventType?: string,
from?: string,
to?: string,
limit?: number,
direction?: 'f' | 'b',
): Promise<IReadEventRelationsResult> {
const client = MatrixClientPeg.get();
const dir =
direction && Object.values(Direction as Record<string, string>).includes(direction)
? direction as Direction
: undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

direction should already be compatible with the Direction enum, which means this shouldn't be needed as a check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it as a safeguard since the type guards of typescript are relatively weak hints considering this value was provided over the post message channel. On the other hand, we don't really do a similar input validation for the other fields so we could as well skip it here.

const room = roomId !== undefined ? roomId : RoomViewStore.instance.getRoomId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"room" typically means a Room object in our code - we should be able to redefine the roomId variable with roomId = roomId ?? RoomViewStore.instance.getRoomId();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRoomId() returns string | null | undefined which is incompatible with roomId?: string, but roomId = roomId ?? RoomViewStore.instance.getRoomId() ?? undefined would work.


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

const {
originalEvent,
events,
nextBatch,
prevBatch,
} = await client.relations(
room,
eventId,
relationType ?? null,
eventType ?? null,
{
from,
to,
limit,
direction: dir,
});

return {
originalEvent: originalEvent?.getEffectiveEvent(),
chunk: events.map(e => e.getEffectiveEvent()),
nextBatch,
prevBatch,
};
}
}
126 changes: 124 additions & 2 deletions test/stores/widgets/StopGapWidgetDriver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ limitations under the License.
*/

import { mocked, MockedObject } from "jest-mock";
import { Widget, WidgetKind, WidgetDriver, ITurnServer } from "matrix-widget-api";
import { MatrixClient, ClientEvent, ITurnServer as IClientTurnServer } from "matrix-js-sdk/src/client";
import { ClientEvent, ITurnServer as IClientTurnServer, MatrixClient } from "matrix-js-sdk/src/client";
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
import { Direction, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { ITurnServer, Widget, WidgetDriver, WidgetKind } from "matrix-widget-api";

import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import { RoomViewStore } from "../../../src/stores/RoomViewStore";
import { StopGapWidgetDriver } from "../../../src/stores/widgets/StopGapWidgetDriver";
import { stubClient } from "../../test-utils";

Expand Down Expand Up @@ -131,4 +133,124 @@ describe("StopGapWidgetDriver", () => {
await servers.return(undefined);
});
});

describe("readEventRelations", () => {
it('reads related events from the current room', async () => {
jest.spyOn(RoomViewStore.instance, 'getRoomId').mockReturnValue('!this-room-id');

client.relations.mockResolvedValue({
originalEvent: new MatrixEvent(),
events: [],
});

await expect(driver.readEventRelations('$event')).resolves.toEqual({
originalEvent: expect.objectContaining({ content: {} }),
chunk: [],
nextBatch: undefined,
prevBatch: undefined,
});

expect(client.relations).toBeCalledWith('!this-room-id', '$event', null, null, {});
});

it('reads related events if the original event is missing', async () => {
client.relations.mockResolvedValue({
// the relations function can return an undefined event, even
// though the typings don't permit an undefined value.
originalEvent: undefined as any,
events: [],
});

await expect(driver.readEventRelations('$event', '!room-id')).resolves.toEqual({
originalEvent: undefined,
chunk: [],
nextBatch: undefined,
prevBatch: undefined,
});

expect(client.relations).toBeCalledWith('!room-id', '$event', null, null, {});
});

it('reads related events from a selected room', async () => {
client.relations.mockResolvedValue({
originalEvent: new MatrixEvent(),
events: [new MatrixEvent(), new MatrixEvent()],
nextBatch: 'next-batch-token',
});

await expect(driver.readEventRelations('$event', '!room-id')).resolves.toEqual({
originalEvent: expect.objectContaining({ content: {} }),
chunk: [
expect.objectContaining({ content: {} }),
expect.objectContaining({ content: {} }),
],
nextBatch: 'next-batch-token',
prevBatch: undefined,
});

expect(client.relations).toBeCalledWith('!room-id', '$event', null, null, {});
});

it('reads related events with custom parameters', async () => {
client.relations.mockResolvedValue({
originalEvent: new MatrixEvent(),
events: [],
});

await expect(driver.readEventRelations(
'$event',
'!room-id',
'm.reference',
'm.room.message',
'from-token',
'to-token',
25,
'f',
)).resolves.toEqual({
originalEvent: expect.objectContaining({ content: {} }),
chunk: [],
nextBatch: undefined,
prevBatch: undefined,
});

expect(client.relations).toBeCalledWith(
'!room-id',
'$event',
'm.reference',
'm.room.message',
{
limit: 25,
from: 'from-token',
to: 'to-token',
direction: Direction.Forward,
},
);
});

it('should ignore invalid direction', async () => {
client.relations.mockResolvedValue({
originalEvent: new MatrixEvent(),
events: [],
});

await driver.readEventRelations(
'$event',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
'q' as any,
);

expect(client.relations).toBeCalledWith(
'!this-room-id',
'$event',
null,
null,
{ direction: undefined },
);
});
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6782,10 +6782,10 @@ matrix-web-i18n@^1.3.0:
"@babel/traverse" "^7.18.5"
walk "^2.3.15"

matrix-widget-api@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.0.0.tgz#0cde6839cca66ad817ab12aca3490ccc8bac97d1"
integrity sha512-cy8p/8EteRPTFIAw7Q9EgPUJc2jD19ZahMR8bMKf2NkILDcjuPMC0UWnsJyB3fSnlGw+VbGepttRpULM31zX8Q==
matrix-widget-api@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.1.0.tgz#d17b6dccf307b0257263efd5259402ae25959853"
integrity sha512-c3jpH3gy0JEjhk81zuY2rbLbSA6EKmy733Ngq5uFr0i9xrFI38CFT8UxGOsc8d5RDtmWOQEq8Jpu1I0/DO114g==
dependencies:
"@types/events" "^3.0.0"
events "^3.2.0"
Expand Down