-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.ts
553 lines (485 loc) · 21.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
Copyright 2020-2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { KJUR } from "jsrsasign";
import {
type IOpenIDCredentials,
type IWidgetApiRequest,
type IWidgetApiRequestData,
type IWidgetApiResponseData,
VideoConferenceCapabilities,
WidgetApi,
type WidgetApiAction,
} from "matrix-widget-api";
import { logger } from "matrix-js-sdk/src/logger";
import type {
JitsiMeetExternalAPIConstructor,
ExternalAPIEventCallbacks,
JitsiMeetExternalAPI as _JitsiMeetExternalAPI,
LogEvent,
VideoMuteStatusChangedEvent,
ExternalAPIOptions as _ExternalAPIOptions,
Config as _Config,
InterfaceConfig as _InterfaceConfig,
} from "jitsi-meet";
import { ElementWidgetActions } from "../../stores/widgets/ElementWidgetActions";
import { type IConfigOptions } from "../../IConfigOptions";
import { SnakedObject } from "../../utils/SnakedObject";
import { ElementWidgetCapabilities } from "../../stores/widgets/ElementWidgetCapabilities";
import { getVectorConfig } from "../getconfig";
interface Config extends _Config {
// Jitsi's types are missing these fields
prejoinConfig?: {
enabled: boolean;
hideDisplayName?: boolean;
hideExtraJoinButtons?: string[];
};
toolbarButtons?: string[];
conferenceInfo?: {
alwaysVIsible?: string[];
autoHide?: string[];
};
disableSelfViewSettings?: boolean;
}
interface InterfaceConfig extends _InterfaceConfig {
// XXX: It is unclear whether this is a typo of TOOLBAR_BUTTONS or if its just really undocumented,
// either way it is missing in types, yet we try and use it
MAIN_TOOLBAR_BUTTONS?: string[];
}
interface ExternalAPIOptions extends _ExternalAPIOptions {
configOverwrite?: Config;
interfaceConfigOverwrite?: InterfaceConfig;
// Jitsi's types are missing these fields
lang?: string;
}
// We have to trick webpack into loading our CSS for us.
// eslint-disable-next-line @typescript-eslint/no-require-imports
require("./index.pcss");
const JITSI_OPENIDTOKEN_JWT_AUTH = "openidtoken-jwt";
// Dev note: we use raw JS without many dependencies to reduce bundle size.
// We do not need all of React to render a Jitsi conference.
declare let JitsiMeetExternalAPI: JitsiMeetExternalAPIConstructor;
let inConference = false;
// Jitsi params
let jitsiDomain: string;
let conferenceId: string;
let displayName: string;
let avatarUrl: string;
let userId: string;
let jitsiAuth: string;
let roomId: string;
let roomName: string;
let startAudioOnly: boolean;
let startWithAudioMuted: boolean | undefined;
let startWithVideoMuted: boolean | undefined;
let isVideoChannel: boolean;
let supportsScreensharing: boolean;
let language: string;
let widgetApi: WidgetApi | undefined;
let meetApi: _JitsiMeetExternalAPI | undefined;
let skipOurWelcomeScreen = false;
async function checkAudioVideoEnabled(): Promise<[audioEnabled: boolean, videoEnabled: boolean]> {
if (!meetApi) return [false, false];
const [audioEnabled, videoEnabled] = (await Promise.all([meetApi.isAudioMuted(), meetApi.isVideoMuted()])).map(
(muted) => !muted,
);
return [audioEnabled, videoEnabled];
}
const setupCompleted = (async (): Promise<string | void> => {
try {
// Queue a config.json lookup asap, so we can use it later on. We want this to be concurrent with
// other setup work and therefore do not block.
const configPromise = getVectorConfig();
// The widget's options are encoded into the fragment to avoid leaking info to the server.
const widgetQuery = new URLSearchParams(window.location.hash.substring(1));
// The widget spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
const realQuery = new URLSearchParams(window.location.search.substring(1));
const qsParam = (name: string, optional = false): string => {
const vals = widgetQuery.has(name) ? widgetQuery.getAll(name) : realQuery.getAll(name);
if (!optional && vals.length !== 1) {
throw new Error(`Expected singular ${name} in query string`);
}
return vals[0];
};
const parseBooleanOrUndefined = (value: string | undefined): boolean | undefined => {
if (value === undefined) {
return undefined;
}
return value === "true";
};
// If we have these params, expect a widget API to be available (ie. to be in an iframe
// inside a matrix client). Otherwise, assume we're on our own, eg. have been popped
// out into a browser.
const parentUrl = qsParam("parentUrl", true);
const widgetId = qsParam("widgetId", true);
const theme = qsParam("theme", true);
language = qsParam("language", true) ?? "en";
if (theme) {
document.body.classList.add(`theme-${theme.replace(" ", "_")}`);
}
// Set this up as early as possible because Element will be hitting it almost immediately.
let widgetApiReady: Promise<void> | undefined;
if (parentUrl && widgetId) {
const parentOrigin = new URL(qsParam("parentUrl")).origin;
widgetApi = new WidgetApi(qsParam("widgetId"), parentOrigin);
widgetApiReady = new Promise<void>((resolve) => widgetApi!.once("ready", resolve));
widgetApi.requestCapabilities(VideoConferenceCapabilities);
// jitsi cannot work in a popup if auth token is provided because widgetApi is not available there
// so check the token and request the 'requires_client' capability to hide the popup icon in the Element
if (qsParam("auth", true) === "openidtoken-jwt") {
widgetApi.requestCapability(ElementWidgetCapabilities.RequiresClient);
}
widgetApi.start();
const handleAction = (
action: WidgetApiAction,
handler: (request: IWidgetApiRequestData) => Promise<IWidgetApiResponseData | void>,
): void => {
widgetApi!.on(`action:${action}`, async (ev: CustomEvent<IWidgetApiRequest>) => {
ev.preventDefault();
await setupCompleted;
let response: IWidgetApiResponseData;
try {
response = (await handler(ev.detail.data)) ?? {};
} catch (e) {
if (e instanceof Error) {
response = { error: { message: e.message } };
} else {
throw e;
}
}
widgetApi!.transport.reply(ev.detail, response);
});
};
handleAction(ElementWidgetActions.JoinCall, async ({ audioInput, videoInput }) => {
void joinConference(audioInput as string | null, videoInput as string | null);
});
handleAction(ElementWidgetActions.HangupCall, async ({ force }) => {
if (force === true) {
meetApi?.dispose();
void notifyHangup();
meetApi = undefined;
closeConference();
} else {
meetApi?.executeCommand("hangup");
}
});
handleAction(ElementWidgetActions.DeviceMute, async (params) => {
if (!meetApi) return;
const [audioEnabled, videoEnabled] = await checkAudioVideoEnabled();
if (Object.keys(params).length === 0) {
// Handle query
return {
audio_enabled: audioEnabled,
video_enabled: videoEnabled,
};
}
if (params.audio_enabled !== audioEnabled) {
meetApi.executeCommand("toggleAudio");
}
if (params.video_enabled !== videoEnabled) {
meetApi.executeCommand("toggleVideo");
}
return params;
});
handleAction(ElementWidgetActions.TileLayout, async () => {
meetApi?.executeCommand("setTileView", true);
});
handleAction(ElementWidgetActions.SpotlightLayout, async () => {
meetApi?.executeCommand("setTileView", false);
});
handleAction(ElementWidgetActions.StartLiveStream, async ({ rtmpStreamKey }) => {
if (!meetApi) throw new Error("Conference not joined");
meetApi.executeCommand("startRecording", {
mode: "stream",
// this looks like it should be rtmpStreamKey but we may be on too old
// a version of jitsi meet
//rtmpStreamKey,
youtubeStreamKey: rtmpStreamKey,
});
});
} else {
logger.warn("No parent URL or no widget ID - assuming no widget API is available");
}
// Populate the Jitsi params now
jitsiDomain = qsParam("conferenceDomain");
conferenceId = qsParam("conferenceId");
displayName = qsParam("displayName", true);
avatarUrl = qsParam("avatarUrl", true); // http not mxc
userId = qsParam("userId");
jitsiAuth = qsParam("auth", true);
roomId = qsParam("roomId", true);
roomName = qsParam("roomName", true);
startAudioOnly = qsParam("isAudioOnly", true) === "true";
startWithAudioMuted = parseBooleanOrUndefined(qsParam("startWithAudioMuted", true));
startWithVideoMuted = parseBooleanOrUndefined(qsParam("startWithVideoMuted", true));
isVideoChannel = qsParam("isVideoChannel", true) === "true";
supportsScreensharing = qsParam("supportsScreensharing", true) === "true";
// We've reached the point where we have to wait for the config, so do that then parse it.
const instanceConfig = new SnakedObject<IConfigOptions>((await configPromise) ?? <IConfigOptions>{});
const jitsiConfig = instanceConfig.get("jitsi_widget");
if (jitsiConfig) {
skipOurWelcomeScreen = new SnakedObject(jitsiConfig).get("skip_built_in_welcome_screen") ?? false;
}
// Either reveal the prejoin screen, or skip straight to Jitsi depending on the config.
// We don't set up the call yet though as this might lead to failure without the widget API.
toggleConferenceVisibility(skipOurWelcomeScreen);
if (widgetApi) {
await widgetApiReady;
}
// Now that everything should be set up, skip to the Jitsi splash screen if needed
if (skipOurWelcomeScreen) {
skipToJitsiSplashScreen();
}
enableJoinButton(); // always enable the button
} catch (e) {
logger.error("Error setting up Jitsi widget", e);
document.getElementById("widgetActionContainer")!.innerText = "Failed to load Jitsi widget";
}
})();
function enableJoinButton(): void {
document.getElementById("joinButton")!.onclick = (): Promise<void> => joinConference();
}
function switchVisibleContainers(): void {
inConference = !inConference;
// Our welcome screen is managed by other code, so just don't switch to it ever
// if we're not supposed to.
if (!skipOurWelcomeScreen) {
toggleConferenceVisibility(inConference);
}
}
function toggleConferenceVisibility(inConference: boolean): void {
document.getElementById("jitsiContainer")!.style.visibility = inConference ? "unset" : "hidden";
document.getElementById("joinButtonContainer")!.style.visibility = inConference ? "hidden" : "unset";
}
function skipToJitsiSplashScreen(): void {
// really just a function alias for self-documenting code
void joinConference();
}
/**
* Create a JWT token for jitsi openidtoken-jwt auth
*
* See https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
*/
function createJWTToken(openIdToken: IOpenIDCredentials): string {
// Header
const header = { alg: "HS256", typ: "JWT" };
// Payload
const payload = {
// As per Jitsi token auth, `iss` needs to be set to something agreed between
// JWT generating side and Prosody config. Since we have no configuration for
// the widgets, we can't set one anywhere. Using the Jitsi domain here probably makes sense.
iss: jitsiDomain,
sub: jitsiDomain,
aud: `https://${jitsiDomain}`,
room: "*",
context: {
matrix: {
token: openIdToken.access_token,
room_id: roomId,
server_name: openIdToken.matrix_server_name,
},
user: {
avatar: avatarUrl,
name: displayName,
},
},
};
// Sign JWT
// The secret string here is irrelevant, we're only using the JWT
// to transport data to Prosody in the Jitsi stack.
return KJUR.jws.JWS.sign("HS256", JSON.stringify(header), JSON.stringify(payload), "notused");
}
async function notifyHangup(errorMessage?: string): Promise<void> {
if (widgetApi) {
// We send the hangup event before setAlwaysOnScreen, because the latter
// can cause the receiving side to instantly stop listening.
try {
await widgetApi.transport.send(ElementWidgetActions.HangupCall, { errorMessage });
} finally {
await widgetApi.setAlwaysOnScreen(false);
}
}
}
function closeConference(): void {
switchVisibleContainers();
document.getElementById("jitsiContainer")!.innerHTML = "";
if (skipOurWelcomeScreen) {
skipToJitsiSplashScreen();
}
}
// Converts from IETF language tags used by Element (en-US) to the format used
// by Jitsi (enUS)
function normalizeLanguage(language: string): string {
const [lang, variant] = language.replace("_", "-").split("-");
if (!variant || lang === variant) {
return lang;
}
return lang + variant.toUpperCase();
}
function mapLanguage(language: string): string {
// Element and Jitsi don't agree how to interpret en, so we go with Elements
// interpretation to stay consistent
switch (language) {
case "en":
return "enGB";
case "enUS":
return "en";
default:
return language;
}
}
// event handler bound in HTML
// An audio input of undefined instructs Jitsi to start unmuted with whatever
// audio input it can find, while an input of null instructs it to start muted,
// and a non-nullish input specifies the label of a specific device to use.
// Same for video inputs.
async function joinConference(audioInput?: string | null, videoInput?: string | null): Promise<void> {
let jwt: string | undefined;
if (jitsiAuth === JITSI_OPENIDTOKEN_JWT_AUTH) {
// See https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
const openIdToken = await widgetApi?.requestOpenIDConnectToken();
logger.log("Got OpenID Connect token");
if (!openIdToken?.access_token) {
// eslint-disable-line camelcase
// We've failing to get a token, don't try to init conference
logger.warn("Expected to have an OpenID credential, cannot initialize widget.");
document.getElementById("widgetActionContainer")!.innerText = "Failed to load Jitsi widget";
return;
}
jwt = createJWTToken(openIdToken);
}
switchVisibleContainers();
logger.warn(
"[Jitsi Widget] The next few errors about failing to parse URL parameters are fine if " +
"they mention 'external_api' or 'jitsi' in the stack. They're just Jitsi Meet trying to parse " +
"our fragment values and not recognizing the options.",
);
const options: ExternalAPIOptions = {
width: "100%",
height: "100%",
parentNode: document.querySelector("#jitsiContainer") ?? undefined,
roomName: conferenceId,
devices: {
audioInput,
videoInput,
},
userInfo: {
displayName,
email: userId,
},
interfaceConfigOverwrite: {
SHOW_JITSI_WATERMARK: false,
SHOW_WATERMARK_FOR_GUESTS: false,
MAIN_TOOLBAR_BUTTONS: [],
VIDEO_LAYOUT_FIT: "height",
},
configOverwrite: {
subject: roomName,
startAudioOnly,
startWithAudioMuted: audioInput === null ? true : startWithAudioMuted,
startWithVideoMuted: videoInput === null ? true : startWithVideoMuted,
// Request some log levels for inclusion in rageshakes
// Ideally we would capture all possible log levels, but this can
// cause Jitsi Meet to try to post various circular data structures
// back over the iframe API, and therefore end up crashing
// https://github.com/jitsi/jitsi-meet/issues/11585
apiLogLevels: ["warn", "error"],
} as any,
jwt: jwt,
lang: mapLanguage(normalizeLanguage(language)),
};
// Video channel widgets need some more tailored config options
if (isVideoChannel) {
// We don't skip jitsi's prejoin screen for video rooms.
options.configOverwrite!.prejoinConfig = { enabled: true };
// Use a simplified set of toolbar buttons
options.configOverwrite!.toolbarButtons = ["microphone", "camera", "tileview", "hangup"];
// Note: We can hide the screenshare button in video rooms but not in
// normal conference calls, since in video rooms we control exactly what
// set of controls appear, but in normal calls we need to leave that up
// to the deployment's configuration.
// https://github.com/element-hq/element-web/issues/4880#issuecomment-940002464
if (supportsScreensharing) options.configOverwrite!.toolbarButtons.splice(2, 0, "desktop");
// Hide all top bar elements
options.configOverwrite!.conferenceInfo = { autoHide: [] };
// Remove the ability to hide your own tile, since we're hiding the
// settings button which would be the only way to get it back
options.configOverwrite!.disableSelfViewSettings = true;
}
meetApi = new JitsiMeetExternalAPI(jitsiDomain, options);
// fires once when user joins the conference
// (regardless of video on or off)
meetApi.on("videoConferenceJoined", onVideoConferenceJoined);
meetApi.on("videoConferenceLeft", onVideoConferenceLeft);
meetApi.on("readyToClose", closeConference as ExternalAPIEventCallbacks["readyToClose"]);
meetApi.on("errorOccurred", onErrorOccurred);
meetApi.on("audioMuteStatusChanged", onMuteStatusChanged);
meetApi.on("videoMuteStatusChanged", onVideoMuteStatusChanged);
(["videoConferenceJoined", "participantJoined", "participantLeft"] as const).forEach((event) => {
meetApi!.on(event, updateParticipants);
});
// Patch logs into rageshakes
meetApi.on("log", onLog);
}
const onVideoConferenceJoined = (): void => {
// Although we set our displayName with the userInfo option above, that
// option has a bug where it causes the name to be the HTML encoding of
// what was actually intended. So, we use the displayName command to at
// least ensure that the name is correct after entering the meeting.
// https://github.com/jitsi/jitsi-meet/issues/11664
// We can't just use these commands immediately after creating the
// iframe, because there's *another* bug where they can crash Jitsi by
// racing with its startup process.
if (displayName) meetApi?.executeCommand("displayName", displayName);
// This doesn't have a userInfo equivalent, so has to be set via commands
if (avatarUrl) meetApi?.executeCommand("avatarUrl", avatarUrl);
if (widgetApi) {
// ignored promise because we don't care if it works
// noinspection JSIgnoredPromiseFromCall
void widgetApi.setAlwaysOnScreen(true);
void widgetApi.transport.send(ElementWidgetActions.JoinCall, {});
}
// Video rooms should start in tile mode
if (isVideoChannel) meetApi?.executeCommand("setTileView", true);
};
const onVideoConferenceLeft = (): void => {
void notifyHangup();
meetApi = undefined;
};
const onErrorOccurred = ({ error }: Parameters<ExternalAPIEventCallbacks["errorOccurred"]>[0]): void => {
if (error.isFatal) {
// We got disconnected. Since Jitsi Meet might send us back to the
// prejoin screen, we're forced to act as if we hung up entirely.
void notifyHangup(error.message);
meetApi = undefined;
closeConference();
}
};
const onMuteStatusChanged = async (): Promise<void> => {
if (!meetApi) return;
const [audioEnabled, videoEnabled] = await checkAudioVideoEnabled();
void widgetApi?.transport.send(ElementWidgetActions.DeviceMute, {
audio_enabled: audioEnabled,
video_enabled: videoEnabled,
});
};
const onVideoMuteStatusChanged = ({ muted }: VideoMuteStatusChangedEvent): void => {
if (muted) {
// Jitsi Meet always sends a "video muted" event directly before
// hanging up, which we need to ignore by padding the timeout here,
// otherwise the React SDK will mistakenly think the user turned off
// their video by hand
setTimeout(() => onMuteStatusChanged, 200);
} else {
void onMuteStatusChanged();
}
};
const updateParticipants = (): void => {
void widgetApi?.transport.send(ElementWidgetActions.CallParticipants, {
participants: meetApi?.getParticipantsInfo(),
});
};
const onLog = ({ logLevel, args }: LogEvent): void =>
(parent as unknown as typeof global).mx_rage_logger?.log(logLevel, ...args);