From d3a4234f4a33e8d0c26b95b9c8aeb7c529c15c7f Mon Sep 17 00:00:00 2001 From: fkwp Date: Fri, 6 Feb 2026 11:54:22 +0100 Subject: [PATCH 01/28] setup webhooks for SFUs --- backend/dev_livekit-othersite.yaml | 4 ++++ backend/dev_livekit.yaml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/backend/dev_livekit-othersite.yaml b/backend/dev_livekit-othersite.yaml index 0ae98c24..53fc9ce9 100644 --- a/backend/dev_livekit-othersite.yaml +++ b/backend/dev_livekit-othersite.yaml @@ -18,3 +18,7 @@ keys: devkey: secret room: auto_create: false +webhook: + api_key: devkey + urls: + - https://matrix-rtc.othersite.m.localhost/livekit/jwt/sfu_webhook diff --git a/backend/dev_livekit.yaml b/backend/dev_livekit.yaml index 157e4d04..6cef4241 100644 --- a/backend/dev_livekit.yaml +++ b/backend/dev_livekit.yaml @@ -18,3 +18,7 @@ keys: devkey: secret room: auto_create: false +webhook: + api_key: devkey + urls: + - https://matrix-rtc.m.localhost/livekit/jwt/sfu_webhook From 7b1eb03e2a8ebd4ffa81338ee9ad7a6628205b5e Mon Sep 17 00:00:00 2001 From: fkwp Date: Fri, 6 Feb 2026 11:54:39 +0100 Subject: [PATCH 02/28] add ca certs to SFU containers such that webhooks are accepted --- dev-backend-docker-compose.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dev-backend-docker-compose.yml b/dev-backend-docker-compose.yml index 28682a33..31694c01 100644 --- a/dev-backend-docker-compose.yml +++ b/dev-backend-docker-compose.yml @@ -62,7 +62,10 @@ services: - 7882:7882/tcp - 50100-50200:50100-50200/udp volumes: + - ./backend/dev_tls_m.localhost.crt:/local_cert.pem:Z - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z + environment: + - SSL_CERT_FILE=/local_cert.pem networks: - ecbackend @@ -82,7 +85,10 @@ services: - 17882:17882/tcp - 50300-50400:50300-50400/udp volumes: + - ./backend/dev_tls_m.localhost.crt:/local_cert.pem:Z - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z + environment: + - SSL_CERT_FILE=/local_cert.pem networks: - ecbackend @@ -164,6 +170,8 @@ services: - "8448:8448" extra_hosts: - "host.docker.internal:host-gateway" + - "auth-server:127.0.0.1" + - "auth-server-1:127.0.0.1" depends_on: - synapse networks: From 9e2eef09d413cd4a6f5f6d34e6f6f9a9cd9ec5c7 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 15:18:50 +0200 Subject: [PATCH 03/28] Add support for a grace period for /sync (aka homeserver disconnected) interruptions --- config/config.sample.json | 1 + src/config/ConfigOptions.ts | 8 ++++++++ src/state/CallViewModel/CallViewModel.ts | 2 ++ .../localMember/HomeserverConnected.test.ts | 19 ++++++++++--------- .../localMember/HomeserverConnected.ts | 14 ++++++++++++-- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/config/config.sample.json b/config/config.sample.json index 126d7626..5fc63667 100644 --- a/config/config.sample.json +++ b/config/config.sample.json @@ -12,6 +12,7 @@ "feature_use_device_session_member_events": true }, "ssla": "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", + "sync_disconnect_grace_period_ms": 60000, "matrix_rtc_session": { "wait_for_key_rotation_ms": 3000, "membership_event_expiry_ms": 180000000, diff --git a/src/config/ConfigOptions.ts b/src/config/ConfigOptions.ts index 1b120546..d5839fb3 100644 --- a/src/config/ConfigOptions.ts +++ b/src/config/ConfigOptions.ts @@ -97,6 +97,13 @@ export interface ConfigOptions { enable_video?: boolean; }; + /** + * Grace period in milliseconds to wait before reporting the sync loop as disconnected. + * This allows brief sync interruptions without triggering a reconnection message. + * Default is 60000ms (60 seconds). Set to 0 to disable the grace period. + */ + sync_disconnect_grace_period_ms?: number; + /** * These are low level options that are used to configure the MatrixRTC session. * Take care when changing these options. @@ -168,5 +175,6 @@ export const DEFAULT_CONFIG: ResolvedConfigOptions = { features: { feature_use_device_session_member_events: true, }, + sync_disconnect_grace_period_ms: 60000, ssla: "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", }; diff --git a/src/state/CallViewModel/CallViewModel.ts b/src/state/CallViewModel/CallViewModel.ts index e298bcfd..8677f2d0 100644 --- a/src/state/CallViewModel/CallViewModel.ts +++ b/src/state/CallViewModel/CallViewModel.ts @@ -83,6 +83,7 @@ import { E2eeType } from "../../e2ee/e2eeType"; import { MatrixKeyProvider } from "../../e2ee/matrixKeyProvider"; import { type MuteStates } from "../MuteStates"; import { getUrlParams, HeaderStyle } from "../../UrlParams"; +import { Config } from "../../config/Config"; import { type ProcessorState } from "../../livekit/TrackProcessorContext"; import { ElementWidgetActions, widget } from "../../widget"; import { @@ -536,6 +537,7 @@ export function createCallViewModel$( scope, client, matrixRTCSession, + Config.get().sync_disconnect_grace_period_ms, ), muteStates, joinMatrixRTC: (transport: LivekitTransportConfig) => { diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 87ca35d0..e8861641 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -96,19 +96,20 @@ describe("createHomeserverConnected$", () => { // LLM generated test cases. They are a bit overkill but I improved the mocking so it is // easy enough to read them so I think they can stay. + // Note: gracePeriodMs is set to 0 to avoid debouncing delays in tests it("is false when sync state is not Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); expect(hsConnected.combined$.value).toBe(false); }); it("remains false while membership status is not Connected even if sync is Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(false); // membership still disconnected }); it("is false when membership status transitions to Connected but ProbablyLeft is true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Make sync loop OK client.setSyncState(SyncState.Syncing); // Indicate probable leave before connection @@ -118,7 +119,7 @@ describe("createHomeserverConnected$", () => { }); it("becomes true only when all three conditions are satisfied", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // 1. Sync loop connected client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(false); // not yet membership connected @@ -128,7 +129,7 @@ describe("createHomeserverConnected$", () => { }); it("drops back to false when sync loop leaves Syncing", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Reach connected state client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); @@ -140,7 +141,7 @@ describe("createHomeserverConnected$", () => { }); it("drops back to false when membership status becomes disconnected", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -150,7 +151,7 @@ describe("createHomeserverConnected$", () => { }); it("drops to false when ProbablyLeft is emitted after being true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -160,7 +161,7 @@ describe("createHomeserverConnected$", () => { }); it("recovers to true if ProbablyLeft becomes false again while other conditions remain true", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); client.setSyncState(SyncState.Syncing); session.setMembershipStatus(Status.Connected); expect(hsConnected.combined$.value).toBe(true); @@ -174,7 +175,7 @@ describe("createHomeserverConnected$", () => { }); it("composite sequence reflects each individual failure reason", () => { - const hsConnected = createHomeserverConnected$(scope, client, session); + const hsConnected = createHomeserverConnected$(scope, client, session, 0); // Initially false (sync error + disconnected + not probably left) expect(hsConnected.combined$.value).toBe(false); diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index c8bcd021..84da6780 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -12,9 +12,11 @@ import { type MatrixRTCSession, } from "matrix-js-sdk/lib/matrixrtc"; import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk"; -import { fromEvent, startWith, map, tap, type Observable } from "rxjs"; +import { fromEvent, startWith, map, tap, type Observable, debounceTime } from "rxjs"; import { logger as rootLogger } from "matrix-js-sdk/lib/logger"; +import { Config } from "../../../config/Config"; + import { type ObservableScope } from "../../ObservableScope"; import { type Behavior } from "../../Behavior"; import { and$ } from "../../../utils/observable"; @@ -35,21 +37,29 @@ export interface HomeserverConnected { * for the purposes of a MatrixRTC session. * * Becomes FALSE if ANY sub-condition is fulfilled: - * 1. Sync loop is not in SyncState.Syncing + * 1. Sync loop is not in SyncState.Syncing (after grace period) * 2. membershipStatus !== Status.Connected * 3. probablyLeft === true + * + * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. + * If not provided, uses the config value (default 60000ms). */ export function createHomeserverConnected$( scope: ObservableScope, client: NodeStyleEventEmitter & Pick, matrixRTCSession: NodeStyleEventEmitter & Pick, + gracePeriodMs?: number, ): HomeserverConnected { + // Get grace period from parameter or config (default 60000ms) + const graceMs = gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> ).pipe( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), + debounceTime(graceMs), ); const rtsSession$ = scope.behavior( From 9fccd49d7ddbf776b68bb72980ba0bb2b2026801 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:08:31 +0200 Subject: [PATCH 04/28] Improve the handling of the grace period for the home server connection to support both immediate and delayed emissions. --- .../localMember/HomeserverConnected.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index 84da6780..cad987c8 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -12,11 +12,20 @@ import { type MatrixRTCSession, } from "matrix-js-sdk/lib/matrixrtc"; import { ClientEvent, type MatrixClient, SyncState } from "matrix-js-sdk"; -import { fromEvent, startWith, map, tap, type Observable, debounceTime } from "rxjs"; +import { + fromEvent, + startWith, + map, + tap, + type Observable, + distinctUntilChanged, + switchMap, + of, + delay, +} from "rxjs"; import { logger as rootLogger } from "matrix-js-sdk/lib/logger"; import { Config } from "../../../config/Config"; - import { type ObservableScope } from "../../ObservableScope"; import { type Behavior } from "../../Behavior"; import { and$ } from "../../../utils/observable"; @@ -41,6 +50,9 @@ export interface HomeserverConnected { * 2. membershipStatus !== Status.Connected * 3. probablyLeft === true * + * @param scope - The observable scope for lifecycle management. + * @param client - The Matrix client to monitor sync state. + * @param matrixRTCSession - The RTC session to monitor membership. * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. * If not provided, uses the config value (default 60000ms). */ @@ -52,14 +64,24 @@ export function createHomeserverConnected$( gracePeriodMs?: number, ): HomeserverConnected { // Get grace period from parameter or config (default 60000ms) - const graceMs = gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + const graceMs = + gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> ).pipe( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), - debounceTime(graceMs), + distinctUntilChanged(), + switchMap((isSyncing) => +{ + if (isSyncing || graceMs <= 0) { + return of(isSyncing); // Sofortige Emission (Synchron) + } + return of(false).pipe(delay(graceMs)); // Verzögertes false + } ), + startWith(client.getSyncState() === SyncState.Syncing), + distinctUntilChanged(), ); const rtsSession$ = scope.behavior( From 032ec662e92b0cbc1f3ab89af5a47334af1119d3 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:08:49 +0200 Subject: [PATCH 05/28] bump lk-jwt-service --- dev-backend-docker-compose.yml | 350 ++++++++++++++++----------------- 1 file changed, 175 insertions(+), 175 deletions(-) diff --git a/dev-backend-docker-compose.yml b/dev-backend-docker-compose.yml index 36fc7f44..0a42dad5 100644 --- a/dev-backend-docker-compose.yml +++ b/dev-backend-docker-compose.yml @@ -1,175 +1,175 @@ -networks: - ecbackend: - -services: - auth-service: - image: ghcr.io/element-hq/lk-jwt-service:0.4.4 - pull_policy: always - hostname: auth-server - environment: - - LIVEKIT_JWT_PORT=6080 - - LIVEKIT_URL=wss://matrix-rtc.m.localhost/livekit/sfu - - LIVEKIT_KEY=devkey - - LIVEKIT_SECRET=secret - # If the configured homeserver runs on localhost, it'll probably be using - # a self-signed certificate - - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING - - LIVEKIT_FULL_ACCESS_HOMESERVERS=* - deploy: - restart_policy: - condition: on-failure - ports: - # HOST_PORT:CONTAINER_PORT - - 6080:6080 - networks: - - ecbackend - - auth-service-1: - image: ghcr.io/element-hq/lk-jwt-service:0.4.4 - pull_policy: always - hostname: auth-server-1 - environment: - - LIVEKIT_JWT_PORT=16080 - - LIVEKIT_URL=wss://matrix-rtc.othersite.m.localhost/livekit/sfu - - LIVEKIT_KEY=devkey - - LIVEKIT_SECRET=secret - # If the configured homeserver runs on localhost, it'll probably be using - # a self-signed certificate - - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING - - LIVEKIT_FULL_ACCESS_HOMESERVERS=* - deploy: - restart_policy: - condition: on-failure - ports: - # HOST_PORT:CONTAINER_PORT - - 16080:16080 - networks: - - ecbackend - - livekit: - image: livekit/livekit-server:v1.10.1 - pull_policy: always - hostname: livekit-sfu - command: --dev --config /etc/livekit.yaml - restart: unless-stopped - # The SFU seems to work far more reliably when we let it share the host - # network rather than opening specific ports (but why?? we're not missing - # any…) - ports: - # HOST_PORT:CONTAINER_PORT - - 7880:7880/tcp - - 7881:7881/tcp - - 7882:7882/tcp - - 50100-50200:50100-50200/udp - volumes: - - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z - networks: - - ecbackend - - livekit-1: - image: livekit/livekit-server:v1.10.1 - pull_policy: always - hostname: livekit-sfu-1 - command: --dev --config /etc/livekit.yaml - restart: unless-stopped - # The SFU seems to work far more reliably when we let it share the host - # network rather than opening specific ports (but why?? we're not missing - # any…) - ports: - # HOST_PORT:CONTAINER_PORT - - 17880:17880/tcp - - 17881:17881/tcp - - 17882:17882/tcp - - 50300-50400:50300-50400/udp - volumes: - - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z - networks: - - ecbackend - - synapse: - hostname: homeserver - image: ghcr.io/element-hq/synapse:latest - pull_policy: always - environment: - - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml - # Needed for rootless podman-compose such that the uid/gid mapping does - # fit local user uid. If the container runs as root (uid 0) it is fine as - # it actually maps to your non-root user on the host (e.g. 1000). - # Otherwise uid mapping will not match your non-root user. - - UID=0 - - GID=0 - volumes: - - ./backend/synapse_tmp:/data:Z - - ./backend/dev_homeserver.yaml:/data/cfg/homeserver.yaml:Z - networks: - - ecbackend - - synapse-1: - hostname: homeserver-1 - image: ghcr.io/element-hq/synapse:latest - pull_policy: always - environment: - - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml - # Needed for rootless podman-compose such that the uid/gid mapping does - # fit local user uid. If the container runs as root (uid 0) it is fine as - # it actually maps to your non-root user on the host (e.g. 1000). - # Otherwise uid mapping will not match your non-root user. - - UID=0 - - GID=0 - volumes: - - ./backend/synapse_tmp_othersite:/data:Z - - ./backend/dev_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z - networks: - - ecbackend - - element-web: - image: ghcr.io/element-hq/element-web:develop - pull_policy: always - volumes: - - ./backend/ew.test.config.json:/app/config.json:Z - environment: - ELEMENT_WEB_PORT: 8081 - ports: - - "8081:8081" - networks: - - ecbackend - - element-web-1: - image: ghcr.io/element-hq/element-web:develop - pull_policy: always - volumes: - - ./backend/ew.test.othersite.config.json:/app/config.json:Z - environment: - ELEMENT_WEB_PORT: 18081 - ports: - # HOST_PORT:CONTAINER_PORT - - "18081:18081" - networks: - - ecbackend - - nginx: - # see backend/dev_tls_setup for how to generate the tls certs - hostname: synapse.m.localhost - image: nginx:latest - pull_policy: always - volumes: - - ./backend/dev_nginx.conf:/etc/nginx/conf.d/default.conf:Z - - ./backend/dev_tls_m.localhost.key:/root/ssl/key.pem:Z - - ./backend/dev_tls_m.localhost.crt:/root/ssl/cert.pem:Z - ports: - # HOST_PORT:CONTAINER_PORT - - "443:443" - - "8008:80" - - "4443:443" - - "8448:8448" - extra_hosts: - - "host.docker.internal:host-gateway" - depends_on: - - synapse - networks: - ecbackend: - aliases: - - synapse.m.localhost - - synapse.othersite.m.localhost - - matrix-rtc.m.localhost - - matrix-rtc.othersite.m.localhost +networks: + ecbackend: + +services: + auth-service: + image: ghcr.io/element-hq/lk-jwt-service:pr_171 + pull_policy: always + hostname: auth-server + environment: + - LIVEKIT_JWT_PORT=6080 + - LIVEKIT_URL=wss://matrix-rtc.m.localhost/livekit/sfu + - LIVEKIT_KEY=devkey + - LIVEKIT_SECRET=secret + # If the configured homeserver runs on localhost, it'll probably be using + # a self-signed certificate + - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING + - LIVEKIT_FULL_ACCESS_HOMESERVERS=* + deploy: + restart_policy: + condition: on-failure + ports: + # HOST_PORT:CONTAINER_PORT + - 6080:6080 + networks: + - ecbackend + + auth-service-1: + image: ghcr.io/element-hq/lk-jwt-service:pr_171 + pull_policy: always + hostname: auth-server-1 + environment: + - LIVEKIT_JWT_PORT=16080 + - LIVEKIT_URL=wss://matrix-rtc.othersite.m.localhost/livekit/sfu + - LIVEKIT_KEY=devkey + - LIVEKIT_SECRET=secret + # If the configured homeserver runs on localhost, it'll probably be using + # a self-signed certificate + - LIVEKIT_INSECURE_SKIP_VERIFY_TLS=YES_I_KNOW_WHAT_I_AM_DOING + - LIVEKIT_FULL_ACCESS_HOMESERVERS=* + deploy: + restart_policy: + condition: on-failure + ports: + # HOST_PORT:CONTAINER_PORT + - 16080:16080 + networks: + - ecbackend + + livekit: + image: livekit/livekit-server:v1.10.1 + pull_policy: always + hostname: livekit-sfu + command: --dev --config /etc/livekit.yaml + restart: unless-stopped + # The SFU seems to work far more reliably when we let it share the host + # network rather than opening specific ports (but why?? we're not missing + # any…) + ports: + # HOST_PORT:CONTAINER_PORT + - 7880:7880/tcp + - 7881:7881/tcp + - 7882:7882/tcp + - 50100-50200:50100-50200/udp + volumes: + - ./backend/dev_livekit.yaml:/etc/livekit.yaml:Z + networks: + - ecbackend + + livekit-1: + image: livekit/livekit-server:v1.10.1 + pull_policy: always + hostname: livekit-sfu-1 + command: --dev --config /etc/livekit.yaml + restart: unless-stopped + # The SFU seems to work far more reliably when we let it share the host + # network rather than opening specific ports (but why?? we're not missing + # any…) + ports: + # HOST_PORT:CONTAINER_PORT + - 17880:17880/tcp + - 17881:17881/tcp + - 17882:17882/tcp + - 50300-50400:50300-50400/udp + volumes: + - ./backend/dev_livekit-othersite.yaml:/etc/livekit.yaml:Z + networks: + - ecbackend + + synapse: + hostname: homeserver + image: ghcr.io/element-hq/synapse:latest + pull_policy: always + environment: + - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml + # Needed for rootless podman-compose such that the uid/gid mapping does + # fit local user uid. If the container runs as root (uid 0) it is fine as + # it actually maps to your non-root user on the host (e.g. 1000). + # Otherwise uid mapping will not match your non-root user. + - UID=0 + - GID=0 + volumes: + - ./backend/synapse_tmp:/data:Z + - ./backend/dev_homeserver.yaml:/data/cfg/homeserver.yaml:Z + networks: + - ecbackend + + synapse-1: + hostname: homeserver-1 + image: ghcr.io/element-hq/synapse:latest + pull_policy: always + environment: + - SYNAPSE_CONFIG_PATH=/data/cfg/homeserver.yaml + # Needed for rootless podman-compose such that the uid/gid mapping does + # fit local user uid. If the container runs as root (uid 0) it is fine as + # it actually maps to your non-root user on the host (e.g. 1000). + # Otherwise uid mapping will not match your non-root user. + - UID=0 + - GID=0 + volumes: + - ./backend/synapse_tmp_othersite:/data:Z + - ./backend/dev_homeserver-othersite.yaml:/data/cfg/homeserver.yaml:Z + networks: + - ecbackend + + element-web: + image: ghcr.io/element-hq/element-web:develop + pull_policy: always + volumes: + - ./backend/ew.test.config.json:/app/config.json:Z + environment: + ELEMENT_WEB_PORT: 8081 + ports: + - "8081:8081" + networks: + - ecbackend + + element-web-1: + image: ghcr.io/element-hq/element-web:develop + pull_policy: always + volumes: + - ./backend/ew.test.othersite.config.json:/app/config.json:Z + environment: + ELEMENT_WEB_PORT: 18081 + ports: + # HOST_PORT:CONTAINER_PORT + - "18081:18081" + networks: + - ecbackend + + nginx: + # see backend/dev_tls_setup for how to generate the tls certs + hostname: synapse.m.localhost + image: nginx:latest + pull_policy: always + volumes: + - ./backend/dev_nginx.conf:/etc/nginx/conf.d/default.conf:Z + - ./backend/dev_tls_m.localhost.key:/root/ssl/key.pem:Z + - ./backend/dev_tls_m.localhost.crt:/root/ssl/cert.pem:Z + ports: + # HOST_PORT:CONTAINER_PORT + - "443:443" + - "8008:80" + - "4443:443" + - "8448:8448" + extra_hosts: + - "host.docker.internal:host-gateway" + depends_on: + - synapse + networks: + ecbackend: + aliases: + - synapse.m.localhost + - synapse.othersite.m.localhost + - matrix-rtc.m.localhost + - matrix-rtc.othersite.m.localhost From 283c606b9d35ad6c04c6c22442e922cb556f6140 Mon Sep 17 00:00:00 2001 From: fkwp Date: Wed, 22 Apr 2026 21:46:25 +0200 Subject: [PATCH 06/28] Add tests for grace period handling in createHomeserverConnected$ --- .../localMember/HomeserverConnected.test.ts | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index e8861641..5b759cd1 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ -import { describe, it, expect, beforeEach, afterEach } from "vitest"; +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; import { EventEmitter } from "events"; import { ClientEvent, SyncState } from "matrix-js-sdk"; import { MembershipManagerEvent, Status } from "matrix-js-sdk/lib/matrixrtc"; @@ -201,3 +201,88 @@ describe("createHomeserverConnected$", () => { expect(hsConnected.combined$.value).toBe(false); }); }); + +describe("createHomeserverConnected$ - Grace Period", () => { + let scope: ObservableScope; + let client: MockMatrixClient; + let session: MockMatrixRTCSession; + const GRACE_PERIOD = 5000; + + beforeEach(() => { + vi.useFakeTimers(); + scope = new ObservableScope(); + // Initialize with values that satisfy the "Connected" condition + client = new MockMatrixClient(SyncState.Syncing); + session = new MockMatrixRTCSession({ + membershipStatus: Status.Connected, + probablyLeft: false, + }); + }); + + afterEach(() => { + scope.end(); + vi.useRealTimers(); + }); + + it("respects gracePeriodMs: stays true during grace period and flips false after", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // Initial state: Everything is connected + expect(hsConnected.combined$.value).toBe(true); + + // 1. Sync loses connection -> should remain TRUE due to grace period + client.setSyncState(SyncState.Error); + expect(hsConnected.combined$.value).toBe(true); + + // 2. Fast forward time (just before expiration) + vi.advanceTimersByTime(GRACE_PERIOD - 1); + expect(hsConnected.combined$.value).toBe(true); + + // 3. Fast forward time (expiration) + vi.advanceTimersByTime(1); + expect(hsConnected.combined$.value).toBe(false); + }); + + it("recovers immediately if sync returns during grace period", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // Initial state: Connected + expect(hsConnected.combined$.value).toBe(true); + + // 1. Sync error occurs + client.setSyncState(SyncState.Error); + vi.advanceTimersByTime(GRACE_PERIOD / 2); + expect(hsConnected.combined$.value).toBe(true); + + // 2. Sync recovers BEFORE the grace period expires + client.setSyncState(SyncState.Syncing); + expect(hsConnected.combined$.value).toBe(true); + + // 3. Fast forward the remaining time -> should stay TRUE + vi.advanceTimersByTime(GRACE_PERIOD); + expect(hsConnected.combined$.value).toBe(true); + }); + + it("flips to true IMMEDIATELY even if a grace period was pending", () => { + const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + + session.setMembershipStatus(Status.Connected); + session.setProbablyLeft(false); + + // 1. Initial error: wait until it flips to false + client.setSyncState(SyncState.Error); + expect(hsConnected.combined$.value).toBe(true); + vi.advanceTimersByTime(GRACE_PERIOD + 1); + expect(hsConnected.combined$.value).toBe(false); + + // 2. Back to Syncing -> Must be TRUE immediately (synchronously) + client.setSyncState(SyncState.Syncing); + expect(hsConnected.combined$.value).toBe(true); + }); +}); \ No newline at end of file From c1f821ca0f1fe002deea4f7efc23e0e09da739fb Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 23 Apr 2026 15:33:00 +0200 Subject: [PATCH 07/28] Update default sync disconnect grace period to 10000ms in configuration and related functions --- src/config/ConfigOptions.ts | 4 ++-- src/state/CallViewModel/CallViewModel.ts | 2 -- src/state/CallViewModel/localMember/HomeserverConnected.ts | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/config/ConfigOptions.ts b/src/config/ConfigOptions.ts index d5839fb3..1781dc97 100644 --- a/src/config/ConfigOptions.ts +++ b/src/config/ConfigOptions.ts @@ -100,7 +100,7 @@ export interface ConfigOptions { /** * Grace period in milliseconds to wait before reporting the sync loop as disconnected. * This allows brief sync interruptions without triggering a reconnection message. - * Default is 60000ms (60 seconds). Set to 0 to disable the grace period. + * Default is 10000ms (10 seconds). Set to 0 to disable the grace period. */ sync_disconnect_grace_period_ms?: number; @@ -175,6 +175,6 @@ export const DEFAULT_CONFIG: ResolvedConfigOptions = { features: { feature_use_device_session_member_events: true, }, - sync_disconnect_grace_period_ms: 60000, + sync_disconnect_grace_period_ms: 10000, ssla: "https://static.element.io/legal/element-software-and-services-license-agreement-uk-1.pdf", }; diff --git a/src/state/CallViewModel/CallViewModel.ts b/src/state/CallViewModel/CallViewModel.ts index 8677f2d0..e298bcfd 100644 --- a/src/state/CallViewModel/CallViewModel.ts +++ b/src/state/CallViewModel/CallViewModel.ts @@ -83,7 +83,6 @@ import { E2eeType } from "../../e2ee/e2eeType"; import { MatrixKeyProvider } from "../../e2ee/matrixKeyProvider"; import { type MuteStates } from "../MuteStates"; import { getUrlParams, HeaderStyle } from "../../UrlParams"; -import { Config } from "../../config/Config"; import { type ProcessorState } from "../../livekit/TrackProcessorContext"; import { ElementWidgetActions, widget } from "../../widget"; import { @@ -537,7 +536,6 @@ export function createCallViewModel$( scope, client, matrixRTCSession, - Config.get().sync_disconnect_grace_period_ms, ), muteStates, joinMatrixRTC: (transport: LivekitTransportConfig) => { diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index cad987c8..d66d8657 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -54,7 +54,7 @@ export interface HomeserverConnected { * @param client - The Matrix client to monitor sync state. * @param matrixRTCSession - The RTC session to monitor membership. * @param gracePeriodMs - Grace period in milliseconds to wait before reporting sync disconnect. - * If not provided, uses the config value (default 60000ms). + * If not provided, uses the config value (default 10000ms). */ export function createHomeserverConnected$( scope: ObservableScope, @@ -63,9 +63,9 @@ export function createHomeserverConnected$( Pick, gracePeriodMs?: number, ): HomeserverConnected { - // Get grace period from parameter or config (default 60000ms) + // Get grace period from parameter or config (default 10000ms) const graceMs = - gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 60000; + gracePeriodMs ?? Config.get().sync_disconnect_grace_period_ms ?? 10000; const syncing$ = ( fromEvent(client, ClientEvent.Sync) as Observable<[SyncState]> From 0abc32b80ee5055fc4106f2baf9d48025116ede1 Mon Sep 17 00:00:00 2001 From: fkwp Date: Thu, 23 Apr 2026 15:47:40 +0200 Subject: [PATCH 08/28] prettier --- .../localMember/HomeserverConnected.test.ts | 23 +++++++++++++++---- .../localMember/HomeserverConnected.ts | 13 +++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts index 5b759cd1..96feeeb1 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.test.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.test.ts @@ -225,7 +225,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("respects gracePeriodMs: stays true during grace period and flips false after", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -247,7 +252,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("recovers immediately if sync returns during grace period", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -270,7 +280,12 @@ describe("createHomeserverConnected$ - Grace Period", () => { }); it("flips to true IMMEDIATELY even if a grace period was pending", () => { - const hsConnected = createHomeserverConnected$(scope, client, session, GRACE_PERIOD); + const hsConnected = createHomeserverConnected$( + scope, + client, + session, + GRACE_PERIOD, + ); session.setMembershipStatus(Status.Connected); session.setProbablyLeft(false); @@ -285,4 +300,4 @@ describe("createHomeserverConnected$ - Grace Period", () => { client.setSyncState(SyncState.Syncing); expect(hsConnected.combined$.value).toBe(true); }); -}); \ No newline at end of file +}); diff --git a/src/state/CallViewModel/localMember/HomeserverConnected.ts b/src/state/CallViewModel/localMember/HomeserverConnected.ts index d66d8657..ff25a250 100644 --- a/src/state/CallViewModel/localMember/HomeserverConnected.ts +++ b/src/state/CallViewModel/localMember/HomeserverConnected.ts @@ -73,13 +73,12 @@ export function createHomeserverConnected$( startWith([client.getSyncState()]), map(([state]) => state === SyncState.Syncing), distinctUntilChanged(), - switchMap((isSyncing) => -{ - if (isSyncing || graceMs <= 0) { - return of(isSyncing); // Sofortige Emission (Synchron) - } - return of(false).pipe(delay(graceMs)); // Verzögertes false - } ), + switchMap((isSyncing) => { + if (isSyncing || graceMs <= 0) { + return of(isSyncing); + } + return of(false).pipe(delay(graceMs)); + }), startWith(client.getSyncState() === SyncState.Syncing), distinctUntilChanged(), ); From 89281c6d702cb17c4b89edbdb1796c71df339935 Mon Sep 17 00:00:00 2001 From: Timo K Date: Mon, 27 Apr 2026 14:20:13 +0200 Subject: [PATCH 09/28] Refactor leveraging the fact, things blocking shortcuts are using react portals. --- src/room/InCallView.tsx | 1 - src/room/LobbyView.tsx | 6 ++++ src/useCallViewKeyboardShortcuts.ts | 52 ++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/room/InCallView.tsx b/src/room/InCallView.tsx index 4940f4d8..d1cfdf10 100644 --- a/src/room/InCallView.tsx +++ b/src/room/InCallView.tsx @@ -230,7 +230,6 @@ export const InCallView: FC = ({ // This function incorrectly assumes that there is a camera and microphone, which is not always the case. // TODO: Make sure that this module is resilient when it comes to camera/microphone availability! useCallViewKeyboardShortcuts( - containerRef1, toggleAudio, toggleVideo, setAudioEnabled, diff --git a/src/room/LobbyView.tsx b/src/room/LobbyView.tsx index 367dc8df..e2d92126 100644 --- a/src/room/LobbyView.tsx +++ b/src/room/LobbyView.tsx @@ -47,6 +47,7 @@ import { usePageTitle } from "../usePageTitle"; import { getValue } from "../utils/observable"; import { useBehavior } from "../useBehavior"; import { CallFooter } from "../components/CallFooter"; +import { useCallViewKeyboardShortcuts } from "../useCallViewKeyboardShortcuts"; interface Props { client: MatrixClient; @@ -91,6 +92,11 @@ export const LobbyView: FC = ({ const [settingsModalOpen, setSettingsModalOpen] = useState(false); const [settingsTab, setSettingsTab] = useState(defaultSettingsTab); + // This function incorrectly assumes that there is a camera and microphone, which is not always the case. + // TODO: Make sure that this module is resilient when it comes to camera/microphone availability! + // Next to the keyboard shortcuts, this is also responsible for catching escape key presses and forwarding the to mobile -> pip. + useCallViewKeyboardShortcuts(toggleAudio, toggleVideo, null, null, null); + const openSettings = useCallback( () => setSettingsModalOpen(true), [setSettingsModalOpen], diff --git a/src/useCallViewKeyboardShortcuts.ts b/src/useCallViewKeyboardShortcuts.ts index 3d9654be..7eb66717 100644 --- a/src/useCallViewKeyboardShortcuts.ts +++ b/src/useCallViewKeyboardShortcuts.ts @@ -6,6 +6,7 @@ Please see LICENSE in the repository root for full details. */ import { type RefObject, useCallback, useMemo, useRef } from "react"; +import { logger } from "matrix-js-sdk/lib/logger"; import { useEventTarget } from "./useEvents"; import { @@ -18,22 +19,46 @@ import { * Determines whether focus is in the same part of the tree as the given * element (specifically, if the element or an ancestor of it is focused). */ -const mayReceiveKeyEvents = (e: HTMLElement): boolean => { - const focusedElement = document.activeElement; - return focusedElement !== null && focusedElement.contains(e); +const mayReceiveKeyEvents = (): boolean => { + const root = document.getElementById("root"); + if (root === null) { + logger.warn( + "[mayReceiveKeyEvents] Root element not found, always allow keyboard shortcuts (m,v,esc...)", + ); + return true; + } + const focusElement = document.activeElement; + const nothingInFocus = focusElement === null; + const focusOnBody = focusElement === document.body; + const noPrimaryFocus = + nothingInFocus || root.contains(focusElement) || focusOnBody; + + // Only if we do not have a primary focus we allow keyboard shortcut events. + return noPrimaryFocus; }; const KeyToReactionMap: Record = Object.fromEntries( ReactionSet.slice(0, ReactionsRowSize).map((r, i) => [(i + 1).toString(), r]), ); +/** + * This hook sets up gloabl keyboard shortcuts. It will filter for keyboard presses that should be ignored due to user + * currently focussing on a modal. + * This is achieved by using the fact, that all modal inputs are outside the #root element and use react portals to get rendered. + * The following shortcuts are auspported (optional): + * @param toggleAudio - triggered on (m) + * @param toggleVideo - triggered on (v) + * @param setAudioEnabled - push to talk behavior controlled via (space) + * @param sendReaction - triggered on (1,2,3,...) + * @param toggleHandRaised - triggered on (h) + * Additionally this method listens to the (escape) key to trigger the onBackButtonPressed callback, which is used to navigate to pip in the native app. + */ export function useCallViewKeyboardShortcuts( - focusElement: RefObject, toggleAudio: (() => void) | null, toggleVideo: (() => void) | null, setAudioEnabled: ((enabled: boolean) => void) | null, - sendReaction: (reaction: ReactionOption) => void, - toggleHandRaised: () => void, + sendReaction: ((reaction: ReactionOption) => void) | null, + toggleHandRaised: (() => void) | null, ): void { const spacebarHeld = useRef(false); @@ -45,8 +70,8 @@ export function useCallViewKeyboardShortcuts( "keydown", useCallback( (event: KeyboardEvent) => { - if (focusElement.current === null) return; - if (!mayReceiveKeyEvents(focusElement.current)) return; + logger.info("Keydown event", event); + if (!mayReceiveKeyEvents()) return; if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return; @@ -64,16 +89,15 @@ export function useCallViewKeyboardShortcuts( } } else if (event.key === "h") { event.preventDefault(); - toggleHandRaised(); + toggleHandRaised?.(); } else if (KeyToReactionMap[event.key]) { event.preventDefault(); - sendReaction(KeyToReactionMap[event.key]); + sendReaction?.(KeyToReactionMap[event.key]); } else if (event.key === "Escape") { window.controls.onBackButtonPressed?.(); } }, [ - focusElement, toggleVideo, toggleAudio, setAudioEnabled, @@ -92,15 +116,13 @@ export function useCallViewKeyboardShortcuts( "keyup", useCallback( (event: KeyboardEvent) => { - if (focusElement.current === null) return; - if (!mayReceiveKeyEvents(focusElement.current)) return; - + if (!mayReceiveKeyEvents()) return; if (event.key === " ") { spacebarHeld.current = false; setAudioEnabled?.(false); } }, - [focusElement, setAudioEnabled], + [setAudioEnabled], ), ); From 6b1b316ce464ae37e9c32154af8d163364be143f Mon Sep 17 00:00:00 2001 From: Timo K Date: Mon, 27 Apr 2026 18:15:29 +0200 Subject: [PATCH 10/28] fix tests --- src/useCallViewKeyboardShortcuts.test.tsx | 26 ++++++++++++----------- src/useCallViewKeyboardShortcuts.ts | 15 ++++++++++--- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/useCallViewKeyboardShortcuts.test.tsx b/src/useCallViewKeyboardShortcuts.test.tsx index 5a327f83..b002c23e 100644 --- a/src/useCallViewKeyboardShortcuts.test.tsx +++ b/src/useCallViewKeyboardShortcuts.test.tsx @@ -6,7 +6,7 @@ Please see LICENSE in the repository root for full details. */ import { render } from "@testing-library/react"; -import { type FC, useRef, useState } from "react"; +import { type FC, useState } from "react"; import { expect, test, vi } from "vitest"; import { Button } from "@vector-im/compound-web"; import userEvent from "@testing-library/user-event"; @@ -39,9 +39,7 @@ const TestComponent: FC = ({ initialModalOpen = false, }) => { const [modalOpen, setModalOpen] = useState(initialModalOpen); - const ref = useRef(null); useCallViewKeyboardShortcuts( - ref, () => {}, () => {}, setAudioEnabled, @@ -49,8 +47,11 @@ const TestComponent: FC = ({ toggleHandRaised, ); return ( -
- + <> +
+ +
+ {/*// modal lives outside of the root*/} {modalOpen && ( = ({ )} -
+ ); }; @@ -164,12 +165,13 @@ test("unmuting happens in place of the default action", async () => { // container element that can be interactive and receive focus / keydown // events.