2022-07-28 04:14:05 +08:00
|
|
|
/*
|
2023-07-03 20:59:26 +08:00
|
|
|
Copyright 2022 - 2023 New Vector Ltd
|
2022-07-28 04:14:05 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-07-04 02:05:08 +08:00
|
|
|
import { useMemo } from "react";
|
2022-07-28 04:14:05 +08:00
|
|
|
import { useLocation } from "react-router-dom";
|
|
|
|
|
2023-07-03 20:59:26 +08:00
|
|
|
import { Config } from "./config/Config";
|
|
|
|
|
2023-08-10 21:19:43 +08:00
|
|
|
export const PASSWORD_STRING = "password=";
|
2023-08-04 19:39:59 +08:00
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
interface RoomIdentifier {
|
2022-07-28 04:14:05 +08:00
|
|
|
roomAlias: string | null;
|
|
|
|
roomId: string | null;
|
|
|
|
viaServers: string[];
|
2023-09-19 00:49:10 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 08:47:47 +08:00
|
|
|
// If you need to add a new flag to this interface, prefer a name that describes
|
|
|
|
// a specific behavior (such as 'confineToRoom'), rather than one that describes
|
|
|
|
// the situations that call for this behavior ('isEmbedded'). This makes it
|
|
|
|
// clearer what each flag means, and helps us avoid coupling Element Call's
|
|
|
|
// behavior to the needs of specific consumers.
|
2023-11-17 00:32:25 +08:00
|
|
|
export interface UrlParams {
|
2023-10-04 03:35:28 +08:00
|
|
|
// Widget api related params
|
|
|
|
widgetId: string | null;
|
|
|
|
parentUrl: string | null;
|
2023-09-19 00:49:10 +08:00
|
|
|
/**
|
|
|
|
* Anything about what room we're pointed to should be from useRoomIdentifier which
|
|
|
|
* parses the path and resolves alias with respect to the default server name, however
|
|
|
|
* roomId is an exception as we need the room ID in embedded (matroyska) mode, and not
|
|
|
|
* the room alias (or even the via params because we are not trying to join it). This
|
|
|
|
* is also not validated, where it is in useRoomIdentifier().
|
|
|
|
*/
|
|
|
|
roomId: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
2023-09-19 08:47:47 +08:00
|
|
|
* Whether the app should keep the user confined to the current call/room.
|
2022-12-10 03:25:02 +08:00
|
|
|
*/
|
2023-09-19 08:47:47 +08:00
|
|
|
confineToRoom: boolean;
|
|
|
|
/**
|
|
|
|
* Whether upon entering a room, the user should be prompted to launch the
|
|
|
|
* native mobile app. (Affects only Android and iOS.)
|
|
|
|
*/
|
|
|
|
appPrompt: boolean;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* Whether the app should pause before joining the call until it sees an
|
|
|
|
* io.element.join widget action, allowing it to be preloaded.
|
|
|
|
*/
|
2022-09-09 14:10:45 +08:00
|
|
|
preload: boolean;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* Whether to hide the room header when in a call.
|
|
|
|
*/
|
2022-09-09 14:04:53 +08:00
|
|
|
hideHeader: boolean;
|
2023-10-27 01:33:59 +08:00
|
|
|
/**
|
|
|
|
* Whether the controls should be shown. For screen recording no controls can be desired.
|
|
|
|
*/
|
|
|
|
showControls: boolean;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* Whether to hide the screen-sharing button.
|
|
|
|
*/
|
2022-10-14 22:17:50 +08:00
|
|
|
hideScreensharing: boolean;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* Whether to use end-to-end encryption.
|
|
|
|
*/
|
2022-07-28 04:14:05 +08:00
|
|
|
e2eEnabled: boolean;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The user's ID (only used in matryoshka mode).
|
|
|
|
*/
|
2022-07-28 04:14:05 +08:00
|
|
|
userId: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The display name to use for auto-registration.
|
|
|
|
*/
|
2022-07-28 04:14:05 +08:00
|
|
|
displayName: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The device's ID (only used in matryoshka mode).
|
|
|
|
*/
|
2022-07-28 04:14:05 +08:00
|
|
|
deviceId: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The base URL of the homeserver to use for media lookups in matryoshka mode.
|
|
|
|
*/
|
2022-10-17 13:46:44 +08:00
|
|
|
baseUrl: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The BCP 47 code of the language the app should use.
|
|
|
|
*/
|
2022-10-10 21:19:10 +08:00
|
|
|
lang: string | null;
|
2022-12-10 03:25:02 +08:00
|
|
|
/**
|
|
|
|
* The fonts which the interface should use, if not empty.
|
|
|
|
*/
|
|
|
|
fonts: string[];
|
|
|
|
/**
|
|
|
|
* The factor by which to scale the interface's font size.
|
|
|
|
*/
|
|
|
|
fontScale: number | null;
|
2022-12-19 19:16:59 +08:00
|
|
|
/**
|
|
|
|
* The Posthog analytics ID. It is only available if the user has given consent for sharing telemetry in element web.
|
|
|
|
*/
|
|
|
|
analyticsID: string | null;
|
2023-06-06 03:52:05 +08:00
|
|
|
/**
|
|
|
|
* Whether the app is allowed to use fallback STUN servers for ICE in case the
|
|
|
|
* user's homeserver doesn't provide any.
|
|
|
|
*/
|
|
|
|
allowIceFallback: boolean;
|
2023-08-04 19:39:59 +08:00
|
|
|
/**
|
|
|
|
* E2EE password
|
|
|
|
*/
|
|
|
|
password: string | null;
|
2023-10-17 00:45:06 +08:00
|
|
|
/**
|
|
|
|
* Whether we the app should use per participant keys for E2EE.
|
|
|
|
*/
|
|
|
|
perParticipantE2EE: boolean;
|
2023-10-25 19:49:18 +08:00
|
|
|
/**
|
|
|
|
* Setting this flag skips the lobby and brings you in the call directly.
|
|
|
|
* In the widget this can be combined with preload to pass the device settings
|
|
|
|
* with the join widget action.
|
|
|
|
*/
|
|
|
|
skipLobby: boolean;
|
2024-01-26 17:03:08 +08:00
|
|
|
/**
|
|
|
|
* Setting this flag makes element call show the lobby after leaving a call.
|
|
|
|
* This is useful for video rooms.
|
|
|
|
*/
|
|
|
|
returnToLobby: boolean;
|
2024-02-22 04:52:31 +08:00
|
|
|
/**
|
2024-02-22 23:15:46 +08:00
|
|
|
* The theme to use for element call.
|
|
|
|
* can be "light", "dark", "light-high-contrast" or "dark-high-contrast".
|
|
|
|
*/
|
|
|
|
theme: string | null;
|
|
|
|
/** This defines the homeserver that is going to be used when joining a room.
|
2024-02-22 04:52:31 +08:00
|
|
|
* It has to be set to a non default value for links to rooms
|
|
|
|
* that are not on the default homeserver,
|
|
|
|
* that is in use for the current user.
|
|
|
|
*/
|
|
|
|
viaServers: string | null;
|
|
|
|
/**
|
|
|
|
* This defines the homeserver that is going to be used when registering
|
|
|
|
* a new (guest) user.
|
|
|
|
* This can be user to configure a non default guest user server when
|
|
|
|
* creating a spa link.
|
|
|
|
*/
|
|
|
|
homeserver: string | null;
|
2022-07-28 04:14:05 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 08:47:47 +08:00
|
|
|
// This is here as a stopgap, but what would be far nicer is a function that
|
|
|
|
// takes a UrlParams and returns a query string. That would enable us to
|
|
|
|
// consolidate all the data about URL parameters and their meanings to this one
|
|
|
|
// file.
|
2023-09-18 05:48:03 +08:00
|
|
|
export function editFragmentQuery(
|
|
|
|
hash: string,
|
2023-10-11 22:42:04 +08:00
|
|
|
edit: (params: URLSearchParams) => URLSearchParams,
|
2023-09-18 05:48:03 +08:00
|
|
|
): string {
|
|
|
|
const fragmentQueryStart = hash.indexOf("?");
|
|
|
|
const fragmentParams = edit(
|
|
|
|
new URLSearchParams(
|
2023-10-11 22:42:04 +08:00
|
|
|
fragmentQueryStart === -1 ? "" : hash.substring(fragmentQueryStart),
|
|
|
|
),
|
2023-09-18 05:48:03 +08:00
|
|
|
);
|
|
|
|
return `${hash.substring(
|
|
|
|
0,
|
2023-10-11 22:42:04 +08:00
|
|
|
fragmentQueryStart,
|
2023-09-18 05:48:03 +08:00
|
|
|
)}?${fragmentParams.toString()}`;
|
|
|
|
}
|
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
class ParamParser {
|
|
|
|
private fragmentParams: URLSearchParams;
|
|
|
|
private queryParams: URLSearchParams;
|
|
|
|
|
2023-09-23 06:05:13 +08:00
|
|
|
public constructor(search: string, hash: string) {
|
2023-09-19 00:49:10 +08:00
|
|
|
this.queryParams = new URLSearchParams(search);
|
|
|
|
|
|
|
|
const fragmentQueryStart = hash.indexOf("?");
|
|
|
|
this.fragmentParams = new URLSearchParams(
|
2023-10-11 22:42:04 +08:00
|
|
|
fragmentQueryStart === -1 ? "" : hash.substring(fragmentQueryStart),
|
2023-09-19 00:49:10 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally, URL params should be encoded in the fragment so as to avoid
|
|
|
|
// leaking them to the server. However, we also check the normal query
|
|
|
|
// string for backwards compatibility with versions that only used that.
|
2023-09-23 06:05:13 +08:00
|
|
|
public getParam(name: string): string | null {
|
2023-09-19 00:49:10 +08:00
|
|
|
return this.fragmentParams.get(name) ?? this.queryParams.get(name);
|
|
|
|
}
|
|
|
|
|
2023-09-23 06:05:13 +08:00
|
|
|
public getAllParams(name: string): string[] {
|
2023-09-19 00:49:10 +08:00
|
|
|
return [
|
|
|
|
...this.fragmentParams.getAll(name),
|
|
|
|
...this.queryParams.getAll(name),
|
|
|
|
];
|
|
|
|
}
|
2023-09-19 19:11:39 +08:00
|
|
|
|
2023-09-23 06:05:13 +08:00
|
|
|
public getFlagParam(name: string, defaultValue = false): boolean {
|
2023-09-19 19:11:39 +08:00
|
|
|
const param = this.getParam(name);
|
|
|
|
return param === null ? defaultValue : param !== "false";
|
|
|
|
}
|
2023-09-19 00:49:10 +08:00
|
|
|
}
|
|
|
|
|
2022-07-28 04:14:05 +08:00
|
|
|
/**
|
2022-10-10 21:19:10 +08:00
|
|
|
* Gets the app parameters for the current URL.
|
2023-07-18 02:22:22 +08:00
|
|
|
* @param search The URL search string
|
|
|
|
* @param hash The URL hash
|
2022-10-10 21:19:10 +08:00
|
|
|
* @returns The app parameters encoded in the URL
|
2022-07-28 04:14:05 +08:00
|
|
|
*/
|
2022-10-10 21:19:10 +08:00
|
|
|
export const getUrlParams = (
|
2023-07-04 02:05:08 +08:00
|
|
|
search = window.location.search,
|
2023-10-11 22:42:04 +08:00
|
|
|
hash = window.location.hash,
|
2022-10-10 21:19:10 +08:00
|
|
|
): UrlParams => {
|
2023-09-19 00:49:10 +08:00
|
|
|
const parser = new ParamParser(search, hash);
|
|
|
|
|
|
|
|
const fontScale = parseFloat(parser.getParam("fontScale") ?? "");
|
|
|
|
|
|
|
|
return {
|
2023-10-04 03:35:28 +08:00
|
|
|
widgetId: parser.getParam("widgetId"),
|
|
|
|
parentUrl: parser.getParam("parentUrl"),
|
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
// NB. we don't validate roomId here as we do in getRoomIdentifierFromUrl:
|
|
|
|
// what would we do if it were invalid? If the widget API says that's what
|
|
|
|
// the room ID is, then that's what it is.
|
|
|
|
roomId: parser.getParam("roomId"),
|
|
|
|
password: parser.getParam("password"),
|
2023-09-19 08:47:47 +08:00
|
|
|
// This flag has 'embed' as an alias for historical reasons
|
2023-09-19 19:11:39 +08:00
|
|
|
confineToRoom:
|
|
|
|
parser.getFlagParam("confineToRoom") || parser.getFlagParam("embed"),
|
|
|
|
appPrompt: parser.getFlagParam("appPrompt", true),
|
|
|
|
preload: parser.getFlagParam("preload"),
|
|
|
|
hideHeader: parser.getFlagParam("hideHeader"),
|
2023-10-31 20:47:24 +08:00
|
|
|
showControls: parser.getFlagParam("showControls", true),
|
2023-09-19 19:11:39 +08:00
|
|
|
hideScreensharing: parser.getFlagParam("hideScreensharing"),
|
2023-11-03 23:04:54 +08:00
|
|
|
e2eEnabled: parser.getFlagParam("enableE2EE", true),
|
2023-09-19 00:49:10 +08:00
|
|
|
userId: parser.getParam("userId"),
|
|
|
|
displayName: parser.getParam("displayName"),
|
|
|
|
deviceId: parser.getParam("deviceId"),
|
|
|
|
baseUrl: parser.getParam("baseUrl"),
|
|
|
|
lang: parser.getParam("lang"),
|
|
|
|
fonts: parser.getAllParams("font"),
|
|
|
|
fontScale: Number.isNaN(fontScale) ? null : fontScale,
|
|
|
|
analyticsID: parser.getParam("analyticsID"),
|
2023-09-19 19:11:39 +08:00
|
|
|
allowIceFallback: parser.getFlagParam("allowIceFallback"),
|
2023-10-17 00:58:21 +08:00
|
|
|
perParticipantE2EE: parser.getFlagParam("perParticipantE2EE"),
|
2023-10-25 19:49:18 +08:00
|
|
|
skipLobby: parser.getFlagParam("skipLobby"),
|
2024-01-26 17:03:08 +08:00
|
|
|
returnToLobby: parser.getFlagParam("returnToLobby"),
|
2024-02-22 23:15:46 +08:00
|
|
|
theme: parser.getParam("theme"),
|
2024-02-22 04:52:31 +08:00
|
|
|
viaServers: parser.getParam("viaServers"),
|
|
|
|
homeserver: parser.getParam("homeserver"),
|
2023-09-19 00:49:10 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to simplify use of getUrlParams.
|
|
|
|
* @returns The app parameters for the current URL
|
|
|
|
*/
|
|
|
|
export const useUrlParams = (): UrlParams => {
|
|
|
|
const { search, hash } = useLocation();
|
|
|
|
return useMemo(() => getUrlParams(search, hash), [search, hash]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getRoomIdentifierFromUrl(
|
|
|
|
pathname: string,
|
|
|
|
search: string,
|
2023-10-11 22:42:04 +08:00
|
|
|
hash: string,
|
2023-09-19 00:49:10 +08:00
|
|
|
): RoomIdentifier {
|
2023-07-30 02:31:18 +08:00
|
|
|
let roomAlias: string | null = null;
|
2023-10-04 21:56:57 +08:00
|
|
|
pathname = pathname.substring(1); // Strip the "/"
|
|
|
|
const pathComponents = pathname.split("/");
|
|
|
|
const pathHasRoom = pathComponents[0] == "room";
|
|
|
|
const hasRoomAlias = pathComponents.length > 1;
|
2023-07-04 02:05:08 +08:00
|
|
|
|
2023-10-04 21:56:57 +08:00
|
|
|
// What type is our url: roomAlias in hash, room alias as the search path, roomAlias after /room/
|
2023-09-19 00:49:10 +08:00
|
|
|
if (hash === "" || hash.startsWith("#?")) {
|
2023-10-04 21:56:57 +08:00
|
|
|
if (hasRoomAlias && pathHasRoom) {
|
|
|
|
roomAlias = pathComponents[1];
|
2023-09-19 00:49:10 +08:00
|
|
|
}
|
2023-10-04 21:56:57 +08:00
|
|
|
if (!pathHasRoom) {
|
|
|
|
roomAlias = pathComponents[0];
|
2023-07-30 02:31:18 +08:00
|
|
|
}
|
2023-09-19 00:49:10 +08:00
|
|
|
} else {
|
|
|
|
roomAlias = hash;
|
2023-07-03 20:59:26 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
// Delete "?" and what comes afterwards
|
2023-10-04 21:56:57 +08:00
|
|
|
roomAlias = roomAlias?.split("?")[0] ?? null;
|
2022-07-28 04:14:05 +08:00
|
|
|
|
2023-10-04 21:56:57 +08:00
|
|
|
if (roomAlias) {
|
2023-09-19 00:49:10 +08:00
|
|
|
// Make roomAlias is null, if it only is a "#"
|
2023-10-04 21:56:57 +08:00
|
|
|
if (roomAlias.length <= 1) {
|
|
|
|
roomAlias = null;
|
|
|
|
} else {
|
|
|
|
// Add "#", if not present
|
|
|
|
if (!roomAlias.startsWith("#")) {
|
|
|
|
roomAlias = `#${roomAlias}`;
|
|
|
|
}
|
|
|
|
// Add server part, if not present
|
|
|
|
if (!roomAlias.includes(":")) {
|
|
|
|
roomAlias = `${roomAlias}:${Config.defaultServerName()}`;
|
|
|
|
}
|
2023-09-19 00:49:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 04:14:05 +08:00
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
const parser = new ParamParser(search, hash);
|
2022-12-10 03:25:02 +08:00
|
|
|
|
2023-07-30 02:31:18 +08:00
|
|
|
// Make sure roomId is valid
|
2023-09-19 00:49:10 +08:00
|
|
|
let roomId: string | null = parser.getParam("roomId");
|
2023-07-30 02:31:18 +08:00
|
|
|
if (!roomId?.startsWith("!")) {
|
|
|
|
roomId = null;
|
|
|
|
} else if (!roomId.includes("")) {
|
|
|
|
roomId = null;
|
|
|
|
}
|
|
|
|
|
2022-07-28 04:14:05 +08:00
|
|
|
return {
|
2023-07-30 02:31:18 +08:00
|
|
|
roomAlias,
|
|
|
|
roomId,
|
2023-09-19 00:49:10 +08:00
|
|
|
viaServers: parser.getAllParams("viaServers"),
|
2022-07-28 04:14:05 +08:00
|
|
|
};
|
2023-09-19 00:49:10 +08:00
|
|
|
}
|
2022-07-28 04:14:05 +08:00
|
|
|
|
2023-09-19 00:49:10 +08:00
|
|
|
export const useRoomIdentifier = (): RoomIdentifier => {
|
|
|
|
const { pathname, search, hash } = useLocation();
|
2023-07-04 02:05:08 +08:00
|
|
|
return useMemo(
|
2023-09-19 00:49:10 +08:00
|
|
|
() => getRoomIdentifierFromUrl(pathname, search, hash),
|
2023-10-11 22:42:04 +08:00
|
|
|
[pathname, search, hash],
|
2023-07-04 02:05:08 +08:00
|
|
|
);
|
2022-07-28 04:14:05 +08:00
|
|
|
};
|