mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-10-11 20:02:30 +00:00
Prepare time-bound Setup URI integration for Commonlib release
Add Time-bound and Compatible generation to the Obsidian dialogue, browser peer check, and setup tools, with boundary and compatibility coverage. Show the fixed window end before sharing a Time-bound URI. This review branch depends on unpublished @vrtmrz/livesync-commonlib 0.1.32-next.0. The root npm pin and Deno lockfile remain unchanged until that candidate is available; a fresh install of this commit is not yet expected to build.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import { LOG_LEVEL_NOTICE, type ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
|
||||
import type { LogFunction } from "@vrtmrz/livesync-commonlib/compat/services/lib/logUtils";
|
||||
import { createInstanceLogFunction } from "@vrtmrz/livesync-commonlib/compat/services/lib/logUtils";
|
||||
import { encodeSettingsToSetupURI } from "@vrtmrz/livesync-commonlib/compat/API/processSetting";
|
||||
import {
|
||||
encodeTimeBoundSetupURI,
|
||||
getTimeBoundSetupURIUsableUntil,
|
||||
isTimeBoundSetupURIUsableNow,
|
||||
type TimeBoundSetupURIMode,
|
||||
} from "@vrtmrz/livesync-commonlib/setup-uri";
|
||||
import { EVENT_REQUEST_COPY_SETUP_URI } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
|
||||
import { fireAndForget } from "@vrtmrz/livesync-commonlib/compat/common/utils";
|
||||
import type { NecessaryServices } from "@vrtmrz/livesync-commonlib/compat/interfaces/ServiceModule";
|
||||
@@ -16,32 +21,89 @@ export async function askEncryptingPassphrase(host: SetupFeatureHost): Promise<s
|
||||
);
|
||||
}
|
||||
|
||||
export async function copySetupURI(host: SetupFeatureHost, log: LogFunction, stripExtra = true) {
|
||||
const encryptingPassphrase = await askEncryptingPassphrase(host);
|
||||
if (encryptingPassphrase === false) return;
|
||||
const encryptedURI = await encodeSettingsToSetupURI(
|
||||
host.services.setting.currentSettings(),
|
||||
encryptingPassphrase,
|
||||
[...((stripExtra ? ["pluginSyncExtendedSetting"] : []) as (keyof ObsidianLiveSyncSettings)[])],
|
||||
true
|
||||
);
|
||||
if (await host.services.UI.promptCopyToClipboard("Setup URI", encryptedURI)) {
|
||||
log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE);
|
||||
function formatWindowEnd(usableUntil: number): string {
|
||||
return new Intl.DateTimeFormat(undefined, {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
weekday: "short",
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
timeZoneName: "short",
|
||||
}).format(new Date(usableUntil));
|
||||
}
|
||||
|
||||
async function askSetupURIMode(
|
||||
host: SetupFeatureHost
|
||||
): Promise<{ mode: TimeBoundSetupURIMode; usableUntil: number | null } | false> {
|
||||
let usableUntil: number | null = null;
|
||||
try {
|
||||
usableUntil = getTimeBoundSetupURIUsableUntil();
|
||||
} catch {
|
||||
// Compatible generation remains available when the device clock is invalid.
|
||||
}
|
||||
const timeBound = "Time-bound";
|
||||
const compatible = "Compatible (no time limit)";
|
||||
const cancel = "Cancel";
|
||||
const buttons = usableUntil === null ? [compatible, cancel] : [timeBound, compatible, cancel];
|
||||
const message =
|
||||
usableUntil === null
|
||||
? "Time-bound Setup URIs require a valid device clock. Compatible URIs have no time limit and work with older clients."
|
||||
: `Time-bound Setup URIs can be opened until ${formatWindowEnd(usableUntil)}. This is the end of the current fixed seven-day UTC window, not seven days from now. Compatible URIs have no time limit and work with older clients.`;
|
||||
const selected = await host.services.UI.confirm.askSelectStringDialogue(message, buttons, {
|
||||
title: "Setup URI availability",
|
||||
defaultAction: usableUntil === null ? compatible : timeBound,
|
||||
});
|
||||
if (selected === timeBound && usableUntil !== null) return { mode: "ephemeral", usableUntil };
|
||||
if (selected === compatible) return { mode: "persistent", usableUntil: null };
|
||||
return false;
|
||||
}
|
||||
|
||||
async function generateAndCopySetupURI(
|
||||
host: SetupFeatureHost,
|
||||
log: LogFunction,
|
||||
removeProperties: (keyof ObsidianLiveSyncSettings)[],
|
||||
skipDefaultValue: boolean
|
||||
) {
|
||||
const passphrase = await askEncryptingPassphrase(host);
|
||||
if (passphrase === false) return;
|
||||
while (true) {
|
||||
const choice = await askSetupURIMode(host);
|
||||
if (choice === false) return;
|
||||
let result;
|
||||
try {
|
||||
result = await encodeTimeBoundSetupURI(host.services.setting.currentSettings(), passphrase, {
|
||||
mode: choice.mode,
|
||||
removeProperties,
|
||||
skipDefaultValue,
|
||||
});
|
||||
} catch (error) {
|
||||
if (
|
||||
choice.mode === "ephemeral" &&
|
||||
error instanceof Error &&
|
||||
error.message === "Setup URI window changed during generation"
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (result.usableUntil !== choice.usableUntil || !isTimeBoundSetupURIUsableNow(result.usableUntil)) {
|
||||
continue;
|
||||
}
|
||||
if (await host.services.UI.promptCopyToClipboard("Setup URI", result.uri)) {
|
||||
log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function copySetupURI(host: SetupFeatureHost, log: LogFunction, stripExtra = true) {
|
||||
await generateAndCopySetupURI(host, log, stripExtra ? ["pluginSyncExtendedSetting"] : [], true);
|
||||
}
|
||||
|
||||
export async function copySetupURIFull(host: SetupFeatureHost, log: LogFunction) {
|
||||
const encryptingPassphrase = await askEncryptingPassphrase(host);
|
||||
if (encryptingPassphrase === false) return;
|
||||
const encryptedURI = await encodeSettingsToSetupURI(
|
||||
host.services.setting.currentSettings(),
|
||||
encryptingPassphrase,
|
||||
[],
|
||||
false
|
||||
);
|
||||
if (await host.services.UI.promptCopyToClipboard("Setup URI", encryptedURI)) {
|
||||
log("Setup URI copied to clipboard", LOG_LEVEL_NOTICE);
|
||||
}
|
||||
await generateAndCopySetupURI(host, log, [], false);
|
||||
}
|
||||
|
||||
export function useSetupURIFeature(host: NecessaryServices<"API" | "UI" | "setting" | "appLifecycle", never>) {
|
||||
|
||||
Reference in New Issue
Block a user