-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Module API Implementation: ClientApi #31138
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
Merged
MidhunSureshR
merged 14 commits into
midhun/module-impl/builtin
from
midhun/module-impl/client
Nov 4, 2025
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2449557
Add implementation for AccountDataApi
MidhunSureshR 335491e
Add implementation for Room
MidhunSureshR 3be766d
Add implementation for ClientApi
MidhunSureshR c2d68f8
Create ClientApi in Api.ts
MidhunSureshR b94d40f
Write tests
MidhunSureshR 507eaa0
Use nullish coalescing assignment
MidhunSureshR f4e8e79
Implement openRoom in NavigationApi
MidhunSureshR 6dc1431
Write tests
MidhunSureshR 044a275
Add implementation for StoresApi
MidhunSureshR 353609c
Write tests
MidhunSureshR fdbe414
Fix circular dependency
MidhunSureshR 6fc6df6
Merge pull request #31141 from element-hq/midhun/module-impl/stores
MidhunSureshR 8a59a16
Merge pull request #31140 from element-hq/midhun/module-impl/navigation
MidhunSureshR 5f8aa32
Change to class field
MidhunSureshR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { Watchable, type AccountDataApi as IAccountDataApi } from "@element-hq/element-web-module-api"; | ||
| import { ClientEvent, type MatrixEvent, type MatrixClient } from "matrix-js-sdk/src/matrix"; | ||
|
|
||
| import { MatrixClientPeg } from "../MatrixClientPeg"; | ||
|
|
||
| export class AccountDataApi implements IAccountDataApi { | ||
| public get(eventType: string): Watchable<unknown> { | ||
| const cli = MatrixClientPeg.safeGet(); | ||
| return new AccountDataWatchable(cli, eventType); | ||
| } | ||
|
|
||
| public async set(eventType: string, content: any): Promise<void> { | ||
| const cli = MatrixClientPeg.safeGet(); | ||
| //@ts-expect-error: JS-SDK accepts known event-types, intentionally allow arbitrary types. | ||
| await cli.setAccountData(eventType, content); | ||
| } | ||
|
|
||
| public async delete(eventType: string): Promise<void> { | ||
| const cli = MatrixClientPeg.safeGet(); | ||
| //@ts-expect-error: JS-SDK accepts known event-types, intentionally allow arbitrary types. | ||
| await cli.deleteAccountData(eventType); | ||
| } | ||
| } | ||
|
|
||
| class AccountDataWatchable extends Watchable<unknown> { | ||
| public constructor( | ||
| private cli: MatrixClient, | ||
| private eventType: string, | ||
| ) { | ||
| //@ts-expect-error: JS-SDK accepts known event-types, intentionally allow arbitrary types. | ||
| super(cli.getAccountData(eventType)?.getContent()); | ||
| } | ||
|
|
||
| private onAccountData = (event: MatrixEvent): void => { | ||
| if (event.getType() === this.eventType) { | ||
| this.value = event.getContent(); | ||
| } | ||
| }; | ||
|
|
||
| protected onFirstWatch(): void { | ||
| this.cli.on(ClientEvent.AccountData, this.onAccountData); | ||
| } | ||
|
|
||
| protected onLastWatch(): void { | ||
| this.cli.off(ClientEvent.AccountData, this.onAccountData); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
| import type { ClientApi as IClientApi, Room } from "@element-hq/element-web-module-api"; | ||
| import { Room as ModuleRoom } from "./models/Room"; | ||
| import { AccountDataApi } from "./AccountDataApi"; | ||
| import { MatrixClientPeg } from "../MatrixClientPeg"; | ||
|
|
||
| export class ClientApi implements IClientApi { | ||
| private accountDataApi?: AccountDataApi; | ||
|
|
||
| public getRoom(roomId: string): Room | null { | ||
| const sdkRoom = MatrixClientPeg.safeGet().getRoom(roomId); | ||
| if (sdkRoom) return new ModuleRoom(sdkRoom); | ||
| return null; | ||
| } | ||
|
|
||
| public get accountData(): AccountDataApi { | ||
| this.accountDataApi ??= new AccountDataApi(); | ||
| return this.accountDataApi; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { type Room as IRoom, Watchable } from "@element-hq/element-web-module-api"; | ||
| import { RoomEvent, type Room as SdkRoom } from "matrix-js-sdk/src/matrix"; | ||
|
|
||
| export class Room implements IRoom { | ||
| public name: Watchable<string>; | ||
|
|
||
| public constructor(private sdkRoom: SdkRoom) { | ||
| this.name = new WatchableName(sdkRoom); | ||
| } | ||
|
|
||
| public getLastActiveTimestamp(): number { | ||
| return this.sdkRoom.getLastActiveTimestamp(); | ||
| } | ||
|
|
||
| public get id(): string { | ||
| return this.sdkRoom.roomId; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A custom watchable for room name. | ||
| */ | ||
| class WatchableName extends Watchable<string> { | ||
| public constructor(private sdkRoom: SdkRoom) { | ||
| super(sdkRoom.name); | ||
| } | ||
|
|
||
| private onNameUpdate = (): void => { | ||
| super.value = this.sdkRoom.name; | ||
| }; | ||
| protected onFirstWatch(): void { | ||
| this.sdkRoom.on(RoomEvent.Name, this.onNameUpdate); | ||
| } | ||
|
|
||
| protected onLastWatch(): void { | ||
| this.sdkRoom.off(RoomEvent.Name, this.onNameUpdate); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { ClientEvent } from "matrix-js-sdk/src/matrix"; | ||
|
|
||
| import { AccountDataApi } from "../../../src/modules/AccountDataApi"; | ||
| import { mkEvent, stubClient } from "../../test-utils/test-utils"; | ||
|
|
||
| describe("AccountDataApi", () => { | ||
| describe("AccountDataWatchable", () => { | ||
| it("should return content of account data event on get()", () => { | ||
| const cli = stubClient(); | ||
| const api = new AccountDataApi(); | ||
| // Mock cli to return a event | ||
| const content = { foo: "bar" }; | ||
| const event = mkEvent({ content, type: "m.test", user: "@foobar:matrix.org", event: true }); | ||
| cli.getAccountData = () => event; | ||
| expect(api.get("m.test").value).toStrictEqual(content); | ||
| }); | ||
|
|
||
| it("should update value on event", () => { | ||
| const cli = stubClient(); | ||
| const api = new AccountDataApi(); | ||
| // Mock cli to return a event | ||
| const content = { foo: "bar" }; | ||
| const event = mkEvent({ content, type: "m.test", user: "@foobar:matrix.org", event: true }); | ||
| cli.getAccountData = () => event; | ||
|
|
||
| const watchable = api.get("m.test"); | ||
| expect(watchable.value).toStrictEqual(content); | ||
|
|
||
| const fn = jest.fn(); | ||
| watchable.watch(fn); | ||
|
|
||
| // Let's say that the account data event changed | ||
| const event2 = mkEvent({ | ||
| content: { foo: "abc" }, | ||
| type: "m.test", | ||
| user: "@foobar:matrix.org", | ||
| event: true, | ||
| }); | ||
| cli.emit(ClientEvent.AccountData, event2); | ||
| // Watchable value should have been updated | ||
| expect(watchable.value).toStrictEqual({ foo: "abc" }); | ||
| // Watched callbacks should be called | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Make sure unwatch removed the event listener | ||
| cli.off = jest.fn(); | ||
| watchable.unwatch(fn); | ||
| expect(cli.off).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| it("should set account data via js-sdk on set()", async () => { | ||
| const cli = stubClient(); | ||
| const api = new AccountDataApi(); | ||
| await api.set("m.test", { foo: "bar" }); | ||
| expect(cli.setAccountData).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should delete account data via js-sdk on set()", async () => { | ||
| const cli = stubClient(); | ||
| const api = new AccountDataApi(); | ||
| await api.delete("m.test"); | ||
| expect(cli.deleteAccountData).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { ClientApi } from "../../../src/modules/ClientApi"; | ||
| import { Room } from "../../../src/modules/models/Room"; | ||
| import { stubClient } from "../../test-utils/test-utils"; | ||
|
|
||
| describe("ClientApi", () => { | ||
| it("should return module room from getRoom()", () => { | ||
| stubClient(); | ||
| const client = new ClientApi(); | ||
| const moduleRoom = client.getRoom("!foo:matrix.org"); | ||
| expect(moduleRoom).toBeInstanceOf(Room); | ||
| expect(moduleRoom?.id).toStrictEqual("!foo:matrix.org"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| Copyright 2025 Element Creations Ltd. | ||
|
|
||
| SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial | ||
| Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| import { Room } from "../../../../src/modules/models/Room"; | ||
| import { mkRoom, stubClient } from "../../../test-utils"; | ||
|
|
||
| describe("Room", () => { | ||
| it("should return id from sdk room", () => { | ||
| const cli = stubClient(); | ||
| const sdkRoom = mkRoom(cli, "!foo:m.org"); | ||
| const room = new Room(sdkRoom); | ||
| expect(room.id).toStrictEqual("!foo:m.org"); | ||
| }); | ||
|
|
||
| it("should return last timestamp from sdk room", () => { | ||
| const cli = stubClient(); | ||
| const sdkRoom = mkRoom(cli, "!foo:m.org"); | ||
| const room = new Room(sdkRoom); | ||
| expect(room.getLastActiveTimestamp()).toStrictEqual(sdkRoom.getLastActiveTimestamp()); | ||
| }); | ||
|
|
||
| describe("watchableName", () => { | ||
| it("should return name from sdkRoom", () => { | ||
| const cli = stubClient(); | ||
| const sdkRoom = mkRoom(cli, "!foo:m.org"); | ||
| sdkRoom.name = "Foo Name"; | ||
| const room = new Room(sdkRoom); | ||
| expect(room.name.value).toStrictEqual("Foo Name"); | ||
| }); | ||
|
|
||
| it("should add/remove event listener on sdk room", () => { | ||
| const cli = stubClient(); | ||
| const sdkRoom = mkRoom(cli, "!foo:m.org"); | ||
| sdkRoom.name = "Foo Name"; | ||
|
|
||
| const room = new Room(sdkRoom); | ||
| const fn = jest.fn(); | ||
|
|
||
| room.name.watch(fn); | ||
| expect(sdkRoom.on).toHaveBeenCalledTimes(1); | ||
|
|
||
| room.name.unwatch(fn); | ||
| expect(sdkRoom.off).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.