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

Commit

Permalink
Add an action to read relations according to MSC3869
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Henneke <[email protected]>
  • Loading branch information
dhenneke committed Aug 22, 2022
1 parent 94d292a commit 87f9207
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import SettingsStore from "../../settings/SettingsStore";
import { RoomViewStore } from "../RoomViewStore";
import { ElementWidgetCapabilities } from "./ElementWidgetCapabilities";
import { navigateToPermalink } from "../../utils/permalinks/navigator";
import { Direction } from "matrix-js-sdk";

// TODO: Purge this from the universe

Expand Down Expand Up @@ -271,6 +272,48 @@ export class StopGapWidgetDriver extends WidgetDriver {
return allResults;
}

public async readEventRelations(
eventId: string,
roomId?: string,
relationType?: string,
eventType?: string,
from?: string,
to?: string,
limit?: number,
direction?: 'f' | 'b',
): Promise<{ originalEvent?: IRoomEvent, chunk: IRoomEvent[], nextBatch?: string, prevBatch?: string }> {
const client = MatrixClientPeg.get();
const dir =
Object.values(Direction as Record<string, string>).includes(direction)
? direction as Direction
: undefined;
const room = roomId !== undefined ? roomId : RoomViewStore.instance.getRoomId();

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,
};
}

public async readStateEvents(
eventType: string,
stateKey: string | undefined,
Expand Down
124 changes: 122 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,122 @@ 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: undefined,
events: [],
});

await expect(driver.readEventRelations('$event')).resolves.toEqual({
originalEvent: undefined,
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({
originalEvent: undefined,
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 },
);
});
});
});

0 comments on commit 87f9207

Please sign in to comment.