2022-09-09 14:08:17 +08:00
|
|
|
/*
|
2024-09-06 16:22:13 +08:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2022-09-09 14:08:17 +08:00
|
|
|
|
2024-09-06 16:22:13 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-09-09 14:08:17 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
|
|
|
import { createRoomWidgetClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { WidgetApi, MatrixCapabilities } from "matrix-widget-api";
|
|
|
|
|
|
|
|
import type { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import type { IWidgetApiRequest } from "matrix-widget-api";
|
|
|
|
import { LazyEventEmitter } from "./LazyEventEmitter";
|
2022-10-10 21:19:10 +08:00
|
|
|
import { getUrlParams } from "./UrlParams";
|
2023-07-10 20:36:07 +08:00
|
|
|
import { Config } from "./config/Config";
|
2022-09-09 14:08:17 +08:00
|
|
|
|
|
|
|
// Subset of the actions in matrix-react-sdk
|
|
|
|
export enum ElementWidgetActions {
|
|
|
|
JoinCall = "io.element.join",
|
|
|
|
HangupCall = "im.vector.hangup",
|
|
|
|
TileLayout = "io.element.tile_layout",
|
|
|
|
SpotlightLayout = "io.element.spotlight_layout",
|
2024-07-30 19:30:33 +08:00
|
|
|
// This can be sent as from or to widget
|
|
|
|
// fromWidget: updates the client about the current device mute state
|
|
|
|
// toWidget: the client requests a specific device mute configuration
|
|
|
|
// The reply will always be the resulting configuration
|
|
|
|
// It is possible to sent an empty configuration to retrieve the current values or
|
|
|
|
// just one of the fields to update that particular value
|
|
|
|
// An undefined field means that EC will keep the mute state as is.
|
|
|
|
// -> this will allow the client to only get the current state
|
|
|
|
//
|
|
|
|
// The data of the widget action request and the response are:
|
|
|
|
// {
|
|
|
|
// audio_enabled?: boolean,
|
|
|
|
// video_enabled?: boolean
|
|
|
|
// }
|
|
|
|
DeviceMute = "io.element.device_mute",
|
2022-09-09 14:08:17 +08:00
|
|
|
}
|
|
|
|
|
2023-06-24 02:17:51 +08:00
|
|
|
export interface JoinCallData {
|
|
|
|
audioInput: string | null;
|
|
|
|
videoInput: string | null;
|
|
|
|
}
|
|
|
|
|
2023-10-31 00:30:30 +08:00
|
|
|
export interface WidgetHelpers {
|
2022-09-09 14:08:17 +08:00
|
|
|
api: WidgetApi;
|
|
|
|
lazyActions: LazyEventEmitter;
|
|
|
|
client: Promise<MatrixClient>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A point of access to the widget API, if the app is running as a widget. This
|
|
|
|
* is declared and initialized on the top level because the widget messaging
|
|
|
|
* needs to be set up ASAP on load to ensure it doesn't miss any requests.
|
|
|
|
*/
|
2023-09-23 06:05:13 +08:00
|
|
|
export const widget = ((): WidgetHelpers | null => {
|
2022-09-09 14:08:17 +08:00
|
|
|
try {
|
2023-10-04 03:35:28 +08:00
|
|
|
const { widgetId, parentUrl } = getUrlParams();
|
2022-09-09 14:08:17 +08:00
|
|
|
|
|
|
|
if (widgetId && parentUrl) {
|
|
|
|
const parentOrigin = new URL(parentUrl).origin;
|
|
|
|
logger.info("Widget API is available");
|
|
|
|
const api = new WidgetApi(widgetId, parentOrigin);
|
|
|
|
api.requestCapability(MatrixCapabilities.AlwaysOnScreen);
|
|
|
|
|
|
|
|
// Set up the lazy action emitter, but only for select actions that we
|
|
|
|
// intend for the app to handle
|
|
|
|
const lazyActions = new LazyEventEmitter();
|
|
|
|
[
|
|
|
|
ElementWidgetActions.JoinCall,
|
|
|
|
ElementWidgetActions.HangupCall,
|
|
|
|
ElementWidgetActions.TileLayout,
|
|
|
|
ElementWidgetActions.SpotlightLayout,
|
2024-07-30 19:30:33 +08:00
|
|
|
ElementWidgetActions.DeviceMute,
|
2022-09-09 14:08:17 +08:00
|
|
|
].forEach((action) => {
|
|
|
|
api.on(`action:${action}`, (ev: CustomEvent<IWidgetApiRequest>) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
lazyActions.emit(action, ev);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now, initialize the matryoshka MatrixClient (so named because it routes
|
|
|
|
// all requests through the host client via the widget API)
|
|
|
|
// We need to do this now rather than later because it has capabilities to
|
|
|
|
// request, and is responsible for starting the transport (should it be?)
|
|
|
|
|
2023-06-06 03:52:05 +08:00
|
|
|
const {
|
|
|
|
roomId,
|
|
|
|
userId,
|
|
|
|
deviceId,
|
|
|
|
baseUrl,
|
|
|
|
e2eEnabled,
|
|
|
|
allowIceFallback,
|
2023-09-19 00:49:10 +08:00
|
|
|
} = getUrlParams();
|
2022-09-09 14:08:17 +08:00
|
|
|
if (!roomId) throw new Error("Room ID must be supplied");
|
|
|
|
if (!userId) throw new Error("User ID must be supplied");
|
|
|
|
if (!deviceId) throw new Error("Device ID must be supplied");
|
2022-10-17 21:30:22 +08:00
|
|
|
if (!baseUrl) throw new Error("Base URL must be supplied");
|
2022-09-09 14:08:17 +08:00
|
|
|
|
|
|
|
// These are all the event types the app uses
|
2024-06-19 22:41:52 +08:00
|
|
|
const sendRecvEvent = [
|
|
|
|
"org.matrix.rageshake_request",
|
|
|
|
EventType.CallEncryptionKeysPrefix,
|
2024-11-04 17:54:13 +08:00
|
|
|
EventType.Reaction,
|
|
|
|
EventType.RoomRedaction,
|
2024-06-19 22:41:52 +08:00
|
|
|
];
|
2024-07-05 03:59:48 +08:00
|
|
|
|
2022-09-09 14:08:17 +08:00
|
|
|
const sendState = [
|
2024-07-25 20:33:37 +08:00
|
|
|
userId, // Legacy call membership events
|
|
|
|
`_${userId}_${deviceId}`, // Session membership events
|
2024-08-02 23:15:54 +08:00
|
|
|
`${userId}_${deviceId}`, // The above with no leading underscore, for room versions whose auth rules allow it
|
2024-07-05 03:59:48 +08:00
|
|
|
].map((stateKey) => ({
|
|
|
|
eventType: EventType.GroupCallMemberPrefix,
|
|
|
|
stateKey,
|
|
|
|
}));
|
2022-09-09 14:08:17 +08:00
|
|
|
const receiveState = [
|
2024-07-05 03:57:45 +08:00
|
|
|
{ eventType: EventType.RoomCreate },
|
2022-09-09 14:08:17 +08:00
|
|
|
{ eventType: EventType.RoomMember },
|
2024-04-23 21:15:13 +08:00
|
|
|
{ eventType: EventType.RoomEncryption },
|
2022-09-09 14:08:17 +08:00
|
|
|
{ eventType: EventType.GroupCallMemberPrefix },
|
|
|
|
];
|
|
|
|
const sendRecvToDevice = [
|
|
|
|
EventType.CallInvite,
|
|
|
|
EventType.CallCandidates,
|
|
|
|
EventType.CallAnswer,
|
|
|
|
EventType.CallHangup,
|
|
|
|
EventType.CallReject,
|
|
|
|
EventType.CallSelectAnswer,
|
|
|
|
EventType.CallNegotiate,
|
|
|
|
EventType.CallSDPStreamMetadataChanged,
|
|
|
|
EventType.CallSDPStreamMetadataChangedPrefix,
|
|
|
|
EventType.CallReplaces,
|
|
|
|
];
|
|
|
|
|
|
|
|
const client = createRoomWidgetClient(
|
|
|
|
api,
|
|
|
|
{
|
2022-10-25 00:30:30 +08:00
|
|
|
sendEvent: sendRecvEvent,
|
|
|
|
receiveEvent: sendRecvEvent,
|
2022-09-09 14:08:17 +08:00
|
|
|
sendState,
|
|
|
|
receiveState,
|
|
|
|
sendToDevice: sendRecvToDevice,
|
|
|
|
receiveToDevice: sendRecvToDevice,
|
2023-09-12 19:51:06 +08:00
|
|
|
turnServers: false,
|
2024-08-01 23:41:47 +08:00
|
|
|
sendDelayedEvents: true,
|
|
|
|
updateDelayedEvents: true,
|
2022-09-09 14:08:17 +08:00
|
|
|
},
|
|
|
|
roomId,
|
|
|
|
{
|
2022-10-17 13:46:44 +08:00
|
|
|
baseUrl,
|
2022-09-09 14:08:17 +08:00
|
|
|
userId,
|
|
|
|
deviceId,
|
|
|
|
timelineSupport: true,
|
2023-03-01 21:30:25 +08:00
|
|
|
useE2eForGroupCall: e2eEnabled,
|
2023-06-06 03:52:05 +08:00
|
|
|
fallbackICEServerAllowed: allowIceFallback,
|
2023-10-11 22:42:04 +08:00
|
|
|
},
|
2024-03-13 21:58:21 +08:00
|
|
|
// ContentLoaded event will be sent as soon as the theme is set (see useTheme.ts)
|
|
|
|
false,
|
2022-09-09 14:08:17 +08:00
|
|
|
);
|
2023-07-10 20:36:07 +08:00
|
|
|
|
2024-09-10 15:49:35 +08:00
|
|
|
const clientPromise = async (): Promise<MatrixClient> => {
|
|
|
|
// Wait for the config file to be ready (we load very early on so it might not
|
|
|
|
// be otherwise)
|
|
|
|
await Config.init();
|
|
|
|
await client.startClient({ clientWellKnownPollPeriod: 60 * 10 });
|
|
|
|
return client;
|
|
|
|
};
|
|
|
|
|
|
|
|
return { api, lazyActions, client: clientPromise() };
|
2022-09-09 14:08:17 +08:00
|
|
|
} else {
|
2024-08-27 21:45:39 +08:00
|
|
|
if (import.meta.env.MODE !== "test")
|
|
|
|
logger.info("No widget API available");
|
2022-09-09 14:08:17 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn("Continuing without the widget API", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})();
|