Compare commits

...
61 Commits
Author SHA1 Message Date
Johannes Marbach 157a81d91a Merge pull request #4514 from element-hq/johannes/key-logging
Make keyboard shortcut logging actually useful
2026-11-27 14:11:54 +01:00
Timo 6f328bac02 Remove crypto wasm from embedded target (#4510)
* remove crypto wasm from embedded target

* over the standalone client loader in both packages
2026-11-27 12:34:06 +00:00
Timo 0118be7ee7 Merge pull request #4515 from element-hq/toger5/sdk-build-modes
[sdk] Add development builds of the matrixrtc-sdk and fix the component's
2026-11-27 13:13:42 +01:00
Timo 788f43f578 Merge pull request #4480 from element-hq/toger5/peer-dependency
Solve EW <-> EC Peer dependency
2026-11-27 13:12:09 +01:00
Timo K. 1816cb3887 use withotuMatrixCrypto for the component build. 2026-11-27 11:32:30 +01:00
Timo K. bddd96e509 remove useRoomState 2026-11-27 11:02:08 +01:00
Johannes Marbach 0ae0dbcbb5 Make keyboard shortcut logging actually useful 2026-11-27 11:00:05 +01:00
renovate[bot] c0370c254c Update ghcr.io/element-hq/element-web:develop Docker digest to 58b673f (#4513)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-11-27 02:08:46 +00:00
Timo K.andClaude Opus 5.5 cd1685091f Declare the js-sdk CallIntent subpath external in the SDK build
src/UrlConfiguration.ts imports matrix-js-sdk/lib/matrixrtc/CallIntent,
which the SDK build would otherwise bundle a copy of.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 18:08:57 +01:00
Timo K.andClaude Opus 5.5 48ada17b19 Narrow the functional members room to what it reads
createFunctionalMembers$ came in from main taking a whole Room, while the
call view model's room is a Pick<>. Take only getLiveTimeline and on/off,
and add getLiveTimeline to the room Picks from CallView down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 18:08:57 +01:00
Timo K.andClaude Opus 5.5 4f6a919aa6 Add production and development builds of the matrixrtc-sdk
build:sdk had a single, always minified build. build:sdk:production and
build:sdk:development now match the component's pair:
vite-sdk.config.ts already minified only in production mode, so only the
scripts were missing. `--mode` goes on build:sdk:js, the Vite step,
since pnpm would append it after the types step.

Source maps stay on in both modes, as for the component: they ship with
the package for the host to debug with. `prepare` keeps calling build:sdk,
so a git-dependency install is unchanged.

Closes #3714

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 18:05:34 +01:00
Timo K.andClaude Opus 5.5 13ce1c2648 Build the component in development mode for build:component:development
pnpm appends extra arguments to the end of a script, so the `--mode
development` that build:component:development passed to build:component:js
landed on check-component-bundle.mjs rather than on Vite, and the
"development" build came out minified like the production one.

The bundle check is now a script of its own, build:component:check, which
leaves build:component:js a single Vite call that `--mode` reaches. The
steps still run in the same order: Vite, the check, the types.

The development build is now unminified (element-call.js 3.64 MB against
2.34 MB) and passes the check, a little under its 4 MiB module budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 18:05:30 +01:00
Timo K. 9a7ce81396 change public api of ElementCall Component: roomId -> room 2026-11-26 17:51:02 +01:00
Timo K.andClaude Opus 5.5 a50a7d19f2 Remove the supplied client's listeners with off
The supplied client was asked for `on` and `removeListener`, while the
call's backend asks the same client for `on` and `off`. Using `off`
everywhere leaves one pair in what a host's client has to provide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 17:51:02 +01:00
Timo K. a96091411c js-sdk error mapping
Maps js-sdk errors from the EW js-sdk to the EC js-sdk.
This is required as we check with `instanceOf` which would fail if we
get an error instance from the EW js-sdk.
2026-11-26 17:51:02 +01:00
Timo K. 532803d5f5 remove components peer-dependency: js-sdk 2026-11-26 17:51:02 +01:00
Timo K. 56d5d87954 Update RTCSlot.test.ts 2026-11-26 17:50:56 +01:00
Timo K. f0ce686a27 Pass the a live room (getting updates) from useLoadGroupCall ->
matrixBackend

Otherwise we only get a room copy without live updates (event emitters)
2026-11-26 17:50:56 +01:00
Timo K. 35310b2771 Introduce Backend concept (just renames for now. no new strucutre) 2026-11-26 17:50:56 +01:00
Timo K. b2b768b4d0 make EC component rtcSession independent, more own js-sdk wrapped
methods
Everything related to the session is now burried inside the SDK.
2026-11-26 17:50:45 +01:00
Timo K. 2362694216 remove the rtcSession from everything below the CallView!!!!
This is a big step in the js-sdk independence of EC.
It concludes a clear position where we can start building the
matrixBackend.
2026-11-26 17:50:33 +01:00
Timo K. 608198be9a Make the reactions logic independent of the rtcSession 2026-11-26 17:50:16 +01:00
Timo K. 08be167644 SDK api updated (rtcMode slot->join, RTCMember.eventId)
Two things were added
 - The rtc mode is now passed on join and not part of the slot (it is a
   participation prop only, uninteresting for non joined slots, its per
   participant anyhow)
 - RTCMember.matrixEventId$ includes the event id which we need in the
   rtc sdk as some apps (e.g. call) might need it.
2026-11-26 17:50:16 +01:00
Timo K. df34bb06ff never use rtcSession.room (Relation classes refactor)
make sure everything is passed through dedicated room / client props.
2026-11-26 17:50:16 +01:00
Timo K. c573afee08 Use implementations for rtc related methods of our own js-sdk
getRTCTransports
getOpenIdToken
getLivekitToken
delegateDelayedLeave
2026-11-26 17:50:16 +01:00
Timo K. ddfd48138a Make the client Picks transparent up to the component 2026-11-26 17:50:16 +01:00
Timo K. 60c2c353c6 Make the room Picks transparent up to the component. 2026-11-26 17:50:16 +01:00
Timo K. fab67321e3 Allocate a new js-sdk matrixRTCSession for the embedded component (no
more peer dep)
2026-11-26 17:50:16 +01:00
Timo K. 00851b0f4f api iteration: only pass the participation the slot data that makes
sense
 - dont pass the participation$ itself
 - dont pass the join fn.
2026-11-26 17:50:16 +01:00
Timo K. 92e0965347 createRTCSlot takes the MatrixRTC session via fn param 2026-11-26 17:50:16 +01:00
Timo K. ada8d6b464 Mark everything below the CallView with Pick<> 2026-11-26 17:49:50 +01:00
Timo K. caf5e28b23 Make callViewModel declare what it really needs from room (using
`Pick<>`
2026-11-26 17:49:50 +01:00
Timo e71203f913 Merge pull request #4326 from toger5/toger5/respect-functional-members-calling
Consider functional members when ringing (fix dialing sound does not stop)
2026-11-26 16:26:08 +01:00
Timo K. 2bf7b1385b Merge remote-tracking branch 'origin/main' into toger5/respect-functional-members-calling 2026-11-26 16:17:54 +01:00
Timo eb69706111 Merge pull request #4511 from element-hq/toger5/fix-toast-dismiss-timeout
Fix toast timeout bug
2026-11-26 12:38:19 +01:00
Timo K. 9cb668eabb Fix toast timeout bug
Always waited 5s on every test to dismiss the toast.
2026-11-26 11:43:58 +01:00
Timo d53e1a25c0 Merge pull request #4461 from element-hq/toger5/widget-sdk-cleanup
Widget sdk cleanup
2026-11-26 11:35:29 +01:00
Timo K. 58f41e1f59 Merge remote-tracking branch 'origin/main' into toger5/widget-sdk-cleanup
# Conflicts:
#	.github/workflows/build.yaml
#	docs/agents/architecture.md
2026-11-26 11:24:27 +01:00
Timo K. e18322462b Add a README caution comment about the widget-sdk deprecation 2026-11-26 11:13:48 +01:00
Timo K. b40ccc4091 Remove widget sdk tests in our CI
It has to be run manually now.
2026-11-26 11:09:04 +01:00
Timo 4daf10d043 Merge pull request #4432 from element-hq/toger5/component-bundle-check
CI to check for bundle size regressions
2026-11-25 17:21:50 +01:00
Johannes Marbach d440cd49b2 Merge pull request #4509 from element-hq/johannes/zizmor
Update and appease zizmor
2026-11-25 15:35:40 +01:00
Timo K. cd44d0ff5b review changes on check-component-bundle 2026-11-25 15:16:37 +01:00
Timo K.andClaude Fable 5.1 88eb3aaf9d Fail the component build on what a host must not be handed
A library build inlines an asset as a data URL the moment its import
loses `no-inline`, and emits the standalone app's store worker the
moment a module the component reaches imports it; neither warns, and
the package grows by 11 MB without anyone seeing it in a diff. It had
been published at 18 MB for several releases before it was measured.

`scripts/check-component-bundle.mjs` now runs at the end of the
component build and fails it on a data URL over 16 kB in a module a
host bundles, an absolute `/assets/` reference, the store worker under
`assets/`, or a main module over 4 MB, each with a message saying what
to do. It passes only once both the assets and the store worker fixes
are in.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-25 12:20:09 +01:00
Timo K. dbfe494852 add matrix 2.0 capabilites for widget-rtc-sdk target 2026-11-25 11:47:19 +01:00
Timo K. 78ddd9839c fix stop() cleanup 2026-11-25 11:47:02 +01:00
Timo K. 64ad1d7314 build, bundle and playwright improvements 2026-11-25 11:46:42 +01:00
Timo K. 1c4d2c5d29 dont use innerHTML 2026-11-25 11:45:41 +01:00
Timo K.andClaude Opus 5.5 b4408dcecf Add an e2e spec for sending data with the widget RTC SDK
Two Element Web users open widget-rtc-sdk/index.html as a widget, join and
exchange data. `pnpm dev:widget-rtc-sdk` serves the example on :3003 with its
import pointed at the source; Playwright builds and previews it instead,
since Firefox now and then stalls on the dev server's module requests.

The widget fixture's display names get the worker's parallelIndex, as
parallel workers could reach it in the same millisecond. The README notes
that the example asks for an OpenID token more than once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:37:06 +01:00
Timo K.andClaude Opus 5.5 98afaa71ce Remove sdk/SdkMigration.md
Drops the inventory of what moved from src/ into sdk/ and the links to it
from sdk/README.md and SdkArchitecture.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:36:59 +01:00
Timo K.andClaude Opus 5.5 aef28af48a Document the widget RTC SDK and point its example at the PR preview
The README now describes the bundle as a widget wrapper around the sdk/
package: what it exposes, how to try it from a pull request's
pr<N>-widget-rtc-sdk preview and how to build it yourself. The example page
imports the bundle from that preview.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:36:54 +01:00
Timo K.andClaude Opus 5.5 35280e3847 Replace the CallViewModel widget bundle with a wrapper around sdk/
widget-rtc-sdk/main.ts no longer builds Element Call's CallViewModel. It
connects to the host over the widget API, builds the room widget client and
hands it to createRTCSlot from the sdk/ package, with a data-only
participation. Nothing of src/ is imported any more.

The new withoutMatrixCrypto Vite plugin leaves the Matrix SDK's Rust crypto
out of the bundle: as a widget the host encrypts and decrypts, so that chunk
and its WASM were shipped but never loaded.

The production build goes from 26.9 MB in 48 files (37 MB with source maps)
to one 2.2 MB file.

The Netlify preview no longer gets a config.json, which only the app reads,
and gets a CORS header instead, so that a widget page on another origin can
import the bundle as a module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:36:47 +01:00
Timo K.andClaude Opus 5.5 2c85cdb308 Rename the CallViewModel SDK bundle to widget-rtc-sdk
The bundle in sdk-target-based-on-call-view-model/ is now widget-rtc-sdk/,
its output widget-rtc-sdk.js and its scripts build:widget-rtc-sdk[:production
|:development]. CI's package follows: the build-output-widget-rtc-sdk
artifact and the pr<N>-widget-rtc-sdk Netlify preview replace the `sdk` ones.

Names only: the bundle is still the CallViewModel one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:35:35 +01:00
Timo K. 87bd273fa5 Address review: fix functional members lookup and tidy tests 2026-11-13 15:06:25 +01:00
TimoandRobin a5dd993dd4 Update src/state/CallViewModel/FunctionalMembers.ts
Co-authored-by: Robin <robin@robin.town>
2026-11-13 14:03:37 +01:00
Timo K. b3fe19f192 regression test 2026-11-12 17:36:26 +01:00
Timo K. e631979256 Merge remote-tracking branch 'origin/main' into toger5/respect-functional-members-calling 2026-11-12 15:47:57 +01:00
Timo K. a27a76d6f1 Merge branch 'main' into toger5/respect-functional-members-calling 2026-11-12 15:47:48 +01:00
Timo K. 8b9dde62d1 Merge branch 'main' into toger5/respect-functional-members-calling 2026-10-12 17:42:15 +02:00
Timo K. 5f776519b1 fix tests 2026-10-06 12:10:55 +02:00
Timo K. 904062b917 Consider functional members when ringing.
Otherwise we never stop ringing if there are functional members.
2026-10-05 19:05:04 +02:00
115 changed files with 4138 additions and 1434 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ on:
type: string
package:
type: string # This would ideally be a `choice` type, but that isn't supported yet
description: The package type to be built. Must be one of 'full', 'embedded', or 'sdk'
description: The package type to be built. Must be one of 'full', 'embedded', or 'widget-rtc-sdk'
required: true
build_mode:
type: string # This would ideally be a `choice` type, but that isn't supported yet
+4 -6
View File
@@ -82,16 +82,14 @@ jobs:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
build_sdk_element_call:
# The 'sdk' package is the CallViewModel bundle that predates the sdk/
# package (build:sdk:production, sdk-target-based-on-call-view-model/), not
# the package itself. The name is shared with pr-deploy.yaml, which always
# runs from main, so it stays until the bundle's consumers have moved over.
build_widget_rtc_sdk:
# The widget wrapper around the sdk/ package (widget-rtc-sdk/), not the
# package itself. Deployed by pr-deploy.yaml as the pr<N>-widget-rtc-sdk preview.
permissions:
contents: read
uses: $/.github/workflows/build-element-call.yaml
with:
package: sdk
package: widget-rtc-sdk
vite_app_version: ${{ github.event.release.tag_name || github.sha }}
build_mode: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'development build') && 'development' || 'production' }}
secrets:
+13 -11
View File
@@ -17,7 +17,7 @@ on:
package:
required: true
type: string
description: Which package to deploy - 'full', 'embedded', 'sdk', or 'storybook'
description: Which package to deploy - 'full', 'embedded', 'widget-rtc-sdk', or 'storybook'
artifact_run_id:
required: false
type: string
@@ -64,25 +64,27 @@ jobs:
run: curl -s https://raw.githubusercontent.com/element-hq/element-call/main/config/netlify_redirects > webapp/_redirects
- name: Add config file
# Not needed for storybook deployments
if: inputs.package != 'storybook'
run: |
if [ "${INPUTS_PACKAGE}" = "full" ]; then
curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview.json" > webapp/config.json
else
curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview_sdk.json" > webapp/config.json
fi
# Only the app reads a config.json beside it. A widget gets everything
# about the homeserver from its host.
if: inputs.package == 'full'
run: curl -s "https://raw.githubusercontent.com/${INPUTS_PR_HEAD_FULL_NAME}/${INPUTS_PR_HEAD_REF}/config/config_netlify_preview.json" > webapp/config.json
env:
INPUTS_PACKAGE: ${{ inputs.package }}
INPUTS_PR_HEAD_FULL_NAME: ${{ inputs.pr_head_full_name }}
INPUTS_PR_HEAD_REF: ${{ inputs.pr_head_ref }}
- name: Add headers file
# A widget page on another origin imports the widget RTC SDK bundle as a
# module, which the browser only allows with CORS
if: inputs.package == 'widget-rtc-sdk'
run: |
printf '/*\n Access-Control-Allow-Origin: *\n' > webapp/_headers
- name: ☁️ Deploy to Netlify
id: netlify
uses: nwtgck/actions-netlify@4cbaf4c08f1a7bfa537d6113472ef4424e4eb654 # v3.0
with:
publish-dir: webapp
deploy-message: "Deploy from GitHub Actions"
alias: ${{ inputs.package == 'sdk' && format('pr{0}-sdk', inputs.pr_number) || inputs.package == 'storybook' && format('pr{0}-storybook', inputs.pr_number) || format('pr{0}', inputs.pr_number) }}
alias: ${{ inputs.package == 'widget-rtc-sdk' && format('pr{0}-widget-rtc-sdk', inputs.pr_number) || inputs.package == 'storybook' && format('pr{0}-storybook', inputs.pr_number) || format('pr{0}', inputs.pr_number) }}
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
+2 -2
View File
@@ -50,7 +50,7 @@ jobs:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
netlify-sdk:
netlify-widget-rtc-sdk:
needs: prdetails
permissions:
deployments: write
@@ -61,7 +61,7 @@ jobs:
pr_head_full_name: ${{ github.event.workflow_run.head_repository.full_name }}
pr_head_ref: ${{ needs.prdetails.outputs.pr_data_json && fromJSON(needs.prdetails.outputs.pr_data_json).head.ref }}
deployment_ref: ${{ needs.prdetails.outputs.pr_data_json && fromJSON(needs.prdetails.outputs.pr_data_json).head.sha || github.ref || github.head_ref }}
package: sdk
package: widget-rtc-sdk
secrets:
ELEMENT_BOT_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
+1
View File
@@ -2,6 +2,7 @@ node_modules
.DS_Store
.env
dist
/dist-widget-rtc-sdk-example
dist-ssr
*.local
*.bkp
+11 -4
View File
@@ -245,12 +245,15 @@ the build (`allowBuilds` in its `pnpm-workspace.yaml`).
##### Use
1. Provide the externals yourself: `react`, `react-dom`, `matrix-js-sdk`,
1. Provide the externals yourself: `react`, `react-dom`,
`@vector-im/compound-web`, `@vector-im/compound-design-tokens`, and
Compound's stylesheet.
Compound's stylesheet. The component bundles its own `matrix-js-sdk`; the
host's client and room can be on any version that has what the `client`
and `room` props pick from them, which the host's type check enforces.
2. Import `@element-hq/element-call-component/style.css`.
3. Await `initializeElementCall(config)` once before rendering.
4. Render the component with `client`, `roomId` and an `intent`; optionally
4. Render the component with `client`, `room` (from that client) and an
`intent`; optionally
pass `config`, `hostBridge`, `language` and `theme` (both can change
mid-call). Control it through the `ref` handle (`join`, `hangUp`,
`setDeviceMute`).
@@ -275,7 +278,11 @@ API is in `component/index.tsx`, `component/api.ts` and `component/host.ts`.
##### Develop
- `pnpm build:component` builds the library into `component/dist`.
- `pnpm build:component` builds the library into `component/dist`. It ends
with `scripts/check-component-bundle.mjs`, which fails the build on what a
host must not be handed: an asset inlined as a data URL, a file referenced
by an absolute path, the standalone app's client store worker, or a module
over its size budget.
- `pnpm dev:component` serves a test host on `https://localhost:3001`: two
calls side by side in resizable boxes, with host messages logged at the
bottom. It uses the development backend, `public/config.json` if present,
+87
View File
@@ -0,0 +1,87 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { mockEmitter, mockMatrixRoom } from "../src/utils/test";
import { ElementCall } from "./index";
// The call itself is out of scope: what matters is what it is handed
const callView = vi.hoisted(() => vi.fn());
vi.mock("../src/room/CallView", () => ({
CallView: (props: object): string => {
callView(props);
return "call";
},
}));
// Media devices need a browser jsdom does not have
vi.mock("../src/state/AppViewModel", () => ({
AppViewModel: class {
public mediaDevices = {};
public backgroundEffects = {};
},
}));
const roomId = "!room:example.org";
let room: Room;
beforeEach(() => {
// What the call's backend takes from the room
room = mockMatrixRoom({
roomId,
getMembersWithMembership: vi.fn(),
getLiveTimeline: vi.fn(),
getVersion: vi.fn(),
hasMembershipState: vi.fn(),
_unstable_getStickyEvents: vi.fn(),
getMxcAvatarUrl: vi.fn(),
getJoinRule: vi.fn(),
});
});
afterEach(() => {
callView.mockClear();
});
test("calls in the room, leaving the session to the call", async () => {
render(<ElementCall client={mockClient()} room={room} />);
await screen.findByText("call");
const props = callView.mock.lastCall?.[0];
expect(props).toMatchObject({ backend: { roomId } });
expect(props).not.toHaveProperty("rtcSession");
});
// No `matrixRTC`: the host's session manager is not ours to use
function mockClient(): MatrixClient {
return {
...mockEmitter(),
// What the call view's client is made from
baseUrl: "https://matrix.example.org",
getUserId: () => "@alice:example.org",
getDeviceId: () => "DEVICE",
getAccessToken: () => "token",
getSyncState: () => null,
getUser: () => null,
getCrypto: () => undefined,
sendEvent: vi.fn(),
sendStateEvent: vi.fn(),
_unstable_sendStickyEvent: vi.fn(),
_unstable_sendStickyDelayedEvent: vi.fn(),
cancelPendingEvent: vi.fn(),
encryptAndSendToDevice: vi.fn(),
redactEvent: vi.fn(),
decryptEventIfNeeded: vi.fn(),
setDisplayName: vi.fn(),
setAvatarUrl: vi.fn(),
uploadContent: vi.fn(),
doesServerSupportUnstableFeature: vi.fn(),
} as unknown as MatrixClient;
}
+36 -4
View File
@@ -25,10 +25,12 @@ Please see LICENSE in the repository root for full details.
*/
import type { Ref } from "react";
import type { MatrixClient } from "matrix-js-sdk";
import type { Room } from "matrix-js-sdk";
import type { UrlProperties } from "../src/UrlParams";
import type { ElementCallHandle, ElementCallHostBridge } from "./host";
import type { HostClientPart } from "./componentLocalJsClient";
import type { SuppliedClient } from "../src/ClientContext";
import {
BackgroundStyle,
configurationForIntent,
@@ -69,14 +71,44 @@ export {
export type ElementCallConfiguration = Partial<UrlConfiguration> &
Partial<Pick<UrlProperties, "background">>;
/**
* Everything Element Call reads of the host's room. Listed here rather than
* taken from the call's backend, whose types a host cannot resolve.
*
* STOP GAP until the host passes an interface of our own instead of its `Room`.
*/
type HostRoomPart = Pick<
Room,
| "roomId"
| "on"
| "off"
| "getMembersWithMembership"
| "getLiveTimeline"
| "getVersion"
| "hasMembershipState"
| "_unstable_getStickyEvents"
| "name"
| "getMxcAvatarUrl"
| "getJoinRule"
| "relations"
>;
export interface ElementCallProps {
/**
* The client to place the call with. Element Call does not authenticate
* anyone or manage a session of its own; this one is the host's.
*
* Typed with the host's own js-sdk, as only what Element Call calls on it: by
* the call's backend, and by the rest of the call through its context. A
* js-sdk too old for Element Call fails to type-check rather than failing
* once a call is shown.
*/
client: MatrixClient;
/** The room to call in. The host's client must already know about it. */
roomId: string;
client: HostClientPart & SuppliedClient;
/**
* The room to call in, from the host's client. Typed like `client`: only what
* Element Call reads of it.
*/
room: HostRoomPart;
/**
* What the user asked for — whether they started the call or joined one that
* was already running, and whether it is a call in a group or a DM. Element
+148
View File
@@ -0,0 +1,148 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, expect, test, vi } from "vitest";
import {
ClientEvent,
ConnectionError,
EventType,
type MatrixClient,
SyncState,
} from "matrix-js-sdk";
import { createComponentLocalJsClient } from "./componentLocalJsClient";
afterEach(() => {
vi.unstubAllGlobals();
});
test("reads the host's live state from the host's client", () => {
const host = mockHost();
const client = createComponentLocalJsClient(host);
const listener = (): void => {};
expect(client.getSyncState()).toBe(SyncState.Syncing);
client.on(ClientEvent.Sync, listener);
client.off(ClientEvent.Sync, listener);
client.getUser("@bob:example.org");
client.getCrypto();
expect(host.getSyncState).toHaveBeenCalledOnce();
expect(host.on).toHaveBeenCalledWith(ClientEvent.Sync, listener);
expect(host.off).toHaveBeenCalledWith(ClientEvent.Sync, listener);
expect(host.getUser).toHaveBeenCalledWith("@bob:example.org");
expect(host.getCrypto).toHaveBeenCalledOnce();
});
test("sends the RTC session's encrypted events with the host's crypto", async () => {
const host = mockHost();
const client = createComponentLocalJsClient(host);
await client._unstable_sendStickyEvent(
"!room:example.org",
1000,
null,
EventType.RTCMembership,
{ slot_id: "m.call#ROOM", msc4354_sticky_key: "key" },
);
await client.encryptAndSendToDevice("m.room.encrypted", [], {});
expect(host._unstable_sendStickyEvent).toHaveBeenCalledOnce();
expect(host.encryptAndSendToDevice).toHaveBeenCalledOnce();
});
test.each([
"sendEvent",
"redactEvent",
"decryptEventIfNeeded",
"_unstable_sendStickyEvent",
"_unstable_sendStickyDelayedEvent",
"encryptAndSendToDevice",
"sendStateEvent",
"setDisplayName",
"setAvatarUrl",
"uploadContent",
"doesServerSupportUnstableFeature",
] as const)(
"the host's %s fails with errors our js-sdk recognises",
async (method) => {
const host = mockHost();
// A ConnectionError of the host's own js-sdk: not ours, but shaped like it
const hostError = Object.assign(new Error("fetch failed"), {
name: "ConnectionError",
});
vi.mocked(host[method]).mockRejectedValue(hostError);
const client = createComponentLocalJsClient(host);
await expect(
(client[method] as () => Promise<unknown>)(),
).rejects.toBeInstanceOf(ConnectionError);
},
);
test("takes its credentials from the host", () => {
const client = createComponentLocalJsClient(mockHost());
expect(client.getUserId()).toBe("@alice:example.org");
expect(client.getDeviceId()).toBe("DEVICE");
expect(client.getDomain()).toBe("example.org");
expect(client.baseUrl).toBe("https://matrix.example.org");
});
test("makes requests with whichever token the host holds now", async () => {
const fetch = vi.fn(
async () =>
await Promise.resolve(
new Response(JSON.stringify({ access_token: "openid" }), {
headers: { "Content-Type": "application/json" },
}),
),
);
vi.stubGlobal("fetch", fetch);
const host = mockHost();
const client = createComponentLocalJsClient(host);
// As if the host had refreshed its token after we were created
host.token = "refreshed";
await client.getOpenIdToken();
expect(fetch).toHaveBeenCalledOnce();
const [url, init] = fetch.mock.calls[0] as unknown as [URL, RequestInit];
expect(url.toString()).toBe(
"https://matrix.example.org/_matrix/client/v3/user/%40alice%3Aexample.org/openid/request_token",
);
expect(new Headers(init.headers).get("Authorization")).toBe(
"Bearer refreshed",
);
});
function mockHost(): MatrixClient & { token: string } {
const host = {
token: "first",
baseUrl: "https://matrix.example.org",
getUserId: (): string => "@alice:example.org",
getDeviceId: (): string => "DEVICE",
getAccessToken: (): string => host.token,
getSyncState: vi.fn(() => SyncState.Syncing),
on: vi.fn(),
off: vi.fn(),
getUser: vi.fn(() => null),
getCrypto: vi.fn(() => undefined),
sendEvent: vi.fn(),
sendStateEvent: vi.fn(),
_unstable_sendStickyEvent: vi.fn(),
_unstable_sendStickyDelayedEvent: vi.fn(),
encryptAndSendToDevice: vi.fn(),
redactEvent: vi.fn(),
decryptEventIfNeeded: vi.fn(),
setDisplayName: vi.fn(),
setAvatarUrl: vi.fn(),
uploadContent: vi.fn(),
doesServerSupportUnstableFeature: vi.fn(),
};
return host as unknown as MatrixClient & { token: string };
}
+152
View File
@@ -0,0 +1,152 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { createClient, type MatrixClient } from "matrix-js-sdk";
import type { createCallMatrixBackend } from "../src/CallMatrixBackend";
import { adoptingHostErrors } from "./hostErrors";
/**
* Everything the component calls on the host's client. Part of the public
* `client` prop (see api.ts), so that a host whose js-sdk lacks one of these
* fails to type-check.
*
* STOP GAP until the host passes an interface of our own instead of its
* `MatrixClient`.
*/
export type HostClientPart = Pick<
MatrixClient,
| "baseUrl"
| "getUserId"
| "getDeviceId"
| "getAccessToken"
| "getSyncState"
| "on"
| "off"
| "getUser"
| "getCrypto"
| "sendEvent"
| "redactEvent"
| "decryptEventIfNeeded"
| "_unstable_sendStickyEvent"
| "_unstable_sendStickyDelayedEvent"
| "encryptAndSendToDevice"
| "sendStateEvent"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
| "doesServerSupportUnstableFeature"
>;
/**
* The client for Element Call in Component mode.
* We do get the full host client but we dont want to use it as it
* might use older versions of methods were we expect changes. (f.ex. getLivekitToken)
* We really want the expected implementation to not get runtime errors.
*
* This creates a new client that does use the hosts token and "stable"
* methods but uses the ElementCall Components js-sdk's client impl for anything else.
*/
export function createComponentLocalJsClient(
host: HostClientPart,
): Parameters<typeof createCallMatrixBackend>[0] {
const own = createClient({
baseUrl: host.baseUrl,
userId: host.getUserId() ?? undefined,
deviceId: host.getDeviceId() ?? undefined,
accessToken: host.getAccessToken() ?? undefined,
// The host refreshes its token without telling us, so each request takes
// whichever token the host holds now
fetchFn: async (input, init) => {
const headers = new Headers(init?.headers);
const token = host.getAccessToken();
if (token !== null) headers.set("Authorization", `Bearer ${token}`);
return globalThis.fetch(input, { ...init, headers });
},
});
return {
// Own has no sync loop: `syncApi?.getSyncState() ?? null` is always null
getSyncState: host.getSyncState.bind(host),
// Only the syncing host emits `Sync`, `ToDeviceEvent` and `Decrypted`
on: host.on.bind(host),
// The same emitter as `on`
off: host.off.bind(host),
// `this.store.getUser`: own's store is empty, only the host's sync fills it
getUser: host.getUser.bind(host),
// Own never inits crypto, and a second one on the device would corrupt it
getCrypto: host.getCrypto.bind(host),
// Every one of the host's methods that can fail rejects with our error
// classes, which ours can recognise (see hostErrors.ts)
// Own has no rooms: `encryptEventIfNeeded`'s `if (!room) return` skips E2EE
sendEvent: adoptingHostErrors(host.sendEvent.bind(host)),
// Not encrypted, but the local echo goes to `getRoom()`: only the host's
redactEvent: adoptingHostErrors(host.redactEvent.bind(host)),
// `attemptDecryption` only runs `if (this.getCrypto())`: a no-op on own
decryptEventIfNeeded: adoptingHostErrors(
host.decryptEventIfNeeded.bind(host),
),
// Like `sendEvent`: on own, `if (!room) return` sends it in plaintext
_unstable_sendStickyEvent: adoptingHostErrors(
host._unstable_sendStickyEvent.bind(host),
),
// Like `sendEvent`: delayed, but still `encryptEventIfNeeded(event, room)`
_unstable_sendStickyDelayedEvent: adoptingHostErrors(
host._unstable_sendStickyDelayedEvent.bind(host),
),
// `if (!this.cryptoBackend) throw`: own has no crypto
encryptAndSendToDevice: adoptingHostErrors(
host.encryptAndSendToDevice.bind(host),
),
// Static: own was created with the host's user id
getUserId: own.getUserId.bind(own),
// Static: own was created with the host's device id
getDeviceId: own.getDeviceId.bind(own),
// Static: derived from the user id
getDomain: own.getDomain.bind(own),
// Static: own was created with the host's base URL
baseUrl: own.baseUrl,
// MSC4143 stable or unstable prefix, picked by /versions: ours knows both
getRTCTransports: own.getRTCTransports.bind(own),
// Plain HTTP with the copied credentials, like the other LiveKit auth
getOpenIdToken: own.getOpenIdToken.bind(own),
// MSC4195 stable or `io.element.msc4195` prefix: an older host may lack it
getLivekitToken: own.getLivekitToken.bind(own),
// MSC4195, like `getLivekitToken`: an older host may lack it
delegateDelayedLeave: own.delegateDelayedLeave.bind(own),
// Plain HTTP into own's cache, which `getCachedCapabilities` reads
fetchCapabilities: own.fetchCapabilities.bind(own),
// Has to be the client whose cache `fetchCapabilities` filled
getCachedCapabilities: own.getCachedCapabilities.bind(own),
// Straight to `scheduleDelayedEvent`: no room, no crypto, plain HTTP
_unstable_sendDelayedStateEvent:
own._unstable_sendDelayedStateEvent.bind(own),
// Plain POST `/delayed_events/{id}/cancel`; newer API an old host may lack
_unstable_cancelScheduledDelayedEvent:
own._unstable_cancelScheduledDelayedEvent.bind(own),
// Plain POST by the server's delay id: restarts the host's sticky leave too
_unstable_restartScheduledDelayedEvent:
own._unstable_restartScheduledDelayedEvent.bind(own),
// Plain POST `/delayed_events/{id}/send`; newer API an old host may lack
_unstable_sendScheduledDelayedEvent:
own._unstable_sendScheduledDelayedEvent.bind(own),
// Plain HTTP, either client works; a stable API, so the host's
sendStateEvent: adoptingHostErrors(host.sendStateEvent.bind(host)),
// Updates the `User` in the host's store, which `useProfile` listens to
setDisplayName: adoptingHostErrors(host.setDisplayName.bind(host)),
// Like `setDisplayName`: updates the `User` in the host's store
setAvatarUrl: adoptingHostErrors(host.setAvatarUrl.bind(host)),
// Plain HTTP, either client works; a stable API, so the host's
uploadContent: adoptingHostErrors(host.uploadContent.bind(host)),
// Reads the host's /versions, which it has already fetched
doesServerSupportUnstableFeature: adoptingHostErrors(
host.doesServerSupportUnstableFeature.bind(host),
),
};
}
+4 -2
View File
@@ -99,6 +99,7 @@ const Pane: FC<{
log: (pane: string, message: string) => void;
}> = ({ session, roomId, theme, language, log }): ReactNode => {
const [mounted, setMounted] = useState(true);
const room = session.client.getRoom(roomId);
const bridge = useMemo(
() =>
@@ -159,11 +160,12 @@ const Pane: FC<{
{/* Resizable, because how Element Call copes with the size it is given is
one of the things we cannot find out from the standalone app */}
<div className={styles.paneCall} data-testid="call-container">
{mounted && (
{mounted && room === null && <p>This client does not know {roomId}</p>}
{mounted && room !== null && (
<ElementCall
ref={handle}
client={session.client}
roomId={roomId}
room={room}
hostBridge={bridge}
theme={theme}
language={language}
+158
View File
@@ -0,0 +1,158 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import {
ConnectionError,
HTTPError,
MatrixError,
UnsupportedDelayedEventsEndpointError,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
import { adoptHostError, adoptingHostErrors } from "./hostErrors";
test("a rate limit from the host is one of ours, retry delay included", () => {
const error = adoptHostError(
new HostMatrixError(
{ errcode: "M_LIMIT_EXCEEDED", error: "Too many requests" },
429,
"https://matrix.example.org/_matrix/client/v3/rooms/x/send",
new Headers({ "Retry-After": "3" }),
),
);
expect(error).toBeInstanceOf(MatrixError);
const matrixError = error as MatrixError;
expect(matrixError.errcode).toBe("M_LIMIT_EXCEEDED");
expect(matrixError.data).toEqual({
errcode: "M_LIMIT_EXCEEDED",
error: "Too many requests",
});
expect(matrixError.httpStatus).toBe(429);
expect(matrixError.url).toBe(
"https://matrix.example.org/_matrix/client/v3/rooms/x/send",
);
expect(matrixError.isRateLimitError()).toBe(true);
expect(matrixError.getRetryAfterMs()).toBe(3000);
});
test("an HTTP error without a Matrix body from the host is one of ours", () => {
const error = adoptHostError(new HostHTTPError("Conflict", 409));
expect(error).toBeInstanceOf(HTTPError);
expect(error).not.toBeInstanceOf(MatrixError);
expect((error as HTTPError).httpStatus).toBe(409);
expect((error as HTTPError).message).toBe("Conflict");
});
test("a connection failure from the host is one of ours", () => {
const error = adoptHostError(new HostConnectionError("fetch failed"));
expect(error).toBeInstanceOf(ConnectionError);
expect((error as ConnectionError).message).toBe("fetch failed");
});
test("missing sticky and delayed event endpoints on the host's server are ours", () => {
const sticky = adoptHostError(
new HostUnsupportedEndpointError(
"UnsupportedStickyEventsEndpointError",
"sendStickyEvent",
),
);
const delayed = adoptHostError(
new HostUnsupportedEndpointError(
"UnsupportedDelayedEventsEndpointError",
"sendDelayedStateEvent",
),
);
expect(sticky).toBeInstanceOf(UnsupportedStickyEventsEndpointError);
expect((sticky as UnsupportedStickyEventsEndpointError).clientEndpoint).toBe(
"sendStickyEvent",
);
expect(delayed).toBeInstanceOf(UnsupportedDelayedEventsEndpointError);
expect(
(delayed as UnsupportedDelayedEventsEndpointError).clientEndpoint,
).toBe("sendDelayedStateEvent");
});
test("keeps where the host's error was thrown", () => {
const original = new HostConnectionError("fetch failed");
expect((adoptHostError(original) as Error).stack).toBe(original.stack);
});
test("leaves our own errors and everything else as they are", () => {
const own = new MatrixError({ errcode: "M_FORBIDDEN" }, 403);
const refusedByWidgetHost = new Error("Action not allowed");
const aborted = new DOMException("Aborted", "AbortError");
expect(adoptHostError(own)).toBe(own);
expect(adoptHostError(refusedByWidgetHost)).toBe(refusedByWidgetHost);
expect(adoptHostError(aborted)).toBe(aborted);
expect(adoptHostError("not an error")).toBe("not an error");
});
test("a wrapped host method rejects with ours and resolves as it did", async () => {
const failing = adoptingHostErrors(async (): Promise<string> =>
Promise.reject(new HostConnectionError("fetch failed")),
);
const succeeding = adoptingHostErrors(
async (a: number, b: number): Promise<number> => Promise.resolve(a + b),
);
await expect(failing()).rejects.toBeInstanceOf(ConnectionError);
await expect(succeeding(1, 2)).resolves.toBe(3);
});
// Lookalikes of the host js-sdk's classes: the same shape, but not ours
class HostHTTPError extends Error {
public constructor(
message: string,
public readonly httpStatus?: number,
public readonly httpHeaders?: Headers,
) {
super(message);
}
public isRateLimitError(): boolean {
return this.httpStatus === 429;
}
}
class HostMatrixError extends HostHTTPError {
public readonly errcode?: string;
public constructor(
public data: { errcode?: string; error?: string },
httpStatus?: number,
public url?: string,
httpHeaders?: Headers,
) {
super(`MatrixError: ${data.error}`, httpStatus, httpHeaders);
this.errcode = data.errcode;
this.name = data.errcode ?? "Unknown error code";
}
}
class HostConnectionError extends Error {
public get name(): string {
return "ConnectionError";
}
}
class HostUnsupportedEndpointError extends Error {
public constructor(
name: string,
public clientEndpoint: string,
) {
super(`Server does not support ${clientEndpoint}`);
this.name = name;
}
}
+92
View File
@@ -0,0 +1,92 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
ConnectionError,
HTTPError,
MatrixError,
UnsupportedDelayedEventsEndpointError,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
/**
* Wraps a method of the host's client so that what it rejects with is
* {@link adoptHostError adopted}.
*/
export function adoptingHostErrors<
F extends (...args: never[]) => Promise<unknown>,
>(call: F): F {
const adopting = async (
...args: Parameters<F>
): Promise<Awaited<ReturnType<F>>> => {
try {
return (await call(...args)) as Awaited<ReturnType<F>>;
} catch (e) {
throw adoptHostError(e);
}
};
return adopting as F;
}
/**
* The same error as one of our own js-sdk's classes, where it is one of the
* host's.
*
* The host's client is a different copy of the js-sdk, and ours (the MatrixRTC
* session above all) tells rate limits, network failures and missing endpoints
* apart with `instanceof`, which never matches the host's classes.
*/
export function adoptHostError(error: unknown): unknown {
if (!(error instanceof Error) || isOwn(error)) return error;
const adopted = adopt(error);
if (adopted === undefined) return error;
adopted.stack = error.stack;
return adopted;
}
function isOwn(error: Error): boolean {
return (
error instanceof HTTPError ||
error instanceof ConnectionError ||
error instanceof UnsupportedStickyEventsEndpointError ||
error instanceof UnsupportedDelayedEventsEndpointError
);
}
/** Recognised by shape, since the host's classes cannot be named. */
function adopt(error: Error & Partial<HostErrorFields>): Error | undefined {
switch (error.name) {
case "ConnectionError":
return new ConnectionError(error.message);
case "UnsupportedStickyEventsEndpointError":
return new UnsupportedStickyEventsEndpointError(
error.message,
error.clientEndpoint as UnsupportedStickyEventsEndpointError["clientEndpoint"],
{ cause: error.cause },
);
case "UnsupportedDelayedEventsEndpointError":
return new UnsupportedDelayedEventsEndpointError(
error.message,
error.clientEndpoint as UnsupportedDelayedEventsEndpointError["clientEndpoint"],
);
}
if (typeof error.isRateLimitError !== "function") return undefined;
if (error.data !== undefined)
return new MatrixError(
error.data,
error.httpStatus,
error.url,
error.event,
error.httpHeaders,
);
return new HTTPError(error.message, error.httpStatus, error.httpHeaders);
}
type HostErrorFields = Pick<
MatrixError,
"data" | "httpStatus" | "httpHeaders" | "url" | "event" | "isRateLimitError"
> & { clientEndpoint: string };
+13 -13
View File
@@ -11,7 +11,7 @@ Please see LICENSE in the repository root for full details.
* Element Call as a React component, for an application that wants to show a
* call inside itself rather than in an iframe.
*
* The host supplies the client and says which room to call in; Element Call
* The host supplies the client and the room to call in; Element Call
* supplies the call. Everything it would otherwise take from the page it is on
* — the URL, the document body, a Matrix session of its own — comes from the
* host instead, or is confined to the container it is mounted in.
@@ -84,7 +84,9 @@ import styles from "./ElementCall.module.css";
import { useComponentHostBridge } from "./host";
import { CallPortalRoot } from "./CallPortalRoot";
import { type ElementCallProps } from "./api";
import { createComponentLocalJsClient } from "./componentLocalJsClient";
import { supportedLanguages, translationsBackend } from "./localization";
import { createCallMatrixBackend } from "../src/CallMatrixBackend";
// The languages Element Call can be shown in
export { supportedLanguages } from "./localization";
@@ -148,7 +150,7 @@ const Decoration: FC<{ children: JSX.Element }> = ({ children }) => {
export const ElementCall: FC<ElementCallProps> = ({
client,
roomId,
room,
intent = UserIntent.JoinExistingCall,
config,
hostBridge: suppliedHostBridge,
@@ -179,6 +181,7 @@ export const ElementCall: FC<ElementCallProps> = ({
// to be stable for as long as its contents are. A host writing `config`
// inline would otherwise tear the call down on every render.
const stableConfig = useStableValue(config);
const { roomId } = room;
const params = useMemo(
(): UrlParams => ({
...componentProperties,
@@ -204,23 +207,17 @@ export const ElementCall: FC<ElementCallProps> = ({
};
}, [controlledAudioDevices, callIntent]);
const room = client.getRoom(roomId);
const rtcSession = useMemo(
() => (room === null ? null : client.matrixRTC.getRoomSession(room)),
const backend = useMemo(
() => createCallMatrixBackend(createComponentLocalJsClient(client), room),
[client, room],
);
if (rtcSession === null)
logger.error(
`Element Call was asked to call in ${roomId}, which its host's client does not know about`,
);
// Everything the call needs is in hand once these exist, and the first
// render with them is where the call itself appears: the moment the host
// is told that Element Call has loaded, as the widget tells its client once
// its own initialisation is over. Once per mount, however often the pieces
// are later swapped out.
const ready = container !== null && rtcSession !== null && vm !== null;
const ready = container !== null && vm !== null;
const announcedLoaded = useRef(false);
useEffect(() => {
if (!ready || announcedLoaded.current) return;
@@ -250,14 +247,17 @@ export const ElementCall: FC<ElementCallProps> = ({
>
<Decoration>
<TooltipProvider>
{/* The host's own client: what reads it through
the context (avatars, the encryption system,
rageshakes, the disconnected banner) uses only its
stable methods. */}
<ClientProvider client={client}>
<MediaDevicesContext value={vm.mediaDevices}>
<BackgroundEffectsProvider
effects={vm.backgroundEffects}
>
<CallView
client={client}
rtcSession={rtcSession}
backend={backend}
isPasswordlessUser={false}
confineToRoom={params.confineToRoom}
preload={params.preload}
-1
View File
@@ -42,7 +42,6 @@
"peerDependencies": {
"@vector-im/compound-design-tokens": "^11.0.0",
"@vector-im/compound-web": "^10.3.1",
"matrix-js-sdk": "*",
"react": "^19",
"react-dom": "^19"
}
-13
View File
@@ -1,13 +0,0 @@
{
"default_server_config": {
"m.homeserver": {
"base_url": "https://call-unstable.ems.host",
"server_name": "call-unstable.ems.host"
}
},
"ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf",
"matrix_rtc_session": {
"membership_event_expiry_ms": 180000000,
"network_error_retry_ms": 100
}
}
+2 -2
View File
@@ -19,10 +19,10 @@ services:
- ./backend/playwright_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z
element-web:
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
image: ghcr.io/element-hq/element-web:develop@sha256:c850f5058acf2eeb9ed4b392f2f3e53b02240e7a74af8e5f7cc8424d12974c49
image: ghcr.io/element-hq/element-web:develop@sha256:58b673f867f78fe83c08f2b688b254e34625ebfcebdc5eff84bfdf407445e88e
element-web-1:
# Pin to a SHA so that upstream cannot break our tests. Renovate handles regular updates.
image: ghcr.io/element-hq/element-web:develop@sha256:c850f5058acf2eeb9ed4b392f2f3e53b02240e7a74af8e5f7cc8424d12974c49
image: ghcr.io/element-hq/element-web:develop@sha256:58b673f867f78fe83c08f2b688b254e34625ebfcebdc5eff84bfdf407445e88e
volumes:
# The other site opens the embedded package under test in place of the
# copy its image bundles (the first site opens the dev server's app.
+7 -6
View File
@@ -108,12 +108,13 @@ Code that builds in only one is a bug.
- `build:sdk` — `@element-hq/matrixrtc-sdk`, sources in `sdk/`, entry `sdk/index.ts`.
Packaged exactly like the component (own pnpm project, run pnpm from the repo
root, types from `sdk/tsconfig.build.json`, externals checked by
`pnpm lint:externals`).
- `build:sdk-target-based-on-call-view-model` — the SDK demo that predates the
package, one bundle of the `CallViewModel`; entry
`sdk-target-based-on-call-view-model/main.ts`. CI's `sdk` package
(`build:sdk:production` / `build:sdk:development`, the Netlify `-sdk` preview)
is still this bundle, not `build:sdk`.
`pnpm lint:externals`). Minified by default (`build:sdk:production`),
unminified with `build:sdk:development`; source maps in both.
- `build:widget-rtc-sdk` — a single bundle for widget pages that wraps the
`sdk/` package and the widget API, nothing of `src/`; entry
`widget-rtc-sdk/main.ts`. CI builds it as the `widget-rtc-sdk` package
(`build:widget-rtc-sdk:production` / `:development`) and deploys it as the
`pr<N>-widget-rtc-sdk` Netlify preview.
- `build:component` — `@element-hq/element-call-component`, sources in `component/`
(its own pnpm project; run pnpm from the repo root). Host API is in the README;
`pnpm lint:externals` rejects an import of a `react` / `react-dom` /
+1
View File
@@ -60,4 +60,5 @@ pnpm backend # Synapse + LiveKit, required for e2e
pnpm test:playwright # or :open
pnpm dev:component # component harness, port 3001
pnpm dev:sdk # SDK harness, port 3002
pnpm dev:widget-rtc-sdk # widget RTC SDK example widget, port 3003
```
+2 -1
View File
@@ -14,7 +14,8 @@ export default {
"vite-embedded.config.ts",
"vite-sdk.config.ts",
"vite-sdk-dev.config.ts",
"vite-sdk-target-based-on-call-view-model.config.ts",
"vite-widget-rtc-sdk.config.ts",
"vite-widget-rtc-sdk-dev.config.ts",
"vite-component.config.ts",
"vite-component-dev.config.ts",
],
-2
View File
@@ -118,8 +118,6 @@
"livekit_connection_error": "Failed to connect to Livekit server",
"livekit_connection_error_description": "An error occurred while connecting to the Livekit server (<1>Reason:</1> <2>{{ reason }}</2>).",
"matrix_rtc_transport_missing": "The server is not configured to work with {{brand}} and no other call participant provides a usable transport. Please contact your server admin (Domain: {{domain}}, Error Code: {{ errorCode }}).",
"membership_manager": "Membership Manager Error",
"membership_manager_description": "The Membership Manager had to shut down. This is caused by many consecutive failed network requests.",
"no_matrix_2_authorization_service": "The authorization service for your media server (SFU) is out of date.",
"open_elsewhere": "Opened in another tab",
"open_elsewhere_description": "{{brand}} has been opened in another tab. If that doesn't sound right, try reloading the page.",
+9 -5
View File
@@ -8,6 +8,7 @@
"dev:embedded": "vite --config vite-embedded.config.js",
"dev:component": "vite --config vite-component-dev.config.ts",
"dev:sdk": "vite --config vite-sdk-dev.config.ts",
"dev:widget-rtc-sdk": "vite --config vite-widget-rtc-sdk-dev.config.ts",
"build": "pnpm build:full",
"build:full": "node --max-old-space-size=16384 node_modules/vite/bin/vite.js build",
"build:full:production": "pnpm build:full",
@@ -18,14 +19,17 @@
"build:sdk": "pnpm build:sdk:js && pnpm build:sdk:types",
"build:sdk:js": "pnpm build:full --config vite-sdk.config.js",
"build:sdk:types": "tsc -p sdk/tsconfig.build.json",
"build:sdk-target-based-on-call-view-model": "pnpm build:full --config vite-sdk-target-based-on-call-view-model.config.js",
"build:sdk:production": "pnpm build:sdk-target-based-on-call-view-model",
"build:sdk:development": "pnpm build:sdk-target-based-on-call-view-model --mode development",
"build:component": "pnpm build:component:js && pnpm build:component:types",
"build:sdk:production": "pnpm build:sdk",
"build:sdk:development": "pnpm build:sdk:js --mode development && pnpm build:sdk:types",
"build:widget-rtc-sdk": "pnpm build:full --config vite-widget-rtc-sdk.config.js",
"build:widget-rtc-sdk:production": "pnpm build:widget-rtc-sdk",
"build:widget-rtc-sdk:development": "pnpm build:widget-rtc-sdk --mode development",
"build:component": "pnpm build:component:js && pnpm build:component:check && pnpm build:component:types",
"build:component:js": "pnpm build:full --config vite-component.config.js",
"build:component:check": "node scripts/check-component-bundle.mjs",
"build:component:types": "tsc -p component/tsconfig.build.json",
"build:component:production": "pnpm build:component",
"build:component:development": "pnpm build:component:js --mode development && pnpm build:component:types",
"build:component:development": "pnpm build:component:js --mode development && pnpm build:component:check && pnpm build:component:types",
"serve": "vite preview",
"format": "oxfmt",
"format:check": "oxfmt --check; rc=$?; [[ $rc -ne 0 ]] && printf '\\033[46;30m INFO \\033[0m To fix, run: pnpm format\\n' >&2; exit $rc",
+34 -1
View File
@@ -13,6 +13,7 @@ import { fileURLToPath } from "node:url";
import { COMPONENT_HARNESS_URL } from "./playwright/component/harness.ts";
import { SDK_HARNESS_URL } from "./playwright/sdk/harness.ts";
import { WIDGET_RTC_SDK_URL } from "./playwright/widget/test-helpers.ts";
const baseURL = process.env.USE_DOCKER
? "http://localhost:8080"
@@ -82,6 +83,11 @@ export default defineConfig({
name: "build-embedded",
testMatch: "**/widget/embedded/build.setup.ts",
},
{
// Builds the widget RTC SDK's example widget for its preview server
name: "build-widget-rtc-sdk",
testMatch: "**/widget/widget-rtc-sdk.setup.ts",
},
{
name: "chromium",
testIgnore: ["**/mobile/**", "**/widget/**"],
@@ -93,12 +99,21 @@ export default defineConfig({
name: "widget",
dependencies: ["build-embedded"],
testMatch: "**/widget/**/*.@(spec|test).ts",
testIgnore: "**/widget/widget-rtc-sdk.spec.ts",
use: desktopChrome,
},
{
// The widget RTC SDK's example widget in Element Web. CI does not run
// this project; see widget-rtc-sdk/README.md for running it by hand.
name: "widget-rtc-sdk",
dependencies: ["build-widget-rtc-sdk"],
testMatch: "**/widget/widget-rtc-sdk.spec.ts",
use: desktopChrome,
},
{
name: "firefox",
dependencies: ["build-embedded"],
testIgnore: "**/mobile/**",
testIgnore: ["**/mobile/**", "**/widget/widget-rtc-sdk.spec.ts"],
use: {
...devices["Desktop Firefox"],
ignoreHTTPSErrors: true,
@@ -183,5 +198,23 @@ export default defineConfig({
timeout: 500,
},
},
{
// The widget RTC SDK's example widget, which Element Web opens as a
// widget. Bundled rather than from the dev server: Firefox now and then
// stalls on the dev server's hundreds of module requests. Servers start
// before any project, so the build-widget-rtc-sdk project fills the
// directory later and only the projects that open the widget wait for
// it. Until then there is no page, hence the port rather than the url.
command:
"mkdir -p dist-widget-rtc-sdk-example && " +
"pnpm exec vite preview --config vite-widget-rtc-sdk-dev.config.ts",
port: Number(new URL(WIDGET_RTC_SDK_URL).port),
reuseExistingServer: !process.env.CI,
ignoreHTTPSErrors: true,
gracefulShutdown: {
signal: "SIGTERM",
timeout: 500,
},
},
],
});
+5 -3
View File
@@ -32,9 +32,11 @@ export interface MyFixtures {
export const widgetTest = test.extend<MyFixtures>({
// allow per-test override: `widgetTest.use({ callType: "dm" })`
callType: ["room", { option: true }],
asWidget: async ({ browser, callType }, pUse) => {
const brooksDisplayName = `brooks_${Date.now()}`;
const whistlerDisplayName = `whistler_${Date.now()}`;
asWidget: async ({ browser, callType }, pUse, testInfo) => {
// Tests in parallel workers can reach this in the same millisecond.
const uniqueSuffix = `${Date.now()}_${testInfo.parallelIndex}`;
const brooksDisplayName = `brooks_${uniqueSuffix}`;
const whistlerDisplayName = `whistler_${uniqueSuffix}`;
// Register users
const {
+7 -2
View File
@@ -5,9 +5,10 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { test as setup } from "@playwright/test";
import { expect, test as setup } from "@playwright/test";
import { execFileSync } from "node:child_process";
import { mkdirSync } from "node:fs";
import { mkdirSync, readdirSync } from "node:fs";
import { join } from "node:path";
const EMBEDDED_DIST = "embedded/web/dist";
@@ -17,4 +18,8 @@ setup("build the embedded package", () => {
execFileSync("pnpm", ["build:embedded", "--outDir", EMBEDDED_DIST], {
stdio: "inherit",
});
// The host does the crypto, and nothing runs the standalone client
expect(readdirSync(join(EMBEDDED_DIST, "assets"))).not.toContainEqual(
expect.stringMatching(/rust-crypto|matrix_sdk_crypto|IndexedDBWorker/),
);
});
+11 -2
View File
@@ -22,6 +22,12 @@ const PASSWORD = "foobarbaz1!";
export const HOST1 = "https://app.m.localhost/#/welcome";
export const HOST2 = "https://app.othersite.m.localhost/#/welcome";
/**
* Where the widget RTC SDK's example widget is served (`pnpm dev:widget-rtc-sdk`):
* `widget-rtc-sdk/index.html`, running the bundle from source.
*/
export const WIDGET_RTC_SDK_URL = "https://localhost:3003";
export type RtcMode = "compat" | "2_0";
export class TestHelpers {
@@ -206,8 +212,11 @@ export class TestHelpers {
await expect(dialog).toBeHidden({ timeout: 5000 });
}
// The same element shows the next toast a moment after the click, so
// reading it straight away acts on the dismissed one a second time
await expect(toastTitle).not.toHaveText(title, { timeout: 5000 });
// reading it straight away acts on the dismissed one a second time.
// Counted rather than read, as the last toast leaves no title behind.
await expect(toastTitle.filter({ hasText: title })).toHaveCount(0, {
timeout: 5000,
});
} else {
// Unknown toast. We don't want to act on unknown toasts
break;
+20
View File
@@ -0,0 +1,20 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { test as setup } from "@playwright/test";
import { execFileSync } from "node:child_process";
// Into the directory the widget RTC SDK's preview server already serves (see
// playwright.config.ts)
setup("build the widget RTC SDK example", () => {
setup.setTimeout(10 * 60 * 1000);
execFileSync(
"pnpm",
["exec", "vite", "build", "--config", "vite-widget-rtc-sdk-dev.config.ts"],
{ stdio: "inherit" },
);
});
+129
View File
@@ -0,0 +1,129 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, type FrameLocator, type Page } from "@playwright/test";
import { widgetTest } from "../fixtures/widget-user.ts";
import { WIDGET_RTC_SDK_URL } from "./test-helpers.ts";
/**
* The widget RTC SDK's example widget, `widget-rtc-sdk/index.html`, added to a
* room as a custom widget and opened in two Element Webs: the bundle's whole
* public API, from the widget API to the data channel.
*/
widgetTest.describe.configure({ timeout: 300_000 });
const WIDGET_ID = "widget-rtc-sdk-example";
const WIDGET_NAME = "Widget RTC SDK";
/** Accepts the prompts Element Web shows before it runs a widget, and finds it. */
async function openWidget(page: Page): Promise<FrameLocator> {
// Only for a widget someone else added
const loadPrompt = page.getByRole("button", { name: "Continue" });
const capabilitiesPrompt = page.getByRole("button", { name: "Approve" });
await expect(loadPrompt.or(capabilitiesPrompt)).toBeVisible({
timeout: 30_000,
});
if (await loadPrompt.isVisible()) await loadPrompt.click();
await capabilitiesPrompt.click({ timeout: 30_000 });
// The SDK authenticates with the media transport by an OpenID token, and
// asks for more than one, so the answer has to be remembered
const openIdPrompt = page.getByRole("dialog", {
name: "Allow this widget to verify your identity",
});
await openIdPrompt.getByText("Remember this").click({ timeout: 30_000 });
await openIdPrompt.getByRole("button", { name: "Continue" }).click();
return page.frameLocator(`iframe[title="${WIDGET_NAME}"]`);
}
widgetTest(
"data sent from one widget arrives in the other",
async ({ asWidget }) => {
const { brooks, whistler } = asWidget;
// Brooks adds the example widget to the room and pins it to the top, so
// that both Element Webs open it without anyone looking for it
await brooks.clientHandle.evaluate(
async (client, { widgetId, name, url }) => {
const roomId = client
.getRooms()
.find((room) => room.name === "Welcome Room")!.roomId;
await client.sendStateEvent(
roomId,
// @ts-expect-error not in the js-sdk's state event map
"im.vector.modular.widgets",
{
id: widgetId,
type: "m.custom",
name,
url,
data: {},
creatorUserId: client.getUserId(),
},
widgetId,
);
await client.sendStateEvent(
roomId,
// @ts-expect-error not in the js-sdk's state event map
"io.element.widgets.layout",
{
widgets: {
[widgetId]: {
container: "top",
index: 0,
width: 100,
height: 50,
},
},
},
"",
);
},
{
widgetId: WIDGET_ID,
name: WIDGET_NAME,
// Element Web fills the $ variables in and adds widgetId and parentUrl
url:
`${WIDGET_RTC_SDK_URL}/?widgetId=$matrix_widget_id&roomId=$matrix_room_id` +
"&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id" +
"&baseUrl=$org.matrix.msc4039.matrix_base_url",
},
);
const brooksWidget = await openWidget(brooks.page);
const whistlerWidget = await openWidget(whistler.page);
// The example joins on load; each sees the other as a member
for (const [widget, other] of [
[brooksWidget, whistler],
[whistlerWidget, brooks],
] as const) {
await expect(widget.locator("#connect_status")).toHaveText("Connected", {
timeout: 60_000,
});
await expect(widget.locator("#members")).toContainText(other.mxId, {
timeout: 60_000,
});
}
// Data is not queued: the receiver drops what arrives before it has matched
// the sender on the media transport, which the example does not show. So
// send until it arrives.
await expect(async () => {
await brooksWidget.getByRole("button", { name: "Send Text" }).click();
await expect(whistlerWidget.locator("#data")).toContainText(
"Hello, world!",
{ timeout: 2_000 },
);
}).toPass({ timeout: 60_000 });
// Our own data is not echoed back
await expect(brooksWidget.locator("#data")).not.toContainText(
"Hello, world!",
);
},
);
+117
View File
@@ -0,0 +1,117 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
/**
* Checks the component build's output for what a host must not be handed.
*
* A library build inlines every asset it meets as a data URL unless the import
* says otherwise, and does so silently: one import without `no-inline` and the
* module grows by the asset, encoded a third larger, that every host then
* downloads and parses before the first call. MediaPipe's runtime alone is
* 11 MB. A reference to a file by an absolute path is just as silent and
* breaks at runtime instead, since a host serves the module from wherever it
* likes. And the standalone app's client store worker, which carries a copy of
* the Matrix SDK and its crypto, has no business in a package whose host
* supplies the client: the bundler emits it on loading its import, used or
* not (see src/utils/initClient.ts). This fails the build on each of those.
*/
import { existsSync } from "node:fs";
import { readdir, readFile, stat } from "node:fs/promises";
import { basename, join } from "node:path";
const dist = "component/dist";
/** The client store worker, whose chunk is named after this file. */
const workerSource = "src/IndexedDBWorker.ts";
/** The longest data URL a chunk may carry: enough for an icon, not a sound. */
const inlineLimit = 16 * 1024;
/**
* The module itself, in bytes. Well above where it sits with its assets as
* files, so that only an asset finding its way back in trips it.
*/
const moduleBudget = 4 * 1024 * 1024;
const problems = [];
// A rename would otherwise turn the worker check off without a word.
if (!existsSync(workerSource))
problems.push(
`${workerSource} is gone, so the check for the client store worker ` +
`cannot recognise its chunk. Point \`workerSource\` in ` +
`scripts/check-component-bundle.mjs at the worker's new name.`,
);
const workerName = basename(workerSource, ".ts");
for (const name of await assets())
if (name.includes(workerName))
problems.push(
`${dist}/assets/${name}: the standalone app's client store worker is ` +
`in the package. Something the component reaches imports the worker; ` +
`keep it on the standalone app's side (see src/utils/initClient.ts ` +
`and the \`loadClient\` prop of ClientProvider).`,
);
for (const file of await modules()) {
const code = await readFile(file, "utf8");
// Up to the closing quote rather than the end of the base64 alphabet, since
// SVGs are inlined URL-encoded rather than as base64.
for (const [, , type, payload] of code.matchAll(
/(["'`])data:([a-z0-9/.+-]*)(?:;base64)?,(.*?)\1/g,
))
if (payload.length > inlineLimit)
problems.push(
`${file}: a ${type} asset of ${Math.round(payload.length / 1024)} kB ` +
`is inlined as a data URL. Import it with \`?url&no-inline\` so ` +
`that it is emitted as a file.`,
);
for (const [reference] of code.matchAll(/["'`]\/assets\/[^"'`]+["'`]/g))
problems.push(
`${file}: ${reference} is an absolute path, which only resolves ` +
`against an origin that serves the package at its root. The build ` +
`is meant to render relative references; check \`base\` in ` +
`vite-component.config.ts.`,
);
}
const moduleSize = (await stat(join(dist, "element-call.js"))).size;
if (moduleSize > moduleBudget)
problems.push(
`${dist}/element-call.js is ${(moduleSize / 1024 / 1024).toFixed(1)} MB, ` +
`over its ${moduleBudget / 1024 / 1024} MB budget. Something large ` +
`has been bundled into the module that a host would rather fetch when ` +
`it is needed.`,
);
if (problems.length > 0) {
console.error(`The component build is not fit to publish:\n`);
for (const problem of problems) console.error(` ${problem}\n`);
process.exit(1);
}
/** The files under `assets/`, none if the build emitted no such directory. */
async function assets() {
try {
return await readdir(join(dist, "assets"));
} catch (e) {
if (e.code === "ENOENT") return [];
throw e;
}
}
/**
* The modules a host bundles: the entry points and their chunks, at the top of
* the package. What sits under `assets/` is fetched as it is, by URL.
*/
async function modules() {
const names = await readdir(dist);
return names.filter((name) => name.endsWith(".js")).map((n) => join(dist, n));
}
+6 -6
View File
@@ -9,11 +9,11 @@ Please see LICENSE in the repository root for full details.
* Checks that the library builds leave the packages a host must supply to the
* host.
*
* A host application already has React, the Matrix SDK and Compound,
* and a second copy of any of them is worse than dead weight: React would hold
* two sets of hooks, the Matrix client would run two sync loops, and a second
* Compound would style the tooltips it floats into the host's body with class
* names the host's stylesheet does not know. So each library build lists them
* A host application already has React and Compound (and, for the SDK build,
* the Matrix SDK), and a second copy of any of them is worse than dead weight:
* React would hold two sets of hooks, and a second Compound would style the
* tooltips it floats into the host's body with class names the host's
* stylesheet does not know. So each library build lists them
* as external — but that list has to name every subpath, since the bundler
* silently ignores the pattern and callback forms of the option, and an import
* it does not cover is bundled with no warning at all. That is the failure
@@ -47,10 +47,10 @@ const TARGETS = [
// `sdk` too: the build resolves `@element-hq/matrixrtc-sdk` to its source
sources: ["src", "component", "sdk"],
excluded: ["component/dev", "sdk/dev"],
// Not the Matrix SDK: the component bundles its own (see the build config)
mustBeExternal: [
"react",
"react-dom",
"matrix-js-sdk",
"@vector-im/compound-web",
"@vector-im/compound-design-tokens",
],
@@ -1,68 +0,0 @@
# SDK mode (LEGACY)
> Superseded by the `sdk/` package (`@element-hq/matrixrtc-sdk`) and its harness in
> `sdk/dev`; see `sdk/SdkArchitecture.md`. This bundle is in production use, so it
> keeps building as a thin layer over that package until its consumers have moved
> to the package directly. It will then be removed. Its members are the package's
> `RTCMember`s now, in place of its own wrapper around the raw membership.
EC can be build in sdk mode. This will result in a compiled js file that can be imported in very simple webapps.
It allows to use matrixRTC in combination with livekit without relying on element call.
This is done by instantiating the call view model and exposing some useful behaviors (observables) and methods.
This folder contains an example index.html file that showcases the sdk in use (hosted on localhost:8123 with a webserver allowing cors (for example `npx serve -l 81234 --cors`)) as a godot engine HTML export template.
## Getting started
To get started run
```
pnpm install
pnpm build:sdk-target-based-on-call-view-model
```
in the repository root. (`pnpm build:sdk` builds the `sdk/` package instead.)
CI's `sdk` package (`build:sdk:production`, `build:sdk:development`, the
`build-output-sdk` artifact and the `pr<N>-sdk` Netlify preview) is still this
bundle, not the `sdk/` package, until its consumers have moved over.
It will create `dist` containing the compiled js file, `matrixrtc-sdk.js`.
This file needs to be hosted. Locally (via `npx serve -l 81234 --cors`) or on a remote server.
Now you just need to add the widget to element web via:
```
/addwidget http://localhost:3000?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
```
## Widgets
The sdk mode is particularly interesting to be used in widgets. In widgets you do not need to pay attention to matrix login/cs api ...
To create a widget see the example `index.html` file in this folder. And add it to EW via:
`/addwidget <widgetUrl>` (see **url parameters** for more details on `<widgetUrl>`)
### url parameters
The url parameters are needed to pass initial data to the widget. They will automatically be used
by the matrixRTCSdk to start the postmessage widget api (communication between the client (e.g. Element Web) and the widget)
```
widgetId = $matrix_widget_id
userId = $matrix_user_id
deviceId = $org.matrix.msc3819.matrix_device_id
baseUrl = $org.matrix.msc4039.matrix_base_url
```
`parentUrl = // will be inserted automatically`
Full template use as `<widgetUrl>`:
```
http://localhost:3000?widgetId=$matrix_widget_id&userId=$matrix_user_id&deviceId=$org.matrix.msc3819.matrix_device_id&baseUrl=$org.matrix.msc4039.matrix_base_url&roomId=$matrix_room_id
```
the `$` prefixed variables will be replaced by EW on widget instantiation. (e.g. `$matrix_user_id` -> `@user:example.com` (url encoding will also be applied automatically by EW) -> `%40user%3Aexample.com`)
-193
View File
@@ -1,193 +0,0 @@
/*
Copyright 2025-2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
/**
* LEGACY, in production use
*
* The entrypoint of `pnpm build:sdk-target-based-on-call-view-model`, a
* bundle for widgets that exposes `createMatrixRTCSdk`: join a slot and
* exchange realtime data, with the `@element-hq/matrixrtc-sdk` package and
* Element Call's call view model doing the work underneath. New consumers use
* the package directly; this bundle goes away once the existing ones have.
*/
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
import { EMPTY, filter, map, type Observable, of, switchMap, take } from "rxjs";
import {
type Behavior,
createRTCSlot,
E2eeType,
type LocalRTCMember,
ObservableScope,
type RemoteRTCMember,
type RTCParticipation,
} from "@element-hq/matrixrtc-sdk";
import {
callViewModelOptionsFromParams,
type CallViewModel,
createCallViewModel$,
initialPublishRequests,
} from "../src/state/CallViewModel/CallViewModel";
import {
captureSettings,
rtcParticipationOptions,
rtcSlotOptions,
} from "../src/room/InCallView";
import { createWidgetHostBridge } from "../src/HostBridge";
import { MediaDevices } from "../src/state/MediaDevices";
import { MuteStates } from "../src/state/MuteStates";
import { getUrlParams } from "../src/UrlParams";
import { observeElementSize$ } from "../src/utils/elementSize";
import { initializeWidget } from "../src/widget";
/** The data channel topic this bundle has always used, so that old and new builds talk. */
const TOPIC = "matrixRTC";
export interface MatrixRTCSdk {
/** Joins the slot; `connected$` tracks the progress. A second call does nothing. */
join: () => void;
leave: () => void;
/** Ends everything. The sdk cannot be restarted; create a new one. */
stop: () => void;
connected$: Behavior<boolean>;
/** Our own member, null until our membership has been seen in the room. */
localMember$: Behavior<LocalRTCMember | null>;
/** The other members of the slot, with the media the participation carries for them. */
remoteMembers$: Behavior<RemoteRTCMember[]>;
/** What other members sent with `sendData`. */
data$: Observable<{ rtcBackendIdentity: string; data: string }>;
/** Sends to every member on our transport. Rejects while not joined. */
sendData: (data: unknown) => Promise<void>;
sendRoomMessage: (message: string) => Promise<void>;
}
export async function createMatrixRTCSdk(
application = "m.call",
id = "",
sticky = false,
): Promise<MatrixRTCSdk> {
const logger = rootLogger.getChild("[MatrixRTCSdk]");
const scope = new ObservableScope();
const widget = initializeWidget(application, true);
if (!widget) throw new Error("No widget: this bundle only runs as a widget");
const client = await widget.client;
const hostBridge = createWidgetHostBridge(widget);
const urlParams = getUrlParams();
const { roomId, controlledAudioDevices, callIntent } = urlParams;
const room = roomId === null ? null : client.getRoom(roomId);
if (room === null) throw new Error("No room: the url needs a joined roomId");
const mediaDevices = new MediaDevices(scope, {
controlledAudioDevices,
callIntent,
});
const muteStates = new MuteStates(
scope,
mediaDevices,
{ audioEnabled: false, videoEnabled: false },
hostBridge,
);
const encryptionSystem = { kind: E2eeType.PER_PARTICIPANT } as const;
const capture = captureSettings();
const slot = createRTCSlot(scope, client, room, {
...rtcSlotOptions(encryptionSystem),
application,
id,
});
let vm: CallViewModel | undefined;
const failed =
(what: string) =>
(e: unknown): void =>
logger.error(what, e);
const over = <T>(
pick: (participation: RTCParticipation) => Observable<T>,
fallback: T,
): Behavior<T> =>
scope.behavior(
slot.participation$.pipe(
switchMap((p) => (p === null ? of(fallback) : pick(p))),
),
);
return {
join: (): void => {
if (slot.participation$.value !== null) return;
if (sticky)
widget.api
.setAlwaysOnScreen(true)
.catch(failed("Failed to make the widget sticky"));
const participation = slot.join({
...rtcParticipationOptions(urlParams),
publish: initialPublishRequests(muteStates, mediaDevices, capture),
});
vm = createCallViewModel$(
scope,
{
rtcParticipation: participation,
matrixRoom: room,
mediaDevices,
muteStates,
encryptionSystem,
// The bundle owns its page, so the body is the space it has
viewSize$: scope.behavior(observeElementSize$(document.body)),
handsRaisedSubject$: of({}),
reactionsSubject$: of({}),
},
{
...callViewModelOptionsFromParams(urlParams),
capture,
hostBridge,
},
);
// Once the hang-up has gone through, unstick and ask the host to close us
vm.leave$
.pipe(
switchMap(() =>
participation.state$.pipe(
filter((state) => state.kind === "left"),
take(1),
),
),
scope.bind(),
)
.subscribe(() => {
hostBridge
.setAlwaysOnScreen(false)
.catch(failed("Failed to unstick the widget"))
.then(() => hostBridge.close?.())
.catch(failed("Failed to ask the host to close"));
});
},
leave: (): void => vm?.leave(),
stop: (): void => scope.end(),
connected$: over(
(p) => p.state$.pipe(map((state) => state.kind === "connected")),
false,
),
localMember$: over((p) => p.localMember$, null),
remoteMembers$: over((p) => p.remoteMembers$, []),
// Attested members only: the SDK has matched the sender
data$: slot.participation$.pipe(
switchMap((p) => p?.data$ ?? EMPTY),
filter(({ topic }) => topic === TOPIC),
map(({ member, text }) => ({
rtcBackendIdentity: member.rtcBackendIdentity,
data: text,
})),
),
sendData: async (data): Promise<void> => {
const participation = slot.participation$.value;
if (participation === null) throw new Error("Not joined");
await participation.sendData(TOPIC, JSON.stringify(data));
},
sendRoomMessage: async (message): Promise<void> => {
await client.sendTextMessage(room.roomId, message);
},
};
}
+9 -8
View File
@@ -5,8 +5,7 @@ it joins a MatrixRTC slot in a Matrix room, writes and watches the memberships,
connects to the media transport (LiveKit today), publishes the local media and
exposes every member and their tracks as observables. It has no UI. Element
Call's own `CallViewModel` is one consumer of it; any host can build a different
view on top. The design is in [`SdkArchitecture.md`](./SdkArchitecture.md), what
moved out of Element Call's `src/` in [`SdkMigration.md`](./SdkMigration.md).
view on top. The design is in [`SdkArchitecture.md`](./SdkArchitecture.md).
**Status:** first implementation. `createRTCSlot` and `slot.join()` join the
session, connect to the transport, publish the local media and expose every
@@ -19,6 +18,7 @@ nothing in Element Call's `src/`.
```ts
import {
createRTCSlot,
createSdkMatrixBackend,
E2eeType,
MatrixRTCMode,
ObservableScope,
@@ -28,15 +28,15 @@ import {
const scope = new ObservableScope();
const slot = createRTCSlot(
scope,
client, // a matrix-js-sdk MatrixClient, logged in and syncing
room, // the matrix-js-sdk Room, from client.getRoom() once the join has synced
{
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
matrixRTCMode: MatrixRTCMode.Compatibility,
},
createSdkMatrixBackend(
client, // a matrix-js-sdk MatrixClient, logged in and syncing
room, // the matrix-js-sdk Room, from client.getRoom() once the join has synced
),
{ encryptionSystem: { kind: E2eeType.PER_PARTICIPANT } }, // the room's call, m.call#ROOM
);
const participation = slot.join({
publish: [{ source: "microphone" }, { source: "camera" }],
matrixRTCMode: MatrixRTCMode.Compatibility,
});
// A <video> for each remote member's camera and an <audio> for its microphone.
@@ -99,6 +99,7 @@ own only so that a host can install it as a git dependency (see
```sh
pnpm dev:sdk # the harness, https://localhost:3002
pnpm build:sdk # dist/matrixrtc-sdk.js and dist/types
pnpm build:sdk:development # the same, unminified
pnpm test:playwright playwright/sdk # needs `pnpm backend`
```
+13 -15
View File
@@ -10,8 +10,7 @@ interface.
Element Call becomes one consumer of it: `CallViewModel` keeps what makes a session
a _call_ (ringing, auto-leave, layout, sounds, reactions, settings, the host
bridge) and takes everything else from the client. The dependency direction is
therefore `Element Call → sdk`, never the reverse. What moved out of Element
Call's `src/` to get there is listed in [`SdkMigration.md`](./SdkMigration.md).
therefore `Element Call → sdk`, never the reverse.
## The architecture
@@ -146,18 +145,18 @@ participation split in
[`client-into-slot-session-split-plan.md`](./client-into-slot-session-split-plan.md);
the sections below still describe `MatrixRTCClient` until that lands.
| Word | Meaning here | Elsewhere |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| slot | an `RTCSlot`: the namespace in a room that memberships join into, `application` plus `id`. It exists while the room does, with nobody in it too. Holds `members$`, its open or closed `status$`, and `join()` | the js-sdk `MatrixRTCSession` is one per slot; the MSC4143 slot event |
| participation | an `RTCParticipation`: the span between our `join()` of a slot and our `leave()`. Holds the connection and its `state$`, `localMember$`, `remoteMembers$`, the connected transports and the data channel. Made by `slot.join()`, never reused | "session" is not used here, because in the js-sdk it means the slot |
| member | an `RTCMember`: one membership in a slot with what the SDK derives from it, `rtcBackendIdentity`, `userId`, `deviceId`, `memberId`, `displayName$`, `avatarUrl$`, `transport$` and `applicationData$`. No media. What the slot hands out in `members$` | the js-sdk `CallMembership` behind it, which never leaves the SDK; Element Call's old "matrixLivekitMember" |
| local member, remote member | `LocalRTCMember` and `RemoteRTCMember`: an `RTCMember` plus its `MemberMedia`, which only a participation can carry. The local one's tracks carry the controls. What the participation hands out in `localMember$` and `remoteMembers$` | LiveKit `LocalParticipant` and `RemoteParticipant`, which never leave the SDK |
| media | a `MemberMedia`: what a member sends once it has arrived on its transport, `tracks$`, null while waiting, and `encryptionError$` beside it. The media backend supplies exactly these two fields per member | Element Call's `MediaViewModel` is a view of it |
| track | a `MediaTrack`: one publication of a member, audio or video, with `source`, `kind`, `id` and the behaviors on it. `LocalAudioMediaTrack` and `LocalVideoMediaTrack` add the controls over our own | LiveKit `TrackPublication` and `Track`, which never leave the SDK |
| publish request | a `PublishRequest`: a source, where to capture it from and how to encode it. Given at the join in `publish`, or later to `publish` on the participation | |
| attach, detach | handing a `<video>` or `<audio>` element to a track and taking it back. The SDK owns the stream and the observers on the element | LiveKit `Track.attach` |
| transport | an `RTCTransport`: where media is exchanged, a LiveKit service url today. Carries the raw js-sdk `Transport` from the membership and, in `resolved$`, what the backend fetched to connect to it | MSC4143 "focus" |
| media backend | a `MediaBackend`: what carries the media, behind `sdk/src/media-backend/api.ts`. One per participation, chosen with `backend` in the join options; LiveKit under `sdk/src/media-backend/livekit/` is the one that exists | "SFU", a LiveKit room, a peer connection |
| Word | Meaning here | Elsewhere |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------- |
| slot | an `RTCSlot`: the namespace in a room that memberships join into, `application` plus `id`. It exists while the room does, with nobody in it too. Holds `members$`, its open or closed `status$`, and `join()` | the js-sdk `MatrixRTCSession` is one per slot; the MSC4143 slot event |
| participation | an `RTCParticipation`: the span between our `join()` of a slot and our `leave()`. Holds the connection and its `state$`, `localMember$`, `remoteMembers$`, the connected transports and the data channel. Made by `slot.join()`, never reused | "session" is not used here, because in the js-sdk it means the slot |
| member | an `RTCMember`: one membership in a slot with what the SDK derives from it, `rtcBackendIdentity`, `userId`, `deviceId`, `memberId`, `matrixEventId$`, `displayName$`, `avatarUrl$`, `transport$` and `applicationData$`. No media. What the slot hands out in `members$` | the js-sdk `CallMembership` behind it, which never leaves the SDK; Element Call's old "matrixLivekitMember" |
| local member, remote member | `LocalRTCMember` and `RemoteRTCMember`: an `RTCMember` plus its `MemberMedia`, which only a participation can carry. The local one's tracks carry the controls. What the participation hands out in `localMember$` and `remoteMembers$` | LiveKit `LocalParticipant` and `RemoteParticipant`, which never leave the SDK |
| media | a `MemberMedia`: what a member sends once it has arrived on its transport, `tracks$`, null while waiting, and `encryptionError$` beside it. The media backend supplies exactly these two fields per member | Element Call's `MediaViewModel` is a view of it |
| track | a `MediaTrack`: one publication of a member, audio or video, with `source`, `kind`, `id` and the behaviors on it. `LocalAudioMediaTrack` and `LocalVideoMediaTrack` add the controls over our own | LiveKit `TrackPublication` and `Track`, which never leave the SDK |
| publish request | a `PublishRequest`: a source, where to capture it from and how to encode it. Given at the join in `publish`, or later to `publish` on the participation | |
| attach, detach | handing a `<video>` or `<audio>` element to a track and taking it back. The SDK owns the stream and the observers on the element | LiveKit `Track.attach` |
| transport | an `RTCTransport`: where media is exchanged, a LiveKit service url today. Carries the raw js-sdk `Transport` from the membership and, in `resolved$`, what the backend fetched to connect to it | MSC4143 "focus" |
| media backend | a `MediaBackend`: what carries the media, behind `sdk/src/media-backend/api.ts`. One per participation, chosen with `backend` in the join options; LiveKit under `sdk/src/media-backend/livekit/` is the one that exists | "SFU", a LiveKit room, a peer connection |
Names use `RTC` in capitals, as the js-sdk does: `RTCSlot`, `RTCParticipation`,
`RTCMember`, `matrixRTCMode`.
@@ -782,7 +781,6 @@ sdk/
src/utils/ mapScoped, network retry, display names, test helpers
dev/ the harness (below)
SdkArchitecture.md this document
SdkMigration.md what moved from Element Call's src/ into the SDK
```
The lint rule `element-call/sdk-import-boundary` keeps it that way: nothing under
-53
View File
@@ -1,53 +0,0 @@
# What moved from `src/` into the SDK
Element Call's `CallViewModel` used to do everything; now it consumes the SDK and
keeps only what makes a session a call. This is the inventory of what moved out
of `src/` on the way, so that a reader of either side knows where to look. It
names source files, so it ages with the code; the shape they make is in
[`SdkArchitecture.md`](./SdkArchitecture.md).
Everything in the table is gone from `src/`. Each module came with its tests;
`sdk/src/utils/test.ts` and `test-fixtures.ts` are the subset of Element Call's
test helpers they need. `Behavior`, `ObservableScope`, the observable operators,
`E2eeType`, `EncryptionSystem` and `MatrixRTCMode` are re-exported from the SDK
by their old `src/` paths, so the rest of Element Call did not have to change.
| Was in `src/` | Is in `sdk/src/` | What changed on the way |
| ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `state/Behavior.ts`, `state/ObservableScope.ts`, `utils/observable.ts` | `reactive/` | as is |
| `state/SessionBehaviors.ts` | `matrixrtc/JsRtcSessionBehaviors.ts` | the memberships and key rotation behaviors only |
| `state/CallViewModel/localMember/HomeserverConnected.ts` | `matrixrtc/HomeserverConnected.ts` | the grace period defaults to `defaultSessionTimings` instead of `Config.get()` |
| `state/CallViewModel/localMember/LocalMember.ts` | `matrixrtc/LocalMembership.ts`, `media-backend/livekit/LocalMedia.ts`, `participationState.ts` | split into the Matrix side, the media side and the state machine that combines them |
| `state/CallViewModel/localMember/LocalTransport.ts`, `RtcTransportAutoDiscovery.ts` | `matrixrtc/LocalTransport.ts`, `TransportAutoDiscovery.ts` | the override and the fallback are transports from the options, not a setting and a resolved config |
| `state/CallViewModel/remoteMembers/MatrixLivekitMembers.ts` | `media-backend/livekit/LivekitBackend.ts` (`mediaFor`) | matches participants to members inside the backend; the SDK hands out `RTCMember`s |
| `state/CallViewModel/remoteMembers/MatrixMemberMetadata.ts`, `utils/displayname.ts` | `matrixrtc/MatrixMemberMetadata.ts`, `utils/displayname.ts` | as is |
| `state/CallViewModel/remoteMembers/Connection.ts` | `media-backend/livekit/Connection.ts` | throws `MatrixRTCError`s; `ConnectionState` is a discriminated union; fetches its own token for its role |
| `state/CallViewModel/remoteMembers/ConnectionFactory.ts` | `media-backend/livekit/ConnectionFactory.ts` | `LivekitConnectionFactory` builds the room options from the publish requests and `mediaQuality`, not from settings |
| `state/CallViewModel/remoteMembers/ConnectionManager.ts` | `media-backend/livekit/ConnectionMap.ts` | keyed by `TransportLocator`; the publisher role is the local transport's |
| `state/CallViewModel/localMember/Publisher.ts` | `media-backend/livekit/Publisher.ts` | rewritten on publish requests instead of `MuteStates` |
| `livekit/openIDSFU.ts`, later `livekit/auth/` | `media-backend/livekit/auth/` | the flow is chosen by the transport's shape and our role; the delegated leave timeout is a parameter |
| `livekit/options.ts` | `media-backend/livekit/livekitOptions.ts` | `buildLiveKitOptions(mediaQuality?)` only; the `Config` singleton is gone |
| `e2ee/matrixKeyProvider.ts`, the key provider selection | `media-backend/livekit/MatrixKeyProvider.ts`, `KeyProvider.ts` | as is; selection keyed off `encryptionSystem` |
| `e2ee/e2eeType.ts`, `EncryptionSystem` from `e2ee/sharedKeyManagement.ts` | `encryption.ts` | the React hooks stayed behind |
| `MatrixRTCMode`, the timings and media quality types from `config/ConfigOptions.ts` | `config.ts` | `defaultSessionTimings` and `defaultMediaQuality` replace `DEFAULT_CONFIG` |
| `utils/errors.ts` | `errors.ts` | `MatrixRTCError` with `code` and `category` replaces `ElementCallError`; messages are plain English, the host translates by code |
| `doNetworkOperationWithRetry` from `utils/matrix.ts` | `utils/network.ts` | takes a `shouldRetry` predicate |
| the join in `CallViewModel.ts` (`enterRTCSession`) | `matrixrtc/joinJsSdkSession.ts` | the timings and the delayed leave variant are parameters |
| `livekit/MatrixAudioRenderer.tsx`, the participant reads in `state/media/` | `media-backend/livekit/LivekitMediaTrack.ts`, `LivekitMemberMedia.ts` | the view models get `MediaTrack`s with `attach`; nothing in `src/` imports a LiveKit participant any more |
## What still binds Element Call to LiveKit
Each is a gap in the SDK's API rather than something to rename; the import goes
away when the SDK grows the piece. The names Element Call owes to the outside
stay as they are: the `livekit.livekit_service_url` key in `config.json`, the
persisted setting keys `extended-livekit-logs` and `custom-transport`, the
Posthog fields `callBackend` and `callReconnectingCountLivekit`, the rageshake
field `call_backend`, and the translated error strings.
| Gap | Where it shows |
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| Preview before `join()`: local tracks for the lobby, handed out as `MediaTrack`s | `LobbyView` (`usePreviewTracks`), `VideoPreview` (`LocalVideoTrack`, `facingModeFromLocalTrack`) |
| Device enumeration without LiveKit's helpers | `MediaDevices` (`createMediaDeviceObserver`), `CallView` (`Room.getLocalDevices`) |
| A `logging` option: log level and a sink for the transport's logs | `initializer.tsx`, the `extended-livekit-logs` setting |
| A `capabilities` field: output selection and background processors, asked of the SDK rather than of LiveKit | `CallFooterViewModel` (`supportsAudioOutputSelection`, `supportsBackgroundProcessors`) |
| Element Call's error and analytics types on the SDK's | `PosthogEvents` (`DisconnectReason`), `errors.ts` (`ConnectionError`, `LivekitConnectionError`) |
+3 -2
View File
@@ -16,6 +16,7 @@ import { combineLatest, type Observable, of, switchMap } from "rxjs";
import {
type AudioMediaTrack,
createRTCSlot,
createSdkMatrixBackend,
E2eeType,
type LocalRTCMember,
MatrixRTCMode,
@@ -70,12 +71,12 @@ async function start(
const room = await joinRoom(client, roomIdOrAlias);
const scope = new ObservableScope();
const slot = createRTCSlot(scope, client, room, {
const slot = createRTCSlot(scope, createSdkMatrixBackend(client, room), {
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
matrixRTCMode: MatrixRTCMode.Compatibility,
});
const rtcParticipation = slot.join({
publish: [{ source: "microphone" }, { source: "camera" }],
matrixRTCMode: MatrixRTCMode.Compatibility,
});
rtcParticipation.state$.pipe(scope.bind()).subscribe((state) => {
status.textContent =
+5
View File
@@ -57,6 +57,11 @@ export * from "./src/media-api";
export * from "./src/media-backend/api";
export { trackBySource$, type TrackOfSource } from "./src/utils/tracks";
export { createRTCSlot } from "./src/RTCSlot";
export {
createSdkMatrixBackend,
type SdkMatrixBackend,
withEmitter,
} from "./src/SdkMatrixBackend";
export {
createLivekitBackend,
type LivekitBackendOptions,
+17 -14
View File
@@ -22,7 +22,6 @@ import {
type ParticipationState,
type RTCMember,
type RTCParticipationOptions,
type RTCSlot,
type TransportMetadata,
} from "./api";
import { defaultSessionTimings, MatrixRTCMode } from "./config";
@@ -86,6 +85,7 @@ function member(
rtcBackendIdentity: overrides.memberId,
backendIdentities: [overrides.memberId],
deviceId: "DEV",
matrixEventId$: constant(`$${overrides.memberId}`),
transport$: constant(metadata(transport)),
applicationData$: constant({}),
displayName$: constant(overrides.userId),
@@ -111,7 +111,6 @@ interface FakeBackend {
function setup(
options: Partial<RTCParticipationOptions> = {},
slotOptions: { matrixRTCMode?: MatrixRTCMode } = {},
{
homeserverTransports = [transport],
memberships = [],
@@ -191,19 +190,24 @@ function setup(
scope,
{
client,
room,
slot: {} as RTCSlot,
jsSdkSession: mockedSession,
members$: scope.behavior(members$),
transports: { get: metadata, connected$: constant([]) },
slotOptions: {
matrixRTCMode: MatrixRTCMode.Compatibility,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
...slotOptions,
slot: {
roomId: room.roomId,
application: "m.call",
id: "ROOM",
status$: constant(undefined),
members$: scope.behavior(members$),
},
jsSdkSession: mockedSession,
transports: { get: metadata, connected$: constant([]) },
slotOptions: { encryptionSystem: { kind: E2eeType.PER_PARTICIPANT } },
logger,
},
{ publish: [], backend: backendFactory, ...options },
{
publish: [],
matrixRTCMode: MatrixRTCMode.Compatibility,
backend: backendFactory,
...options,
},
);
const states: ParticipationState[] = [];
participation.state$.subscribe((state) => states.push(state));
@@ -254,7 +258,6 @@ describe("createRTCParticipation", () => {
fociPreferred: [{ type: "livekit", url: "wss://sfu.example.org" }],
});
const { join, fake } = setup(
{},
{},
{ homeserverTransports: [], memberships: [bobOnSfu] },
);
@@ -349,7 +352,7 @@ describe("createRTCParticipation", () => {
});
compatibility.scope.end();
const matrix2 = setup({}, { matrixRTCMode: MatrixRTCMode.Matrix_2_0 });
const matrix2 = setup({ matrixRTCMode: MatrixRTCMode.Matrix_2_0 });
const { memberId } = matrix2.fake.context.ownMembershipIdentity;
expect(memberId).not.toBe(legacyMemberId);
expect(memberId).toMatch(/^[0-9a-f-]{36}$/);
+53 -33
View File
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { type MatrixClient } from "matrix-js-sdk";
import { type Logger } from "matrix-js-sdk/lib/logger";
import {
type MatrixRTCSession as JsSdkRTCSession,
@@ -30,19 +30,17 @@ import {
import { defaultSessionTimings, MatrixRTCMode } from "./config";
import { E2eeType } from "./encryption";
import { type Behavior } from "./reactive/Behavior";
import { type ObservableScope } from "./reactive/ObservableScope";
import { generateItems } from "./reactive/observable";
import { mapScoped } from "./utils/mapScoped";
import {
type ParticipationSlot,
type DataMessage,
type LocalRTCMember,
type ParticipationState,
type RemoteRTCMember,
type RTCMember,
type RTCParticipation,
type RTCParticipationOptions,
type RTCSlot,
type RTCSlotOptions,
} from "./api";
import {
@@ -67,15 +65,34 @@ import { createKeyRotationSuppressed$ } from "./matrixrtc/JsRtcSessionBehaviors"
import { type TransportRegistry } from "./matrixrtc/Transports";
import { participationState } from "./participationState";
/** What the slot lends a participation. */
/** What is needed for our own participation. */
export interface ParticipationDeps {
client: MatrixClient;
room: Room;
slot: RTCSlot;
client: Pick<
MatrixClient,
// Our own membership identity
| "getUserId"
| "getDeviceId"
// Local transport discovery
| "getDomain"
| "getRTCTransports"
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
| "getOpenIdToken"
| "getLivekitToken"
| "delegateDelayedLeave"
| "baseUrl"
// Homeserver connection: whether the client is syncing
| "getSyncState"
// `on`/`off`, to listen for:
// - `ClientEvent.Sync` (homeserver connection)
| "on"
| "off"
>;
slot: ParticipationSlot;
jsSdkSession: JsSdkRTCSession;
members$: Behavior<RTCMember[]>;
transports: TransportRegistry;
slotOptions: Pick<RTCSlotOptions, "matrixRTCMode" | "encryptionSystem">;
slotOptions: RTCSlotOptions;
logger: Logger;
}
@@ -94,16 +111,19 @@ export function createRTCParticipation(
scope: ObservableScope,
{
client,
room,
slot,
jsSdkSession,
members$,
transports,
slotOptions: { matrixRTCMode, encryptionSystem },
slotOptions: { encryptionSystem },
logger: parentLogger,
}: ParticipationDeps,
options: RTCParticipationOptions,
): ParticipationInternals {
const { matrixRTCMode } = options;
const { roomId, members$ } = slot;
// The full `{application}#{id}`, which the auth service and the other
// members key the media room on
const slotId = jsSdkSession.slotId;
const logger = parentLogger.getChild("[RTCParticipation]");
const userId = client.getUserId()!;
const deviceId = client.getDeviceId()!;
@@ -122,8 +142,8 @@ export function createRTCParticipation(
};
const backend = createBackend(scope, client, options, {
roomId: room.roomId,
slotId: jsSdkSession.slotId,
roomId,
slotId,
ownMembershipIdentity,
publish: options.publish,
encryptionSystem,
@@ -144,8 +164,8 @@ export function createRTCParticipation(
// to come back later rather than wait in a half-working lobby
memberships: jsSdkSession.memberships,
ownMembershipIdentity,
roomId: room.roomId,
slotId: jsSdkSession.slotId,
roomId,
slotId,
logger,
})
: null;
@@ -175,21 +195,15 @@ export function createRTCParticipation(
timings.syncDisconnectGracePeriodMs,
),
joinMatrixRTC: (transport, delayedLeave) =>
joinJsSdkSession(
room.roomId,
jsSdkSession,
ownMembershipIdentity,
transport,
{
encryptMedia: encryptionSystem.kind !== E2eeType.NONE,
matrixRTCMode,
sendNotificationType: options.sendNotificationType,
callIntent: options.callIntent,
applicationData: options.applicationData,
timings,
delayedLeave,
},
),
joinJsSdkSession(roomId, jsSdkSession, ownMembershipIdentity, transport, {
encryptMedia: encryptionSystem.kind !== E2eeType.NONE,
matrixRTCMode,
sendNotificationType: options.sendNotificationType,
callIntent: options.callIntent,
applicationData: options.applicationData,
timings,
delayedLeave,
}),
membershipManagerError$: fromEvent(
jsSdkSession,
MatrixRTCSessionEvent.MembershipManagerError,
@@ -315,7 +329,13 @@ export function createRTCParticipation(
/** The host's backend, or LiveKit configured from the participation options. */
function createBackend(
scope: ObservableScope,
client: MatrixClient,
client: Pick<
MatrixClient,
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
"getOpenIdToken" | "getLivekitToken" | "delegateDelayedLeave" | "baseUrl"
>,
{ backend, mediaQuality }: RTCParticipationOptions,
context: MediaBackendContext,
): MediaBackend {
+202 -9
View File
@@ -5,23 +5,39 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, it } from "vitest";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { EMPTY } from "rxjs";
import { describe, expect, it, vi } from "vitest";
import { type MatrixClient, type Room, SyncState } from "matrix-js-sdk";
import { EventType } from "matrix-js-sdk/lib/@types/event";
import { type MatrixEvent } from "matrix-js-sdk/lib/models/event";
import {
type RoomState,
RoomStateEvent,
} from "matrix-js-sdk/lib/models/room-state";
import { RoomStickyEventsEvent } from "matrix-js-sdk/lib/models/room-sticky-events";
import {
MatrixRTCSessionEvent,
type MatrixRTCSessionEventHandlerMap,
} from "matrix-js-sdk/lib/matrixrtc";
import { createRTCSlot } from "./RTCSlot";
import { type RTCSlotOptions } from "./api";
import { type SdkMatrixBackend } from "./SdkMatrixBackend";
import { ObservableScope } from "./reactive/ObservableScope";
import { type RTCParticipationOptions, type RTCSlotOptions } from "./api";
import { MatrixRTCMode } from "./config";
import { E2eeType } from "./encryption";
import { mockEmitter, MockRTCSession, testScope } from "./utils/test";
import { MatrixRTCError } from "./errors";
import { type MediaBackend, MediaConnectionState } from "./media-backend/api";
import { constant } from "./reactive/Behavior";
import {
mockEmitter,
mockRtcMembership,
MockRTCSession,
testScope,
} from "./utils/test";
const options: RTCSlotOptions = {
matrixRTCMode: MatrixRTCMode.Compatibility,
encryptionSystem: { kind: E2eeType.NONE },
};
@@ -34,28 +50,140 @@ function setup(): {
...mockEmitter(),
roomId: "!room:example.org",
getMembersWithMembership: (): [] => [],
// An empty room, for a session of the slot's own
getLiveTimeline: () => ({ getState: (): undefined => undefined }),
hasMembershipState: (): boolean => false,
getVersion: (): string => "11",
_unstable_getStickyEvents: (): [] => [],
} as unknown as Room;
const session = new MockRTCSession(room);
const client = {
...mockEmitter(),
getUserId: (): string => "@alice:example.org",
getDeviceId: (): string => "AAAA",
matrixRTC: { getRoomSession: (): MockRTCSession => session },
} as unknown as MatrixClient;
return { client, room, session };
}
function backendOf(client: MatrixClient, room: Room): SdkMatrixBackend {
return {
...client,
...room,
clientEmitter: client,
roomEmitter: room,
} as unknown as SdkMatrixBackend;
}
/** A media backend whose transport never gets ready: a participation on it stays waiting. */
function idleBackend(): MediaBackend {
return {
transportTypes: ["livekit"],
prepareLocalTransport: async () => new Promise(() => {}),
delegateDelayedLeave: vi.fn(),
local: {
connectionState$: constant(MediaConnectionState.Initialized),
setPublishing: vi.fn(),
publishError$: constant(null),
publish: vi.fn(),
unpublish: vi.fn(),
tracks$: constant(null),
encryptionError$: EMPTY,
},
mediaFor: vi.fn(),
connections$: constant([]),
setAudioOutputDeviceId: vi.fn(),
sendData: vi.fn(),
data$: EMPTY,
};
}
describe("createRTCSlot", () => {
describe("session", () => {
it("joins the room's call slot by default", () => {
const { client, room } = setup();
const slot = createRTCSlot(testScope(), backendOf(client, room), options);
expect(slot).toMatchObject({ application: "m.call", id: "ROOM" });
});
it("joins the slot the options name", () => {
const { client, room } = setup();
const slot = createRTCSlot(testScope(), backendOf(client, room), {
...options,
slotDescription: { application: "io.element.example", id: "" },
});
expect(slot).toMatchObject({ application: "io.element.example", id: "" });
});
it("stops its own session when the scope ends", async () => {
const { client, room } = setup();
const off = vi.spyOn(room, "off");
const scope = new ObservableScope();
createRTCSlot(scope, backendOf(client, room), options);
scope.end();
await vi.waitFor(() =>
expect(off).toHaveBeenCalledWith(
RoomStickyEventsEvent.Update,
expect.any(Function),
),
);
});
it("leaves a session it is given to its caller", () => {
const { client, room, session } = setup();
const given = session.asMockedSession();
given.stop = vi.fn();
const scope = new ObservableScope();
createRTCSlot(scope, backendOf(client, room), options, given);
scope.end();
expect(given.stop).not.toHaveBeenCalled();
});
it("has the session's statistics and sent call notifications", () => {
const { client, room, session } = setup();
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
expect(slot.statistics).toBe(session.statistics);
const sent: unknown[] = [];
slot.sentCallNotification$.subscribe((n) => sent.push(n));
const notification = { event_id: "$notification" };
session.emit(
MatrixRTCSessionEvent.DidSendCallNotification,
notification as Parameters<
MatrixRTCSessionEventHandlerMap[MatrixRTCSessionEvent.DidSendCallNotification]
>[0],
);
expect(sent).toEqual([notification]);
});
});
describe("status$", () => {
it("is undefined while the room has no slot event", () => {
const { client, room } = setup();
const slot = createRTCSlot(testScope(), client, room, options);
const { client, room, session } = setup();
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
expect(slot.status$.value).toBeUndefined();
});
it("follows the slot event", () => {
const { client, room, session } = setup();
session.rtcSlot = { status: "open" };
const slot = createRTCSlot(testScope(), client, room, options);
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
expect(slot.status$.value).toBe("open");
session.rtcSlot = { status: "closed" };
@@ -68,4 +196,69 @@ describe("createRTCSlot", () => {
expect(slot.status$.value).toBe("closed");
});
});
describe("join", () => {
it("holds one participation at a time, and is free again once it has left", () => {
const { client, room, session } = setup();
const slot = createRTCSlot(
testScope(),
backendOf(
{
...client,
getDomain: (): string => "example.org",
getSyncState: (): SyncState => SyncState.Syncing,
getRTCTransports: vi.fn().mockResolvedValue([]),
} as unknown as MatrixClient,
room,
),
options,
session.asMockedSession(),
);
const joinOptions: RTCParticipationOptions = {
publish: [],
matrixRTCMode: MatrixRTCMode.Compatibility,
backend: idleBackend,
};
expect(slot.participation$.value).toBeNull();
const participation = slot.join(joinOptions);
expect(slot.participation$.value).toBe(participation);
expect(participation.slot).toMatchObject({
roomId: room.roomId,
application: "m.call",
id: "ROOM",
});
expect(() => slot.join(joinOptions)).toThrow(MatrixRTCError);
participation.leave();
expect(slot.participation$.value).toBeNull();
const again = slot.join(joinOptions);
expect(slot.participation$.value).toBe(again);
again.leave();
});
});
describe("members$", () => {
it("keeps the member when its membership is sent again, with the new event id", () => {
const { client, room, session } = setup();
const first = mockRtcMembership("@bob:example.org", "BOB");
session.memberships = [first];
const slot = createRTCSlot(
testScope(),
backendOf(client, room),
options,
session.asMockedSession(),
);
const [bob] = slot.members$.value;
expect(bob.matrixEventId$.value).toBe(first.eventId);
const resent = mockRtcMembership("@bob:example.org", "BOB");
session.memberships = [resent];
session.emit(MatrixRTCSessionEvent.MembershipsChanged, [first], [resent]);
expect(slot.members$.value).toHaveLength(1);
expect(slot.members$.value[0]).toBe(bob);
expect(bob.matrixEventId$.value).toBe(resent.eventId);
expect(resent.eventId).not.toBe(first.eventId);
});
});
});
+124 -74
View File
@@ -5,12 +5,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { type MatrixClient } from "matrix-js-sdk";
import { EventType } from "matrix-js-sdk/lib/@types/event";
import { type Logger, logger as rootLogger } from "matrix-js-sdk/lib/logger";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
import {
type MatrixRTCSession as JsSdkRTCSession,
MatrixRTCSessionManager,
type Statistics,
MatrixRTCSession as JsMatrixRTCSession,
MatrixRTCSessionEvent,
type IRTCNotificationContent,
type SlotDescription,
} from "matrix-js-sdk/lib/matrixrtc";
import { type MatrixEvent } from "matrix-js-sdk/lib/models/event";
@@ -20,6 +22,7 @@ import {
filter,
fromEvent,
map,
type Observable,
of,
startWith,
switchMap,
@@ -28,6 +31,7 @@ import {
import { ObservableScope } from "./reactive/ObservableScope";
import { generateItems } from "./reactive/observable";
import {
type ParticipationSlot,
type RTCMember,
type RTCParticipation,
type RTCParticipationOptions,
@@ -50,33 +54,64 @@ import {
membershipKeys,
} from "./matrixrtc/Members";
import { createTransportRegistry } from "./matrixrtc/Transports";
import {
type createSdkMatrixBackend,
type SdkMatrixBackend,
withEmitter,
} from "./SdkMatrixBackend";
const ROOM_CALL_SLOT: SlotDescription = { application: "m.call", id: "ROOM" };
/**
* Takes the whole `MatrixClient` rather than a slice of it, and finds the
* MatrixRTC session itself. The js-sdk is the MatrixRTC implementation today;
* when the rust-rtc crate replaces it, the SDK has to bridge the crate to the
* js-sdk client, and only the SDK knows what that bridge needs. Holding the
* client keeps that change inside the SDK.
* The slot runs on a matrix-js-sdk session of its own, from the SDK's copy of
* matrix-js-sdk rather than the client's, and stops it when the scope ends.
*/
export function createRTCSlot(
scope: ObservableScope,
client: MatrixClient,
room: Room,
backend: SdkMatrixBackend,
options: RTCSlotOptions,
): RTCSlot {
/**
* @deprecated A session the caller owns and stops, in place of the slot's
* own; `options.slotDescription` is then the session's. Only for the
* standalone app, which still takes its session from its client's session
* manager.
*/
externalJsSdkSession?: JsMatrixRTCSession,
): RTCSlot & {
// STOP GAP: This should be its own external machine. It is call
// specific and should not live inside the sdk/rtc-slot.
// We only put it here today, to make sure the rtcSession is fully
// hidden inside the sdk.
// Next step. disable sending call notificaitons in the sdk, write a new
// notification sender in EC directly on client + room.
sentCallNotification$: Observable<
{
event_id: string;
} & IRTCNotificationContent
>;
// STOP GAP: the same as above. Has to live here. but should become a native
// SDK propery independent of the rtcSession
statistics: Statistics;
} {
// STOP GAP: the slot is still written against a client and a room. Both
// are refactored along with the backend.
const client = withEmitter(backend, backend.clientEmitter);
const room = withEmitter(backend, backend.roomEmitter);
const logger = rootLogger.getChild("[RTCSlot]");
const userId = client.getUserId();
const deviceId = client.getDeviceId();
const roomId = room.roomId;
if (!(userId && deviceId))
throw new MatrixRTCError("The client has to be logged in");
const { application = "m.call", id = "ROOM" } = options;
const jsSdkSession = sessionManager(
scope,
client,
{ application, id },
logger,
).getRoomSession(room);
const jsSdkSession =
externalJsSdkSession ??
createJsSdkSession(
scope,
client,
room,
options.slotDescription ?? ROOM_CALL_SLOT,
);
const memberships$ = createMemberships$(scope, jsSdkSession);
const active$ = new BehaviorSubject<ParticipationInternals | null>(null);
@@ -126,70 +161,85 @@ export function createRTCSlot(
),
);
const slot: RTCSlot = {
roomId: room.roomId,
application,
id,
const participationSlot: ParticipationSlot = {
roomId,
status$,
members$,
...jsSdkSession.slotDescription,
};
const join = (
participationOptions: RTCParticipationOptions,
): RTCParticipation => {
if (active$.value !== null)
throw new MatrixRTCError(
"Already in this slot: leave the participation before joining again",
);
const participationScope = new ObservableScope();
participationScopes.add(participationScope);
const internals = createRTCParticipation(
participationScope,
{
client,
slot: participationSlot,
jsSdkSession,
transports,
slotOptions: options,
logger,
},
participationOptions,
);
participationScope.onEnd(() => {
participationScopes.delete(participationScope);
if (active$.value === internals) active$.next(null);
});
active$.next(internals);
return internals.participation;
};
const slot = {
join,
participation$: scope.behavior(
active$.pipe(map((active) => active?.participation ?? null)),
),
join: (participationOptions: RTCParticipationOptions): RTCParticipation => {
if (active$.value !== null)
throw new MatrixRTCError(
"Already in this slot: leave the participation before joining again",
);
const participationScope = new ObservableScope();
participationScopes.add(participationScope);
const internals = createRTCParticipation(
participationScope,
{
client,
room,
slot,
jsSdkSession,
members$,
transports,
slotOptions: options,
logger,
},
participationOptions,
);
participationScope.onEnd(() => {
participationScopes.delete(participationScope);
if (active$.value === internals) active$.next(null);
});
active$.next(internals);
return internals.participation;
},
...participationSlot,
};
return slot;
return {
...slot,
sentCallNotification$: fromEvent(
jsSdkSession,
MatrixRTCSessionEvent.DidSendCallNotification,
(notification: { event_id: string } & IRTCNotificationContent) =>
notification,
),
statistics: jsSdkSession.statistics,
};
}
function createJsSdkSession(
scope: ObservableScope,
client: Pick<
MatrixClient,
keyof Parameters<typeof createSdkMatrixBackend>[0]
>,
room: ConstructorParameters<typeof JsMatrixRTCSession>[1],
slotDescription: SlotDescription,
): JsMatrixRTCSession {
const session = new JsMatrixRTCSession(
// The session's type also asks for `cancelPendingEvent`,
// `_unstable_updateDelayedEvent` and `decryptEventIfNeeded`, which it
// never calls
client as ConstructorParameters<typeof JsMatrixRTCSession>[0],
room,
slotDescription,
);
scope.onEnd(() => void session.stop());
return session;
}
function slotStatus(
session: Pick<JsSdkRTCSession, "getRtcSlot">,
session: Pick<JsMatrixRTCSession, "getRtcSlot">,
): "open" | "closed" | undefined {
return session.getRtcSlot()?.status;
}
/**
* The js-sdk keeps one session manager per slot. The client comes with the
* manager for the default slot; any other slot gets a manager of its own,
* which lives as long as the scope.
*/
function sessionManager(
scope: ObservableScope,
client: MatrixClient,
{ application, id }: SlotDescription,
logger: Logger,
): MatrixRTCSessionManager {
if (application === "m.call" && id === "ROOM") return client.matrixRTC;
const manager = new MatrixRTCSessionManager(logger, client, {
application,
id,
});
manager.start();
scope.onEnd(() => manager.stop());
return manager;
}
+85
View File
@@ -0,0 +1,85 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, it, vi } from "vitest";
import {
ClientEvent,
createClient,
type MatrixClient,
Room,
RoomEvent,
SyncState,
} from "matrix-js-sdk";
import { createSdkMatrixBackend, withEmitter } from "./SdkMatrixBackend";
describe("createSdkMatrixBackend", () => {
it("calls each member on the client or the room it came from", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
expect(backend.getUserId()).toBe("@alice:example.org");
expect(backend.roomId).toBe("!room:example.org");
expect(backend.getVersion()).toBe(room.getVersion());
});
it("keeps the client's and the room's events apart", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
const onClient = vi.fn();
const onRoom = vi.fn();
backend.clientEmitter.on(ClientEvent.Sync, onClient);
backend.roomEmitter.on(RoomEvent.Name, onRoom);
client.emit(ClientEvent.Sync, SyncState.Syncing, null);
expect(onClient).toHaveBeenCalledOnce();
expect(onRoom).not.toHaveBeenCalled();
room.emit(RoomEvent.Name, room);
expect(onRoom).toHaveBeenCalledOnce();
});
});
describe("withEmitter", () => {
it("is the backend with one side's events", () => {
const { client, room } = setup();
const backend = createSdkMatrixBackend(client, room);
const asRoom = withEmitter(backend, backend.roomEmitter);
const listener = vi.fn();
asRoom.on(RoomEvent.Name, listener);
room.emit(RoomEvent.Name, room);
expect(listener).toHaveBeenCalledOnce();
expect(asRoom.getUserId()).toBe("@alice:example.org");
});
it("reads the backend's getters through", () => {
const { room } = setup();
const asRoom = withEmitter(
{
get name() {
return room.name;
},
},
room,
);
room.name = "Renamed";
expect(asRoom.name).toBe("Renamed");
});
});
function setup(): { client: MatrixClient; room: Room } {
const client = createClient({
baseUrl: "https://matrix.example.org",
userId: "@alice:example.org",
deviceId: "DEVICE",
});
const room = new Room("!room:example.org", client, "@alice:example.org");
return { client, room };
}
+129
View File
@@ -0,0 +1,129 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
/** What the SDK needs of the client. */
type SdkClientPart = Pick<
MatrixClient,
// Our own membership identity
| "getUserId"
| "getDeviceId"
// The session: memberships, delayed leave events, sticky events, call
// notifications, and the media keys sent to device
| "fetchCapabilities"
| "getCachedCapabilities"
| "sendEvent"
| "sendStateEvent"
| "_unstable_sendDelayedStateEvent"
| "_unstable_cancelScheduledDelayedEvent"
| "_unstable_restartScheduledDelayedEvent"
| "_unstable_sendScheduledDelayedEvent"
| "_unstable_sendStickyEvent"
| "_unstable_sendStickyDelayedEvent"
| "encryptAndSendToDevice"
// Local transport discovery
| "getDomain"
| "getRTCTransports"
// LiveKit auth: `getOpenIdToken` for the JWT service flow, `getLivekitToken`
// for the client-server API flow (MSC4195), `delegateDelayedLeave` and
// `baseUrl` for the delayed leave delegation and its probe
| "getOpenIdToken"
| "getLivekitToken"
| "delegateDelayedLeave"
| "baseUrl"
// Homeserver connection: whether the client is syncing
| "getSyncState"
>;
/** What the SDK needs of the room. */
type SdkRoomPart = Pick<
Room,
| "roomId"
// The members' display names and avatars
| "getMembersWithMembership"
// The session: memberships from the room's state, timeline and sticky events
| "getLiveTimeline"
| "getVersion"
| "hasMembershipState"
| "_unstable_getStickyEvents"
>;
/**
* STOP GAP: what the SDK needs of a Matrix client and room, on one object.
* Only their emitters stay apart, since both have an `on` and an `off`. It is
* to converge, along with the SDK that takes it, on whichever API fits every
* layer best.
*/
export interface SdkMatrixBackend extends SdkClientPart, SdkRoomPart {
/**
* `ClientEvent.Sync` (homeserver connection) and `ClientEvent.ToDeviceEvent`
* (the session's media keys)
*/
clientEmitter: Pick<MatrixClient, "on" | "off">;
/** The room's state, member and sticky events */
roomEmitter: Pick<Room, "on" | "off">;
}
/** The SDK's backend from a client and a room. */
export function createSdkMatrixBackend(
client: SdkClientPart & Pick<MatrixClient, "on" | "off">,
room: SdkRoomPart & Pick<Room, "on" | "off">,
): SdkMatrixBackend {
return {
getUserId: client.getUserId.bind(client),
getDeviceId: client.getDeviceId.bind(client),
fetchCapabilities: client.fetchCapabilities.bind(client),
getCachedCapabilities: client.getCachedCapabilities.bind(client),
sendEvent: client.sendEvent.bind(client),
sendStateEvent: client.sendStateEvent.bind(client),
_unstable_sendDelayedStateEvent:
client._unstable_sendDelayedStateEvent.bind(client),
_unstable_cancelScheduledDelayedEvent:
client._unstable_cancelScheduledDelayedEvent.bind(client),
_unstable_restartScheduledDelayedEvent:
client._unstable_restartScheduledDelayedEvent.bind(client),
_unstable_sendScheduledDelayedEvent:
client._unstable_sendScheduledDelayedEvent.bind(client),
_unstable_sendStickyEvent: client._unstable_sendStickyEvent.bind(client),
_unstable_sendStickyDelayedEvent:
client._unstable_sendStickyDelayedEvent.bind(client),
encryptAndSendToDevice: client.encryptAndSendToDevice.bind(client),
getDomain: client.getDomain.bind(client),
getRTCTransports: client.getRTCTransports.bind(client),
getOpenIdToken: client.getOpenIdToken.bind(client),
getLivekitToken: client.getLivekitToken.bind(client),
delegateDelayedLeave: client.delegateDelayedLeave.bind(client),
baseUrl: client.baseUrl,
getSyncState: client.getSyncState.bind(client),
roomId: room.roomId,
getMembersWithMembership: room.getMembersWithMembership.bind(room),
getLiveTimeline: room.getLiveTimeline.bind(room),
getVersion: room.getVersion.bind(room),
hasMembershipState: room.hasMembershipState.bind(room),
_unstable_getStickyEvents: room._unstable_getStickyEvents.bind(room),
clientEmitter: client,
roomEmitter: room,
};
}
/**
* STOP GAP: the backend as the client (or the room) code written against one
* still expects, with that side's `on` and `off`.
*/
export function withEmitter<
Backend extends object,
Emitter extends {
on: (...args: never[]) => unknown;
off: (...args: never[]) => unknown;
},
>(backend: Backend, emitter: Emitter): Backend & Pick<Emitter, "on" | "off"> {
return Object.defineProperties<object>(
{ on: emitter.on.bind(emitter), off: emitter.off.bind(emitter) },
Object.getOwnPropertyDescriptors(backend),
) as Backend & Pick<Emitter, "on" | "off">;
}
+23 -7
View File
@@ -14,6 +14,7 @@ Please see LICENSE in the repository root for full details.
import {
type RTCCallIntent,
type RTCNotificationType,
type SlotDescription,
type Transport,
} from "matrix-js-sdk/lib/matrixrtc";
import { type Observable } from "rxjs";
@@ -43,13 +44,9 @@ export { MatrixRTCError } from "./errors";
// Slot
export interface RTCSlotOptions {
/** The application the slot belongs to, as named in the membership. Default `m.call`. */
application?: string;
/** The application's slot in the room. Default `ROOM`. */
id?: string;
/** How memberships and keys in this slot are written. Resolved by the host. */
matrixRTCMode: MatrixRTCMode;
encryptionSystem: EncryptionSystem;
/** Which slot of the room to join. Defaults to the room's call, `m.call#ROOM`. */
slotDescription?: SlotDescription;
}
/**
@@ -81,6 +78,12 @@ export interface RTCSlot {
join(options: RTCParticipationOptions): RTCParticipation; // Maybe MediaSession,
}
/**
* The slot as its participation sees it: members, status and identity, without
* `join` and `participation$`, which belong to whoever joins.
*/
export type ParticipationSlot = Omit<RTCSlot, "join" | "participation$">;
// ---------------------------------------------------------------------------
// Participation
@@ -95,6 +98,12 @@ export interface RTCParticipationOptions {
* before the first connection exists.
*/
publish: PublishRequest[] | LocalMedia;
/**
* How our membership and keys are written: the member id and whether the
* membership is a sticky event. Resolved by the host. Others' memberships
* are read whatever their mode.
*/
matrixRTCMode: MatrixRTCMode;
/**
* LEGACY
*
@@ -166,7 +175,8 @@ export type ParticipationState =
/** The span between our `join()` of a slot and our `leave()`. */
export interface RTCParticipation {
readonly slot: RTCSlot;
/** Required slot information (no `join` fn and `participation`) */
readonly slot: ParticipationSlot;
/** Sends the leave, tears the connections down and ends the participation. */
leave(): void;
state$: Behavior<ParticipationState>;
@@ -289,6 +299,12 @@ export interface RTCMember {
deviceId: string;
/** The membership's own id; what the Matrix 2.0 identity is derived from. */
memberId: string;
/**
* The Matrix event currently carrying the membership, which events about
* it (reactions, raised hands) relate to. Changes when the membership is
* sent again while the member stays the same.
*/
matrixEventId$: Behavior<string>;
/** Which transport this member is on; undefined when the membership has none. */
transport$: Behavior<TransportMetadata | undefined>;
/** What the member's application says about itself, e.g. `intent`. */
+69 -21
View File
@@ -8,7 +8,7 @@ Please see LICENSE in the repository root for full details.
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { EventEmitter } from "events";
import { ClientEvent, SyncState } from "matrix-js-sdk";
import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk";
import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc";
import { ObservableScope } from "../reactive/ObservableScope";
@@ -20,7 +20,7 @@ import { testScope, withTestScheduler } from "../utils/test";
```
createHomeserverConnected$(
scope: ObservableScope,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
client: Pick<MatrixClient, "getSyncState" | "on" | "off">,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
)
@@ -34,12 +34,15 @@ class MockMatrixClient extends EventEmitter {
}
public setSyncState(state: SyncState): void {
this.syncState = state;
// Matrix's Sync event in createHomeserverConnected$ expects [SyncState]
this.emit(ClientEvent.Sync, [state]);
this.emit(ClientEvent.Sync, state);
}
public getSyncState(): SyncState {
return this.syncState;
}
/** `MatrixClient`'s `on`/`off` return the client itself, which no mock is. */
public get asClient(): Pick<MatrixClient, "getSyncState" | "on" | "off"> {
return this as unknown as Pick<MatrixClient, "getSyncState" | "on" | "off">;
}
}
/**
@@ -47,7 +50,7 @@ class MockMatrixClient extends EventEmitter {
```
createHomeserverConnected$(
scope: ObservableScope,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
client: Pick<MatrixClient, "getSyncState" | "on" | "off">,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
)
@@ -99,18 +102,33 @@ describe("createHomeserverConnected$", () => {
// easy enough to read them so I think they can stay.
// Note: gracePeriodMs is set to 0 to avoid debouncing delays in tests
it("reports syncing reason when sync state is not Syncing", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
expect(hsConnected.disconnectReason$.value).toEqual("sync");
});
it("reports membership reason when sync is Syncing but membership is not Connected", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
client.setSyncState(SyncState.Syncing);
expect(hsConnected.disconnectReason$.value).toEqual("membership");
});
it("reports probablyLeft reason when membership transitions to Connected but ProbablyLeft is true", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
// Make sync loop OK
client.setSyncState(SyncState.Syncing);
// Indicate probable leave before connection
@@ -120,7 +138,12 @@ describe("createHomeserverConnected$", () => {
});
it("becomes null (connected) only when all three conditions are satisfied", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
// 1. Sync loop connected
client.setSyncState(SyncState.Syncing);
expect(hsConnected.disconnectReason$.value).toEqual("membership"); // not yet membership connected
@@ -130,7 +153,12 @@ describe("createHomeserverConnected$", () => {
});
it("returns syncing reason when sync loop leaves Syncing", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
// Reach connected state
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
@@ -142,7 +170,12 @@ describe("createHomeserverConnected$", () => {
});
it("returns membershipConnected reason when membership status becomes disconnected", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -152,7 +185,12 @@ describe("createHomeserverConnected$", () => {
});
it("returns certainlyConnected reason when ProbablyLeft is emitted", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -162,7 +200,12 @@ describe("createHomeserverConnected$", () => {
});
it("recovers to null (connected) if ProbablyLeft becomes false again while other conditions remain true", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
client.setSyncState(SyncState.Syncing);
session.setMembershipStatus(Status.Connected);
expect(hsConnected.disconnectReason$.value).toEqual(null);
@@ -176,7 +219,12 @@ describe("createHomeserverConnected$", () => {
});
it("composite sequence reflects each individual failure reason", () => {
const hsConnected = createHomeserverConnected$(scope, client, session, 0);
const hsConnected = createHomeserverConnected$(
scope,
client.asClient,
session,
0,
);
// Initially: sync error + membership disconnected → syncing wins (highest priority)
expect(hsConnected.disconnectReason$.value).toEqual("sync");
@@ -225,7 +273,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("is null when all three conditions are satisfied", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -237,7 +285,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports syncing when sync loop is not Syncing", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -248,7 +296,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports membershipConnected when sync is fine but membership is not Connected", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -260,7 +308,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("reports certainlyConnected when probablyLeft is true", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -273,7 +321,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("prioritises syncing over membershipConnected when both fail", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -284,7 +332,7 @@ describe("createHomeserverConnected$ - disconnectReason$ reason values", () => {
it("updates reason as conditions change", () => {
const { disconnectReason$ } = createHomeserverConnected$(
scope,
client,
client.asClient,
session,
0,
);
@@ -329,7 +377,7 @@ describe("createHomeserverConnected$ - Grace Period", () => {
});
const hsConnected = createHomeserverConnected$(
testScope(),
client,
client.asClient,
session,
GRACE_PERIOD,
);
+15 -6
View File
@@ -17,7 +17,6 @@ import {
startWith,
map,
tap,
type Observable,
distinctUntilChanged,
switchMap,
of,
@@ -61,7 +60,15 @@ export interface HomeserverConnected {
*/
export function createHomeserverConnected$(
scope: ObservableScope,
client: NodeStyleEventEmitter & Pick<MatrixClient, "getSyncState">,
client: Pick<
MatrixClient,
// Whether the client is syncing
| "getSyncState"
// `on`/`off`, to listen for:
// - `ClientEvent.Sync`
| "on"
| "off"
>,
matrixRTCSession: NodeStyleEventEmitter &
Pick<MatrixRTCSession, "membershipStatus" | "probablyLeft">,
gracePeriodMs?: number,
@@ -71,11 +78,13 @@ export function createHomeserverConnected$(
const graceMs =
gracePeriodMs ?? defaultSessionTimings.syncDisconnectGracePeriodMs;
const syncing$ = (
fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]>
const syncing$ = fromEvent(
client,
ClientEvent.Sync,
(state: SyncState) => state,
).pipe(
startWith([client.getSyncState()]),
map(([state]) => state === SyncState.Syncing),
startWith(client.getSyncState()),
map((state) => state === SyncState.Syncing),
distinctUntilChanged(),
switchMap((isSyncing) => {
if (isSyncing || graceMs <= 0) {
+5 -2
View File
@@ -23,7 +23,9 @@ export type RoomMemberMap = Map<
string,
Pick<RoomMember, "userId" | "getMxcAvatarUrl" | "rawDisplayName">
>;
export function roomToMembersMap(matrixRoom: MatrixRoom): RoomMemberMap {
export function roomToMembersMap(
matrixRoom: Pick<MatrixRoom, "getMembersWithMembership">,
): RoomMemberMap {
const members = matrixRoom
.getMembersWithMembership(KnownMembership.Join)
.concat(matrixRoom.getMembersWithMembership(KnownMembership.Invite));
@@ -37,9 +39,10 @@ export function roomToMembersMap(matrixRoom: MatrixRoom): RoomMemberMap {
}, new Map());
}
/** Listens for `RoomStateEvent.Members` on the room. */
export function createRoomMembers$(
scope: ObservableScope,
matrixRoom: MatrixRoom,
matrixRoom: Pick<MatrixRoom, "getMembersWithMembership" | "on" | "off">,
): Behavior<RoomMemberMap> {
return scope.behavior(
fromEvent(matrixRoom, RoomStateEvent.Members).pipe(
+6
View File
@@ -57,6 +57,12 @@ export function createRTCMember(
userId,
deviceId,
memberId,
matrixEventId$: scope.behavior(
membership$.pipe(
map((membership) => membership.eventId),
distinctUntilChanged(),
),
),
displayName$: scope.behavior(
metadata
.createDisplayNameBehavior$(scope, userId)
@@ -344,6 +344,7 @@ describe("createLivekitBackend", () => {
userId: "@bob:example.org",
deviceId: "DEV000",
memberId: legacyId,
matrixEventId$: constant("$bob"),
transport$: constant({
type: "livekit",
id: JSON.stringify(transport),
+1
View File
@@ -276,6 +276,7 @@ export class MockRTCSession extends TypedEventEmitter<
return this.joined;
}
public slotDescription = { application: "m.call", id: "ROOM" };
public slotId = "m.call#ROOM";
/** The id of the delayed leave event; undefined until the manager has sent one. */
public delayId: string | undefined = undefined;
+37
View File
@@ -0,0 +1,37 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, expect, test, vi } from "vitest";
const initSPA = vi.hoisted(() => vi.fn());
vi.mock("./utils/spa", () => ({ initSPA }));
afterEach(() => {
vi.unstubAllEnvs();
vi.resetModules();
initSPA.mockReset();
});
test("the app restores its own session", async () => {
vi.stubEnv("VITE_PACKAGE", "full");
const { loadStandaloneClient } = await import("./App");
const loadSession = vi.fn();
const clearSession = vi.fn();
initSPA.mockResolvedValue(null);
await expect(loadStandaloneClient!(loadSession, clearSession)).resolves.toBe(
null,
);
expect(initSPA).toHaveBeenCalledWith(loadSession, clearSession);
});
test("the embedded package has no session of its own", async () => {
vi.stubEnv("VITE_PACKAGE", "embedded");
const { loadStandaloneClient } = await import("./App");
expect(loadStandaloneClient).toBeUndefined();
});
+6 -2
View File
@@ -64,8 +64,12 @@ const SentryRoute = Sentry.withSentryReactRouterV7Routing(Route);
// - Imported on demand, as it was before, so that a widget, which gets its
// client from the host, never downloads it.
// - Module-level, so that the provider's effect sees one stable loader.
const loadStandaloneClient: ClientLoader = async (...args) =>
(await import("./utils/spa")).initSPA(...args);
// - None in the embedded package, which only runs as a widget: the constant
// condition lets its build drop the client setup and the store worker.
export const loadStandaloneClient: ClientLoader | undefined =
import.meta.env.VITE_PACKAGE === "embedded"
? undefined
: async (...args) => (await import("./utils/spa")).initSPA(...args);
interface SimpleProviderProps {
children: JSX.Element;
+42
View File
@@ -0,0 +1,42 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import { createClient, Room } from "matrix-js-sdk";
import { withEmitter } from "@element-hq/matrixrtc-sdk";
import { createCallMatrixBackend } from "./CallMatrixBackend";
test("has the SDK's part and the call's own", () => {
const { backend, room } = setup();
expect(backend.getUserId()).toBe("@alice:example.org");
expect(backend.getJoinRule()).toBe(room.getJoinRule());
expect(backend.relations).toBe(room.relations);
});
test("reads the room's name as it is now, also as a room", () => {
const { backend, room } = setup();
const asRoom = withEmitter(backend, backend.roomEmitter);
room.name = "Renamed";
expect(backend.name).toBe("Renamed");
expect(asRoom.name).toBe("Renamed");
});
function setup(): {
backend: ReturnType<typeof createCallMatrixBackend>;
room: Room;
} {
const client = createClient({
baseUrl: "https://matrix.example.org",
userId: "@alice:example.org",
deviceId: "DEVICE",
});
const room = new Room("!room:example.org", client, "@alice:example.org");
return { backend: createCallMatrixBackend(client, room), room };
}
+77
View File
@@ -0,0 +1,77 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type MatrixClient, type Room } from "matrix-js-sdk";
import {
createSdkMatrixBackend,
type SdkMatrixBackend,
} from "@element-hq/matrixrtc-sdk";
/** What a call needs of the client beyond the SDK's part. */
type CallClientPart = Pick<
MatrixClient,
// Reactions and raised hands: decrypting, and lowering a hand
| "decryptEventIfNeeded"
| "redactEvent"
// Our own display name and avatar
| "getUser"
// Changing them, where the host lets us (`supportsProfileChanges`)
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer settings
| "doesServerSupportUnstableFeature"
| "getCrypto"
>;
/** What a call needs of the room beyond the SDK's part. */
type CallRoomPart = Pick<
Room,
// The header and the invite link
| "name"
| "getMxcAvatarUrl"
// Whether to offer the invite link
| "getJoinRule"
// Raised hands already in the room
| "relations"
>;
/**
* STOP GAP: everything a call needs from Matrix on one object. Its
* `clientEmitter` also carries `MatrixEventEvent.Decrypted` (reactions); its
* `roomEmitter` every room event.
*/
export interface CallMatrixBackend
extends SdkMatrixBackend, CallClientPart, CallRoomPart {}
/** A call's backend from a client and a room. */
export function createCallMatrixBackend(
client: Parameters<typeof createSdkMatrixBackend>[0] & CallClientPart,
room: Parameters<typeof createSdkMatrixBackend>[1] & CallRoomPart,
): CallMatrixBackend {
return {
...createSdkMatrixBackend(client, room),
decryptEventIfNeeded: client.decryptEventIfNeeded.bind(client),
redactEvent: client.redactEvent.bind(client),
getUser: client.getUser.bind(client),
setDisplayName: client.setDisplayName.bind(client),
setAvatarUrl: client.setAvatarUrl.bind(client),
uploadContent: client.uploadContent.bind(client),
doesServerSupportUnstableFeature:
client.doesServerSupportUnstableFeature.bind(client),
getCrypto: client.getCrypto.bind(client),
// Read through: the room is renamed while we show it
get name() {
return room.name;
},
getMxcAvatarUrl: room.getMxcAvatarUrl.bind(room),
getJoinRule: room.getJoinRule.bind(room),
get relations() {
return room.relations;
},
};
}
+1 -1
View File
@@ -20,7 +20,7 @@ import {
const mockClient = (userId = "@alice:example.org"): MatrixClient =>
({
on: vi.fn(),
removeListener: vi.fn(),
off: vi.fn(),
getUserId: () => userId,
getDeviceId: () => "AAAA",
stopClient: vi.fn(),
+41 -3
View File
@@ -142,7 +142,7 @@ interface Props {
* itself — from the widget API, or by restoring or creating a session of its
* own.
*/
client?: MatrixClient;
client?: SuppliedClient;
/**
* How Element Call finds a client when none is supplied: the standalone app
* restores or creates a session of its own. Stable, as a hook dependency.
@@ -161,6 +161,37 @@ interface Props {
loadClient?: ClientLoader;
}
/**
* What is read of a supplied client under a call, and by this provider and
* analytics. Stable methods only, so a host's client, from the host's own copy
* of matrix-js-sdk, will do.
*/
export type SuppliedClient = Pick<
MatrixClient,
// The disconnected banner, and stopping a client opened elsewhere
| "on"
| "off"
| "stopClient"
// Analytics, through `window.matrixclient`
| "getCrypto"
| "isGuest"
| "getAccountDataFromServer"
| "setAccountData"
// The room's encryption system
| "getRoom"
// Avatars
| "mxcUrlToHttp"
| "getAccessToken"
// Rageshakes: who sends them, their crypto state, and asking others for theirs
| "getUserId"
| "getUser"
| "credentials"
| "deviceId"
| "secretStorage"
| "isKeyBackupKeyStored"
| "sendEvent"
>;
/**
* Restores or creates a session of Element Call's own, kept where the given
* functions keep it. Resolves to null when no one is signed in.
@@ -170,7 +201,14 @@ export type ClientLoader = (
clearSession: () => void,
) => Promise<InitResult | null>;
export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
export const ClientProvider: FC<Props> = ({
children,
client: suppliedClient,
loadClient,
}) => {
// STOP GAP: held as a whole client, as the standalone and widget pages need
// one. A component host's client is only read through `SuppliedClient`.
const client = suppliedClient as MatrixClient | undefined;
const leaveToHome = useLeaveToHome();
const hostBridge = useHostBridge();
@@ -372,7 +410,7 @@ export const ClientProvider: FC<Props> = ({ children, client, loadClient }) => {
return (): void => {
if (initClientState.client) {
initClientState.client.removeListener(ClientEvent.Sync, onSync);
initClientState.client.off(ClientEvent.Sync, onSync);
}
};
}, [initClientState, onSync, hostBridge]);
+8 -2
View File
@@ -18,11 +18,17 @@ Please see LICENSE in the repository root for full details.
*/
import {
RTC_CALL_INTENT_AUDIO,
RTC_CALL_INTENT_VIDEO,
type RTCCallIntent,
type RTCNotificationType,
} from "matrix-js-sdk/lib/matrixrtc";
// Not from the `matrixrtc` index above. Those are not type imports,
// So those two imports would add a whole copy of "matrix-js-sdk/lib/matrixrtc"
// The "api.ts" file is meant to be the only thing loaded by EW on pageload.
// Everything beyond it should be lazy loaded on first call start.
import {
RTC_CALL_INTENT_AUDIO,
RTC_CALL_INTENT_VIDEO,
} from "matrix-js-sdk/lib/matrixrtc/CallIntent";
import { platform } from "./Platform";
+20 -22
View File
@@ -15,7 +15,7 @@ import {
afterAll,
} from "vitest";
import { logger } from "matrix-js-sdk/lib/logger";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { type Statistics } from "matrix-js-sdk/lib/matrixrtc";
import { PosthogAnalytics } from "./PosthogAnalytics";
import {
@@ -34,16 +34,14 @@ const defaultTotals = {
roomEventEncryptionKeysReceivedTotalAge: 500,
};
function createMockRtcSession(overrides?: {
function createMockStatistics(overrides?: {
counters?: Partial<typeof defaultCounters>;
totals?: Partial<typeof defaultTotals>;
}): MatrixRTCSession {
}): Statistics {
return {
statistics: {
counters: { ...defaultCounters, ...overrides?.counters },
totals: { ...defaultTotals, ...overrides?.totals },
},
} as unknown as MatrixRTCSession;
counters: { ...defaultCounters, ...overrides?.counters },
totals: { ...defaultTotals, ...overrides?.totals },
} as Statistics;
}
describe("CallEnded", () => {
@@ -65,9 +63,9 @@ describe("CallEnded", () => {
it("warns if startTime is missing when track is called", () => {
const warnSpy = vi.spyOn(logger, "warn");
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession();
const statistics = createMockStatistics();
tracker.track("test-call-id", 2, false, mockSession);
tracker.track("test-call-id", 2, false, statistics);
expect(warnSpy).toHaveBeenCalledWith(
"[PosthogEvents] Failed to send posthog callEnded event due to missing startTime",
@@ -77,11 +75,11 @@ describe("CallEnded", () => {
it("tracks event with correct properties when startTime is set", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession();
const statistics = createMockStatistics();
tracker.cacheStartCall(new Date(Date.now() - 60000));
tracker.cacheParticipantCountChanged(5);
tracker.track("test-call-id", 3, true, mockSession);
tracker.track("test-call-id", 3, true, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
{
@@ -105,13 +103,13 @@ describe("CallEnded", () => {
it("tracks maxParticipantsCount correctly across multiple changes", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession();
const statistics = createMockStatistics();
tracker.cacheStartCall(new Date());
tracker.cacheParticipantCountChanged(3);
tracker.cacheParticipantCountChanged(7);
tracker.cacheParticipantCountChanged(2);
tracker.track("test-call-id", 1, false, mockSession);
tracker.track("test-call-id", 1, false, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -123,12 +121,12 @@ describe("CallEnded", () => {
it("computes roomEventEncryptionKeysReceivedAverageAge as 0 when no keys received", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession({
const statistics = createMockStatistics({
counters: { roomEventEncryptionKeysReceived: 0 },
});
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, mockSession);
tracker.track("test-call-id", 1, false, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -140,13 +138,13 @@ describe("CallEnded", () => {
it("computes roomEventEncryptionKeysReceivedAverageAge correctly when keys are received", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession({
const statistics = createMockStatistics({
counters: { roomEventEncryptionKeysReceived: 4 },
totals: { roomEventEncryptionKeysReceivedTotalAge: 200 },
});
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, mockSession);
tracker.track("test-call-id", 1, false, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
@@ -158,10 +156,10 @@ describe("CallEnded", () => {
it("passes send_instantly option correctly", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession();
const statistics = createMockStatistics();
tracker.cacheStartCall(new Date());
tracker.track("test-call-id", 1, false, mockSession);
tracker.track("test-call-id", 1, false, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.anything(),
@@ -171,14 +169,14 @@ describe("CallEnded", () => {
it("includes per-reason reconnecting counts in CallEnded", () => {
const tracker = new CallEndedTracker();
const mockSession = createMockRtcSession();
const statistics = createMockStatistics();
tracker.cacheStartCall(new Date());
tracker.cacheReconnecting("sync");
tracker.cacheReconnecting("sync");
tracker.cacheReconnecting("livekit");
tracker.cacheReconnecting("membership");
tracker.track("test-call-id", 1, false, mockSession);
tracker.track("test-call-id", 1, false, statistics);
expect(PosthogAnalytics.instance.trackEvent).toHaveBeenCalledWith(
expect.objectContaining({
+7 -8
View File
@@ -7,7 +7,7 @@ Please see LICENSE in the repository root for full details.
import { type DisconnectReason } from "livekit-client";
import { logger } from "matrix-js-sdk/lib/logger";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { type Statistics } from "matrix-js-sdk/lib/matrixrtc";
import {
type IPosthogEvent,
@@ -80,7 +80,7 @@ export class CallEndedTracker {
callId: string,
callParticipantsNow: number,
sendInstantly: boolean,
rtcSession: MatrixRTCSession,
statistics: Statistics,
): void {
if (this.cache.startTime) {
PosthogAnalytics.instance.trackEvent<CallEnded>(
@@ -91,14 +91,13 @@ export class CallEndedTracker {
callParticipantsOnLeave: callParticipantsNow,
callDuration: (Date.now() - this.cache.startTime.getTime()) / 1000,
roomEventEncryptionKeysSent:
rtcSession.statistics.counters.roomEventEncryptionKeysSent,
statistics.counters.roomEventEncryptionKeysSent,
roomEventEncryptionKeysReceived:
rtcSession.statistics.counters.roomEventEncryptionKeysReceived,
statistics.counters.roomEventEncryptionKeysReceived,
roomEventEncryptionKeysReceivedAverageAge:
rtcSession.statistics.counters.roomEventEncryptionKeysReceived > 0
? rtcSession.statistics.totals
.roomEventEncryptionKeysReceivedTotalAge /
rtcSession.statistics.counters.roomEventEncryptionKeysReceived
statistics.counters.roomEventEncryptionKeysReceived > 0
? statistics.totals.roomEventEncryptionKeysReceivedTotalAge /
statistics.counters.roomEventEncryptionKeysReceived
: 0,
callReconnectingCount: this.cache.reconnectingCount,
callReconnectingCountSync: this.cache.reconnectingCountByReason.sync,
+35 -27
View File
@@ -10,13 +10,13 @@ import { expect, test } from "vitest";
import { TooltipProvider } from "@vector-im/compound-web";
import { userEvent } from "@testing-library/user-event";
import { type ReactNode } from "react";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { ReactionToggleButton } from "./ReactionToggleButton";
import { ElementCallReactionEventType } from "../reactions";
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
import { getBasicCallViewModelEnvironment } from "../utils/test-viewmodel";
import { alice, local, localRtcMember } from "../utils/test-fixtures";
import { type MockRTCSession } from "../utils/test";
import { ReactionsSenderProvider } from "../reactions/useReactionsSender";
import { initializeWidget } from "../widget";
initializeWidget();
@@ -24,18 +24,17 @@ initializeWidget();
const localIdent = `${localRtcMember.userId}:${localRtcMember.deviceId}`;
function TestComponent({
rtcSession,
matrixRoom,
client,
vm,
}: {
rtcSession: MockRTCSession;
matrixRoom: Room;
client: MatrixClient;
vm: CallViewModel;
}): ReactNode {
return (
<TooltipProvider>
<ReactionsSenderProvider
vm={vm}
rtcSession={rtcSession.asMockedSession()}
>
<ReactionsSenderProvider vm={vm} room={matrixRoom} client={client}>
<ReactionToggleButton
reactionData={{
reactions$: vm.reactions$,
@@ -50,9 +49,9 @@ function TestComponent({
test("Can open menu", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([alice]);
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
expect(container).toMatchSnapshot();
@@ -60,15 +59,15 @@ test("Can open menu", async () => {
test("Can raise hand", async () => {
const user = userEvent.setup();
const { vm, rtcSession, handRaisedSubject$ } =
const { vm, matrixRoom, client, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
"m.reaction",
{
"m.relates_to": {
@@ -94,10 +93,10 @@ test("Can raise hand", async () => {
test("Can lower hand", async () => {
const reactionEventId = "$my-reaction-event:example.org";
const user = userEvent.setup();
const { vm, rtcSession, handRaisedSubject$ } =
const { vm, matrixRoom, client, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
@@ -112,8 +111,8 @@ test("Can lower hand", async () => {
});
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Lower hand"));
expect(rtcSession.room.client.redactEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
expect(client.redactEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
reactionEventId,
);
act(() => {
@@ -125,14 +124,17 @@ test("Can lower hand", async () => {
test("Can react with emoji", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { getByLabelText, getByText } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByText("🐶"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
@@ -147,16 +149,19 @@ test("Can react with emoji", async () => {
test("Can fully expand emoji picker", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { getByLabelText, container, getByText } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
expect(container).toMatchSnapshot();
await user.click(getByText("🦗"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
expect(client.sendEvent).toHaveBeenCalledWith(
matrixRoom.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
@@ -171,9 +176,12 @@ test("Can fully expand emoji picker", async () => {
test("Can close reaction dialog", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { vm, matrixRoom, client } = getBasicCallViewModelEnvironment([
local,
alice,
]);
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
<TestComponent vm={vm} matrixRoom={matrixRoom} client={client} />,
);
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
+6 -1
View File
@@ -37,7 +37,12 @@ interface UseProfile extends ProfileLoadState {
saveProfile: ProfileSaveCallback;
}
export function useProfile(client: MatrixClient | undefined): UseProfile {
export type ProfileClientParts = Pick<
MatrixClient,
"getUserId" | "getUser" | "setDisplayName" | "setAvatarUrl" | "uploadContent"
>;
export function useProfile(client: ProfileClientParts | undefined): UseProfile {
const [{ success, loading, displayName, avatarUrl, error }, setState] =
useState<ProfileLoadState>(() => {
let user: User | undefined = undefined;
+173 -57
View File
@@ -23,30 +23,130 @@ import {
localRtcMember,
} from "../utils/test-fixtures";
import { getBasicRTCSession } from "../utils/test-viewmodel";
import { testScope, withTestScheduler } from "../utils/test";
import { mockRtcMembership, testScope, withTestScheduler } from "../utils/test";
import { ElementCallReactionEventType, ReactionSet } from ".";
afterEach(() => {
vitest.useRealTimers();
});
test("handles a hand raised reaction", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
function raiseLocalHand(
matrixRoom: ReturnType<typeof getBasicRTCSession>["matrixRoom"],
reactionEventId: string,
localTimestamp: Date,
): void {
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
origin_server_ts: localTimestamp.getTime(),
content: {
"m.relates_to": { event_id: localRtcMember.eventId, key: "🖐️" },
},
}),
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
);
}
test("lowers the hand of a member who leaves", () => {
const { members$, matrixRoom, client, rtcMemberships$ } = getBasicRTCSession([
local,
alice,
]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => raiseLocalHand(matrixRoom, reactionEventId, localTimestamp),
c: () => rtcMemberships$.next([aliceRtcMember]),
});
expectObservable(raisedHands$).toBe("abc", {
a: {},
b: {
[`${localRtcMember.userId}:${localRtcMember.deviceId}`]: {
reactionEventId,
membershipEventId: localRtcMember.eventId,
time: localTimestamp,
},
},
c: {},
});
});
});
});
test("lowers the hand of a member whose membership is sent again", () => {
const { members$, matrixRoom, client, rtcMemberships$ } = getBasicRTCSession([
local,
alice,
]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
const resent = mockRtcMembership(
localRtcMember.userId,
localRtcMember.deviceId,
);
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => raiseLocalHand(matrixRoom, reactionEventId, localTimestamp),
c: () => rtcMemberships$.next([resent, aliceRtcMember]),
});
expectObservable(raisedHands$).toBe("abc", {
a: {},
b: {
[`${localRtcMember.userId}:${localRtcMember.deviceId}`]: {
reactionEventId,
membershipEventId: localRtcMember.eventId,
time: localTimestamp,
},
},
c: {},
});
});
});
});
test("handles a hand raised reaction", () => {
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
members$,
matrixRoom,
client,
);
schedule("ab", {
a: () => {},
b: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -58,7 +158,7 @@ test("handles a hand raised reaction", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
@@ -80,22 +180,24 @@ test("handles a hand raised reaction", () => {
});
test("handles a redaction", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -107,23 +209,23 @@ test("handles a redaction", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Redaction,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.RoomRedaction,
redacts: reactionEventId,
}),
rtcSession.room,
matrixRoom,
undefined,
);
},
@@ -144,20 +246,22 @@ test("handles a redaction", () => {
});
test("handles waiting for event decryption", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule("abc", {
a: () => {},
b: () => {
const encryptedEvent = new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -171,20 +275,20 @@ test("handles waiting for event decryption", () => {
});
// Should ignore encrypted events that are still encrypting
encryptedEvent["decryptionPromise"] = Promise.resolve();
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
encryptedEvent,
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
rtcSession.room.client.emit(
client.emit(
MatrixEventEvent.Decrypted,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -214,22 +318,24 @@ test("handles waiting for event decryption", () => {
});
test("hands rejecting events without a proper membership", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const localTimestamp = new Date();
withTestScheduler(({ schedule, expectObservable }) => {
renderHook(() => {
const { raisedHands$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule("ab", {
a: () => {},
b: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: EventType.Reaction,
@@ -241,7 +347,7 @@ test("hands rejecting events without a proper membership", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
@@ -256,7 +362,7 @@ test("hands rejecting events without a proper membership", () => {
});
test("handles a reaction", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reaction = ReactionSet[1];
@@ -267,15 +373,17 @@ test("handles a reaction", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule(`abc`, {
a: () => {},
b: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -287,7 +395,7 @@ test("handles a reaction", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
@@ -316,7 +424,11 @@ test("handles a reaction", () => {
});
test("ignores bad reaction events", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const {
members$,
matrixRoom: room,
client,
} = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reaction = ReactionSet[1];
@@ -326,31 +438,33 @@ test("ignores bad reaction events", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
room,
client,
);
schedule("ab", {
a: () => {},
b: () => {
// Missing content
rtcSession.room.emit(
room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
content: {},
}),
rtcSession.room,
room,
undefined,
false,
{} as IRoomTimelineData,
);
// Wrong relates event
rtcSession.room.emit(
room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -362,16 +476,16 @@ test("ignores bad reaction events", () => {
},
},
}),
rtcSession.room,
room,
undefined,
false,
{} as IRoomTimelineData,
);
// Wrong rtc member event
rtcSession.room.emit(
room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: room.roomId,
event_id: reactionEventId,
sender: aliceRtcMember.userId,
type: ElementCallReactionEventType,
@@ -383,16 +497,16 @@ test("ignores bad reaction events", () => {
},
},
}),
rtcSession.room,
room,
undefined,
false,
{} as IRoomTimelineData,
);
// No emoji
rtcSession.room.emit(
room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -403,16 +517,16 @@ test("ignores bad reaction events", () => {
},
},
}),
rtcSession.room,
room,
undefined,
false,
{} as IRoomTimelineData,
);
// Invalid emoji
rtcSession.room.emit(
room.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: room.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -424,7 +538,7 @@ test("ignores bad reaction events", () => {
},
},
}),
rtcSession.room,
room,
undefined,
false,
{} as IRoomTimelineData,
@@ -439,7 +553,7 @@ test("ignores bad reaction events", () => {
});
test("that reactions cannot be spammed", () => {
const { rtcSession } = getBasicRTCSession([local, alice]);
const { members$, matrixRoom, client } = getBasicRTCSession([local, alice]);
const reactionEventId = "$my_event_id:example.org";
const reactionA = ReactionSet[1];
const reactionB = ReactionSet[2];
@@ -451,15 +565,17 @@ test("that reactions cannot be spammed", () => {
renderHook(() => {
const { reactions$ } = new ReactionsReader(
testScope(),
rtcSession.asMockedSession(),
members$,
matrixRoom,
client,
);
schedule("abcd", {
a: () => {},
b: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -471,17 +587,17 @@ test("that reactions cannot be spammed", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
);
},
c: () => {
rtcSession.room.emit(
matrixRoom.emit(
MatrixRoomEvent.Timeline,
new MatrixEvent({
room_id: rtcSession.room.roomId,
room_id: matrixRoom.roomId,
event_id: reactionEventId,
sender: localRtcMember.userId,
type: ElementCallReactionEventType,
@@ -493,7 +609,7 @@ test("that reactions cannot be spammed", () => {
},
},
}),
rtcSession.room,
matrixRoom,
undefined,
false,
{} as IRoomTimelineData,
+87 -66
View File
@@ -5,20 +5,28 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
type CallMembership,
MatrixRTCSessionEvent,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { logger } from "matrix-js-sdk/lib/logger";
import { type MatrixEvent, MatrixEventEvent } from "matrix-js-sdk";
import {
type MatrixClient,
type MatrixEvent,
MatrixEventEvent,
type Room,
} from "matrix-js-sdk";
import { type ReactionEventContent } from "matrix-js-sdk/lib/types";
import {
RelationType,
EventType,
RoomEvent as MatrixRoomEvent,
} from "matrix-js-sdk";
import { BehaviorSubject, delay } from "rxjs";
import {
BehaviorSubject,
combineLatest,
delay,
map,
of,
switchMap,
} from "rxjs";
import { type RTCMember } from "@element-hq/matrixrtc-sdk";
import {
ElementCallReactionEventType,
@@ -29,13 +37,20 @@ import {
type ReactionInfo,
} from ".";
import { type ObservableScope } from "../state/ObservableScope";
import { type Behavior } from "../state/Behavior";
export const REACTION_ACTIVE_TIME_MS = 3000;
/** A member's membership event, which its reactions relate to. */
interface MembershipEvent {
userId: string;
deviceId: string;
eventId: string;
}
/**
* Listens for reactions from a RTCSession and populates subjects
* for consumption by the CallViewModel.
* @param rtcSession
* Listens for reactions to the slot members' memberships and populates
* subjects for consumption by the CallViewModel.
*/
export class ReactionsReader {
private readonly raisedHandsSubject$ = new BehaviorSubject<
@@ -55,9 +70,33 @@ export class ReactionsReader {
*/
public readonly reactions$ = this.reactionsSubject$.asObservable();
private memberships: MembershipEvent[] = [];
public constructor(
private readonly scope: ObservableScope,
private readonly rtcSession: MatrixRTCSession,
members$: Behavior<RTCMember[]>,
private readonly room: Pick<
Room,
// To ignore events from other rooms
| "roomId"
// The raised hands already in the room, as reactions to memberships
| "relations"
// `on`/`off`, to listen for:
// - `RoomEvent.Timeline` (new reactions)
// - `RoomEvent.Redaction` (lowered hands)
// - `RoomEvent.LocalEchoUpdated` (the real id of our own reactions)
| "on"
| "off"
>,
private readonly client: Pick<
MatrixClient,
// Reactions that arrived encrypted
| "decryptEventIfNeeded"
// `on`/`off`, to listen for:
// - `MatrixEventEvent.Decrypted` (reactions once decrypted)
| "on"
| "off"
>,
) {
// Hide reactions after a given time.
this.reactionsSubject$
@@ -76,62 +115,44 @@ export class ReactionsReader {
// TODO: Convert this class to the functional reactive style and get rid of
// all this manual setup and teardown for event listeners
this.rtcSession.room.on(MatrixRoomEvent.Timeline, this.handleReactionEvent);
this.room.on(MatrixRoomEvent.Timeline, this.handleReactionEvent);
this.scope.onEnd(() =>
this.rtcSession.room.off(
MatrixRoomEvent.Timeline,
this.handleReactionEvent,
),
this.room.off(MatrixRoomEvent.Timeline, this.handleReactionEvent),
);
this.rtcSession.room.on(
MatrixRoomEvent.Redaction,
this.handleReactionEvent,
);
this.room.on(MatrixRoomEvent.Redaction, this.handleReactionEvent);
this.scope.onEnd(() =>
this.rtcSession.room.off(
MatrixRoomEvent.Redaction,
this.handleReactionEvent,
),
this.room.off(MatrixRoomEvent.Redaction, this.handleReactionEvent),
);
this.rtcSession.room.client.on(
MatrixEventEvent.Decrypted,
this.handleReactionEvent,
);
this.client.on(MatrixEventEvent.Decrypted, this.handleReactionEvent);
this.scope.onEnd(() =>
this.rtcSession.room.client.off(
MatrixEventEvent.Decrypted,
this.handleReactionEvent,
),
this.client.off(MatrixEventEvent.Decrypted, this.handleReactionEvent),
);
// We listen for a local echo to get the real event ID, as timeline events
// may still be sending.
this.rtcSession.room.on(
MatrixRoomEvent.LocalEchoUpdated,
this.handleReactionEvent,
);
this.room.on(MatrixRoomEvent.LocalEchoUpdated, this.handleReactionEvent);
this.scope.onEnd(() =>
this.rtcSession.room.off(
MatrixRoomEvent.LocalEchoUpdated,
this.handleReactionEvent,
),
this.room.off(MatrixRoomEvent.LocalEchoUpdated, this.handleReactionEvent),
);
this.rtcSession.on(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
);
this.scope.onEnd(() =>
this.rtcSession.off(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
),
);
// Run this once to ensure we have fetched the state from the call.
this.onMembershipsChanged([]);
members$
.pipe(
switchMap((members) =>
members.length === 0
? of([])
: combineLatest(
members.map(({ userId, deviceId, matrixEventId$ }) =>
matrixEventId$.pipe(
map((eventId) => ({ userId, deviceId, eventId })),
),
),
),
),
this.scope.bind(),
)
.subscribe(this.onMembershipsChanged);
}
/**
@@ -145,7 +166,7 @@ export class ReactionsReader {
membershipEventId: string,
expectedSender: string,
): MatrixEvent | undefined {
const relations = this.rtcSession.room.relations.getChildEventsForEvent(
const relations = this.room.relations.getChildEventsForEvent(
membershipEventId,
RelationType.Annotation,
EventType.Reaction,
@@ -162,22 +183,23 @@ export class ReactionsReader {
/**
* Will remove any hand raises by old members, and look for any
* existing hand raises by new members.
* @param oldMemberships Any members who have left the call.
* @param memberships The membership events of everyone in the call now.
*/
private onMembershipsChanged = (oldMemberships: CallMembership[]): void => {
private onMembershipsChanged = (memberships: MembershipEvent[]): void => {
this.memberships = memberships;
// Remove any raised hands for users no longer joined to the call.
const present = new Set(
memberships.map((m) => `${m.userId}:${m.deviceId}`),
);
for (const identifier of Object.keys(this.raisedHandsSubject$.value).filter(
(rhId) => oldMemberships.find((u) => u.userId == rhId),
(rhId) => !present.has(rhId),
)) {
this.removeRaisedHand(identifier);
}
// For each member in the call, check to see if a reaction has
// been raised and adjust.
for (const m of this.rtcSession.memberships) {
if (!m.userId || !m.eventId) {
continue;
}
for (const m of memberships) {
const identifier = `${m.userId}:${m.deviceId}`;
if (
this.raisedHandsSubject$.value[identifier] &&
@@ -235,9 +257,8 @@ export class ReactionsReader {
* @param event The incoming matrix event, which may or may not be decrypted.
*/
private handleReactionEvent = (event: MatrixEvent): void => {
const room = this.rtcSession.room;
// Decrypted events might come from a different room
if (event.getRoomId() !== room.roomId) return;
if (event.getRoomId() !== this.room.roomId) return;
// Skip any events that are still sending.
if (event.isSending()) return;
@@ -246,7 +267,7 @@ export class ReactionsReader {
// Skip any event without a sender or event ID.
if (!sender || !reactionEventId) return;
room.client
this.client
.decryptEventIfNeeded(event)
.catch((e) => logger.warn(`Failed to decrypt ${event.getId()}`, e));
if (event.isBeingDecrypted() || event.isDecryptionFailure()) return;
@@ -255,7 +276,7 @@ export class ReactionsReader {
const content: ECallReactionEventContent = event.getContent();
const membershipEventId = content?.["m.relates_to"]?.event_id;
const membershipEvent = this.rtcSession.memberships.find(
const membershipEvent = this.memberships.find(
(e) => e.eventId === membershipEventId && e.userId === sender,
);
// Check to see if this reaction was made to a membership event (and the
@@ -315,7 +336,7 @@ export class ReactionsReader {
// Check to see if this reaction was made to a membership event (and the
// sender of the reaction matches the membership)
const membershipEvent = this.rtcSession.memberships.find(
const membershipEvent = this.memberships.find(
(e) => e.eventId === membershipEventId && e.userId === sender,
);
if (!membershipEvent) {
+40 -38
View File
@@ -5,7 +5,12 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { EventType, RelationType } from "matrix-js-sdk";
import {
EventType,
type MatrixClient,
RelationType,
type Room,
} from "matrix-js-sdk";
import {
createContext,
use,
@@ -14,14 +19,15 @@ import {
useMemo,
type JSX,
} from "react";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { logger } from "matrix-js-sdk/lib/logger";
import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships";
import { useClientState } from "../ClientContext";
import { ElementCallReactionEventType, type ReactionOption } from ".";
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
import { useBehavior } from "../useBehavior";
import { constant } from "../state/Behavior";
const noMembershipEvent$ = constant(undefined);
interface ReactionsSenderContextType {
supportsReactions: boolean;
@@ -46,28 +52,34 @@ export const useReactionsSender = (): ReactionsSenderContextType => {
*/
export const ReactionsSenderProvider = ({
children,
rtcSession,
room,
client,
vm,
}: {
children: ReactNode;
rtcSession: MatrixRTCSession;
/** The room the reactions are sent in. */
room: Pick<Room, "roomId">;
client: Pick<
MatrixClient,
// Our own identity, which reactions and raised hands are keyed by
| "getUserId"
| "getDeviceId"
// Sending reactions and raising a hand
| "sendEvent"
// Lowering a hand
| "redactEvent"
>;
/** Our own membership, which the reactions relate to, and the current reactions. */
vm: CallViewModel;
}): JSX.Element => {
const memberships = useMatrixRTCSessionMemberships(rtcSession);
const clientState = useClientState();
const supportsReactions =
clientState?.state === "valid" && clientState.supportedFeatures.reactions;
const room = rtcSession.room;
const myUserId = room.client.getUserId();
const myDeviceId = room.client.getDeviceId();
const myMembershipIdentifier = `${myUserId}:${myDeviceId}`;
const myMembershipIdentifier = `${client.getUserId()}:${client.getDeviceId()}`;
const myMembershipEvent = useMemo(
() =>
memberships.find(
(m) => m.userId === myUserId && m.deviceId === myDeviceId,
)?.eventId,
[memberships, myUserId, myDeviceId],
const localMember = useBehavior(vm.localMember$);
const myMembershipEvent = useBehavior(
localMember?.matrixEventId$ ?? noMembershipEvent$,
);
const reactions = useBehavior(vm.reactions$);
@@ -99,8 +111,8 @@ export const ReactionsSenderProvider = ({
if (!myMembershipEvent) {
throw new Error("Cannot find own membership event");
}
const reaction = await room.client.sendEvent(
rtcSession.room.roomId,
const reaction = await client.sendEvent(
room.roomId,
EventType.Reaction,
{
"m.relates_to": {
@@ -116,20 +128,14 @@ export const ReactionsSenderProvider = ({
}
} else {
try {
await room.client.redactEvent(rtcSession.room.roomId, myReactionId);
await client.redactEvent(room.roomId, myReactionId);
logger.debug("Redacted raise hand event");
} catch (ex) {
logger.error("Failed to redact reaction event", myReactionId, ex);
throw ex;
}
}
}, [
myMembershipEvent,
myMembershipIdentifier,
myRaisedHand,
rtcSession,
room,
]);
}, [myMembershipEvent, myMembershipIdentifier, myRaisedHand, client, room]);
const sendReaction = useCallback(
async (reaction: ReactionOption) => {
@@ -140,20 +146,16 @@ export const ReactionsSenderProvider = ({
if (!myMembershipEvent) {
throw new Error("Cannot find own membership event");
}
await room.client.sendEvent(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
rel_type: RelationType.Reference,
event_id: myMembershipEvent,
},
emoji: reaction.emoji,
name: reaction.name,
await client.sendEvent(room.roomId, ElementCallReactionEventType, {
"m.relates_to": {
rel_type: RelationType.Reference,
event_id: myMembershipEvent,
},
);
emoji: reaction.emoji,
name: reaction.name,
});
},
[myMembershipEvent, myReaction, room, myMembershipIdentifier, rtcSession],
[myMembershipEvent, myReaction, client, room, myMembershipIdentifier],
);
return (
+2 -3
View File
@@ -6,13 +6,12 @@ Please see LICENSE in the repository root for full details.
*/
import { type FC, type FormEventHandler, useCallback, useState } from "react";
import { type MatrixClient } from "matrix-js-sdk";
import { Trans, useTranslation } from "react-i18next";
import { Button, Heading, Text } from "@vector-im/compound-web";
import styles from "./CallEndedView.module.css";
import feedbackStyle from "../input/FeedbackInput.module.css";
import { useProfile } from "../profile/useProfile";
import { type ProfileClientParts, useProfile } from "../profile/useProfile";
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
import { PosthogAnalytics } from "../analytics/PosthogAnalytics";
import { FieldRow, InputField } from "../input/Input";
@@ -22,7 +21,7 @@ import { LeaveToHomeLink } from "../button/LeaveToHomeLink";
import { useLeaveToHome } from "../LeaveToHomeContext";
interface Props {
client: MatrixClient;
client: ProfileClientParts;
isPasswordlessUser: boolean;
hideHeader: boolean;
confineToRoom: boolean;
+46 -80
View File
@@ -19,16 +19,8 @@ import {
vitest,
} from "vitest";
import { render, waitFor, screen, act } from "@testing-library/react";
import {
type MatrixClient,
JoinRule,
type RoomState,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
import {
MatrixRTCSessionEvent,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { type MatrixClient, JoinRule } from "matrix-js-sdk";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { BrowserRouter } from "react-router-dom";
import userEvent, {
PointerEventsCheckLevel,
@@ -53,7 +45,7 @@ import {
testScope,
} from "../utils/test";
import { CallView } from "./CallView";
import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary";
import { type CallMatrixBackend } from "../CallMatrixBackend";
import {
type HostBridge,
HostBridgeProvider,
@@ -61,7 +53,10 @@ import {
nullHostBridge,
} from "../HostBridge";
import { type JoinCallData } from "../widget";
import { MatrixRTCTransportMissingError } from "../utils/errors";
import {
ConnectionLostError,
MatrixRTCTransportMissingError,
} from "../utils/errors";
import {
BackgroundEffectsProvider,
createAppBackgroundEffects,
@@ -146,7 +141,6 @@ function createCallView(
hostBridge: HostBridge,
joined = true,
options: {
withErrorBoundary?: boolean;
/** Wait for the host to say when to join, rather than joining at once. */
preload?: boolean;
} = {},
@@ -155,6 +149,7 @@ function createCallView(
getByText: ReturnType<typeof render>["getByText"];
} {
const client = {
...mockEmitter(),
getUser: () => null,
getUserId: () => localRtcMember.userId,
getDeviceId: () => localRtcMember.deviceId,
@@ -170,28 +165,14 @@ function createCallView(
client,
roomId,
getMember: (userId) => roomMembers.get(userId) ?? null,
getMembersWithMembership: () => [carol],
getMxcAvatarUrl: () => null,
getCanonicalAlias: () => null,
currentState: {
...mockEmitter(),
getJoinRule: () => JoinRule.Invite,
} as Partial<RoomState> as RoomState,
getJoinRule: () => JoinRule.Invite,
});
const rtcSession = new MockRTCSession(room, []).withMemberships(
constant([localRtcMember]),
);
rtcSession.joined = joined;
const callView = (
<CallView
client={client}
isPasswordlessUser={false}
confineToRoom={false}
preload={options.preload ?? false}
// Straight into the (mocked) call, past the lobby
skipLobby
rtcSession={rtcSession.asMockedSession()}
/>
);
const { getByText } = render(
<BrowserRouter>
<HostBridgeProvider value={hostBridge}>
@@ -200,13 +181,22 @@ function createCallView(
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
{options.withErrorBoundary ? (
<GroupCallErrorBoundary recoveryActionHandler={vi.fn()}>
{callView}
</GroupCallErrorBoundary>
) : (
callView
)}
<CallView
backend={
{
...client,
...room,
clientEmitter: client,
roomEmitter: room,
} as unknown as CallMatrixBackend
}
isPasswordlessUser={false}
confineToRoom={false}
preload={options.preload ?? false}
// Straight into the (mocked) call, past the lobby
skipLobby
rtcSession={rtcSession.asMockedSession()}
/>
</BackgroundEffectsProvider>
</MediaDevicesContext>
</TooltipProvider>
@@ -371,52 +361,28 @@ test.skip("CallView shows errors that occur during joining", async () => {
screen.getByText("Call is not supported");
});
test("translates wrapped UnsupportedStickyEventsEndpointError to the StickyEventsRequiredError screen", async () => {
// Mirror the shape the SDK emits: the MembershipManager scheduler wraps
// the original UnsupportedStickyEventsEndpointError in a generic Error
// but preserves the original on `.cause`.
const stickyError = new UnsupportedStickyEventsEndpointError(
"Server does not support the sticky events",
"sendStickyEvent",
test("user can reconnect after losing the connection", async () => {
// What the call throws once the view model reports the connection lost
(ActiveCall as MockedFunction<typeof ActiveCall>).mockImplementation(
({ onLeft }) => {
const [error, setError] = useState<Error | null>(null);
if (error !== null) throw error;
return (
<div>
<button onClick={() => onLeft("user")}>Leave</button>
<button onClick={() => setError(new ConnectionLostError())}>
LoseConnection
</button>
</div>
);
},
);
const wrappedError = new Error(
"The MembershipManager shut down because of the end condition: " +
String(stickyError),
{ cause: stickyError },
);
const { rtcSession } = createCallView(nullHostBridge, true, {
withErrorBoundary: true,
});
await act(() =>
rtcSession.emit(MatrixRTCSessionEvent.MembershipManagerError, wrappedError),
);
await screen.findByText("Homeserver does not support Matrix 2.0 calls");
});
test("falls back to ConnectionLostError for unrecognised membership manager errors", async () => {
const { rtcSession } = createCallView(nullHostBridge, true, {
withErrorBoundary: true,
});
await act(() =>
rtcSession.emit(
MatrixRTCSessionEvent.MembershipManagerError,
new Error("something else broke"),
),
);
await screen.findByText("Connection lost");
});
test("user can reconnect after a membership manager error", async () => {
const user = userEvent.setup();
const { rtcSession } = createCallView(nullHostBridge, true);
await act(() =>
rtcSession.emit(MatrixRTCSessionEvent.MembershipManagerError, undefined),
);
createCallView(nullHostBridge, true);
await user.click(screen.getByRole("button", { name: "LoseConnection" }));
await screen.findByText("Connection lost");
// Never joined, so there was no call to leave
expect(playSound).not.toHaveBeenCalled();
// XXX: Wrapping the following click in act() shouldn't be necessary (the
// async state update should be processed automatically by the waitFor call),
// and yet here we are.
+184 -101
View File
@@ -13,21 +13,24 @@ import {
useMemo,
useState,
} from "react";
import {
type MatrixClient,
JoinRule,
type Room,
UnsupportedStickyEventsEndpointError,
} from "matrix-js-sdk";
import { type MatrixClient, JoinRule, type Room } from "matrix-js-sdk";
import {
Room as LivekitRoom,
isE2EESupported as isE2EESupportedBrowser,
} from "livekit-client";
import { logger } from "matrix-js-sdk/lib/logger";
import {
MatrixRTCSessionEvent,
type MatrixRTCSession,
type Statistics,
} from "matrix-js-sdk/lib/matrixrtc";
import { type Observable } from "rxjs";
import {
createRTCSlot,
type EncryptionSystem,
MembershipManagerError as RTCMembershipManagerError,
type RTCSlot,
withEmitter,
} from "@element-hq/matrixrtc-sdk";
import { type JoinCallData } from "../widget";
import { LobbyView } from "./LobbyView";
@@ -39,7 +42,6 @@ import { findDeviceByName } from "../utils/media";
import { ActiveCall } from "./InCallView";
import { type MuteStates } from "../state/MuteStates";
import { useMediaDevices } from "../MediaDevicesContext";
import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships";
import {
saveKeyForRoom,
useRoomEncryptionSystem,
@@ -57,14 +59,11 @@ import {
} from "./CallEventAudioRenderer";
import { useLatest } from "../useLatest";
import {
ConnectionLostError,
E2EENotSupportedError,
ElementCallError,
StickyEventsRequiredError,
UnknownCallError,
} from "../utils/errors.ts";
import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary.tsx";
import { useTypedEventEmitter } from "../useEvents";
import { muteAllAudio$ } from "../state/MuteAllAudioModel.ts";
import { useAppBarTitle } from "../AppBar.tsx";
import { useBehavior } from "../useBehavior.ts";
@@ -73,6 +72,9 @@ import { useHostBridge } from "../HostBridge.ts";
import { useMuteStates } from "../state/useMuteStates.ts";
import { useLeaveToHome } from "../LeaveToHomeContext.ts";
import { useLocalMedia } from "../useLocalMedia.ts";
import { ObservableScope } from "../state/ObservableScope.ts";
import { type CallNotificationWrapper } from "../state/CallViewModel/CallNotificationLifecycle.ts";
import { type CallMatrixBackend } from "../CallMatrixBackend.ts";
/**
* If there already are this many participants in the call, we automatically mute
@@ -80,17 +82,19 @@ import { useLocalMedia } from "../useLocalMedia.ts";
*/
export const MUTE_PARTICIPANT_COUNT = 8;
declare global {
interface Window {
rtcSession?: MatrixRTCSession;
}
}
interface Props {
/** The client to place the call with. */
client: MatrixClient;
/** The call to join. */
rtcSession: MatrixRTCSession;
/** The client and room to place the call with and in. */
backend: CallMatrixBackend;
/**
* The call to join, which the RTC slot is built on.
* Passing undefined -> the SDK will create a inner RTCSession (or in the future a rust equivalent)
* @deprecated: It should not get its rtcSession from here.
* The goal is that the RTC session is fully encapsulated inside the SDK (`createRTCSlot`)
* We keep this as a fallback for the SPA. In that mode we currently expect to manage Our
* own rtcSession with `useLoadGroupCall`. This is an open TODO. Until then we have this
* alternative.
*/
rtcSession?: MatrixRTCSession;
/**
* Whether the user is signed in as a guest, and so should be offered the
* chance to create an account when the call ends.
@@ -121,18 +125,58 @@ interface Props {
* whatever is hosting it — the standalone app's own shell, or an application
* embedding Element Call as a component. Both render this.
*/
export const CallView: FC<Props> = (props): ReactNode => {
export const CallView: FC<Props> = ({
backend,
rtcSession: externalRtcSession,
...props
}): ReactNode => {
// STOP GAP: what is below is still written against a client and a room.
// Both are refactored along with the backend.
const client = useMemo(
() => withEmitter(backend, backend.clientEmitter),
[backend],
);
const room = useMemo(
() => withEmitter(backend, backend.roomEmitter),
[backend],
);
// Whether the user is in the call is the call's own business, not its host's.
// Held here rather than below so that it survives the mute state being
// rebuilt.
const [joined, setJoined] = useState(false);
const muteStates = useMuteStates();
const e2eeSystem = useRoomEncryptionSystem(backend.roomId);
if (muteStates === null) return null;
// One slot for every join of this call: leaving and joining again, to
// reconnect or from the lobby, happens on the same one
const [slot, setSlot] = useState<ReturnType<typeof createRTCSlot> | null>(
null,
);
useEffect(() => {
const scope = new ObservableScope();
setSlot(
createRTCSlot(
scope,
backend,
{ encryptionSystem: e2eeSystem },
externalRtcSession,
),
);
return (): void => scope.end();
}, [backend, e2eeSystem, externalRtcSession]);
if (muteStates === null || slot === null) return null;
return (
<LoadedCallView
{...props}
client={client}
room={room}
sentCallNotification$={slot.sentCallNotification$}
statistics={slot.statistics}
slot={slot}
e2eeSystem={e2eeSystem}
joined={joined}
setJoined={setJoined}
muteStates={muteStates}
@@ -140,20 +184,100 @@ export const CallView: FC<Props> = (props): ReactNode => {
);
};
interface LoadedProps extends Props {
// Here we do not pass the rtcSession anymore. This is a huge step!!
// We only rely on our sdk abstraction (+ client and +room but only a
// smaller list of callbacks.)
interface LoadedProps extends Omit<Props, "rtcSession" | "backend"> {
// Narrower than `Props.client`: the `slot` already has what only it and its
// session need, `getRTCTransports`, `delegateDelayedLeave`, `baseUrl`,
// `getSyncState`, `ClientEvent.Sync`, and the session's sending
client: Pick<
MatrixClient,
| "getUserId"
| "getDeviceId"
| "getDomain"
| "getOpenIdToken"
| "getLivekitToken"
| "sendEvent"
| "redactEvent"
| "decryptEventIfNeeded"
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
| "doesServerSupportUnstableFeature"
| "getCrypto"
// `on`/`off`, to listen for `MatrixEventEvent.Decrypted` (reactions once
// decrypted)
| "on"
| "off"
>;
// Narrower than `Props.room`: the `slot` already follows
// `RoomStateEvent.Events` for its status, `RoomStateEvent.Members` for its
// own members, and the timeline its session reads the memberships from
room: Pick<
Room,
| "roomId"
| "name"
| "getMxcAvatarUrl"
| "getJoinRule"
| "getMembersWithMembership"
| "_unstable_getStickyEvents"
| "getLiveTimeline"
| "relations"
// `on`/`off`, to listen for:
// - `RoomEvent.Name` (room name)
// - `RoomStateEvent.Update` and `RoomEvent.CurrentStateUpdated` (avatar
// and join rule hooks)
// - `RoomStateEvent.Members` (call view model members)
// - `RoomStateEvent.Events` (call view model functional members)
// - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications)
// - `RoomEvent.Timeline`, `RoomEvent.Redaction` and
// `RoomEvent.LocalEchoUpdated` (reactions and raised hands)
| "on"
| "off"
>;
/**
* The call notifications the session sends with our join, so the call can
* show it ringing: the slot's `sentCallNotification$`.
*/
sentCallNotification$: Observable<CallNotificationWrapper>;
/** The session's media key statistics, reported when the call ends. */
statistics: Statistics;
slot: RTCSlot;
e2eeSystem: EncryptionSystem;
joined: boolean;
setJoined: (value: boolean) => void;
muteStates: MuteStates;
}
/** {@link CallView}, once it has the mute state everything below needs. */
/**
* Whether our membership is live in the slot: seen in the room, and not taken
* down by the membership manager stopping. Leaving the call only makes sense
* then; an error before the join, or one that ended the membership, leaves
* nothing to leave.
*/
function isInCall(slot: RTCSlot): boolean {
const participation = slot.participation$.value;
if (participation?.localMember$.value == null) return false;
const state = participation.state$.value;
return !(
state.kind === "failed" && state.error instanceof RTCMembershipManagerError
);
}
/** {@link CallView}, once it has the slot and mute state everything below needs. */
const LoadedCallView: FC<LoadedProps> = ({
client,
room,
isPasswordlessUser,
confineToRoom,
preload,
skipLobby,
rtcSession,
slot,
sentCallNotification$,
statistics,
e2eeSystem,
joined,
setJoined,
muteStates,
@@ -162,7 +286,7 @@ const LoadedCallView: FC<LoadedProps> = ({
const [externalError, setExternalError] = useState<ElementCallError | null>(
null,
);
const memberships = useMatrixRTCSessionMemberships(rtcSession);
const members = useBehavior(slot.members$);
const rootElement = useRootElement();
const hostBridge = useHostBridge();
// A host that can close us is a host that decides when we stop existing, so
@@ -181,7 +305,7 @@ const LoadedCallView: FC<LoadedProps> = ({
);
// This should use `useEffectEvent` (only available in experimental versions)
useEffect(() => {
if (memberships.length >= MUTE_PARTICIPANT_COUNT)
if (members.length >= MUTE_PARTICIPANT_COUNT)
muteStates.audio.setEnabled$.value?.(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
@@ -203,48 +327,10 @@ const LoadedCallView: FC<LoadedProps> = ({
};
}, [rootElement]);
useEffect(() => {
window.rtcSession = rtcSession;
return (): void => {
delete window.rtcSession;
};
}, [rtcSession]);
// TODO move this into the callViewModel LocalMembership.ts
// We might actually not need this at all. Since we get into fatalError on those errors already?
useTypedEventEmitter(
rtcSession,
MatrixRTCSessionEvent.MembershipManagerError,
(error) => {
// When matrix_rtc_mode=matrix_2_0 is in effect but the homeserver does
// not advertise MSC4354 (sticky events), the SDK throws an
// `UnsupportedStickyEventsEndpointError`. The MembershipManager
// scheduler wraps it and exposes the original via `.cause`.
if (
error instanceof Error &&
error.cause instanceof UnsupportedStickyEventsEndpointError
) {
setExternalError(new StickyEventsRequiredError());
} else {
setExternalError(new ConnectionLostError());
}
},
);
useEffect(() => {
// Sanity check the room object
if (client.getRoom(rtcSession.room.roomId) !== rtcSession.room)
logger.warn(
`We've ended up with multiple rooms for the same ID (${rtcSession.room.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`,
);
}, [client, rtcSession.room]);
const room = rtcSession.room as Room;
const { displayName, avatarUrl } = useProfile(client);
const roomName = useRoomName(room);
const roomAvatar = useRoomAvatar(room);
const { returnToLobby, password: passwordFromUrl, header } = useUrlParams();
const e2eeSystem = useRoomEncryptionSystem(room.roomId);
// Save the password once we start the groupCallView
useEffect(() => {
@@ -260,7 +346,6 @@ const LoadedCallView: FC<LoadedProps> = ({
avatarUrl: avatarUrl!,
roomId: room.roomId,
roomName,
roomAlias: room.getCanonicalAlias(),
roomAvatar,
e2eeSystem,
};
@@ -268,8 +353,8 @@ const LoadedCallView: FC<LoadedProps> = ({
// Count each member only once, regardless of how many devices they use
const participantCount = useMemo(
() => new Set<string>(memberships.map((m) => m.userId!)).size,
[memberships],
() => new Set<string>(members.map((m) => m.userId)).size,
[members],
);
const mediaDevices = useMediaDevices();
@@ -277,27 +362,24 @@ const LoadedCallView: FC<LoadedProps> = ({
// Opened by the lobby, published by the call: one capture for both
const media = useLocalMedia();
const enterRTCSessionOrError = useCallback(
async (rtcSession: MatrixRTCSession): Promise<void> => {
try {
setJoined(true);
// TODO-MULTI-SFU what to do with error handling now that we don't use this function?
// @BillCarsonFr
} catch (e) {
if (e instanceof ElementCallError) {
setExternalError(e);
} else {
logger.error(`Unknown Error while entering RTC session`, e);
const error = new UnknownCallError(
e instanceof Error ? e : new Error("Unknown error", { cause: e }),
);
setExternalError(error);
}
const enterRTCSessionOrError = useCallback(async (): Promise<void> => {
try {
setJoined(true);
// TODO-MULTI-SFU what to do with error handling now that we don't use this function?
// @BillCarsonFr
} catch (e) {
if (e instanceof ElementCallError) {
setExternalError(e);
} else {
logger.error(`Unknown Error while entering RTC session`, e);
const error = new UnknownCallError(
e instanceof Error ? e : new Error("Unknown error", { cause: e }),
);
setExternalError(error);
}
return Promise.resolve();
},
[setJoined],
);
}
return Promise.resolve();
}, [setJoined]);
useEffect(() => {
const defaultDeviceSetup = async ({
@@ -360,7 +442,6 @@ const LoadedCallView: FC<LoadedProps> = ({
}
}, [
hostBridge,
rtcSession,
preload,
skipLobby,
mediaDevices,
@@ -407,9 +488,9 @@ const LoadedCallView: FC<LoadedProps> = ({
const sendInstantly = hostControlsLifetime;
PosthogAnalytics.instance.eventCallEnded.track(
room.roomId,
rtcSession.memberships.length,
slot.members$.value.length,
sendInstantly,
rtcSession,
statistics,
);
// Unfortunately the PostHog library provides no way to await the
// tracking of an event, but we don't really want it to hold up our
@@ -455,7 +536,8 @@ const LoadedCallView: FC<LoadedProps> = ({
hostBridge,
hostControlsLifetime,
room.roomId,
rtcSession,
slot,
statistics,
isPasswordlessUser,
confineToRoom,
returnToLobby,
@@ -469,7 +551,7 @@ const LoadedCallView: FC<LoadedProps> = ({
hostBridge.setAlwaysOnScreen(true).catch((e) => {
logger.error("Error calling setAlwaysOnScreen(true)", e);
});
}, [hostBridge, joined, rtcSession]);
}, [hostBridge, joined]);
const joinRule = useJoinRule(room);
@@ -530,7 +612,8 @@ const LoadedCallView: FC<LoadedProps> = ({
<ActiveCall
client={client}
matrixInfo={matrixInfo}
rtcSession={rtcSession as MatrixRTCSession}
slot={slot}
sentCallNotification$={sentCallNotification$}
matrixRoom={room}
onLeft={onLeft}
media={media}
@@ -553,7 +636,7 @@ const LoadedCallView: FC<LoadedProps> = ({
if (isPasswordlessUser || PosthogAnalytics.instance.isEnabled()) {
body = (
<CallEndedView
endedCallId={rtcSession.room.roomId}
endedCallId={room.roomId}
client={client}
isPasswordlessUser={isPasswordlessUser}
hideHeader={header === HeaderStyle.None}
@@ -570,7 +653,7 @@ const LoadedCallView: FC<LoadedProps> = ({
// Left, and the host decides what happens next:
body = returnToLobby ? lobbyView : null;
} else if (preload || skipLobby) {
// The RTC session is not joined to yet (`isJoined`), but enterRTCSessionOrError should have been called.
// Not in the call yet: waiting for the host to say when, or about to join
body = null;
} else {
body = lobbyView;
@@ -582,16 +665,16 @@ const LoadedCallView: FC<LoadedProps> = ({
setExternalError(null);
if (action == "reconnect") {
setLeft(false);
await enterRTCSessionOrError(rtcSession).catch((e) => {
await enterRTCSessionOrError().catch((e) => {
logger.error("Error re-entering RTC session", e);
});
}
}}
onError={(_error) => {
if (rtcSession.isJoined()) onLeft("error");
if (isInCall(slot)) onLeft("error");
// If there is an error we need to be dismissible again. This is done in
// `onLeft` as well; we need it here explicitly in case
// rtcSession.isJoined is false.
// `onLeft` as well; we need it here explicitly in case we were not in
// the call.
void hostBridge.setAlwaysOnScreen(false);
}}
>
+67 -66
View File
@@ -15,7 +15,7 @@ import {
vi,
} from "vitest";
import { act, render, type RenderResult } from "@testing-library/react";
import { BehaviorSubject } from "rxjs";
import { BehaviorSubject, NEVER } from "rxjs";
import { BrowserRouter } from "react-router-dom";
import { TooltipProvider } from "@vector-im/compound-web";
import userEvent from "@testing-library/user-event";
@@ -25,7 +25,6 @@ import {
mockMediaDevices,
mockMuteStates,
mockRtcMembership,
type MockRTCSession,
mockRTCSlot,
mockRTCMember,
testScope,
@@ -44,7 +43,7 @@ import { ReactionsSenderProvider } from "../reactions/useReactionsSender";
import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement";
import { MemberAudioRenderer } from "../tracks/MemberAudioRenderer";
import type * as MatrixRTCSdk from "@element-hq/matrixrtc-sdk";
import { constant, type ObservableScope } from "@element-hq/matrixrtc-sdk";
import { constant, type RTCSlot } from "@element-hq/matrixrtc-sdk";
import { MediaDevicesContext } from "../MediaDevicesContext";
import { type MediaDevices as ECMediaDevices } from "../state/MediaDevices";
import { AppBar } from "../AppBar";
@@ -73,12 +72,6 @@ vi.mock("../tile/GridTile");
vi.mock("../tile/SpotlightTile");
vi.mock("../e2ee/sharedKeyManagement");
vi.mock("../tracks/MemberAudioRenderer");
// ActiveCall builds the slot itself; the view is what these tests are about
const createRTCSlotMock = vi.hoisted(() => vi.fn());
vi.mock("@element-hq/matrixrtc-sdk", async (importOriginal) => ({
...(await importOriginal<typeof MatrixRTCSdk>()),
createRTCSlot: createRTCSlotMock,
}));
vi.mock("react-use-measure", () => ({
default: (): [() => void, object] => [(): void => {}, {}],
}));
@@ -91,32 +84,33 @@ const matrixInfo = {
avatarUrl: "",
roomId: "",
roomName: "",
roomAlias: null,
roomAvatar: null,
e2eeSystem: { kind: E2eeType.NONE },
} satisfies MatrixInfo;
let useRoomEncryptionSystemMock: MockedFunction<typeof useRoomEncryptionSystem>;
// The view is what these tests are about, so the slot is a stand-in
function mockSlot(): RTCSlot {
return mockRTCSlot(testScope(), {
localMember$: constant(
mockRTCMember(true, {
membership: localRtcMember,
tracks$: constant([]),
}),
),
remoteMembers$: constant([
mockRTCMember(false, {
membership: aliceRtcMember,
tracks$: constant([]),
}),
]),
});
}
beforeEach(() => {
vi.clearAllMocks();
createRTCSlotMock.mockImplementation((scope: ObservableScope) =>
mockRTCSlot(scope, {
localMember$: constant(
mockRTCMember(true, {
membership: localRtcMember,
tracks$: constant([]),
}),
),
remoteMembers$: constant([
mockRTCMember(false, {
membership: aliceRtcMember,
tracks$: constant([]),
}),
]),
}),
);
// MemberAudioRenderer is tested separately.
(
MemberAudioRenderer as MockedFunction<typeof MemberAudioRenderer>
@@ -132,12 +126,11 @@ interface CreateInCallViewArgs {
withAppBar?: boolean;
}
function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
rtcSession: MockRTCSession;
vm: CallViewModel;
} {
const mediaDevices = args.mediaDevices ?? mockMediaDevices({});
const muteState = mockMuteStates();
const { vm, footerVm, developerSettingsVm, rtcSession } =
const { vm, footerVm, developerSettingsVm, matrixRoom, client } =
getBasicCallViewModelEnvironment(
[local, alice],
undefined,
@@ -145,20 +138,15 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
args.callViewModelOptions,
);
rtcSession.joined = true;
const room = rtcSession.room;
const client = room.client;
const inCallView = (
<InCallView
client={client}
rtcSession={rtcSession.asMockedSession()}
muteStates={muteState}
vm={vm}
footerVm={footerVm}
developerSettingsVm={developerSettingsVm}
matrixInfo={matrixInfo}
matrixRoom={room}
matrixRoom={matrixRoom}
onShareClick={null}
/>
);
@@ -168,10 +156,7 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
const renderResult = render(
<BrowserRouter>
<MediaDevicesContext value={mediaDevices}>
<ReactionsSenderProvider
vm={vm}
rtcSession={rtcSession.asMockedSession()}
>
<ReactionsSenderProvider vm={vm} room={matrixRoom} client={client}>
<TooltipProvider>{content}</TooltipProvider>
</ReactionsSenderProvider>
</MediaDevicesContext>
@@ -179,7 +164,6 @@ function createInCallView(args: CreateInCallViewArgs = {}): RenderResult & {
);
return {
...renderResult,
rtcSession,
vm,
};
}
@@ -243,37 +227,53 @@ function fakeLocalMedia(): MatrixRTCSdk.LocalMedia {
};
}
function renderActiveCall(slot: RTCSlot): RenderResult {
const { matrixRoom } = getBasicRTCSession([local, alice]);
return render(
<BrowserRouter>
<MediaDevicesContext value={mockMediaDevices({})}>
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
slot={slot}
sentCallNotification$={NEVER}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
onShareClick={null}
e2eeSystem={{ kind: E2eeType.NONE }}
media={fakeLocalMedia()}
onLeft={(): void => {}}
/>
</TooltipProvider>
</BackgroundEffectsProvider>
</MediaDevicesContext>
</BrowserRouter>,
);
}
describe("ActiveCall", () => {
it("creates the view models and renders the call", async () => {
const mediaDevices = mockMediaDevices({});
const { rtcSession, matrixRoom } = getBasicRTCSession([local, alice]);
const { findByTestId } = render(
<BrowserRouter>
<MediaDevicesContext value={mediaDevices}>
<BackgroundEffectsProvider
effects={createAppBackgroundEffects(testScope())}
>
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
rtcSession={rtcSession.asMockedSession()}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
onShareClick={null}
e2eeSystem={{ kind: E2eeType.NONE }}
media={fakeLocalMedia()}
onLeft={(): void => {}}
/>
</TooltipProvider>
</BackgroundEffectsProvider>
</MediaDevicesContext>
</BrowserRouter>,
);
const { findByTestId } = renderActiveCall(mockSlot());
// Rendering at all proves ActiveCall created all of its view models
expect(await findByTestId("incall_leave")).toBeVisible();
});
it("leaves the slot once gone, so that the next call can join it", async () => {
const slot = mockSlot();
const { findByTestId, unmount } = renderActiveCall(slot);
await findByTestId("incall_leave");
expect(slot.join).toHaveBeenCalled();
const participation = slot.participation$.value!;
expect(participation.leave).not.toHaveBeenCalled();
unmount();
expect(participation.leave).toHaveBeenCalled();
});
it("lays the call out for the size of its root element", async () => {
// jsdom has no layout and no ResizeObserver: the root reports whatever
// size we say, and the observer notifies whenever we tell it to
@@ -296,7 +296,7 @@ describe("ActiveCall", () => {
try {
const mediaDevices = mockMediaDevices({});
const { rtcSession, matrixRoom } = getBasicRTCSession([local, alice]);
const { matrixRoom } = getBasicRTCSession([local, alice]);
const { findByTestId, container } = render(
<BrowserRouter>
<RootElementProvider value={root}>
@@ -307,7 +307,8 @@ describe("ActiveCall", () => {
<TooltipProvider>
<ActiveCall
client={matrixRoom.client}
rtcSession={rtcSession.asMockedSession()}
slot={mockSlot()}
sentCallNotification$={NEVER}
matrixRoom={matrixRoom}
muteStates={mockMuteStates()}
matrixInfo={matrixInfo}
+93 -39
View File
@@ -18,7 +18,7 @@ import {
type ReactNode,
} from "react";
import useMeasure from "react-use-measure";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { type Observable } from "rxjs";
import classNames from "classnames";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
import { useTranslation } from "react-i18next";
@@ -31,7 +31,11 @@ import { useRootElement } from "../RootElementContext";
import { observeElementSize$ } from "../utils/elementSize";
import styles from "./InCallView.module.css";
import { GridTile } from "../tile/GridTile";
import { SettingsModal, defaultSettingsTab } from "../settings/SettingsModal";
import {
SettingsModal,
type SettingsClientParts,
defaultSettingsTab,
} from "../settings/SettingsModal";
import { useRageshakeRequestModal } from "../settings/submit-rageshake";
import { RageshakeRequestModal } from "./RageshakeRequestModal";
import { useWakeLock } from "../useWakeLock";
@@ -70,9 +74,8 @@ import { ReactionsAudioRenderer } from "./ReactionAudioRenderer";
import { ReactionsOverlay } from "./ReactionsOverlay";
import { CallEventAudioRenderer } from "./CallEventAudioRenderer";
import {
createRTCSlot,
type RTCParticipationOptions,
type RTCSlotOptions,
type RTCSlot,
constant,
} from "@element-hq/matrixrtc-sdk";
import {
@@ -94,7 +97,10 @@ import {
screenShareResolution,
} from "../settings/settings";
import { Config } from "../config/Config";
import { createSentCallNotification$ } from "../state/CallViewModel/CallNotificationLifecycle.ts";
import {
type CallNotificationWrapper,
createSentCallNotification$,
} from "../state/CallViewModel/CallNotificationLifecycle.ts";
import { ReactionsReader } from "../reactions/ReactionsReader";
import { MemberAudioRenderer } from "../tracks/MemberAudioRenderer.tsx";
import { muteAllAudio$ } from "../state/MuteAllAudioModel.ts";
@@ -132,6 +138,59 @@ export interface ActiveCallProps extends Omit<
InCallViewProps,
"vm" | "footerVm" | "developerSettingsVm"
> {
client: Pick<
MatrixClient,
// Our own identity: call view model, footer, settings
| "getUserId"
| "getDeviceId"
// Developer settings: the local transport and authenticating with it,
// `getOpenIdToken` for the JWT service flow, `getLivekitToken` for the
// client-server API flow (MSC4195)
| "getDomain"
| "getOpenIdToken"
| "getLivekitToken"
// Reactions and raised hands: sending, lowering, and decrypting
| "sendEvent"
| "redactEvent"
| "decryptEventIfNeeded"
// `on`/`off`, to listen for `MatrixEventEvent.Decrypted` (reactions once
// decrypted)
| "on"
| "off"
// Profile: display name and avatar, for the settings and the call ended view
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer settings
| "doesServerSupportUnstableFeature"
| "getCrypto"
>;
matrixRoom: Pick<
MatrixRoom,
// Call view model and reactions
| "roomId"
// Joined and invited members, for the call view model
| "getMembersWithMembership"
// Call notifications, for the call view model
| "_unstable_getStickyEvents"
// Functional members, for the call view model
| "getLiveTimeline"
// Raised hands already in the room, for the reactions reader
| "relations"
// `on`/`off`, to listen for:
// - `RoomStateEvent.Members` (call view model members)
// - `RoomStateEvent.Events` (call view model functional members)
// - `RoomEvent.Timeline` and `RoomStickyEventsEvent.Update` (call notifications)
// - `RoomEvent.Timeline`, `RoomEvent.Redaction` and
// `RoomEvent.LocalEchoUpdated` (reactions and raised hands)
| "on"
| "off"
>;
/** The slot to join; the call leaves it again when it ends. */
slot: RTCSlot;
/** The call notifications sent with our join, so the call can show it ringing. */
sentCallNotification$: Observable<CallNotificationWrapper>;
e2eeSystem: EncryptionSystem;
/** The tracks the lobby opened, for the join to publish. */
media: LocalMedia;
@@ -159,18 +218,17 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
useEffect(() => {
rootLogger.info("START CALL VIEW SCOPE");
const scope = new ObservableScope();
const reactionsReader = new ReactionsReader(scope, props.rtcSession);
const reactionsReader = new ReactionsReader(
scope,
props.slot.members$,
props.matrixRoom,
props.client,
);
const { autoLeaveWhenOthersLeft, waitForCallPickup, sendNotificationType } =
urlParams;
const capture = captureSettings();
const slot = createRTCSlot(
scope,
props.client,
props.matrixRoom,
rtcSlotOptions(props.e2eeSystem),
);
const rtcParticipation = slot.join({
const rtcParticipation = props.slot.join({
...rtcParticipationOptions(urlParams),
// Without a lobby nothing was captured and the join captures itself
publish:
@@ -189,6 +247,7 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
{
rtcParticipation,
matrixRoom: props.matrixRoom,
client: props.client,
mediaDevices,
muteStates: props.muteStates,
encryptionSystem: props.e2eeSystem,
@@ -205,7 +264,7 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
waitForCallPickup: waitForCallPickup && sendNotificationType === "ring",
sentCallNotification$: createSentCallNotification$(
scope,
props.rtcSession,
props.sentCallNotification$,
),
},
);
@@ -214,10 +273,13 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
vm.leave$.pipe(scope.bind()).subscribe(props.onLeft);
return (): void => {
// The slot outlives the call, and joins again only once this has left
rtcParticipation.leave();
scope.end();
};
}, [
props.rtcSession,
props.slot,
props.sentCallNotification$,
props.matrixRoom,
props.muteStates,
props.e2eeSystem,
@@ -251,7 +313,6 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
scope.end();
};
}, [
props.rtcSession,
props.matrixRoom,
props.muteStates,
props.e2eeSystem,
@@ -268,7 +329,11 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
if (developerSettingsVm === null) return null;
return (
<ReactionsSenderProvider vm={vm} rtcSession={props.rtcSession}>
<ReactionsSenderProvider
vm={vm}
room={props.matrixRoom}
client={props.client}
>
<InCallView
{...props}
vm={vm}
@@ -280,13 +345,12 @@ export const ActiveCall: FC<ActiveCallProps> = (props) => {
};
export interface InCallViewProps {
client: MatrixClient;
client: SettingsClientParts;
vm: CallViewModel;
footerVm: ViewModel<FooterSnapshot>;
developerSettingsVm: ViewModel<DeveloperSettingsSnapshot>;
matrixInfo: MatrixInfo;
rtcSession: MatrixRTCSession;
matrixRoom: MatrixRoom;
matrixRoom: Pick<MatrixRoom, "roomId">;
muteStates: MuteStates;
onShareClick: (() => void) | null;
}
@@ -750,8 +814,7 @@ export const InCallView: FC<InCallViewProps> = ({
/**
* How the tracks are captured and encoded, from Element Call's settings and
* configuration. Shared with the legacy SDK bundle so that a widget built from
* it behaves like the app.
* configuration.
*/
export function captureSettings(): CaptureSettings {
const config = Config.get();
@@ -798,31 +861,22 @@ export function captureSettings(): CaptureSettings {
}
/**
* What the client needs from Element Call's configuration, settings and URL:
* What the join needs from Element Call's configuration, settings and URL:
* the SDK reads none of them itself. What to publish depends on the mute
* switches and the devices, so the caller adds that. Shared with the legacy
* SDK bundle so that a widget built from it behaves like the app.
* switches and the devices, so the caller adds that.
*/
export function rtcSlotOptions(
encryptionSystem: EncryptionSystem,
): RTCSlotOptions {
return {
encryptionSystem,
// matrix_rtc_mode in config.json overrides the user's Developer Settings
// choice. We merely sample the current mode here, so the user would need
// to manually rejoin to switch to a different one.
matrixRTCMode:
(Config.get().matrix_rtc_mode as MatrixRTCMode | undefined) ??
matrixRTCModeSetting.value$.value,
};
}
export function rtcParticipationOptions(
urlParams: ReturnType<typeof useUrlParams>,
): Omit<RTCParticipationOptions, "publish"> {
const config = Config.get();
const session = config.matrix_rtc_session;
return {
// matrix_rtc_mode in config.json overrides the user's Developer Settings
// choice. We merely sample the current mode here, so the user would need
// to manually rejoin to switch to a different one.
matrixRTCMode:
(config.matrix_rtc_mode as MatrixRTCMode | undefined) ??
matrixRTCModeSetting.value$.value,
sendNotificationType: urlParams.sendNotificationType,
callIntent: urlParams.callIntent,
timings: {
+1 -1
View File
@@ -29,7 +29,7 @@ import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement";
import { QrCode } from "../QrCode";
interface Props {
room: Room;
room: Pick<Room, "roomId" | "name">;
open: boolean;
onDismiss: () => void;
}
-1
View File
@@ -71,7 +71,6 @@ export const KnockLobbyView: FC<Props> = ({
userId: client.getUserId() ?? "",
displayName: profile.displayName,
avatarUrl: profile.avatarUrl,
roomAlias: null,
roomId: roomSummary.room_id,
roomName: roomSummary.name ?? "",
roomAvatar: roomSummary.avatar_url ?? null,
-1
View File
@@ -91,7 +91,6 @@ const matrixInfo = {
avatarUrl: "",
roomId: "!room:example.org",
roomName: "Test Room",
roomAlias: null,
roomAvatar: null,
e2eeSystem: { kind: E2eeType.NONE } satisfies EncryptionSystem,
};
+6 -3
View File
@@ -7,7 +7,6 @@ Please see LICENSE in the repository root for full details.
import { type FC, useCallback, useState, type JSX, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { type MatrixClient } from "matrix-js-sdk";
import { Button } from "@vector-im/compound-web";
import classNames from "classnames";
import { logger } from "matrix-js-sdk/lib/logger";
@@ -23,7 +22,11 @@ import { Header, LeftNav, RightNav, RoomHeaderInfo } from "../Header";
import { type MatrixInfo, VideoPreview } from "./VideoPreview";
import { type MuteStates } from "../state/MuteStates";
import { InviteButton } from "../button/InviteButton";
import { SettingsModal, defaultSettingsTab } from "../settings/SettingsModal";
import {
SettingsModal,
type SettingsClientParts,
defaultSettingsTab,
} from "../settings/SettingsModal";
import { useRootSizeMatches } from "../useRootSize";
import { E2eeType } from "../e2ee/e2eeType";
import { LeaveToHomeLink } from "../button/LeaveToHomeLink";
@@ -46,7 +49,7 @@ import { captureSettings } from "./InCallView";
const noVideoTrack = constant<LocalVideoMediaTrack | null>(null);
interface Props {
client: MatrixClient;
client: SettingsClientParts;
matrixInfo: MatrixInfo;
/** The media to open the devices on; the join takes it over. */
media: LocalMedia;
+29 -4
View File
@@ -6,7 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { type FC, useEffect, useState, type ReactNode, useRef } from "react";
import {
type FC,
useEffect,
useMemo,
useState,
type ReactNode,
useRef,
} from "react";
import { type MatrixError } from "matrix-js-sdk";
import { logger } from "matrix-js-sdk/lib/logger";
import { Trans, useTranslation } from "react-i18next";
@@ -27,6 +34,7 @@ import { Link } from "../button/Link";
import { ErrorView } from "../ErrorView";
import { usePageTitle } from "../usePageTitle";
import { useRoomName } from "./useRoomName";
import { createCallMatrixBackend } from "../CallMatrixBackend";
export const RoomPage: FC = (): ReactNode => {
const urlParams = useUrlParams();
@@ -51,9 +59,14 @@ export const RoomPage: FC = (): ReactNode => {
// The page title is the page's to set, not the call's: a host embedding the
// call has a title of its own. So it is set here, for whichever room we have
// got as far as knowing about.
const roomName = useRoomName(
groupCallState.kind === "loaded" ? groupCallState.rtcSession.room : null,
const loadedRoom =
groupCallState.kind === "loaded" ? groupCallState.room : null;
const backend = useMemo(
() =>
client && loadedRoom ? createCallMatrixBackend(client, loadedRoom) : null,
[client, loadedRoom],
);
const roomName = useRoomName(loadedRoom);
usePageTitle(
roomName ??
(groupCallState.kind === "canKnock" ||
@@ -90,6 +103,18 @@ export const RoomPage: FC = (): ReactNode => {
if (optInAnalytics === null && setOptInAnalytics) setOptInAnalytics(true);
}, [optInAnalytics, setOptInAnalytics]);
useEffect(() => {
// Sanity check the room object
if (
client &&
loadedRoom !== null &&
client.getRoom(loadedRoom.roomId) !== loadedRoom
)
logger.warn(
`We've ended up with multiple rooms for the same ID (${loadedRoom.roomId}). This indicates a bug in the group call loading code, and may lead to incomplete room state.`,
);
}, [client, loadedRoom]);
const wasInWaitForInviteState = useRef<boolean>(false);
useEffect(() => {
@@ -103,7 +128,7 @@ export const RoomPage: FC = (): ReactNode => {
case "loaded":
return (
<CallView
client={client!}
backend={backend!}
rtcSession={groupCallState.rtcSession}
isPasswordlessUser={passwordlessUser}
confineToRoom={confineToRoom}
-1
View File
@@ -19,7 +19,6 @@ describe("VideoPreview", () => {
roomId: "",
roomName: "",
e2eeSystem: { kind: E2eeType.NONE },
roomAlias: null,
roomAvatar: null,
};
-1
View File
@@ -25,7 +25,6 @@ export type MatrixInfo = {
avatarUrl: string;
roomId: string;
roomName: string;
roomAlias: string | null;
roomAvatar: string | null;
e2eeSystem: EncryptionSystem;
};
+27
View File
@@ -0,0 +1,27 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import { act, renderHook } from "@testing-library/react";
import { JoinRule, type RoomState, RoomStateEvent } from "matrix-js-sdk";
import { mockMatrixRoom } from "../utils/test";
import { useJoinRule } from "./useJoinRule";
// The hook reads the room, not the state the events carry
const state = {} as RoomState;
test("the join rule follows the room's state updates", () => {
let joinRule = JoinRule.Invite;
const room = mockMatrixRoom({ getJoinRule: () => joinRule });
const { result } = renderHook(() => useJoinRule(room));
expect(result.current).toBe(JoinRule.Invite);
joinRule = JoinRule.Public;
act(() => void room.emit(RoomStateEvent.Update, state));
expect(result.current).toBe(JoinRule.Public);
});
+21 -8
View File
@@ -5,14 +5,27 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { useCallback } from "react";
import { useCallback, useSyncExternalStore } from "react";
import {
type JoinRule,
type Room,
RoomEvent,
RoomStateEvent,
} from "matrix-js-sdk";
import { fromEvent, merge } from "rxjs";
import type { JoinRule, Room } from "matrix-js-sdk";
import { useRoomState } from "./useRoomState";
export function useJoinRule(room: Room): JoinRule {
return useRoomState(
room,
useCallback((state) => state.getJoinRule(), []),
export function useJoinRule(
room: Pick<Room, "getJoinRule" | "on" | "off">,
): JoinRule {
const subscribe = useCallback(
(onChange: () => void) => {
const s = merge(
fromEvent(room, RoomStateEvent.Update),
fromEvent(room, RoomEvent.CurrentStateUpdated),
).subscribe(onChange);
return (): void => s.unsubscribe();
},
[room],
);
return useSyncExternalStore(subscribe, () => room.getJoinRule());
}
+49
View File
@@ -0,0 +1,49 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import {
JoinRule,
type MatrixClient,
type Room,
SyncState,
} from "matrix-js-sdk";
import { type MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { mockEmitter, mockMatrixRoom } from "../utils/test";
import { type GroupCallLoaded, useLoadGroupCall } from "./useLoadGroupCall";
test("a joined room is the client's own, not the detached one joinRoom returns", async () => {
const roomId = "!call:example.org";
const detachedRoom = mockMatrixRoom({ roomId });
const syncedRoom = mockMatrixRoom({ roomId });
const rtcSession = {} as MatrixRTCSession;
// Unknown before the join, known once the sync has brought it in
let knownRoom: Room | null = null;
const getRoomSession = vi.fn().mockReturnValue(rtcSession);
const client = {
...mockEmitter(),
getSyncState: () => SyncState.Syncing,
getRoom: () => knownRoom,
getRoomSummary: vi.fn().mockResolvedValue({ join_rule: JoinRule.Public }),
joinRoom: vi.fn().mockResolvedValue(detachedRoom),
waitUntilRoomReadyForGroupCalls: vi.fn().mockImplementation(async () => {
knownRoom = syncedRoom;
return Promise.resolve();
}),
matrixRTC: { getRoomSession },
} as unknown as MatrixClient;
const { result } = renderHook(() => useLoadGroupCall(client, roomId, []));
await waitFor(() => expect(result.current.kind).toBe("loaded"));
const loaded = result.current as GroupCallLoaded;
expect(loaded.room).toBe(syncedRoom);
expect(loaded.rtcSession).toBe(rtcSession);
expect(getRoomSession).toHaveBeenCalledWith(syncedRoom);
});
+11 -4
View File
@@ -38,6 +38,7 @@ import { useUrlParams } from "../UrlParams";
export type GroupCallLoaded = {
kind: "loaded";
room: Room;
rtcSession: MatrixRTCSession;
};
@@ -324,16 +325,20 @@ export const useLoadGroupCall = (
);
await client.waitUntilRoomReadyForGroupCalls(room.roomId);
logger.info(`${roomIdOrAlias}, is ready for group calls`);
return room;
// `joinRoom` hands back a detached room when the sync has not brought the
// room in yet; only the client's own copy is kept up to date
return client.getRoom(room.roomId) ?? room;
};
const fetchOrCreateGroupCall = async (): Promise<MatrixRTCSession> => {
const fetchOrCreateGroupCall = async (): Promise<
Pick<GroupCallLoaded, "room" | "rtcSession">
> => {
const room = await fetchOrCreateRoom();
activeRoom.current = room;
logger.debug(`Fetched / joined room ${roomIdOrAlias}`);
const rtcSession = client.matrixRTC.getRoomSession(room);
return rtcSession;
return { room, rtcSession };
};
const waitForClientSyncing = async (): Promise<void> => {
@@ -367,7 +372,9 @@ export const useLoadGroupCall = (
logger.log("Start loading group call");
waitForClientSyncing()
.then(fetchOrCreateGroupCall)
.then((rtcSession) => setState({ kind: "loaded", rtcSession }))
.then(({ room, rtcSession }) =>
setState({ kind: "loaded", room, rtcSession }),
)
.then(observeMyMembership)
.catch((error) => setState({ kind: "failed", error }));
}
+27
View File
@@ -0,0 +1,27 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { expect, test } from "vitest";
import { act, renderHook } from "@testing-library/react";
import { RoomEvent, type RoomState } from "matrix-js-sdk";
import { mockMatrixRoom } from "../utils/test";
import { useRoomAvatar } from "./useRoomAvatar";
// The hook reads the room, not the state the events carry
const state = {} as RoomState;
test("the avatar follows the room's state being replaced", () => {
let avatar: string | null = null;
const room = mockMatrixRoom({ getMxcAvatarUrl: () => avatar });
const { result } = renderHook(() => useRoomAvatar(room));
expect(result.current).toBeNull();
avatar = "mxc://example.org/avatar";
act(() => void room.emit(RoomEvent.CurrentStateUpdated, room, state, state));
expect(result.current).toBe("mxc://example.org/avatar");
});
+16 -8
View File
@@ -5,14 +5,22 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { useCallback } from "react";
import { type Room } from "matrix-js-sdk";
import { useCallback, useSyncExternalStore } from "react";
import { type Room, RoomEvent, RoomStateEvent } from "matrix-js-sdk";
import { fromEvent, merge } from "rxjs";
import { useRoomState } from "./useRoomState";
export function useRoomAvatar(room: Room): string | null {
return useRoomState(
room,
useCallback(() => room.getMxcAvatarUrl(), [room]),
export function useRoomAvatar(
room: Pick<Room, "getMxcAvatarUrl" | "on" | "off">,
): string | null {
const subscribe = useCallback(
(onChange: () => void) => {
const s = merge(
fromEvent(room, RoomStateEvent.Update),
fromEvent(room, RoomEvent.CurrentStateUpdated),
).subscribe(onChange);
return (): void => s.unsubscribe();
},
[room],
);
return useSyncExternalStore(subscribe, () => room.getMxcAvatarUrl());
}
+5 -3
View File
@@ -12,9 +12,11 @@ import { useCallback, useSyncExternalStore } from "react";
* The room's name, kept up to date. Null when there is no room yet, for a
* caller that only sometimes has one.
*/
export function useRoomName(room: Room): string;
export function useRoomName(room: Room | null): string | null;
export function useRoomName(room: Room | null): string | null {
type RoomNameParts = Pick<Room, "name" | "on" | "off">;
export function useRoomName(room: RoomNameParts): string;
export function useRoomName(room: RoomNameParts | null): string | null;
export function useRoomName(room: RoomNameParts | null): string | null {
const subscribe = useCallback(
(onChange: () => void) => {
if (room === null) return (): void => {};
-37
View File
@@ -1,37 +0,0 @@
/*
Copyright 2022-2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { useCallback } from "react";
import {
type RoomState,
RoomStateEvent,
type Room,
RoomEvent,
} from "matrix-js-sdk";
import { useTypedEventEmitterState } from "../useEvents";
/**
* A React hook for values computed from room state.
* @param room The room.
* @param f A mapping from the current room state to the computed value.
* @returns The computed value.
*/
export function useRoomState<T>(room: Room, f: (state: RoomState) => T): T {
// TODO: matrix-js-sdk says that Room.currentState is deprecated, but it's not
// clear how to reactively track the current state of the room without it
const currentState = useTypedEventEmitterState(
room,
RoomEvent.CurrentStateUpdated,
useCallback(() => room.currentState, [room]),
);
return useTypedEventEmitterState(
currentState,
RoomStateEvent.Update,
useCallback(() => f(currentState), [f, currentState]),
);
}
+13 -1
View File
@@ -111,8 +111,20 @@ const KeyRotationStatus: FC<{ info: KeyRotationInfo }> = ({ info }) => (
</p>
);
export type DeveloperSettingsClientParts = Pick<
MatrixClient,
| "doesServerSupportUnstableFeature"
| "getCrypto"
| "getUserId"
| "getDeviceId"
| "getDomain"
// To authenticate with a transport
| "getLivekitToken"
| "getOpenIdToken"
>;
interface Props {
client: MatrixClient;
client: DeveloperSettingsClientParts;
roomId?: string;
/** The transports the client is connected to, while in a call. */
transports?: TransportInfo[];
+2 -3
View File
@@ -6,17 +6,16 @@ Please see LICENSE in the repository root for full details.
*/
import { type FC, useCallback, useEffect, useMemo, useRef } from "react";
import { type MatrixClient } from "matrix-js-sdk";
import { useTranslation } from "react-i18next";
import { logger } from "matrix-js-sdk/lib/logger";
import { useProfile } from "../profile/useProfile";
import { type ProfileClientParts, useProfile } from "../profile/useProfile";
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
import { AvatarInputField } from "../input/AvatarInputField";
import styles from "./ProfileSettingsTab.module.css";
interface Props {
client: MatrixClient;
client: ProfileClientParts;
}
export const ProfileSettingsTab: FC<Props> = ({ client }) => {
const { t } = useTranslation();
+21 -2
View File
@@ -7,8 +7,8 @@ Please see LICENSE in the repository root for full details.
import { type FC, type ReactNode, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { type MatrixClient } from "matrix-js-sdk";
import { Button, Root as Form, Separator } from "@vector-im/compound-web";
import { type MatrixClient } from "matrix-js-sdk";
import { Modal } from "../Modal";
import styles from "./SettingsModal.module.css";
@@ -53,12 +53,31 @@ type SettingsTab =
| "more"
| "developer";
export type SettingsClientParts = Pick<
MatrixClient,
// Profile and developer tabs
| "getUserId"
// Profile tab
| "getUser"
| "setDisplayName"
| "setAvatarUrl"
| "uploadContent"
// Developer tab
| "doesServerSupportUnstableFeature"
| "getCrypto"
| "getDeviceId"
| "getDomain"
// Developer tab, to authenticate with a transport
| "getLivekitToken"
| "getOpenIdToken"
>;
interface Props {
open: boolean;
onDismiss: () => void;
tab: SettingsTab;
onTabChange: (tab: SettingsTab) => void;
client: MatrixClient;
client: SettingsClientParts;
roomId?: string;
/** The transports the client is connected to, while in a call. */
transports?: TransportInfo[];
+1 -1
View File
@@ -377,7 +377,7 @@ export function useRageshakeRequestModal(
client.on(ClientEvent.Event, onEvent);
return (): void => {
client.removeListener(ClientEvent.Event, onEvent);
client.off(ClientEvent.Event, onEvent);
};
}, [setOpen, roomId, client]);
@@ -53,7 +53,7 @@ function mockRingEvent(
const defaultProps = {
memberUserIds$: constant<string[]>([]),
roomMemberUserIds$: constant([alice.userId]),
humanRoomMemberUserIds$: constant([alice.userId]),
receivedDecline$: NEVER,
options: {
waitForCallPickup: true,
@@ -63,44 +63,57 @@ export type CallNotificationWrapper = {
event_id: string;
} & IRTCNotificationContent;
/**
* The call notifications the session sends with our join: what a host hands
* the call as `sentCallNotification$`.
*/
export function observeSentCallNotification$(
matrixRTCSession: Pick<MatrixRTCSession, "on" | "off">,
): Observable<CallNotificationWrapper> {
return fromEvent(
matrixRTCSession,
MatrixRTCSessionEvent.DidSendCallNotification,
(notification: CallNotificationWrapper) => notification,
);
}
export function createSentCallNotification$(
scope: ObservableScope,
matrixRTCSession: MatrixRTCSession,
sentCallNotification$: Observable<CallNotificationWrapper>,
): Behavior<CallNotificationWrapper | null> {
const sentCallNotification$ = scope.behavior(
fromEvent(matrixRTCSession, MatrixRTCSessionEvent.DidSendCallNotification),
return scope.behavior<CallNotificationWrapper | null>(
sentCallNotification$,
null,
) as Behavior<CallNotificationWrapper | null>;
return sentCallNotification$;
);
}
export function createReceivedDecline$(
matrixRoom: MatrixRoom,
matrixRoom: Pick<MatrixRoom, "_unstable_getStickyEvents" | "on" | "off">,
): Observable<MatrixEvent> {
// A decline is sticky, so after a gappy sync it arrives only in the sticky
// section, which never reaches the timeline. Racing for the first outcome
// makes a decline seen in both places harmless.
const timeline$ = (
fromEvent(matrixRoom, RoomEvent.Timeline) as Observable<
Parameters<EventTimelineSetHandlerMap[RoomEvent.Timeline]>
>
).pipe(map(([event]) => event));
const timeline$ = fromEvent(
matrixRoom,
RoomEvent.Timeline,
(...[event]: Parameters<EventTimelineSetHandlerMap[RoomEvent.Timeline]>) =>
event,
);
// The decline can arrive via sync before the send of our notification
// resolves, which is when we start listening. To guard against this, we
// include what is already in the sticky section.
const currentSticky$ = defer(() =>
from(matrixRoom._unstable_getStickyEvents()),
);
const sticky$ = (
fromEvent(matrixRoom, RoomStickyEventsEvent.Update) as Observable<
Parameters<RoomStickyEventsMap[RoomStickyEventsEvent.Update]>
>
).pipe(
mergeMap(([added, updated]) => [
...added,
...updated.map(({ current }) => current),
]),
);
const sticky$ = fromEvent(
matrixRoom,
RoomStickyEventsEvent.Update,
(
...[added, updated]: Parameters<
RoomStickyEventsMap[RoomStickyEventsEvent.Update]
>
) => [...added, ...updated.map(({ current }) => current)],
).pipe(mergeMap((events) => events));
return merge(currentSticky$, sticky$, timeline$).pipe(
filter((event) => event.getType() === EventType.RTCDecline),
);
@@ -110,8 +123,12 @@ export interface Props {
scope: ObservableScope;
/** The user ids of the session's members, ours included. */
memberUserIds$: Behavior<string[]>;
/** The user ids of the room's members, ours included. */
roomMemberUserIds$: Behavior<string[]>;
/**
* The user ids of the room's human members, ours included. Functional
* members (bots) must be excluded: the ring recipient is the first one that
* is not us.
*/
humanRoomMemberUserIds$: Behavior<string[]>;
sentCallNotification$: Observable<CallNotificationWrapper | null>;
receivedDecline$: Observable<MatrixEvent>;
options: { waitForCallPickup?: boolean; autoLeaveWhenOthersLeft?: boolean };
@@ -121,7 +138,7 @@ export interface Props {
export function createCallNotificationLifecycle$({
scope,
memberUserIds$,
roomMemberUserIds$,
humanRoomMemberUserIds$,
sentCallNotification$,
receivedDecline$,
options,
@@ -153,8 +170,7 @@ export function createCallNotificationLifecycle$({
),
switchMap((notificationEvent) => {
// We assume that there is only one other user in the room when ringing
// TODO: Respect io.element.functional_members
const recipient = roomMemberUserIds$.value.find(
const recipient = humanRoomMemberUserIds$.value.find(
(userId) => userId !== localUser.userId,
);
if (recipient === undefined) {
@@ -567,6 +567,58 @@ describe.each(modes)("CallViewModel (%s mode)", (mode) => {
});
});
test("one-on-one mobile layout hides name tags in room with 2 members and a bot", () => {
withTestScheduler(({ expectObservable }) => {
withCallViewModel(
{
remoteParticipants$: constant([aliceParticipant]),
roomMembers: [local, alice, bob],
functionalMembers: [bob.userId],
rtcMembers$: constant([localRtcMember, aliceRtcMember]),
viewSize$: constant({ width: 380, height: 700 }), // Mobile phone
},
(vm) => {
expectObservable(summarizeLayout$(vm.layout$)).toBe("a", {
a: {
type: "one-on-one-mobile",
spotlight: [`${aliceId}:0`],
pip: undefined,
pipSize: undefined,
},
});
// Bob is a bot, so the room is still a DM with Alice
expectObservable(vm.showNameTags$).toBe("n", yesNo);
},
);
});
});
test("one-on-one mobile layout shows name tags when a bot is in the call", () => {
withTestScheduler(({ expectObservable }) => {
withCallViewModel(
{
remoteParticipants$: constant([bobParticipant]),
roomMembers: [local, alice, bob],
functionalMembers: [bob.userId],
rtcMembers$: constant([localRtcMember, bobRtcMember]),
viewSize$: constant({ width: 380, height: 700 }), // Mobile phone
},
(vm) => {
expectObservable(summarizeLayout$(vm.layout$)).toBe("a", {
a: {
type: "one-on-one-mobile",
spotlight: [`${bobId}:0`],
pip: undefined,
pipSize: undefined,
},
});
// The remote tile is the bot's media, which must never go unlabelled
expectObservable(vm.showNameTags$).toBe("y", yesNo);
},
);
});
});
test("landscape mobile layouts show screen shares and group call participants", () => {
withTestScheduler(({ behavior, expectObservable }) => {
// Starts as a one-on-one call, then Alice shares her screen, then Bob
@@ -1624,6 +1676,47 @@ describe.each(modes)("CallViewModel (%s mode)", (mode) => {
});
});
test("ringing picks the human recipient over a functional member listed first", () => {
withTestScheduler(({ behavior, schedule, expectObservable }) => {
withCallViewModel(
{
// Alice answers after 20ms
rtcMembers$: behavior("a 20ms b", {
a: [localRtcMember],
b: [localRtcMember, aliceRtcMember],
}),
// A DM with a bot, where the bot happens to come first
roomMembers: [bob, alice, local],
functionalMembers: [bob.userId],
},
(vm, rtcSession) => {
// Fire a ringing notification with a 30ms lifetime
schedule("n", {
n: () => {
rtcSession.emit(
MatrixRTCSessionEvent.DidSendCallNotification,
mockRingEvent("$notif1", 30),
);
},
});
// Alice, not the bot, is the recipient, and her pickup stops the ring
expectObservable(vm.ringingVm$).toBe("(ab) 17ms a", {
a: null,
b: expect.objectContaining({
type: "ringing",
userId: alice.userId,
intent: RTC_CALL_INTENT_AUDIO,
}),
});
// The ring was answered, so the lifetime running out must not leave
expectObservable(vm.autoLeave$).toBe("-");
},
{ waitForCallPickup: true },
);
});
});
it.skip("audio output changes when toggling earpiece mode", () => {
withTestScheduler(({ schedule, expectObservable }) => {
const devices = new MediaDevices(testScope(), {
+36 -12
View File
@@ -28,6 +28,7 @@ import {
} from "@element-hq/matrixrtc-sdk";
import {
KnownMembership,
type MatrixClient,
type Room as MatrixRoom,
type RoomMember,
RoomStateEvent,
@@ -114,6 +115,7 @@ import {
createCallNotificationLifecycle$,
createReceivedDecline$,
} from "./CallNotificationLifecycle.ts";
import { createFunctionalMembers$ } from "./FunctionalMembers.ts";
import {
type LayoutSwitchViewModel,
createLayoutSwitchViewModel,
@@ -193,7 +195,16 @@ export function initialPublishRequests(
export interface CallViewModelProps {
rtcParticipation: RTCParticipation;
/** A call is permanently tied to a single Matrix room. */
matrixRoom: MatrixRoom;
matrixRoom: Pick<
MatrixRoom,
| "roomId"
| "getMembersWithMembership"
| "_unstable_getStickyEvents"
| "getLiveTimeline"
| "on"
| "off"
>;
client: Pick<MatrixClient, "getUserId" | "getDeviceId">;
mediaDevices: MediaDevices;
muteStates: MuteStates;
encryptionSystem: EncryptionSystem;
@@ -492,6 +503,7 @@ export function createCallViewModel$(
{
rtcParticipation,
matrixRoom,
client,
mediaDevices,
muteStates,
encryptionSystem,
@@ -502,7 +514,6 @@ export function createCallViewModel$(
options: CallViewModelOptions = {},
): CallViewModel {
const logger = rootLogger.getChild("[CallViewModel]");
const client = matrixRoom.client;
const userId = client.getUserId();
const deviceId = client.getDeviceId();
if (!(userId && deviceId))
@@ -655,6 +666,14 @@ export function createCallViewModel$(
),
roomMembers(matrixRoom),
);
const functionalMembers$ = createFunctionalMembers$(scope, matrixRoom);
const humanRoomMembers$ = scope.behavior(
combineLatest([matrixRoomMembers$, functionalMembers$]).pipe(
map(([members, functional]) =>
members.filter((m) => !functional.has(m.userId)),
),
),
);
// TODO if we are in "unknown" state we need a loading rendering (or empty screen)
// Otherwise it looks like we already connected and only than the ringing starts which is weird.
@@ -663,8 +682,8 @@ export function createCallViewModel$(
memberUserIds$: scope.behavior(
members$.pipe(map((members) => members.map((m) => m.userId))),
),
roomMemberUserIds$: scope.behavior(
matrixRoomMembers$.pipe(map((members) => members.map((m) => m.userId))),
humanRoomMemberUserIds$: scope.behavior(
humanRoomMembers$.pipe(map((members) => members.map((m) => m.userId))),
),
sentCallNotification$,
receivedDecline$: createReceivedDecline$(matrixRoom),
@@ -1120,18 +1139,21 @@ export function createCallViewModel$(
layoutMedia$.pipe(
switchMap((l) =>
l.type === "pip" || l.type === "one-on-one-mobile"
? matrixRoomMembers$.pipe(
? combineLatest([
humanRoomMembers$,
members$,
functionalMembers$,
]).pipe(
map(
(members) =>
([humanMembers, callMembers, functional]) =>
// Hide name tags by default in these layouts. For safety we
// still need to show them in case it wouldn't be clear who
// the spotlight media belongs to.
// TODO: Respect io.element.functional_members (while still
// being careful to never show a functional member's media
// without a name tag!)
// the spotlight media belongs to: a third person in the room,
// or a functional member in the call.
// TODO: Only hide name tags in DMs, not group chats that just
// happen to have only 2 users
members.length > 2,
humanMembers.length > 2 ||
callMembers.some((m) => functional.has(m.userId)),
),
)
: of(true),
@@ -1646,7 +1668,9 @@ function isPermissionDenied(e: unknown): boolean {
}
/** The members a call can be with: those in the room and those invited. */
function roomMembers(room: MatrixRoom): RoomMember[] {
function roomMembers(
room: Pick<MatrixRoom, "getMembersWithMembership">,
): RoomMember[] {
return room
.getMembersWithMembership(KnownMembership.Join)
.concat(room.getMembersWithMembership(KnownMembership.Invite));
@@ -19,7 +19,14 @@ import {
} from "@element-hq/matrixrtc-sdk";
import { SyncState } from "matrix-js-sdk/lib/sync";
import { BehaviorSubject, map, of, switchMap } from "rxjs";
import { ClientEvent, type RoomMember, type MatrixClient } from "matrix-js-sdk";
import {
ClientEvent,
KnownMembership,
type MatrixEvent,
type RoomMember,
type MatrixClient,
UNSTABLE_ELEMENT_FUNCTIONAL_USERS,
} from "matrix-js-sdk";
import EventEmitter from "events";
import type { CallMembership } from "matrix-js-sdk/lib/matrixrtc";
@@ -29,9 +36,13 @@ import {
createCallViewModel$,
type CallViewModelOptions,
} from "./CallViewModel";
import { createSentCallNotification$ } from "./CallNotificationLifecycle";
import {
createSentCallNotification$,
observeSentCallNotification$,
} from "./CallNotificationLifecycle";
import {
mockConfig,
mockLiveTimeline,
mockMatrixRoom,
mockMatrixRoomMember,
mockRTCParticipation,
@@ -74,6 +85,8 @@ export interface CallViewModelInputs {
remoteParticipants$: Behavior<Participant[]>;
rtcMembers$: Behavior<Partial<CallMembership>[]>;
roomMembers: RoomMember[];
/** The user IDs the room marks as functional members (bots). */
functionalMembers: string[];
/** Whether the local transport is connected. */
connected$: Behavior<boolean>;
speaking: Map<Participant, Behavior<boolean>>;
@@ -100,6 +113,7 @@ export function withCallViewModel(mode: MatrixRTCMode) {
dave,
daveRTL,
],
functionalMembers = [],
connected$ = constant(true),
speaking = new Map(),
videoEnabled = new Map(),
@@ -145,7 +159,15 @@ export function withCallViewModel(mode: MatrixRTCMode) {
}
})() as Partial<MatrixClient> as MatrixClient,
getMembers: () => roomMembers,
getMembersWithMembership: () => roomMembers,
getMembersWithMembership: (membership: string) =>
membership === KnownMembership.Join ? roomMembers : [],
getLiveTimeline: mockLiveTimeline((type, stateKey) =>
UNSTABLE_ELEMENT_FUNCTIONAL_USERS.matches(type) && stateKey === ""
? ({
getContent: () => ({ service_members: functionalMembers }),
} as Partial<MatrixEvent> as MatrixEvent)
: null,
),
_unstable_getStickyEvents: () => [],
});
const rtcSession = new MockRTCSession(room, []).withMemberships(
@@ -265,6 +287,7 @@ export function withCallViewModel(mode: MatrixRTCMode) {
{
rtcParticipation,
matrixRoom: room,
client: room.client,
mediaDevices,
muteStates,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
@@ -276,7 +299,7 @@ export function withCallViewModel(mode: MatrixRTCMode) {
autoLeaveWhenOthersLeft: false,
sentCallNotification$: createSentCallNotification$(
scope,
rtcSession.asMockedSession(),
observeSentCallNotification$(rtcSession.asMockedSession()),
),
...options,
},
@@ -0,0 +1,94 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { beforeEach, describe, it } from "vitest";
import {
type MatrixEvent,
type Room as MatrixRoom,
type RoomState,
RoomStateEvent,
UNSTABLE_ELEMENT_FUNCTIONAL_USERS,
} from "matrix-js-sdk";
import {
mockLiveTimeline,
mockMatrixRoom,
testScope,
withTestScheduler,
} from "../../utils/test";
import { createFunctionalMembers$ } from "./FunctionalMembers";
describe("createFunctionalMembers$", () => {
let room: MatrixRoom;
let serviceMembers: string[] | undefined;
beforeEach(() => {
serviceMembers = undefined;
room = mockMatrixRoom({
getLiveTimeline: mockLiveTimeline((type, stateKey) =>
serviceMembers !== undefined &&
UNSTABLE_ELEMENT_FUNCTIONAL_USERS.matches(type) &&
stateKey === ""
? ({
getContent: () => ({ service_members: serviceMembers }),
} as Partial<MatrixEvent> as MatrixEvent)
: null,
),
});
});
function emitStateEvent(type: string): void {
room.emit(
RoomStateEvent.Events,
{ getType: () => type } as unknown as MatrixEvent,
{} as RoomState,
null,
);
}
it("is empty without a functional members event", () => {
withTestScheduler(({ expectObservable }) => {
expectObservable(createFunctionalMembers$(testScope(), room)).toBe("a", {
a: new Set(),
});
});
});
it("tracks the functional members event", () => {
withTestScheduler(({ expectObservable, schedule }) => {
serviceMembers = ["@bot:example.com"];
const functional$ = createFunctionalMembers$(testScope(), room);
schedule("-a", {
a: () => {
serviceMembers = ["@bot:example.com", "@bot2:example.com"];
emitStateEvent(UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name);
},
});
expectObservable(functional$).toBe("ab", {
a: new Set(["@bot:example.com"]),
b: new Set(["@bot:example.com", "@bot2:example.com"]),
});
});
});
it("ignores unrelated state events", () => {
withTestScheduler(({ expectObservable, schedule }) => {
const functional$ = createFunctionalMembers$(testScope(), room);
schedule("-a", { a: () => emitStateEvent("m.room.topic") });
expectObservable(functional$).toBe("a", { a: new Set() });
});
});
it("drops entries that are not user IDs", () => {
withTestScheduler(({ expectObservable }) => {
serviceMembers = ["@bot:example.com", 42 as unknown as string];
expectObservable(createFunctionalMembers$(testScope(), room)).toBe("a", {
a: new Set(["@bot:example.com"]),
});
});
});
});
@@ -0,0 +1,55 @@
/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
EventTimeline,
type MatrixEvent,
type Room as MatrixRoom,
RoomStateEvent,
UNSTABLE_ELEMENT_FUNCTIONAL_USERS,
} from "matrix-js-sdk";
import { filter, fromEvent, map } from "rxjs";
import { type Behavior, type ObservableScope } from "@element-hq/matrixrtc-sdk";
/**
* The user IDs that the room marks as functional members (bots and other
* service accounts). They should not be treated as participants when deciding who
* to ring or whether the room is a DM.
*/
export function createFunctionalMembers$(
scope: ObservableScope,
matrixRoom: Pick<MatrixRoom, "getLiveTimeline" | "on" | "off">,
): Behavior<Set<string>> {
return scope.behavior(
fromEvent(
matrixRoom,
RoomStateEvent.Events,
(event: MatrixEvent) => event,
).pipe(
filter((event) =>
UNSTABLE_ELEMENT_FUNCTIONAL_USERS.matches(event.getType()),
),
map(() => functionalMembers(matrixRoom)),
),
functionalMembers(matrixRoom),
);
}
function functionalMembers(
matrixRoom: Pick<MatrixRoom, "getLiveTimeline">,
): Set<string> {
const serviceMembers: unknown = matrixRoom
.getLiveTimeline()
.getState(EventTimeline.FORWARDS)
?.getStateEvents(UNSTABLE_ELEMENT_FUNCTIONAL_USERS.name, "")
?.getContent().service_members;
return new Set(
Array.isArray(serviceMembers)
? serviceMembers.filter((id): id is string => typeof id === "string")
: [],
);
}
+13 -23
View File
@@ -5,13 +5,10 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { test, expect } from "vitest";
import { test, expect, vi } from "vitest";
import { act, render, screen } from "@testing-library/react";
import { axe } from "vitest-axe";
import {
RTC_CALL_INTENT_AUDIO,
type MatrixRTCSession,
} from "matrix-js-sdk/lib/matrixrtc";
import { RTC_CALL_INTENT_AUDIO } from "matrix-js-sdk/lib/matrixrtc";
import { BehaviorSubject } from "rxjs";
import { GridTile } from "./GridTile";
@@ -36,25 +33,18 @@ global.IntersectionObserver = class MockIntersectionObserver {
public disconnect(): void {}
} as unknown as typeof IntersectionObserver;
const fakeRtcSession = {
on: () => {},
off: () => {},
room: {
on: () => {},
off: () => {},
client: {
getUserId: () => null,
getDeviceId: () => null,
on: () => {},
off: () => {},
},
},
memberships: [],
} as unknown as MatrixRTCSession;
const fakeRoom = { roomId: "!room:example.org" };
const fakeClient = {
getUserId: () => null,
getDeviceId: () => null,
sendEvent: vi.fn(),
redactEvent: vi.fn(),
};
const callVm = {
reactions$: constant({}),
handsRaised$: constant({}),
localMember$: constant(null),
} as Partial<CallViewModel> as CallViewModel;
test("GridTile displays remote media", async () => {
@@ -68,7 +58,7 @@ test("GridTile displays remote media", async () => {
);
const { container } = render(
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
@@ -99,7 +89,7 @@ test("GridTile displays local media", async () => {
);
const { container } = render(
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
@@ -132,7 +122,7 @@ test("GridTile displays ringing media", async () => {
});
const { container } = render(
<ReactionsSenderProvider vm={callVm} rtcSession={fakeRtcSession}>
<ReactionsSenderProvider vm={callVm} room={fakeRoom} client={fakeClient}>
<GridTile
vm={new GridTileViewModel(constant(vm))}
onOpenProfile={() => {}}
+10 -3
View File
@@ -102,31 +102,36 @@ export function useCallViewKeyboardShortcuts(
"keydown",
useCallback(
(event: KeyboardEvent) => {
logger.info("Keydown event", event);
if (focusIsClaimed()) return;
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
return;
if (event.key === "m") {
event.preventDefault();
logger.info("Keyboard shortcut: toggle audio");
toggleAudio?.();
} else if (event.key === "v") {
event.preventDefault();
logger.info("Keyboard shortcut: toggle video");
toggleVideo?.();
} else if (event.key === " " && mayReceiveSpaceKeyEvents()) {
event.preventDefault();
if (!spacebarHeld.current) {
spacebarHeld.current = true;
logger.info("Keyboard shortcut: push to talk pressed");
setAudioEnabled?.(true);
}
} else if (event.key === "h") {
event.preventDefault();
logger.info("Keyboard shortcut: toggle raised hand");
toggleHandRaised?.();
} else if (KeyToReactionMap[event.key]) {
event.preventDefault();
sendReaction?.(KeyToReactionMap[event.key]);
const reaction = KeyToReactionMap[event.key];
logger.info(`Keyboard shortcut: send reaction ${reaction.name}`);
sendReaction?.(reaction);
} else if (event.key === "Escape") {
logger.info("Escape key pressed, triggering onBackButtonPressed");
logger.info("Keyboard shortcut: back");
window.controls.onBackButtonPressed?.();
}
},
@@ -151,6 +156,8 @@ export function useCallViewKeyboardShortcuts(
(event: KeyboardEvent) => {
if (focusIsClaimed() || !mayReceiveSpaceKeyEvents()) return;
if (event.key === " ") {
if (spacebarHeld.current)
logger.info("Keyboard shortcut: push to talk released");
spacebarHeld.current = false;
setAudioEnabled?.(false);
}
+1 -21
View File
@@ -35,26 +35,6 @@ export function useEventTarget<T extends Event>(
}, [target, eventType, listener, options]);
}
/**
* Shortcut for registering a listener on a TypedEventEmitter.
*/
export function useTypedEventEmitter<
Events extends string,
Arguments extends ListenerMap<Events>,
T extends Events,
>(
emitter: TypedEventEmitter<Events, Arguments>,
eventType: T,
listener: Listener<Events, Arguments, T>,
): void {
useEffect(() => {
emitter.on(eventType, listener);
return (): void => {
emitter.off(eventType, listener);
};
}, [emitter, eventType, listener]);
}
/**
* Reactively tracks a value which is recalculated whenever the provided event
* emitter emits an event. This is useful for bridging state from matrix-js-sdk
@@ -66,7 +46,7 @@ export function useTypedEventEmitterState<
T extends Events,
State,
>(
emitter: TypedEventEmitter<Events, Arguments>,
emitter: Pick<TypedEventEmitter<Events, Arguments>, "on" | "off">,
eventType: T,
getState: () => State,
): State {
-25
View File
@@ -1,25 +0,0 @@
/*
Copyright 2023, 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import {
type CallMembership,
type MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/lib/matrixrtc";
import { useCallback } from "react";
import { useTypedEventEmitterState } from "./useEvents";
export function useMatrixRTCSessionMemberships(
rtcSession: MatrixRTCSession,
): CallMembership[] {
return useTypedEventEmitterState(
rtcSession,
MatrixRTCSessionEvent.MembershipsChanged,
useCallback(() => rtcSession.memberships, [rtcSession]),
);
}
+21 -15
View File
@@ -7,6 +7,7 @@ Please see LICENSE in the repository root for full details.
import { describe, expect, test } from "vitest";
import { ConnectionError } from "livekit-client";
import { UnsupportedStickyEventsEndpointError } from "matrix-js-sdk";
import {
ErrorCode as RTCErrorCode,
FailToGetOpenIdToken as RTCFailToGetOpenIdToken,
@@ -32,10 +33,10 @@ import {
InsufficientCapacityError,
LivekitConnectionError,
MatrixRTCTransportMissingError,
MembershipManagerError,
NoMatrix2AuthorizationService,
PeerConnectionTimeoutError,
SFURoomCreationRestrictedError,
StickyEventsRequiredError,
UnknownCallError,
} from "./errors";
@@ -43,17 +44,6 @@ import {
// rather than the global one, so each needs to come out translated rather than
// as a raw key.
describe("localised errors", () => {
test("MembershipManagerError describes the failure and keeps its cause", () => {
const cause = new Error("the underlying problem");
const error = new MembershipManagerError(cause);
expect(error.code).toBe(ErrorCode.INTERNAL_MEMBERSHIP_MANAGER);
expect(error.category).toBe(ErrorCategory.SYSTEM_FAILURE);
expect(error.localisedTitle).not.toContain("error.");
expect(error.localisedMessage).not.toContain("error.");
expect(error.cause).toBe(cause);
});
test("NoMatrix2AuthorizationService is a configuration problem", () => {
const cause = new Error("404");
const error = new NoMatrix2AuthorizationService(cause);
@@ -100,8 +90,22 @@ describe("fromMatrixRTCError", () => {
{
name: "membership manager failure",
input: new RTCMembershipManagerError(cause),
expected: MembershipManagerError,
code: ErrorCode.INTERNAL_MEMBERSHIP_MANAGER,
expected: ConnectionLostError,
code: ErrorCode.CONNECTION_LOST_ERROR,
},
{
name: "membership manager stopped by missing sticky events",
// The membership manager's scheduler wraps the original error
input: new RTCMembershipManagerError(
new Error("The MembershipManager shut down", {
cause: new UnsupportedStickyEventsEndpointError(
"Server does not support the sticky events",
"sendStickyEvent",
),
}),
),
expected: StickyEventsRequiredError,
code: ErrorCode.STICKY_EVENTS_NOT_SUPPORTED,
},
{
name: "OpenID token failure",
@@ -169,7 +173,9 @@ describe("fromMatrixRTCError", () => {
});
test("keeps the SDK error's cause for reporting", () => {
const error = fromMatrixRTCError(new RTCMembershipManagerError(cause));
const error = fromMatrixRTCError(
new RTCNoMatrix2AuthorizationService(cause),
);
expect(error.cause).toBe(cause);
});

Some files were not shown because too many files have changed in this diff Show More