2026-06-29 23:05:20 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2026 Lotus Guild
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shared helpers for the Lotus fork's widget extensions.
|
|
|
|
|
*
|
|
|
|
|
* Everything here is **opt-in**: each feature is gated behind a `lotus*` URL
|
|
|
|
|
* param that the Lotus host (cinny) appends to the widget iframe URL. With no
|
|
|
|
|
* param present, none of this code changes Element Call's behaviour — which
|
|
|
|
|
* keeps the fork a minimal, additive, easy-to-rebase diff over upstream.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { logger } from "matrix-js-sdk/lib/logger";
|
|
|
|
|
|
|
|
|
|
import { widget } from "../widget";
|
2026-09-12 11:35:57 -04:00
|
|
|
import type { LotusWidgetActions } from "./lotusActions";
|
2026-06-29 23:05:20 -04:00
|
|
|
|
2026-06-29 23:10:16 -04:00
|
|
|
export { LotusWidgetActions } from "./lotusActions";
|
2026-06-29 23:05:20 -04:00
|
|
|
|
|
|
|
|
let cachedParams: URLSearchParams | undefined;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read a URL param from either the query string or the hash fragment (Element
|
|
|
|
|
* Call passes widget params via both depending on host), without depending on
|
|
|
|
|
* EC's own `getUrlParams` parser (keeps the rebase surface small).
|
|
|
|
|
*/
|
|
|
|
|
export function lotusParam(name: string): string | null {
|
|
|
|
|
if (!cachedParams) {
|
2026-06-29 23:14:42 -04:00
|
|
|
// Match EC's own ParamParser precedence: the hash fragment wins over the
|
|
|
|
|
// query string. So seed from the fragment first, then fill gaps from query.
|
2026-06-29 23:05:20 -04:00
|
|
|
const hash = window.location.hash.replace(/^#\/?/, "");
|
2026-09-12 11:35:57 -04:00
|
|
|
const hashQuery = hash.includes("?")
|
|
|
|
|
? hash.slice(hash.indexOf("?") + 1)
|
|
|
|
|
: "";
|
2026-06-29 23:14:42 -04:00
|
|
|
cachedParams = new URLSearchParams(hashQuery);
|
|
|
|
|
for (const [k, v] of new URLSearchParams(window.location.search)) {
|
2026-06-29 23:05:20 -04:00
|
|
|
if (!cachedParams.has(k)) cachedParams.append(k, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cachedParams.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Whether a boolean-ish Lotus feature flag is enabled. */
|
|
|
|
|
export function lotusFlag(name: string): boolean {
|
|
|
|
|
const v = lotusParam(name);
|
|
|
|
|
return v === "1" || v === "true";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a fromWidget message to the Lotus host, swallowing the inevitable
|
|
|
|
|
* rejection when the host hasn't (yet) registered a handler for it. Returns
|
|
|
|
|
* true if the widget transport was available to attempt the send.
|
|
|
|
|
*/
|
2026-09-12 11:35:57 -04:00
|
|
|
export function lotusSendToHost(
|
|
|
|
|
action: LotusWidgetActions,
|
|
|
|
|
data: unknown,
|
|
|
|
|
): boolean {
|
2026-06-29 23:05:20 -04:00
|
|
|
const api = widget?.api;
|
|
|
|
|
if (!api) return false;
|
2026-09-12 11:35:57 -04:00
|
|
|
void api.transport
|
|
|
|
|
.send(action, data as Record<string, unknown>)
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
logger.debug(`[lotus] host did not ack ${action}`, e);
|
|
|
|
|
});
|
2026-06-29 23:05:20 -04:00
|
|
|
return true;
|
|
|
|
|
}
|