web: use turnstile & session only when the processing instance has them

now also always fetching server info in the save tab
This commit is contained in:
wukko 2024-09-18 00:24:54 +06:00
parent 0cc18b488c
commit fdd5feac92
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
6 changed files with 24 additions and 13 deletions

View file

@ -1,14 +1,14 @@
<script lang="ts"> <script lang="ts">
import env from "$lib/env";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { cachedInfo } from "$lib/api/server-info";
import { turnstileLoaded, turnstileCreated } from "$lib/state/turnstile"; import { turnstileLoaded, turnstileCreated } from "$lib/state/turnstile";
let turnstileElement: HTMLElement; let turnstileElement: HTMLElement;
let turnstileScript: HTMLElement; let turnstileScript: HTMLElement;
onMount(() => { onMount(() => {
const sitekey = env.TURNSTILE_KEY; const sitekey = $cachedInfo?.info?.cobalt?.turnstileSitekey;
if (!sitekey) return; if (!sitekey) return;
$turnstileCreated = true; $turnstileCreated = true;
@ -17,7 +17,7 @@
window.turnstile?.render(turnstileElement, { window.turnstile?.render(turnstileElement, {
sitekey, sitekey,
"error-callback": (error) => { "error-callback": (error) => {
console.log("turnstile error code:", error); console.log("error code from turnstile:", error);
return true; return true;
}, },
callback: () => { callback: () => {

View file

@ -4,12 +4,12 @@
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import { SvelteComponent, tick } from "svelte"; import { SvelteComponent, tick } from "svelte";
import env from "$lib/env";
import { t } from "$lib/i18n/translations"; import { t } from "$lib/i18n/translations";
import dialogs from "$lib/dialogs"; import dialogs from "$lib/dialogs";
import { link } from "$lib/state/omnibox"; import { link } from "$lib/state/omnibox";
import { cachedInfo } from "$lib/api/server-info";
import { updateSetting } from "$lib/state/settings"; import { updateSetting } from "$lib/state/settings";
import { turnstileLoaded } from "$lib/state/turnstile"; import { turnstileLoaded } from "$lib/state/turnstile";
@ -57,7 +57,9 @@
goto("/", { replaceState: true }); goto("/", { replaceState: true });
} }
$: if (env.TURNSTILE_KEY) { // FIXME: figure out why regular processing spinner
// doesn't show up after turnstile loads
$: if ($cachedInfo?.info?.cobalt?.turnstileSitekey) {
if ($turnstileLoaded) { if ($turnstileLoaded) {
isDisabled = false; isDisabled = false;
} else { } else {

View file

@ -1,12 +1,15 @@
import { get } from "svelte/store"; import { get } from "svelte/store";
import settings from "$lib/state/settings"; import settings from "$lib/state/settings";
import lazySettingGetter from "$lib/settings/lazy-get";
import { getSession } from "$lib/api/session"; import { getSession } from "$lib/api/session";
import { currentApiURL } from "$lib/api/api-url"; import { currentApiURL } from "$lib/api/api-url";
import { cachedInfo } from "$lib/api/server-info";
import { apiOverrideWarning } from "$lib/api/safety-warning"; import { apiOverrideWarning } from "$lib/api/safety-warning";
import type { Optional } from "$lib/types/generic"; import type { Optional } from "$lib/types/generic";
import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api"; import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api";
import lazySettingGetter from "$lib/settings/lazy-get";
const request = async (url: string) => { const request = async (url: string) => {
const getSetting = lazySettingGetter(get(settings)); const getSetting = lazySettingGetter(get(settings));
@ -34,11 +37,10 @@ const request = async (url: string) => {
await apiOverrideWarning(); await apiOverrideWarning();
const usingCustomInstance = getSetting("processing", "enableCustomInstances")
&& getSetting("processing", "customInstanceURL");
const api = currentApiURL(); const api = currentApiURL();
// FIXME: rewrite this to allow custom instances to specify their own turnstile tokens
const session = usingCustomInstance ? undefined : await getSession(); const session = get(cachedInfo)?.info?.cobalt?.turnstileSitekey
? await getSession() : undefined;
let extraHeaders = {} let extraHeaders = {}

View file

@ -14,7 +14,6 @@ const variables = {
PLAUSIBLE_HOST: getEnv('PLAUSIBLE_HOST'), PLAUSIBLE_HOST: getEnv('PLAUSIBLE_HOST'),
PLAUSIBLE_ENABLED: getEnv('HOST') && getEnv('PLAUSIBLE_HOST'), PLAUSIBLE_ENABLED: getEnv('HOST') && getEnv('PLAUSIBLE_HOST'),
DEFAULT_API: getEnv('DEFAULT_API'), DEFAULT_API: getEnv('DEFAULT_API'),
TURNSTILE_KEY: getEnv('TURNSTILE_KEY'),
} }
const contacts = { const contacts = {

View file

@ -51,6 +51,7 @@ export type CobaltServerInfo = {
url: string, url: string,
startTime: string, startTime: string,
durationLimit: number, durationLimit: number,
turnstileSitekey?: string,
services: string[] services: string[]
}, },
git: { git: {

View file

@ -7,6 +7,7 @@
import { updated } from "$app/stores"; import { updated } from "$app/stores";
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import { afterNavigate } from "$app/navigation"; import { afterNavigate } from "$app/navigation";
import { getServerInfo, cachedInfo } from "$lib/api/server-info";
import "$lib/polyfills"; import "$lib/polyfills";
import env from "$lib/env"; import env from "$lib/env";
@ -31,10 +32,16 @@
$settings.appearance.reduceTransparency || $settings.appearance.reduceTransparency ||
device.prefers.reducedTransparency; device.prefers.reducedTransparency;
afterNavigate(() => { $: spawnTurnstile = !!$cachedInfo?.info?.cobalt?.turnstileSitekey;
afterNavigate(async() => {
const to_focus: HTMLElement | null = const to_focus: HTMLElement | null =
document.querySelector("[data-first-focus]"); document.querySelector("[data-first-focus]");
to_focus?.focus(); to_focus?.focus();
if ($page.url.pathname === "/") {
await getServerInfo();
}
}); });
</script> </script>
@ -77,7 +84,7 @@
<DialogHolder /> <DialogHolder />
<Sidebar /> <Sidebar />
<div id="content"> <div id="content">
{#if (env.TURNSTILE_KEY && $page.url.pathname === "/") || $turnstileCreated} {#if (spawnTurnstile && $page.url.pathname === "/") || $turnstileCreated}
<Turnstile /> <Turnstile />
{/if} {/if}
<slot></slot> <slot></slot>