From 65b8acb47936e4b5b02deb15276e49811d78cd6a Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 5 Jun 2023 15:26:10 +0100 Subject: [PATCH 1/2] Add config to skip widget_build_url for DM rooms --- src/IConfigOptions.ts | 1 + src/LegacyCallHandler.tsx | 2 +- src/utils/WellKnownUtils.ts | 1 + src/widgets/ManagedHybrid.ts | 20 +++++++++++++++----- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/IConfigOptions.ts b/src/IConfigOptions.ts index c6913d2eb25..1fa873ec87a 100644 --- a/src/IConfigOptions.ts +++ b/src/IConfigOptions.ts @@ -106,6 +106,7 @@ export interface IConfigOptions { }; widget_build_url?: string; // url called to replace jitsi/call widget creation + widget_build_url_ignore_dm?: boolean; audio_stream_url?: string; jitsi?: { preferred_domain: string; diff --git a/src/LegacyCallHandler.tsx b/src/LegacyCallHandler.tsx index 3dc7fcc4835..eb9e2754344 100644 --- a/src/LegacyCallHandler.tsx +++ b/src/LegacyCallHandler.tsx @@ -961,7 +961,7 @@ export default class LegacyCallHandler extends EventEmitter { } // We might be using managed hybrid widgets - if (isManagedHybridWidgetEnabled()) { + if (isManagedHybridWidgetEnabled(roomId)) { await addManagedHybridWidget(roomId); return; } diff --git a/src/utils/WellKnownUtils.ts b/src/utils/WellKnownUtils.ts index 369de6c6b01..a17f721c469 100644 --- a/src/utils/WellKnownUtils.ts +++ b/src/utils/WellKnownUtils.ts @@ -26,6 +26,7 @@ const EMBEDDED_PAGES_WK_PROPERTY = "io.element.embedded_pages"; /* eslint-disable camelcase */ export interface ICallBehaviourWellKnown { widget_build_url?: string; + ignore_dm?: boolean; } export interface IE2EEWellKnown { diff --git a/src/widgets/ManagedHybrid.ts b/src/widgets/ManagedHybrid.ts index c820fee668b..21c432aa3b6 100644 --- a/src/widgets/ManagedHybrid.ts +++ b/src/widgets/ManagedHybrid.ts @@ -24,6 +24,7 @@ import { IStoredLayout, WidgetLayoutStore } from "../stores/widgets/WidgetLayout import WidgetEchoStore from "../stores/WidgetEchoStore"; import WidgetStore from "../stores/WidgetStore"; import SdkConfig from "../SdkConfig"; +import DMRoomMap from "../utils/DMRoomMap"; /* eslint-disable camelcase */ interface IManagedHybridWidgetData { @@ -33,16 +34,25 @@ interface IManagedHybridWidgetData { } /* eslint-enable camelcase */ -function getWidgetBuildUrl(): string | undefined { +function getWidgetBuildUrl(roomId: string): string | undefined { + const isDm = !!DMRoomMap.shared().getUserIdForRoomId(roomId); if (SdkConfig.get().widget_build_url) { + if (isDm && SdkConfig.get().widget_build_url_ignore_dm) { + return undefined; + } return SdkConfig.get().widget_build_url; } + + const wellKnown = getCallBehaviourWellKnown(MatrixClientPeg.get()); + if (isDm && wellKnown?.ignore_dm) { + return undefined; + } /* eslint-disable-next-line camelcase */ - return getCallBehaviourWellKnown(MatrixClientPeg.get())?.widget_build_url; + return wellKnown?.widget_build_url; } -export function isManagedHybridWidgetEnabled(): boolean { - return !!getWidgetBuildUrl(); +export function isManagedHybridWidgetEnabled(roomId: string): boolean { + return !!getWidgetBuildUrl(roomId); } export async function addManagedHybridWidget(roomId: string): Promise { @@ -60,7 +70,7 @@ export async function addManagedHybridWidget(roomId: string): Promise { // Get widget data /* eslint-disable-next-line camelcase */ - const widgetBuildUrl = getWidgetBuildUrl(); + const widgetBuildUrl = getWidgetBuildUrl(roomId); if (!widgetBuildUrl) { return; } From 876ba310170336a3d9b286b87b7fcc46ff2a703d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 6 Jun 2023 14:44:11 +0100 Subject: [PATCH 2/2] Add tests --- test/widgets/ManagedHybrid-test.ts | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/widgets/ManagedHybrid-test.ts diff --git a/test/widgets/ManagedHybrid-test.ts b/test/widgets/ManagedHybrid-test.ts new file mode 100644 index 00000000000..b91db09dc19 --- /dev/null +++ b/test/widgets/ManagedHybrid-test.ts @@ -0,0 +1,51 @@ +/* +Copyright 2023 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { isManagedHybridWidgetEnabled } from "../../src/widgets/ManagedHybrid"; +import DMRoomMap from "../../src/utils/DMRoomMap"; +import { stubClient } from "../test-utils"; +import SdkConfig from "../../src/SdkConfig"; + +describe("isManagedHybridWidgetEnabled", () => { + let dmRoomMap: DMRoomMap; + + beforeEach(() => { + stubClient(); + dmRoomMap = { + getUserIdForRoomId: jest.fn().mockReturnValue("@user:server"), + } as unknown as DMRoomMap; + DMRoomMap.setShared(dmRoomMap); + }); + + it("should return false if widget_build_url is unset", () => { + expect(isManagedHybridWidgetEnabled("!room:server")).toBeFalsy(); + }); + + it("should return true for DMs when widget_build_url_ignore_dm is unset", () => { + SdkConfig.put({ + widget_build_url: "https://url", + }); + expect(isManagedHybridWidgetEnabled("!room:server")).toBeTruthy(); + }); + + it("should return false for DMs when widget_build_url_ignore_dm is true", () => { + SdkConfig.put({ + widget_build_url: "https://url", + widget_build_url_ignore_dm: true, + }); + expect(isManagedHybridWidgetEnabled("!room:server")).toBeFalsy(); + }); +});