From 0704ee86c2253031d925c15a392724eff793b818 Mon Sep 17 00:00:00 2001 From: Xylight Date: Tue, 12 Dec 2023 16:36:13 -0800 Subject: [PATCH] feat: add cookie migration --- README.md | 6 +++-- src/lib/auth.ts | 27 +++++++++++++++++++ src/lib/components/lemmy/post/Post.svelte | 14 +++++----- src/lib/components/lemmy/post/helpers.ts | 12 ++++----- .../lemmy/post/media/PostMedia.svelte | 18 +++---------- .../post/[instance]/[id=integer]/+page.svelte | 2 +- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 2ce6d07b..f9bcadac 100644 --- a/README.md +++ b/README.md @@ -48,16 +48,18 @@ There you go, you now have an instance of Photon running. The most common settings you'll use are `PUBLIC_INSTANCE_URL`. Some selfhosters with `PUBLIC_SSR_ENABLED` set to true might want the instance url to be different for the server. You can use the `PUBLIC_INTERNAL_INSTANCE` variable for that. +`PUBLIC_MIGRATE_COOKIE` is useful if you want to switch Photon to your default frontend. It'll convert the logged in cookie from lemmy-ui to a Photon account. It will only work if you have `PUBLIC_INSTANCE_URL` set, and it will login with that instance. + The following environment variables can be set to override the default settings: | Variable | Values | Default Value | -| --------------------------------- | --------------------- | ---------------------------------------- | +| ------------------------------- | ------------------- | -------------------------------------- | | PUBLIC_INSTANCE_URL | URL | `lemmy.ml` | | PUBLIC_INTERNAL_INSTANCE | URL? | null | | PUBLIC_LOCK_TO_INSTANCE | `bool` | `true` if `PUBLIC_INSTANCE_URL` is set | | PUBLIC_SSR_ENABLED | `bool` | `false` | -| | | | +| PUBLIC_MIGRATE_COOKIE | `bool` | `false` | | PUBLIC_THEME | system\|dark\|light | system | | PUBLIC_EXPANDABLE_IMAGES | `bool` | true | | PUBLIC_MARK_READ_POSTS | `bool` | true | diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 8762e8f4..654fdd93 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -14,6 +14,8 @@ import { } from 'lemmy-js-client' import { get, writable } from 'svelte/store' import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js' +import { browser } from '$app/environment' +import { env } from '$env/dynamic/public' const getDefaultProfile = (): Profile => ({ id: -1, @@ -61,6 +63,14 @@ interface PersonData extends MyUserInfo { } } +const getCookie = (key: string): string | undefined => { + if (!browser) return undefined + + // sorry i was dying when i wrote this line + // ask chatgpt or something to explain this for you + return document?.cookie?.split(';').map(c => c.trim()).find(c => c.split('=')?.[0] == key)?.split('=')?.[1] +} + export let profileData = writable( getFromStorage('profileData') ?? { profiles: [], profile: -1 } ) @@ -79,6 +89,23 @@ profileData.subscribe(async (pd) => { } }) +if (env.PUBLIC_MIGRATE_COOKIE && get(profileData).profiles.length == 0 && env.PUBLIC_INSTANCE_URL) { + const jwt = getCookie('jwt') + if (jwt) { + new Promise(async () => { + const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL) + if (!user) return + + const result = await setUser(jwt, env.PUBLIC_INSTANCE_URL, user?.user.local_user_view.person.name) + + if (result) + toast({ content: 'Your instance migrated to Photon, and you were logged in using a leftover cookie.', type: 'success' }) + + }) + + } +} + export let profile = writable(getProfile()) profile.subscribe(async (p) => { diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index 04baf9d0..275a1a18 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -25,6 +25,8 @@ export let hideCommunity = false export let view = $userSettings.view + $: type = mediaType(post.post.url, view) + let loaded = false @@ -35,8 +37,8 @@ {view != 'card' ? 'bg-transparent !border-0' : 'p-5'} {view == 'compact' ? 'py-4' : view == 'list' - ? 'py-5' - : 'py-5'} {$$props.class}" + ? 'py-5' + : 'py-5'} {$$props.class}" id={post.post.id} >
- {#if post.post.embed_title != post.post.name || view == 'compact'} + {#if !(post.post.embed_title == post.post.name && type == 'embed') || view == 'compact'} {/if}
- + {#if post.post.body && !post.post.nsfw && view != 'compact'}
-
- - Video -

Go to the post to view this video.

-
-{:else if type == 'iframe' && (view == 'cozy' || view == 'card') && post.url} +{:else if (type == 'iframe' || type == 'video') && (view == 'cozy' || view == 'card') && post.url}