2021-01-29 14:26:33 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-01-29 14:26:33 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-01-29 14:26:33 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type IWidget } from "matrix-widget-api";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type Room } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-01-29 14:26:33 +00:00
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
|
|
|
|
import { getCallBehaviourWellKnown } from "../utils/WellKnownUtils";
|
|
|
|
|
import WidgetUtils from "../utils/WidgetUtils";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type IStoredLayout, WidgetLayoutStore } from "../stores/widgets/WidgetLayoutStore";
|
2021-01-29 14:26:33 +00:00
|
|
|
import WidgetEchoStore from "../stores/WidgetEchoStore";
|
2025-02-05 13:25:06 +00:00
|
|
|
import WidgetStore, { type IApp } from "../stores/WidgetStore";
|
2021-01-29 16:26:31 +00:00
|
|
|
import SdkConfig from "../SdkConfig";
|
2024-07-15 10:08:34 +01:00
|
|
|
import { getJoinedNonFunctionalMembers } from "../utils/room/getJoinedNonFunctionalMembers";
|
2021-01-29 14:26:33 +00:00
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
|
interface IManagedHybridWidgetData {
|
|
|
|
|
widget_id: string;
|
|
|
|
|
widget: IWidget;
|
|
|
|
|
layout: IStoredLayout;
|
|
|
|
|
}
|
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
2024-07-15 10:08:34 +01:00
|
|
|
function getWidgetBuildUrl(room: Room): string | undefined {
|
|
|
|
|
const functionalMembers = getJoinedNonFunctionalMembers(room);
|
|
|
|
|
const isDm = functionalMembers.length === 2;
|
2021-01-29 16:26:31 +00:00
|
|
|
if (SdkConfig.get().widget_build_url) {
|
2023-06-06 15:07:51 +01:00
|
|
|
if (isDm && SdkConfig.get().widget_build_url_ignore_dm) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2021-01-29 16:26:31 +00:00
|
|
|
return SdkConfig.get().widget_build_url;
|
|
|
|
|
}
|
2023-06-06 15:07:51 +01:00
|
|
|
|
2023-06-21 17:29:44 +01:00
|
|
|
const wellKnown = getCallBehaviourWellKnown(MatrixClientPeg.safeGet());
|
2023-06-06 15:07:51 +01:00
|
|
|
if (isDm && wellKnown?.ignore_dm) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2021-01-29 14:26:33 +00:00
|
|
|
/* eslint-disable-next-line camelcase */
|
2023-06-06 15:07:51 +01:00
|
|
|
return wellKnown?.widget_build_url;
|
2021-01-29 16:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 10:08:34 +01:00
|
|
|
export function isManagedHybridWidgetEnabled(room: Room): boolean {
|
|
|
|
|
return !!getWidgetBuildUrl(room);
|
2021-01-29 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-15 10:08:34 +01:00
|
|
|
export async function addManagedHybridWidget(room: Room): Promise<void> {
|
2021-01-29 14:26:33 +00:00
|
|
|
// Check for permission
|
2024-07-15 10:08:34 +01:00
|
|
|
if (!WidgetUtils.canUserModifyWidgets(room.client, room.roomId)) {
|
|
|
|
|
logger.error(`User not allowed to modify widgets in ${room.roomId}`);
|
2021-01-29 14:26:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get widget data
|
|
|
|
|
/* eslint-disable-next-line camelcase */
|
2024-07-15 10:08:34 +01:00
|
|
|
const widgetBuildUrl = getWidgetBuildUrl(room);
|
2021-01-29 14:26:33 +00:00
|
|
|
if (!widgetBuildUrl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let widgetData: IManagedHybridWidgetData;
|
|
|
|
|
try {
|
2024-07-15 10:08:34 +01:00
|
|
|
const response = await fetch(`${widgetBuildUrl}?roomId=${room.roomId}`);
|
2021-01-29 14:26:33 +00:00
|
|
|
widgetData = await response.json();
|
|
|
|
|
} catch (e) {
|
2024-07-15 10:08:34 +01:00
|
|
|
logger.error(`Managed hybrid widget builder failed for room ${room.roomId}`, e);
|
2021-01-29 14:26:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!widgetData) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const { widget_id: widgetId, widget: widgetContent, layout } = widgetData;
|
|
|
|
|
|
|
|
|
|
// Ensure the widget is not already present in the room
|
2024-07-15 10:08:34 +01:00
|
|
|
let widgets = WidgetStore.instance.getApps(room.roomId);
|
|
|
|
|
const existing = widgets.some((w) => w.id === widgetId) || WidgetEchoStore.roomHasPendingWidgets(room.roomId, []);
|
2021-01-29 14:26:33 +00:00
|
|
|
if (existing) {
|
2024-07-15 10:08:34 +01:00
|
|
|
logger.error(`Managed hybrid widget already present in room ${room.roomId}`);
|
2021-01-29 14:26:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the widget
|
|
|
|
|
try {
|
2024-07-15 10:08:34 +01:00
|
|
|
await WidgetUtils.setRoomWidgetContent(room.client, room.roomId, widgetId, {
|
2023-09-11 11:39:30 +01:00
|
|
|
...widgetContent,
|
|
|
|
|
"io.element.managed_hybrid": true,
|
|
|
|
|
});
|
2021-01-29 14:26:33 +00:00
|
|
|
} catch (e) {
|
2024-07-15 10:08:34 +01:00
|
|
|
logger.error(`Unable to add managed hybrid widget in room ${room.roomId}`, e);
|
2021-01-29 14:26:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Move the widget into position
|
|
|
|
|
if (!WidgetLayoutStore.instance.canCopyLayoutToRoom(room)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-15 10:08:34 +01:00
|
|
|
widgets = WidgetStore.instance.getApps(room.roomId);
|
2021-01-29 14:26:33 +00:00
|
|
|
const installedWidget = widgets.find((w) => w.id === widgetId);
|
|
|
|
|
if (!installedWidget) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
WidgetLayoutStore.instance.moveToContainer(room, installedWidget, layout.container);
|
|
|
|
|
WidgetLayoutStore.instance.setContainerHeight(room, layout.container, layout.height);
|
|
|
|
|
WidgetLayoutStore.instance.copyLayoutToRoom(room);
|
|
|
|
|
}
|
2023-09-11 11:39:30 +01:00
|
|
|
|
|
|
|
|
export function isManagedHybridWidget(widget: IApp): boolean {
|
|
|
|
|
return !!widget["io.element.managed_hybrid"];
|
|
|
|
|
}
|