diff --git a/src/lib/auth.svelte.ts b/src/lib/auth.svelte.ts index 7af3bc23..8717ae9e 100644 --- a/src/lib/auth.svelte.ts +++ b/src/lib/auth.svelte.ts @@ -20,11 +20,6 @@ import { site } from './lemmy.svelte' import { errorMessage } from './lemmy/error' import { publishedToDate } from './components/util/date' -const getDefaultProfile = (): Profile => ({ - id: -1, - instance: profileData.defaultInstance ?? DEFAULT_INSTANCE_URL, -}) - function getFromStorage(key: string): T | undefined { if (typeof localStorage == 'undefined') return undefined const lc = localStorage.getItem(key) @@ -39,7 +34,7 @@ function setFromStorage(key: string, item: any, stringify: boolean = true) { return localStorage.setItem(key, stringify ? JSON.stringify(item) : item) } -export interface Profile { +export interface ProfileInfo { id: number instance: string jwt?: string @@ -54,9 +49,9 @@ export interface Profile { * What gets stored in localstorage. */ interface ProfileData { - profiles: Profile[] + profiles: ProfileInfo[] + // should be named currentId profile: number - defaultInstance?: string } interface Notifications { @@ -68,8 +63,6 @@ interface Notifications { 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()) @@ -77,42 +70,110 @@ const getCookie = (key: string): string | undefined => { ?.split('=')?.[1] } -export const profileData = $state( - getFromStorage('profileData') ?? { - profiles: [ - { - id: 1, - instance: DEFAULT_INSTANCE_URL, - username: 'Guest', - color: '#505050', - }, - ], - profile: 1, - }, -) - -class CurrentProfile { - #data = $derived( - profileData.profiles.find(i => i.id == profileData.profile) ?? - getDefaultProfile(), +class Profile { + meta = $state( + getFromStorage('profileData') ?? { + profiles: [ + { + id: 1, + instance: DEFAULT_INSTANCE_URL, + username: 'Guest', + color: '#505050', + }, + ], + profile: 1, + }, + ) + #current = $derived( + this.meta.profiles.find(i => i.id == this.meta.profile) ?? + this.getDefaultProfile(), ) - get data() { - return this.#data + getDefaultProfile(): ProfileInfo { + return { + id: -1, + instance: DEFAULT_INSTANCE_URL, + } } - set data(value) { + + constructor() { + // cookie migration code + if ( + env.PUBLIC_MIGRATE_COOKIE && + this.meta.profiles.length == 0 && + env.PUBLIC_INSTANCE_URL + ) { + const jwt = getCookie('jwt') + if (jwt) { + ;(async () => { + const result = await this.add(jwt, env.PUBLIC_INSTANCE_URL ?? '') + + if (result) + toast({ + content: + 'Your instance migrated to Photon, and your account was transferred.', + type: 'success', + }) + })() + } + } + + setInterval( + () => { + if (profile.current.jwt) + this.checkInbox().then(res => notifications.update(() => res)) + }, + 4 * 60 * 1000, + ) + + // hacky way to check donation status + setTimeout(() => { + if ( + profile.current.user?.local_user_view.local_user + .last_donation_notification + ) { + const donationDate = publishedToDate( + profile.current.user?.local_user_view.local_user + .last_donation_notification, + ) + if (Date.now() - donationDate.getTime() > 365 * 24 * 60 * 60 * 1000) { + toast({ + content: t.get('toast.lemmyDonate'), + duration: 3600 * 1000, + long: true, + }) + + // lemmy js client donation dialog is broken + fetch( + `${instanceToURL(profile.current.instance)}/api/v3/user/donation_dialog_shown`, + { + method: 'POST', + headers: { + authorization: `Bearer ${profile.current.jwt}`, + }, + }, + ) + } + } + }, 3 * 1000) + } + + get current() { + return this.#current + } + set current(value) { if (!value) return - const index = profileData.profiles.findIndex(i => i.id == value?.id) - profileData.profiles[index] = value + const index = this.meta.profiles.findIndex(i => i.id == value?.id) + this.meta.profiles[index] = value } async fetchUserData() { - const startId = this.#data.id - if (this.#data.jwt) { + const startId = this.#current.id + if (this.#current.jwt) { site.data = undefined notifications.set({ applications: 0, inbox: 0, reports: 0 }) - const res = await userFromJwt(this.#data.jwt, this.#data.instance) + const res = await userFromJwt(this.#current.jwt, this.#current.instance) if (!res?.user) toast({ content: @@ -121,21 +182,21 @@ class CurrentProfile { }) // TODO update authentication handling to not be this dynamic - if (this.#data.id != startId) { + if (this.#current.id != startId) { console.error('profile was switched too fast, ID mismatch') return } site.data = res?.site - this.#data.user = res?.user - if (profile.data.user) { - this.#data.avatar = res?.user?.local_user_view.person.avatar - this.#data.username = res?.user?.local_user_view.person.name + this.#current.user = res?.user + if (profile.current.user) { + this.#current.avatar = res?.user?.local_user_view.person.avatar + this.#current.username = res?.user?.local_user_view.person.name } } else { if (browser) { site.data = undefined - client({ instanceURL: this.#data.instance }) + client({ instanceURL: this.#current.instance }) .getSite() .then(res => (site.data = res)) } @@ -143,9 +204,98 @@ class CurrentProfile { return this } + + async checkInbox() { + const { user, jwt } = profile.current + if (!user || !jwt) throw new Error('checkInbox() called with invalid user') + + const notifs = await getNotificationCount( + jwt, + amModOfAny(user) ?? false, + user ? isAdmin(user) : false, + ) + + return { + inbox: notifs.unreads, + applications: notifs.applications, + reports: notifs.reports, + } + } + + async add(jwt: string, instance: string) { + const user = await userFromJwt(jwt, instance) + .then(u => u) + .catch(err => { + toast({ content: errorMessage(err as string), type: 'error' }) + }) + if (!user?.user) { + toast({ + content: "Your instance's API did not return your user data.", + type: 'error', + }) + } + + const id = Math.max(...this.meta.profiles.map(p => p.id)) + 1 + this.meta.profile = id + this.meta.profiles.push({ + id: id, + instance: instance, + jwt: jwt, + username: user?.user?.local_user_view.person.name, + avatar: user?.user?.local_user_view.person.avatar, + }) + + return user + } + + remove(id: number) { + this.meta.profiles.splice( + this.meta.profiles.findIndex(p => p.id == id), + 1, + ) + if (id == this.meta.profile) this.meta.profile = -1 + } + + move(id: number, up: boolean) { + try { + const index = this.meta.profiles.findIndex(i => i.id == id) + this.meta.profiles = moveItem( + this.meta.profiles, + index, + index + (up ? -1 : 1), + ) + } catch { + /* empty */ + } + } + + mainEffect = $effect.root(() => { + // Sync with localStorage + $effect(() => { + const serialized = { + ...this.meta, + profiles: this.meta.profiles.map(p => serializeUser(p)), + } + + setFromStorage('profileData', serialized) + + // no more profiles left + if (serialized.profiles.length == 0) { + this.meta.profiles = [this.getDefaultProfile()] + this.meta.profile = 1 + } + }) + + $effect(() => { + if (this.current.jwt) + this.fetchUserData().then(() => { + this.checkInbox().then(res => notifications.update(() => res)) + }) + }) + }) } -export const profile = new CurrentProfile() +export const profile = new Profile() export const notifications = writable({ applications: 0, @@ -153,90 +303,6 @@ export const notifications = writable({ reports: 0, }) -$effect.root(() => { - $effect(() => { - const serialized = { - ...profileData, - profiles: profileData.profiles.map(p => serializeUser(p)), - } - - setFromStorage('profileData', serialized) - if (serialized.profiles.length == 0) { - profileData.profiles = [ - { - id: 1, - instance: DEFAULT_INSTANCE_URL, - username: t.get('account.guest') || 'Guest', - }, - ] - profileData.profile = 1 - } - }) - - $effect(() => { - if (profile.data.id || profileData) - profile.fetchUserData().then(() => { - checkInbox() - }) - }) -}) - -// cookie migration code -if ( - env.PUBLIC_MIGRATE_COOKIE && - profileData.profiles.length == 0 && - env.PUBLIC_INSTANCE_URL -) { - const jwt = getCookie('jwt') - if (jwt) { - ;(async () => { - const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL ?? '') - if (!user) return - - const result = await setUser(jwt, env.PUBLIC_INSTANCE_URL ?? '') - - if (result) - toast({ - content: - 'Your instance migrated to Photon, and you were logged in using a leftover cookie.', - type: 'success', - }) - })() - } -} - -export async function setUser(jwt: string, inst: string) { - try { - new URL(instanceToURL(inst)) - } catch { - return - } - - const user = await userFromJwt(jwt, inst) - .then(u => u) - .catch(err => { - toast({ content: errorMessage(err as string), type: 'error' }) - }) - if (!user?.user) { - toast({ - content: "Your instance's API did not return your user data.", - type: 'error', - }) - } - - 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 -} - async function userFromJwt( jwt: string, instance: string, @@ -281,44 +347,13 @@ async function userFromJwt( } } -export function resetProfile() { - profileData.profile = -1 -} - -export function deleteProfile(id: number) { - profileData.profiles.splice( - profileData.profiles.findIndex(p => p.id == id), - 1, - ) - if (id == profileData.profile) resetProfile() -} - -function serializeUser(user: Profile): Profile { +function serializeUser(user: ProfileInfo): ProfileInfo { return { ...user, user: undefined, } } -export async function setUserID(id: number) { - if (!profileData.profiles.find(p => p.id == id)) return -1 - profileData.profile = id - return profile -} - -export function moveProfile(id: number, up: boolean) { - try { - const index = profileData.profiles.findIndex(i => i.id == id) - profileData.profiles = moveItem( - profileData.profiles, - index, - index + (up ? -1 : 1), - ) - } catch { - /* empty */ - } -} - async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { const unreadsPromise = getClient() .getUnreadCount() @@ -356,56 +391,3 @@ async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { applications: applications, } } - -async function checkInbox() { - const { user, jwt } = profile.data - if (!jwt || !user) return - - const notifs = await getNotificationCount( - jwt, - amModOfAny(user) ?? false, - user ? isAdmin(user) : false, - ) - - notifications.set({ - inbox: notifs.unreads, - applications: notifs.applications, - reports: notifs.reports, - }) -} - -async function init() { - setInterval(checkInbox, 4 * 60 * 1000) - - // hacky way to check donation status - setTimeout(() => { - if ( - profile.data.user?.local_user_view.local_user.last_donation_notification - ) { - const donationDate = publishedToDate( - profile.data.user?.local_user_view.local_user - .last_donation_notification, - ) - if (Date.now() - donationDate.getTime() > 365 * 24 * 60 * 60 * 1000) { - toast({ - content: t.get('toast.lemmyDonate'), - duration: 3600 * 1000, - long: true, - }) - - // lemmy js client donation dialog is broken - fetch( - `${instanceToURL(profile.data.instance)}/api/v3/user/donation_dialog_shown`, - { - method: 'POST', - headers: { - authorization: `Bearer ${profile.data.jwt}`, - }, - }, - ) - } - } - }, 3 * 1000) -} - -init() diff --git a/src/lib/components/lemmy/PictrsImage.svelte b/src/lib/components/lemmy/PictrsImage.svelte index 2ec0324e..d6568487 100644 --- a/src/lib/components/lemmy/PictrsImage.svelte +++ b/src/lib/components/lemmy/PictrsImage.svelte @@ -14,7 +14,7 @@ let loading = $state(false) async function deleteImage(image: LocalImage) { - if (!profile.data?.jwt) return + if (!profile.current?.jwt) return try { loading = true diff --git a/src/lib/components/lemmy/comment/Comment.svelte b/src/lib/components/lemmy/comment/Comment.svelte index e2e717a4..c61ce8d6 100644 --- a/src/lib/components/lemmy/comment/Comment.svelte +++ b/src/lib/components/lemmy/comment/Comment.svelte @@ -50,7 +50,7 @@ let editingLoad = $state(false) async function save(type: 'reply' | 'edit') { - if (!profile.data?.jwt || newComment.length <= 0) return + if (!profile.current?.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 78685723..3c04f8cf 100644 --- a/src/lib/components/lemmy/comment/CommentActions.svelte +++ b/src/lib/components/lemmy/comment/CommentActions.svelte @@ -60,7 +60,7 @@ {$t('comment.reply')} - {#if profile.data?.user && (amMod(profile.data?.user, comment.community) || isAdmin(profile.data.user))} + {#if profile.current?.user && (amMod(profile.current?.user, comment.community) || isAdmin(profile.current.user))} {/if} @@ -90,8 +90,8 @@
{$t('post.actions.more.share')}
- {#if profile.data?.jwt} - {#if comment.creator.id == profile.data.user?.local_user_view.person.id} + {#if profile.current?.jwt} + {#if comment.creator.id == profile.current.user?.local_user_view.person.id} onedit?.(comment)}> {$t('post.actions.more.edit')} @@ -99,7 +99,7 @@ {/if} { - if (profile.data?.jwt) + if (profile.current?.jwt) comment.saved = await save(comment, !comment.saved) }} > @@ -108,11 +108,11 @@ {comment.saved ? $t('post.actions.unsave') : $t('post.actions.save')} - {#if profile.data?.user && profile.data.jwt && profile.data.user.local_user_view.person.id == comment.creator.id} + {#if profile.current?.user && profile.current.jwt && profile.current.user.local_user_view.person.id == comment.creator.id} { - if (profile.data?.jwt) + if (profile.current?.jwt) comment.comment.deleted = await deleteItem( comment, !comment.comment.deleted, @@ -127,7 +127,7 @@ {/if} - {#if profile.data.jwt && profile.data.user?.local_user_view.person.id != comment.creator.id} + {#if profile.current.jwt && profile.current.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 c27c7236..c4afdb62 100644 --- a/src/lib/components/lemmy/comment/CommentForm.svelte +++ b/src/lib/components/lemmy/comment/CommentForm.svelte @@ -51,7 +51,7 @@ let language: number | undefined = $state() async function submit() { - if (!profile.data?.user || !profile.data?.jwt || value == '') return + if (!profile.current?.user || !profile.current?.jwt || value == '') return loading = true diff --git a/src/lib/components/lemmy/comment/CommentVote.svelte b/src/lib/components/lemmy/comment/CommentVote.svelte index 4cb300ce..b30fcc37 100644 --- a/src/lib/components/lemmy/comment/CommentVote.svelte +++ b/src/lib/components/lemmy/comment/CommentVote.svelte @@ -24,7 +24,7 @@ }: Props = $props() const castVote = async (newVote: number) => { - if (!profile.data?.jwt) { + if (!profile.current?.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 ae055a4c..c81233f2 100644 --- a/src/lib/components/lemmy/community/CommunityCard.svelte +++ b/src/lib/components/lemmy/community/CommunityCard.svelte @@ -113,7 +113,7 @@ }) async function subscribe() { - if (!profile.data?.jwt) return + if (!profile.current?.jwt) return loading.subscribing = true const subscribed = community_view.subscribed == 'Subscribed' || @@ -227,7 +227,7 @@ {/if}
- {#if profile.data?.jwt} + {#if profile.current?.jwt} {/snippet} - {#if (profile.data?.user && amMod(profile.data.user, item.community)) || (profile.data?.user && isAdmin(profile.data.user))} + {#if (profile.current?.user && amMod(profile.current.user, item.community)) || (profile.current?.user && isAdmin(profile.current.user))} - {#if !item.community.local && !amMod(profile.data.user, item.community)} + {#if !item.community.local && !amMod(profile.current.user, item.community)} {$t('moderation.labelInstanceOnly')} {:else} {$t('moderation.label')} @@ -52,7 +52,7 @@ : $t('moderation.remove')} {/if} - {#if profile.data?.user && profile.data.user?.local_user_view.person.id != item.creator.id} + {#if profile.current?.user && profile.current.user?.local_user_view.person.id != item.creator.id} { - if (!profile.data.jwt) return + if (!profile.current.jwt) return item.comment = ( await feature( !item.comment.distinguished, item.comment, - profile.data.jwt, + profile.current.jwt, ) ).comment_view.comment }} @@ -104,7 +104,7 @@ {/if} - {#if profile.data?.user && isAdmin(profile.data.user)} + {#if profile.current?.user && isAdmin(profile.current.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 5338e3dc..ee02024e 100644 --- a/src/lib/components/lemmy/moderation/ModerationMenu.svelte +++ b/src/lib/components/lemmy/moderation/ModerationMenu.svelte @@ -32,7 +32,7 @@ let acting = $derived(locking || pinning) async function lock(lock: boolean) { - if (!profile.data?.jwt || !isPostView(item)) return + if (!profile.current?.jwt || !isPostView(item)) return locking = true try { @@ -53,7 +53,7 @@ } async function pin(pinned: boolean, toInstance: boolean = false) { - if (!profile.data?.jwt || !isPostView(item)) return + if (!profile.current?.jwt || !isPostView(item)) return pinning = true @@ -88,9 +88,9 @@ {/snippet} - {#if (profile.data?.user && amMod(profile.data.user, item.community)) || (profile.data?.user && isAdmin(profile.data.user))} + {#if (profile.current?.user && amMod(profile.current.user, item.community)) || (profile.current?.user && isAdmin(profile.current.user))} - {#if !item.community.local && !amMod(profile.data.user, item.community)} + {#if !item.community.local && !amMod(profile.current.user, item.community)} {$t('moderation.labelInstanceOnly')} {:else} {$t('moderation.label')} @@ -124,7 +124,7 @@ ? $t('moderation.unfeature') : $t('moderation.feature')} - {#if isAdmin(profile.data.user)} + {#if isAdmin(profile.current.user)} {$t('form.post.community')} {/if}
@@ -139,7 +139,7 @@ {item.post.removed ? $t('moderation.restore') : $t('moderation.remove')} {/if}
- {#if profile.data?.user && profile.data.user.local_user_view.person.id != item.creator.id} + {#if profile.current?.user && profile.current.user.local_user_view.person.id != item.creator.id} @@ -164,7 +164,7 @@ {$t('moderation.votes')} {/if} - {#if profile.data?.user && isAdmin(profile.data.user)} + {#if profile.current?.user && isAdmin(profile.current.user)} {$t('admin.label')} - {#if !removed && profile.data?.user && (amMod(profile.data.user, item.community) || (isAdmin(profile.data.user) && item.community.local))} + {#if !removed && profile.current?.user && (amMod(profile.current.user, item.community) || (isAdmin(profile.current.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 465b9d09..d3a7aa2d 100644 --- a/src/lib/components/lemmy/moderation/ReportModal.svelte +++ b/src/lib/components/lemmy/moderation/ReportModal.svelte @@ -39,7 +39,7 @@ let reason = $state('') async function report() { - if (!item || !profile.data?.jwt || reason == '') return + if (!item || !profile.current?.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 9dcbba94..88e1be40 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -112,7 +112,7 @@ user={post.creator} published={publishedToDate(post.post.published)} {badges} - subscribed={profile.data?.user?.follows + subscribed={profile.current?.user?.follows .map(c => c.community.id) .includes(post.community.id) ? 'Subscribed' diff --git a/src/lib/components/lemmy/post/PostActions.svelte b/src/lib/components/lemmy/post/PostActions.svelte index df1d1774..0f7f1027 100644 --- a/src/lib/components/lemmy/post/PostActions.svelte +++ b/src/lib/components/lemmy/post/PostActions.svelte @@ -117,8 +117,8 @@ bind:score={post.counts.score} bind:upvotes={post.counts.upvotes} bind:downvotes={post.counts.downvotes} - showCounts={profile.data?.user?.local_user_view?.local_user?.show_scores ?? - true} + showCounts={profile.current?.user?.local_user_view?.local_user + ?.show_scores ?? true} /> {/snippet} {$t('nav.menu.label')} - {#if profile.data?.jwt} + {#if profile.current?.jwt} {#snippet prefix()} diff --git a/src/lib/components/ui/navbar/commands/Commands.svelte b/src/lib/components/ui/navbar/commands/Commands.svelte index f4417c33..c830e9e2 100644 --- a/src/lib/components/ui/navbar/commands/Commands.svelte +++ b/src/lib/components/ui/navbar/commands/Commands.svelte @@ -2,7 +2,7 @@ import { browser } from '$app/environment' import { goto } from '$app/navigation' import { page } from '$app/state' - import { profile, profileData } from '$lib/auth.svelte' + import { profile } from '$lib/auth.svelte' import { t } from '$lib/i18n/translations' import { resumables } from '$lib/lemmy/item' import { theme } from '$lib/ui/colors.svelte' @@ -23,8 +23,8 @@ $effect(() => { groups = getGroups( $resumables, - profile.data, - profileData.profiles, + profile.current, + profile.meta.profiles, theme.data, page.data.contextual?.actions, ) diff --git a/src/lib/components/ui/navbar/commands/actions.svelte.ts b/src/lib/components/ui/navbar/commands/actions.svelte.ts index 718073e7..57f7321d 100644 --- a/src/lib/components/ui/navbar/commands/actions.svelte.ts +++ b/src/lib/components/ui/navbar/commands/actions.svelte.ts @@ -1,6 +1,6 @@ import { goto } from '$app/navigation' import { page } from '$app/state' -import { setUserID, type Profile } from '$lib/auth.svelte' +import { profile as currentProfile, type ProfileInfo } from '$lib/auth.svelte' import { amModOfAny, isAdmin, @@ -64,8 +64,8 @@ export interface Action { export function getGroups( resumables: ResumableItem[], - profile: Profile, - profiles: Profile[], + profile: ProfileInfo, + profiles: ProfileInfo[], td: ThemeData, contextual?: Action[], ) { @@ -311,7 +311,7 @@ export function getGroups( detail: p.instance, handle: async () => { if (profile.id != p.id) { - await setUserID(p.id) + currentProfile.meta.profile = p.id } await goto(page.url, { diff --git a/src/lib/components/ui/sidebar/CommunityList.svelte b/src/lib/components/ui/sidebar/CommunityList.svelte index 6b70ecf7..8a433872 100644 --- a/src/lib/components/ui/sidebar/CommunityList.svelte +++ b/src/lib/components/ui/sidebar/CommunityList.svelte @@ -25,7 +25,7 @@ animate:flip={{ duration: 500, easing: expoOut }} >
- {#if profile && !hasFavorite(profile.data, follow.id)} + {#if profile && !hasFavorite(profile.current, follow.id)} - {:else if isFavorites && profile && hasFavorite(profile.data, follow.id)} + {:else if isFavorites && profile && hasFavorite(profile.current, follow.id)}
- {#if profileData.profiles} - {@const filtered = profileData.profiles.filter( + {#if profile.meta.profiles} + {@const filtered = profile.meta.profiles.filter( i => i.instance == page.params.instance, )}
diff --git a/src/routes/comment/[instance]/[id]/confirm/+page.ts b/src/routes/comment/[instance]/[id]/confirm/+page.ts index 937a8b40..df9980ee 100644 --- a/src/routes/comment/[instance]/[id]/confirm/+page.ts +++ b/src/routes/comment/[instance]/[id]/confirm/+page.ts @@ -2,6 +2,6 @@ import { profile } from '$lib/auth.svelte.js' import { redirect } from '@sveltejs/kit' export async function load({ params }) { - if (profile.data.instance == params.instance) + if (profile.current.instance == params.instance) redirect(302, `/comment/${params.instance}/${params.id}`) } diff --git a/src/routes/communities/Subscribe.svelte b/src/routes/communities/Subscribe.svelte index 2d160ef4..64ac5a34 100644 --- a/src/routes/communities/Subscribe.svelte +++ b/src/routes/communities/Subscribe.svelte @@ -32,7 +32,7 @@ id: number | undefined = community?.community.id, subscribed: SubscribedType | undefined = community?.subscribed, ) { - if (!profile.data?.jwt) return + if (!profile.current?.jwt) return if (!id || !subscribed) return subscribing = true diff --git a/src/routes/create/post/+page.svelte b/src/routes/create/post/+page.svelte index b1b7fbf7..80f1ecba 100644 --- a/src/routes/create/post/+page.svelte +++ b/src/routes/create/post/+page.svelte @@ -10,7 +10,7 @@ let { data } = $props() onMount(() => { - if (!profile.data?.jwt) { + if (!profile.current?.jwt) { goto('/login') } }) diff --git a/src/routes/inbox/+page.svelte b/src/routes/inbox/+page.svelte index 4dae89b9..241e35ec 100644 --- a/src/routes/inbox/+page.svelte +++ b/src/routes/inbox/+page.svelte @@ -19,7 +19,7 @@ let markingAsRead = $state(false) async function markAllAsRead() { - if (!profile.data?.user) { + if (!profile.current?.user) { goto('/login') return } @@ -156,7 +156,7 @@ items-center px-2 w-max top-6 lg:top-22" /> {:else} {#each data.inbox.value as item, index} - {#if item.creator.id != profile.data.user?.local_user_view.person.id} + {#if item.creator.id != profile.current.user?.local_user_view.person.id}
c.creator.id != c.recipient.id) // you messaged yourself .map(i => ({ user: - i.creator.id != profile.data.user?.local_user_view.person.id + i.creator.id != profile.current.user?.local_user_view.person.id ? i.creator : i.recipient, message: { @@ -153,8 +153,8 @@ bg-linear-to-r from-slate-700 via-slate-700 to-slate-700/0 dark:from-zinc-300 dark:via-zinc-300 dark:to-zinc-300/0 text-transparent bg-clip-text flex-1 overflow-hidden" > - {#if preview.message.last_sender == profile.data.user?.local_user_view.person.id} - {profile.data.user?.local_user_view.person.name}: + {#if preview.message.last_sender == profile.current.user?.local_user_view.person.id} + {profile.current.user?.local_user_view.person.name}: {/if} {preview.message.content}
diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte index e25fc7b6..4584cf85 100644 --- a/src/routes/login/+page.svelte +++ b/src/routes/login/+page.svelte @@ -3,7 +3,6 @@ import { goto } from '$app/navigation' import { page } from '$app/state' - import { setUser } from '$lib/auth.svelte.js' import ErrorContainer, { clearErrorScope, pushError, @@ -25,6 +24,7 @@ QuestionMarkCircle, UserCircle, } from 'svelte-hero-icons' + import { profile } from '$lib/auth.svelte' interface Props { ref?: string @@ -57,7 +57,7 @@ }) if (response?.jwt) { - const result = await setUser(response.jwt, data.instance, data.username) + const result = await profile.add(response.jwt, data.instance) if (result) { toast({ content: $t('toast.logIn'), type: 'success' }) diff --git a/src/routes/login/guest/+page.svelte b/src/routes/login/guest/+page.svelte index bb90d6b1..cf66f90c 100644 --- a/src/routes/login/guest/+page.svelte +++ b/src/routes/login/guest/+page.svelte @@ -3,7 +3,6 @@ import { goto } from '$app/navigation' import { page } from '$app/state' - import { profileData } from '$lib/auth.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte' import { t } from '$lib/i18n/translations' import { LINKED_INSTANCE_URL } from '$lib/instance.svelte' @@ -12,6 +11,7 @@ import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte' import { MINIMUM_VERSION } from '$lib/version' import { Button, Note, TextInput, toast } from 'mono-svelte' + import { profile } from '$lib/auth.svelte' interface Props { ref?: string @@ -23,7 +23,7 @@ let form = $state({ instance: '', - username: `${$t('account.guest')} ${profileData.profiles.filter(p => p.jwt == undefined).length + 1}`, + username: `${$t('account.guest')} ${profile.meta.profiles.filter(p => p.jwt == undefined).length + 1}`, loading: false, }) @@ -35,13 +35,13 @@ return } - const id = Math.max(...profileData.profiles.map(i => i.id)) + 1 - profileData.profiles.push({ + const id = Math.max(...profile.meta.profiles.map(i => i.id)) + 1 + profile.meta.profiles.push({ id: id, instance: form.instance, username: form.username, }) - profileData.profile = id + profile.meta.profile = id toast({ content: $t('toast.addAccount'), type: 'success' }) diff --git a/src/routes/moderation/+layout.ts b/src/routes/moderation/+layout.ts index ad029a68..5b3f1567 100644 --- a/src/routes/moderation/+layout.ts +++ b/src/routes/moderation/+layout.ts @@ -2,5 +2,5 @@ import { profile } from '$lib/auth.svelte' import { error } from '@sveltejs/kit' export function load() { - if (!profile.data.jwt) error(401) + if (!profile.current.jwt) error(401) } diff --git a/src/routes/moderation/+page.ts b/src/routes/moderation/+page.ts index 8ffb893e..751efe78 100644 --- a/src/routes/moderation/+page.ts +++ b/src/routes/moderation/+page.ts @@ -14,7 +14,7 @@ import { error } from '@sveltejs/kit' type ReportListType = 'unread' | 'all' export async function load({ url, fetch }) { - if (!profile.data.jwt) error(401) + if (!profile.current.jwt) error(401) const page = Number(url.searchParams.get('page')) || 1 const type: ReportListType = @@ -30,7 +30,7 @@ export async function load({ url, fetch }) { community_id: community, } - const admin = profile.data?.user ? isAdmin(profile!.data.user!) : false + const admin = profile.current?.user ? isAdmin(profile!.data.user!) : false const [posts, comments, messages] = await Promise.all([ client.listPostReports({ diff --git a/src/routes/moderation/Report.svelte b/src/routes/moderation/Report.svelte index b2786b28..9d3439eb 100644 --- a/src/routes/moderation/Report.svelte +++ b/src/routes/moderation/Report.svelte @@ -24,7 +24,7 @@ let resolving = $state(false) async function resolve() { - if (!profile.data?.jwt || !profile.data.user) return + if (!profile.current?.jwt || !profile.current.user) return resolving = true try { diff --git a/src/routes/moderation/communities/+page.svelte b/src/routes/moderation/communities/+page.svelte index 6399e1f0..a35009d7 100644 --- a/src/routes/moderation/communities/+page.svelte +++ b/src/routes/moderation/communities/+page.svelte @@ -10,7 +10,7 @@ {$t('nav.communities')}
- {#if !profile.data.user} + {#if !profile.current.user}
@@ -20,7 +20,7 @@ rounding="2xl" class="flex flex-col dark:bg-zinc-950 max-h-[32rem] overflow-auto" > - {#each profile.data.user.moderates as moderate} + {#each profile.current.user.moderates as moderate} {@const community = moderate.community}
searchParam(page.url, 'user', e?.id.toString() ?? '', 'page')} /> - {#if profile.data?.user && isAdmin(profile.data?.user)} + {#if profile.current?.user && isAdmin(profile.current?.user)} { if ( !(data.post.value.post_view.read && settings.markPostsAsRead) && - profile.data?.jwt + profile.current?.jwt ) { getClient().markPostAsRead({ read: settings.markPostsAsRead, diff --git a/src/routes/post/[instance]/[id=integer]/+page.ts b/src/routes/post/[instance]/[id=integer]/+page.ts index 8607f102..280bd315 100644 --- a/src/routes/post/[instance]/[id=integer]/+page.ts +++ b/src/routes/post/[instance]/[id=integer]/+page.ts @@ -28,7 +28,7 @@ function buildContext(thread?: string) { } export async function load({ params, url, fetch }) { - if (profile.data.instance != params.instance) + if (profile.current.instance != params.instance) redirect(302, resolveRoute('/post/[instance]/[id]/confirm', params)) // TODO use Lemmy profile default settings diff --git a/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte b/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte index f2f19f3f..3109b9a3 100644 --- a/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte +++ b/src/routes/post/[instance]/[id=integer]/CommentProvider.svelte @@ -51,7 +51,7 @@ }) -{#if profile.data?.jwt} +{#if profile.current?.jwt} {#if !commenting}