From 6bb47c2b1d658c182d4e2d528eeff0430cc9fb6c Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Tue, 1 Aug 2023 13:27:24 -0700 Subject: [PATCH] feat: add guest instance --- src/lib/auth.ts | 15 ++- .../moderation/CommentModerationMenu.svelte | 2 +- src/routes/accounts/+page.svelte | 104 ++++++++++++++++++ 3 files changed, 114 insertions(+), 7 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 8962f2c8..0ec0fc67 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -1,13 +1,8 @@ -import { env } from '$env/dynamic/public' import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js' -import { getClient, instance } from '$lib/lemmy.js' -import { userSettings } from '$lib/settings.js' +import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js' import type { MyUserInfo } from 'lemmy-js-client' import { get, writable } from 'svelte/store' -const LINKED_INSTANCE_URL = env.PUBLIC_INSTANCE_URL -const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml' - function getFromStorage(key: string): T | undefined { if (typeof localStorage == 'undefined') return undefined const lc = localStorage.getItem(key) @@ -35,6 +30,7 @@ interface Profile { interface ProfileData { profiles: Profile[] profile: number + defaultInstance?: string } interface PersonData extends MyUserInfo { @@ -48,11 +44,18 @@ export const profileData = writable( profileData.subscribe((pd) => { setFromStorage('profileData', pd) + + if (pd.profile == -1) { + instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) + } }) export const profile = writable(getProfile()) profile.subscribe(async (p) => { + if (p?.id == -1) { + instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) + } if (!p || !p.jwt) { profileData.update((pd) => ({ ...pd, profile: -1 })) return diff --git a/src/lib/components/lemmy/moderation/CommentModerationMenu.svelte b/src/lib/components/lemmy/moderation/CommentModerationMenu.svelte index 53002003..f2d6ddc1 100644 --- a/src/lib/components/lemmy/moderation/CommentModerationMenu.svelte +++ b/src/lib/components/lemmy/moderation/CommentModerationMenu.svelte @@ -54,7 +54,7 @@ {item.post.removed ? 'Restore' : 'Remove'} {/if} - {#if $profile.user && $profile.user?.local_user_view.person.id != item.creator.id} + {#if $profile?.user && $profile.user?.local_user_view.person.id != item.creator.id} User diff --git a/src/routes/accounts/+page.svelte b/src/routes/accounts/+page.svelte index 85ebf03f..a124546e 100644 --- a/src/routes/accounts/+page.svelte +++ b/src/routes/accounts/+page.svelte @@ -7,11 +7,21 @@ deleteProfile, } from '$lib/auth.js' import Button from '$lib/components/input/Button.svelte' + import TextInput from '$lib/components/input/TextInput.svelte' import EditableList from '$lib/components/ui/list/EditableList.svelte' + import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js' + import { instance, validateInstance } from '$lib/lemmy.js' import { ArrowLeftOnRectangle, Icon, Plus, Trash } from 'svelte-hero-icons' import { flip } from 'svelte/animate' + + let newInstance: string = $instance + let loading = false + + Accounts + + {#if $profileData.profiles.length == 0}
Log in +
+ { + loading = true + try { + const valid = await validateInstance(newInstance.trim()) + + if (!valid) { + throw new Error('invalid instance') + } + + toast({ + content: 'Changed guest instance.', + type: ToastType.success, + }) + } catch (err) { + toast({ + content: 'Failed to contact that instance URL. Is it down?', + type: ToastType.error, + }) + + loading = false + + return + } + + $profileData.defaultInstance = newInstance + if ($currentProfile && $currentProfile.id == -1) { + $instance = newInstance + } + loading = false + }} + placeholder="Instance URL" + bind:value={newInstance} + /> + +
{:else} @@ -79,6 +135,54 @@ {/each} +
+ + Guest +
+ { + loading = true + try { + const valid = await validateInstance(newInstance.trim()) + + if (!valid) { + throw new Error('invalid instance') + } + + toast({ + content: 'Changed guest instance.', + type: ToastType.success, + }) + } catch (err) { + toast({ + content: 'Failed to contact that instance URL. Is it down?', + type: ToastType.error, + }) + } + + $profileData.defaultInstance = newInstance + if ($currentProfile && $currentProfile.id == -1) { + $instance = newInstance + } + loading = false + }} + placeholder="Instance URL" + bind:value={newInstance} + /> + +
+
+
+ + +