From 5651ad475d4ab02f41a5bcd328736469c7113ef5 Mon Sep 17 00:00:00 2001 From: Xylight <80978739+Xyphyn@users.noreply.github.com> Date: Sat, 8 Feb 2025 13:49:09 -0800 Subject: [PATCH] feat: use state based auth instead of store based (#488) * misc: switch to state for auth * fix: class based auth not working --- src/lib/auth.svelte.ts | 223 +++++++----------- .../components/lemmy/comment/Comment.svelte | 2 +- .../lemmy/comment/CommentActions.svelte | 22 +- .../lemmy/comment/CommentForm.svelte | 4 +- .../lemmy/comment/CommentItem.svelte | 1 - .../lemmy/comment/CommentVote.svelte | 2 +- .../lemmy/community/CommunityCard.svelte | 12 +- .../lemmy/community/CommunityForm.svelte | 22 +- .../lemmy/community/CommunityHeader.svelte | 10 +- .../lemmy/community/CommunityItem.svelte | 2 +- .../lemmy/dropdowns/Location.svelte | 4 +- .../lemmy/inbox/PrivateMessage.svelte | 2 +- .../lemmy/modal/ApplicationDenyModal.svelte | 2 +- .../lemmy/modal/ImageUploadModal.svelte | 4 +- .../lemmy/modal/PrivateMessageModal.svelte | 4 +- .../lemmy/moderation/BanModal.svelte | 2 +- .../moderation/CommentModerationMenu.svelte | 12 +- .../lemmy/moderation/ModerationMenu.svelte | 14 +- .../lemmy/moderation/RemoveModal.svelte | 4 +- .../lemmy/moderation/ReportModal.svelte | 2 +- src/lib/components/lemmy/post/Post.svelte | 14 +- .../components/lemmy/post/PostActions.svelte | 50 ++-- src/lib/components/lemmy/post/PostMeta.svelte | 6 +- src/lib/components/lemmy/post/PostVote.svelte | 4 +- .../lemmy/post/form/PostForm.svelte | 4 +- src/lib/components/lemmy/post/helpers.ts | 7 +- src/lib/components/ui/Pageination.svelte | 14 +- src/lib/components/ui/navbar/Navbar.svelte | 14 +- src/lib/components/ui/navbar/Profile.svelte | 12 +- .../ui/navbar/commands/CommandItem.svelte | 1 - .../ui/navbar/commands/Commands.svelte | 4 +- .../ui/navbar/commands/actions.svelte.ts | 20 +- .../ui/sidebar/CommunityList.svelte | 6 +- .../ui/sidebar/ProfileButton.svelte | 17 +- src/lib/components/ui/sidebar/Sidebar.svelte | 28 ++- .../components/util/debug/DebugObject.svelte | 2 +- src/lib/lemmy.ts | 16 +- src/lib/lemmy/item.ts | 2 +- src/lib/lemmy/user.ts | 8 +- src/lib/promise.ts | 2 - src/lib/session.ts | 7 - src/routes/accounts/+page.svelte | 17 +- src/routes/admin/+layout.ts | 4 +- src/routes/admin/applications/+page.ts | 4 +- .../admin/applications/Application.svelte | 4 +- src/routes/admin/config/+page.svelte | 8 +- src/routes/admin/federation/+page.svelte | 4 +- src/routes/admin/taglines/+page.svelte | 2 +- src/routes/admin/team/+page.svelte | 10 +- .../c/[name]/settings/team/+page.svelte | 4 +- src/routes/comment/[instance]/+page.ts | 5 +- src/routes/communities/Subscribe.svelte | 2 +- src/routes/create/post/+page.svelte | 2 +- src/routes/inbox/+page.svelte | 2 +- src/routes/inbox/+page.ts | 2 +- src/routes/inbox/InboxItem.svelte | 4 +- src/routes/inbox/messages/+page.svelte | 2 +- src/routes/login/+page.svelte | 3 - src/routes/login/guest/+page.svelte | 22 +- src/routes/moderation/+page.ts | 4 +- src/routes/moderation/Report.svelte | 2 +- src/routes/modlog/+page.svelte | 10 +- .../password_change/[token]/+page.svelte | 4 +- src/routes/post/[instance]/+page.ts | 5 +- .../post/[instance]/[id=integer]/+page.svelte | 8 +- .../[id=integer]/CommentProvider.svelte | 8 +- .../profile/(local_user)/blocks/+page.svelte | 6 +- .../(local_user)/password/+layout.svelte | 2 +- .../(local_user)/password/+page.svelte | 2 +- .../(local_user)/password/2fa/+page.svelte | 4 +- .../(local_user)/password/change/+page.svelte | 10 +- .../(local_user)/password/delete/+page.svelte | 21 +- .../(local_user)/settings/+page.svelte | 49 ++-- src/routes/profile/+layout.ts | 4 +- src/routes/profile/media/+page.ts | 2 +- src/routes/profile/media/PictrsImage.svelte | 2 +- src/routes/saved/+page.ts | 2 +- src/routes/search/+page.svelte | 2 +- src/routes/search/+page.ts | 2 +- src/routes/settings/+page.svelte | 2 +- src/routes/signup/[instance]/+page.svelte | 2 +- src/routes/u/[name]/+page.svelte | 15 +- src/routes/util/photonify/+page.svelte | 7 +- 83 files changed, 395 insertions(+), 469 deletions(-) diff --git a/src/lib/auth.svelte.ts b/src/lib/auth.svelte.ts index 59741b14..7d21bb12 100644 --- a/src/lib/auth.svelte.ts +++ b/src/lib/auth.svelte.ts @@ -27,7 +27,7 @@ import { t } from './translations' const getDefaultProfile = (): Profile => ({ id: -1, - instance: get(profileData)?.defaultInstance ?? get(instance), + instance: profileData.defaultInstance ?? get(instance), }) function getFromStorage(key: string): T | undefined { @@ -83,7 +83,7 @@ const getCookie = (key: string): string | undefined => { ?.split('=')?.[1] } -export let profileData = writable( +export let profileData = $state( getFromStorage('profileData') ?? { profiles: [ { @@ -97,88 +97,55 @@ export let profileData = writable( }, ) -let fetchUser = { - loading: false, - prevProfile: -1, -} +class CurrentProfile { + #data = $derived( + profileData.profiles.find((i) => i.id == profileData.profile) ?? + getDefaultProfile(), + ) -function profileStore( - data: Writable, - fn: ( - values: ProfileData, - set: (value: Profile) => void, - update: (fn: Updater) => void, - ) => any, -) { - const derive: Readable = derived(data, fn) - const { subscribe } = derive - - return { - subscribe, - set: (value: Profile) => { - data.update((data) => { - let index = data.profiles.findIndex((t) => t.id == value.id) - data.profiles[index] = value - return data - }) - }, + get data() { + return this.#data + } + set data(value) { + if (!value) return + const index = profileData.profiles.findIndex((i) => i.id == value?.id) + profileData.profiles[index] = value } } -export let profile: Readable & { set: (v: Profile) => void } = - profileStore(profileData, (pd, set, update) => { - const profile = - pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() +export let profile = new CurrentProfile() - instance.set(profile.instance) - set(profile) +async function fetchUserData(profile: CurrentProfile) { + instance.set(profile.data.instance) + if (profile.data.jwt) { + site.set(undefined) + notifications.set({ applications: 0, inbox: 0, reports: 0 }) - if (profile?.jwt) { - if (profile.id != fetchUser.prevProfile && !fetchUser.loading) { - site.set(undefined) + const res = await userFromJwt(profile.data.jwt, profile.data.instance) + if (!res?.user) + toast({ + content: + "Your account's instance did not return your user data. Your login may have expired.", + type: 'error', + }) - fetchUser.loading = true - - notifications.set({ applications: 0, inbox: 0, reports: 0 }) - - userFromJwt(profile.jwt, profile.instance) - .then((res) => { - if (!res?.user) - toast({ - content: - "Your account's instance did not return your user data. Your login may have expired.", - type: 'error', - }) - - site.set(res?.site) - - profile.user = res?.user - if (profile.user) { - profile.avatar = res?.user?.local_user_view.person.avatar - profile.username = res?.user?.local_user_view.person.name - } - - fetchUser.loading = false - fetchUser.prevProfile = profile.id - checkInbox() - - update(() => profile) - }) - .catch((e) => { - fetchUser.loading = false - toast({ content: e, type: 'error' }) - }) - } - } else { - if (browser && !fetchUser.loading) { - site.set(undefined) - client({ instanceURL: profile.instance }) - .getSite() - .then((res) => site.set(res)) - fetchUser.loading = false - } + site.set(res?.site) + profile.data.user = res?.user + if (profile.data.user) { + profile.data.avatar = res?.user?.local_user_view.person.avatar + profile.data.username = res?.user?.local_user_view.person.name } - }) + } else { + if (browser) { + site.set(undefined) + client({ instanceURL: profile.data.instance }) + .getSite() + .then((res) => site.set(res)) + } + } + + return profile +} export let notifications = writable({ applications: 0, @@ -186,35 +153,40 @@ export let notifications = writable({ reports: 0, }) -profileData.subscribe(async (pd) => { - const serialized: ProfileData = { - ...pd, - profiles: pd.profiles.map((p) => serializeUser(p)), - } +$effect.root(() => { + $effect(() => { + const serialized = { + ...profileData, + profiles: profileData.profiles.map((p) => serializeUser(p)), + } - setFromStorage('profileData', serialized) + setFromStorage('profileData', serialized) - if (serialized.profile == -1) { - instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) - } - if (serialized.profiles.length == 0) { - profileData.update((pd) => ({ - ...pd, - profiles: [ + if (serialized.profile == -1) { + instance?.set(profileData.defaultInstance ?? DEFAULT_INSTANCE_URL) + } + if (serialized.profiles.length == 0) { + profileData.profiles = [ { id: 1, instance: DEFAULT_INSTANCE_URL, username: t.get('account.guest') || 'Guest', }, - ], - profile: 1, - })) - } + ] + profileData.profile = 1 + } + }) + + $effect(() => {}) + + $effect(() => { + if (profile.data.id || profileData) fetchUserData(profile).then((res) => {}) + }) }) if ( env.PUBLIC_MIGRATE_COOKIE && - get(profileData).profiles.length == 0 && + profileData.profiles.length == 0 && env.PUBLIC_INSTANCE_URL ) { const jwt = getCookie('jwt') @@ -260,22 +232,14 @@ export async function setUser(jwt: string, inst: string, username?: string) { instance.set(inst) - profileData.update((pd) => { - // too lazy to make a decent system - const id = Math.max(...pd.profiles.map((p) => p.id)) + 1 - - const newProfile: Profile = { - id: id, - instance: inst, - jwt: jwt, - username: user?.user?.local_user_view.person.name, - avatar: user?.user?.local_user_view.person.avatar, - } - - return { - profile: id, - profiles: [newProfile, ...pd.profiles], - } + const id = Math.max(...profileData.profiles.map((p) => p.id)) + 1 + profileData.profile = id + profileData.profiles.push({ + id: id, + instance: inst, + jwt: jwt, + username: user?.user?.local_user_view.person.name, + avatar: user?.user?.local_user_view.person.avatar, }) return user @@ -326,20 +290,15 @@ async function userFromJwt( } export function resetProfile() { - profileData.update((p) => ({ ...p, profile: -1 })) + profileData.profile = -1 } export function deleteProfile(id: number) { - profileData.update((pd) => { - const index = pd.profiles.findIndex((p) => p.id == id) - if (index <= -1) return pd - - pd.profiles.splice(index, 1) - - if (id == pd.profile) resetProfile() - - return pd - }) + profileData.profiles.splice( + profileData.profiles.findIndex((p) => p.id == id), + 1, + ) + if (id == profileData.profile) resetProfile() } function serializeUser(user: Profile): Profile { @@ -350,19 +309,19 @@ function serializeUser(user: Profile): Profile { } export async function setUserID(id: number) { - profileData.update((p) => ({ ...p, profile: id })) - return get(profile) + if (!profileData.profiles.find((p) => p.id == id)) return -1 + profileData.profile = id + return profile } export function moveProfile(id: number, up: boolean) { - const pd = get(profileData) try { - const index = pd.profiles.findIndex((i) => i.id == id) - - profileData.set({ - ...pd, - profiles: moveItem(pd.profiles, index, index + (up ? -1 : 1)), - }) + const index = profileData.profiles.findIndex((i) => i.id == id) + profileData.profiles = moveItem( + profileData.profiles, + index, + index + (up ? -1 : 1), + ) } catch (err) { // we dont care } @@ -406,9 +365,7 @@ async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { } async function checkInbox() { - if (!get(profile)) return - - const { user, jwt } = get(profile)! + const { user, jwt } = profile.data if (!jwt || !user) return const notifs = await getNotificationCount( diff --git a/src/lib/components/lemmy/comment/Comment.svelte b/src/lib/components/lemmy/comment/Comment.svelte index 268856b3..9f35f770 100644 --- a/src/lib/components/lemmy/comment/Comment.svelte +++ b/src/lib/components/lemmy/comment/Comment.svelte @@ -66,7 +66,7 @@ let editingLoad = $state(false) async function save() { - if (!$profile?.jwt || newComment.length <= 0) return + if (!profile.data?.jwt || newComment.length <= 0) return editingLoad = true diff --git a/src/lib/components/lemmy/comment/CommentActions.svelte b/src/lib/components/lemmy/comment/CommentActions.svelte index 37578bcb..05c4dc19 100644 --- a/src/lib/components/lemmy/comment/CommentActions.svelte +++ b/src/lib/components/lemmy/comment/CommentActions.svelte @@ -64,7 +64,7 @@ {$t('comment.reply')} - {#if $profile?.user && (amMod($profile?.user, comment.community) || isAdmin($profile.user))} + {#if profile.data?.user && (amMod(profile.data?.user, comment.community) || isAdmin(profile.data.user))} {/if} @@ -92,8 +92,8 @@
{$t('comment.actions.link')}
- {#if $profile?.jwt} - {#if comment.creator.id == $profile.user?.local_user_view.person.id} + {#if profile.data?.jwt} + {#if comment.creator.id == profile.data.user?.local_user_view.person.id} onedit?.(comment)}> {$t('post.actions.more.edit')} @@ -101,8 +101,12 @@ {/if} { - if ($profile?.jwt) - comment.saved = await save(comment, !comment.saved, $profile.jwt) + if (profile.data?.jwt) + comment.saved = await save( + comment, + !comment.saved, + profile.data.jwt, + ) }} > @@ -110,15 +114,15 @@ {comment.saved ? $t('post.actions.unsave') : $t('post.actions.save')} - {#if $profile?.user && $profile.jwt && $profile.user.local_user_view.person.id == comment.creator.id} + {#if profile.data?.user && profile.data.jwt && profile.data.user.local_user_view.person.id == comment.creator.id} { - if ($profile?.jwt) + if (profile.data?.jwt) comment.comment.deleted = await deleteItem( comment, !comment.comment.deleted, - $profile.jwt, + profile.data.jwt, ) }} > @@ -130,7 +134,7 @@ {/if} - {#if $profile.jwt && $profile.user?.local_user_view.person.id != comment.creator.id} + {#if profile.data.jwt && profile.data.user?.local_user_view.person.id != comment.creator.id} report(comment)} color="danger-subtle"> {$t('moderation.report')} diff --git a/src/lib/components/lemmy/comment/CommentForm.svelte b/src/lib/components/lemmy/comment/CommentForm.svelte index d5672bdc..11ff5df6 100644 --- a/src/lib/components/lemmy/comment/CommentForm.svelte +++ b/src/lib/components/lemmy/comment/CommentForm.svelte @@ -49,7 +49,7 @@ let language: number | undefined = $state() async function submit() { - if (!$profile?.user || !$profile?.jwt || value == '') return + if (!profile.data?.user || !profile.data?.jwt || value == '') return loading = true @@ -107,7 +107,7 @@ size="square-md" rounding="pill" color={language != undefined ? 'primary' : 'ghost'} - title={$t('form.profile.languages.title')} + title={$t('form.profile.data.languages.title')} > diff --git a/src/lib/components/lemmy/comment/CommentItem.svelte b/src/lib/components/lemmy/comment/CommentItem.svelte index 76a3444a..92d75644 100644 --- a/src/lib/components/lemmy/comment/CommentItem.svelte +++ b/src/lib/components/lemmy/comment/CommentItem.svelte @@ -2,7 +2,6 @@ import Comment from '$lib/components/lemmy/comment/Comment.svelte' import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte' import { publishedToDate } from '$lib/components/util/date.js' - import { getInstance } from '$lib/lemmy.js' import { settings } from '$lib/settings.svelte.js' import { t } from '$lib/translations' import type { CommentView } from 'lemmy-js-client' diff --git a/src/lib/components/lemmy/comment/CommentVote.svelte b/src/lib/components/lemmy/comment/CommentVote.svelte index 42d8451d..0f64e97d 100644 --- a/src/lib/components/lemmy/comment/CommentVote.svelte +++ b/src/lib/components/lemmy/comment/CommentVote.svelte @@ -33,7 +33,7 @@ }: Props = $props() const castVote = async (newVote: number) => { - if (!$profile?.jwt) { + if (!profile.data?.jwt) { toast({ content: $t('toast.loginVoteGate'), type: 'warning' }) return } diff --git a/src/lib/components/lemmy/community/CommunityCard.svelte b/src/lib/components/lemmy/community/CommunityCard.svelte index b854c037..547ed08a 100644 --- a/src/lib/components/lemmy/community/CommunityCard.svelte +++ b/src/lib/components/lemmy/community/CommunityCard.svelte @@ -117,7 +117,7 @@ }) async function subscribe() { - if (!$profile?.jwt) return + if (!profile.data?.jwt) return loading.subscribing = true const subscribed = community_view.subscribed == 'Subscribed' || @@ -226,7 +226,7 @@
- {#if $profile?.jwt} + {#if profile.data?.jwt} {/snippet} - {#if ($profile?.user && amMod($profile.user, item.community)) || ($profile?.user && isAdmin($profile.user))} + {#if (profile.data?.user && amMod(profile.data.user, item.community)) || (profile.data?.user && isAdmin(profile.data.user))} - {#if !item.community.local && !amMod($profile.user, item.community)} + {#if !item.community.local && !amMod(profile.data.user, item.community)} {$t('moderation.labelInstanceOnly')} {:else} {$t('moderation.label')} @@ -53,7 +53,7 @@ {item.post.removed ? $t('moderation.restore') : $t('moderation.remove')} {/if} - {#if $profile?.user && $profile.user?.local_user_view.person.id != item.creator.id} + {#if profile.data?.user && profile.data.user?.local_user_view.person.id != item.creator.id} { - if (!$profile.jwt) return + if (!profile.data.jwt) return item.comment = ( await feature( !item.comment.distinguished, item.comment, - $profile.jwt, + profile.data.jwt, ) ).comment_view.comment }} @@ -92,7 +92,7 @@ {/if} {/if} - {#if $profile?.user && isAdmin($profile.user)} + {#if profile.data?.user && isAdmin(profile.data.user)} {$t('admin.label')} remove(item, true)}> {#snippet prefix()} diff --git a/src/lib/components/lemmy/moderation/ModerationMenu.svelte b/src/lib/components/lemmy/moderation/ModerationMenu.svelte index 1668b6fc..b1b51114 100644 --- a/src/lib/components/lemmy/moderation/ModerationMenu.svelte +++ b/src/lib/components/lemmy/moderation/ModerationMenu.svelte @@ -31,7 +31,7 @@ let acting = $derived(locking || pinning) async function lock(lock: boolean) { - if (!$profile?.jwt || !isPostView(item)) return + if (!profile.data?.jwt || !isPostView(item)) return locking = true try { @@ -52,7 +52,7 @@ } async function pin(pinned: boolean, toInstance: boolean = false) { - if (!$profile?.jwt || !isPostView(item)) return + if (!profile.data?.jwt || !isPostView(item)) return pinning = true @@ -87,9 +87,9 @@ {/snippet} - {#if ($profile?.user && amMod($profile.user, item.community)) || ($profile?.user && isAdmin($profile.user))} + {#if (profile.data?.user && amMod(profile.data.user, item.community)) || (profile.data?.user && isAdmin(profile.data.user))} - {#if !item.community.local && !amMod($profile.user, item.community)} + {#if !item.community.local && !amMod(profile.data.user, item.community)} {$t('moderation.labelInstanceOnly')} {:else} {$t('moderation.label')} @@ -123,7 +123,7 @@ ? $t('moderation.unfeature') : $t('moderation.feature')} - {#if isAdmin($profile.user)} + {#if isAdmin(profile.data.user)} {$t('form.post.community')} {/if}
@@ -138,7 +138,7 @@ {item.post.removed ? $t('moderation.restore') : $t('moderation.remove')} {/if}
- {#if $profile?.user && $profile.user.local_user_view.person.id != item.creator.id} + {#if profile.data?.user && profile.data.user.local_user_view.person.id != item.creator.id} @@ -151,7 +151,7 @@ {/if} {/if} - {#if $profile?.user && isAdmin($profile.user)} + {#if profile.data?.user && isAdmin(profile.data.user)} {$t('admin.label')} - {#if !removed && $profile?.user && (amMod($profile.user, item.community) || (isAdmin($profile.user) && item.community.local))} + {#if !removed && profile.data?.user && (amMod(profile.data.user, item.community) || (isAdmin(profile.data.user) && item.community.local))} {$t('moderation.removeSubmission.withReason')} diff --git a/src/lib/components/lemmy/moderation/ReportModal.svelte b/src/lib/components/lemmy/moderation/ReportModal.svelte index 54f6cfef..70492da7 100644 --- a/src/lib/components/lemmy/moderation/ReportModal.svelte +++ b/src/lib/components/lemmy/moderation/ReportModal.svelte @@ -41,7 +41,7 @@ let reason = $state('') async function report() { - if (!item || !$profile?.jwt || reason == '') return + if (!item || !profile.data?.jwt || reason == '') return loading = true try { diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index a800f9b2..fa5903a0 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -1,21 +1,13 @@ - {#snippet title()} + {#snippet customTitle()} {#if title}{@render title()}{:else}Debug{/if} diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index 925e9033..e0b8e1b2 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -11,16 +11,6 @@ export const site = writable(undefined) const isURL = (input: RequestInfo | URL): input is URL => typeof input == 'object' && 'searchParams' in input -const toURL = (input: RequestInfo | URL): URL | undefined => { - if (isURL(input)) return input - - try { - return new URL(input.toString()) - } catch (e) { - return - } -} - async function customFetch( func: | (( @@ -67,9 +57,9 @@ export function client({ ) => Promise auth?: string } = {}) { - if (!instanceURL) instanceURL = get(profile).instance + if (!instanceURL) instanceURL = profile.data.instance - let jwt = auth ? auth : get(profile)?.jwt + let jwt = auth ? auth : profile.data?.jwt const headers = jwt ? { authorization: `Bearer ${jwt}` } : {} @@ -91,8 +81,6 @@ export function getClient( return client({ instanceURL, func, auth }) } -export const getInstance = () => encodeURIComponent(get(instance)) - export async function validateInstance(instance: string): Promise { if (instance == '') return false diff --git a/src/lib/lemmy/item.ts b/src/lib/lemmy/item.ts index 73d0f625..28658efc 100644 --- a/src/lib/lemmy/item.ts +++ b/src/lib/lemmy/item.ts @@ -105,7 +105,7 @@ export let resumables = resumableStore() export function addFavorite(item: Community, add: boolean = true) { const pd = get(profileData) - const p = pd.profiles.find((p) => p.id == get(profile)?.id) + const p = pd.profiles.find((p) => p.id == profile.data?.id) if (!p) return let favs = p.favorites ?? [] diff --git a/src/lib/lemmy/user.ts b/src/lib/lemmy/user.ts index 6162d287..a2c5bb8d 100644 --- a/src/lib/lemmy/user.ts +++ b/src/lib/lemmy/user.ts @@ -5,7 +5,7 @@ import type { Community, MyUserInfo, PersonView } from 'lemmy-js-client' import { get } from 'svelte/store' export const blockUser = async (block: boolean, id: number) => { - const auth = get(profile)?.jwt + const auth = profile.data?.jwt if (!auth) throw new Error('Unauthorized') @@ -22,7 +22,7 @@ export const addSubscription = ( community: Community, subscribe: boolean = true, ) => { - const p = get(profile) + const p = profile if (!p.user) return @@ -38,7 +38,7 @@ export const addSubscription = ( p.user.follows.splice(index, 1) } - profile.set(p) + profile.data.set(p) } export const addAdmin = async (handle: string, added: boolean, jwt: string) => @@ -56,4 +56,4 @@ export const addAdmin = async (handle: string, added: boolean, jwt: string) => }) export const hasFavorite = (profile: Profile, id: number): boolean => - profile?.favorites?.map((i) => i.id).includes(id) ?? false + profile.data?.favorites?.map((i) => i.id).includes(id) ?? false diff --git a/src/lib/promise.ts b/src/lib/promise.ts index 820f6a12..1f389eed 100644 --- a/src/lib/promise.ts +++ b/src/lib/promise.ts @@ -2,8 +2,6 @@ import { browser } from '$app/environment' -type AnyFunction = (...args: any[]) => any - export const awaitIfServer = async ( promise: Promise, ): Promise<{ diff --git a/src/lib/session.ts b/src/lib/session.ts index a46accbd..fd4ecfce 100644 --- a/src/lib/session.ts +++ b/src/lib/session.ts @@ -1,6 +1,5 @@ import { browser } from '$app/environment' import type { Community } from 'lemmy-js-client' -import { writable } from 'svelte/store' interface SessionStorage { lastSeenCommunity: Community | undefined @@ -35,9 +34,3 @@ export const getSessionStorage = ( if (!browser) return return JSON.parse(sessionStorage.getItem(key)!) } - -interface Route { - title: string -} - -export const route = writable(undefined) diff --git a/src/routes/accounts/+page.svelte b/src/routes/accounts/+page.svelte index 3d7ef23b..f2bb42a7 100644 --- a/src/routes/accounts/+page.svelte +++ b/src/routes/accounts/+page.svelte @@ -52,7 +52,7 @@ {#if debugging} {#snippet title()} @@ -101,7 +101,7 @@ {/if} -{#if $profileData.profiles.length == 0} +{#if profileData.profiles.length == 0}