diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 6617c494..0105f0ce 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -4,7 +4,8 @@ import { } from '$lib/components/lemmy/moderation/moderation.js' import { toast } from 'mono-svelte' import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js' -import { client, getClient, site } from '$lib/lemmy.js' +import { client, getClient } from '$lib/lemmy.js' +import { site } from './lemmy' import { userSettings } from '$lib/settings.js' import { instanceToURL, moveItem } from '$lib/util.js' import { @@ -13,7 +14,7 @@ import { type MyUserInfo, type Community, } from 'lemmy-js-client' -import { get, writable } from 'svelte/store' +import { derived, get, writable, type Writable } from 'svelte/store' import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js' import { browser } from '$app/environment' import { env } from '$env/dynamic/public' @@ -91,19 +92,53 @@ export let profileData = writable( } ) -// stupid hack to get dev server working -// why does this always happen to me +export let profile = derived, Profile>( + profileData, + (pd) => { + const profile = + pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() -let initialInstance = get(profileData).defaultInstance + if (profile?.jwt) { + if (profile.user) { + site.set(undefined) + + userFromJwt(profile.jwt, profile.instance).then((res) => { + if (!res?.user) + toast({ + content: 'Your login has expired. Re-login to fix this issue.', + type: 'warning', + }) + + site.set(res?.site) + + profile.user = res?.user + }) + } + } else { + site.set(undefined) + client({ instanceURL: profile.instance }) + .getSite() + .then((res) => site.set(res)) + } + + instance.set(profile.instance) + + return profile + } +) profileData.subscribe(async (pd) => { - setFromStorage('profileData', pd) + const serialized: ProfileData = { + ...pd, + profiles: pd.profiles.map((p) => serializeUser(p)), + } - if (pd.profile == -1 && initialInstance != pd.defaultInstance) { - initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL + setFromStorage('profileData', serialized) + + if (serialized.profile == -1) { instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) } - if (pd.profiles.length == 0) { + if (serialized.profiles.length == 0) { profileData.update((pd) => ({ ...pd, profiles: [ @@ -145,43 +180,6 @@ if ( } } -export let profile = writable(getProfile()) - -profile.subscribe(async (p) => { - if (p?.id == -1) { - instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) - } - if (!p) return - - instance.set(p.instance) - // fetch the user because p.user is undefined - if (p?.jwt && !p?.user) { - const user = await userFromJwt(p.jwt, p.instance) - .then((user) => { - if (!user?.user) - toast({ - content: 'Your login has expired. Re-login to fix this issue.', - type: 'warning', - }) - site.set(user?.site) - - return user - }) - .catch((err) => { - toast({ content: err as any, type: 'error' }) - }) - - if (!user?.user) return - - profile.update(() => ({ - ...p, - user: user!.user, - username: user?.user.local_user_view.person.name, - avatar: user?.user.local_user_view.person.avatar, - })) - } -}) - export async function setUser(jwt: string, inst: string, username: string) { try { new URL(instanceToURL(inst)) @@ -217,11 +215,6 @@ export async function setUser(jwt: string, inst: string, username: string) { avatar: user.user.local_user_view.person.avatar, } - profile.set({ - ...newProfile, - user: user!.user, - }) - return { profile: id, profiles: [newProfile, ...pd.profiles], @@ -273,20 +266,7 @@ async function userFromJwt( } } -function getProfile() { - const id = get(profileData).profile - - if (id == -1) { - return getDefaultProfile() - } - - const pd = get(profileData) - - return pd.profiles.find((p) => p.id == id) -} - export function resetProfile() { - profile.set(getDefaultProfile()) profileData.update((p) => ({ ...p, profile: -1 })) } @@ -308,10 +288,12 @@ export function deleteProfile(id: number) { } } -const serializeUser = (user: Profile): Profile => ({ - ...user, - user: undefined, -}) +function serializeUser(user: Profile): Profile { + return { + ...user, + user: undefined, + } +} instance.subscribe(async (i) => { try { @@ -324,35 +306,8 @@ instance.subscribe(async (i) => { }) export async function setUserID(id: number) { - const pd = get(profileData) - if (id == -1) { - resetProfile() - instance.set(DEFAULT_INSTANCE_URL) - return - } - - let prof = pd.profiles.find((p) => p.id == id) - - if (!prof) return profile.update(() => getDefaultProfile()) - prof = serializeUser(prof) - profileData.update((p) => ({ ...p, profile: id })) - - if (prof?.jwt) { - const user = await userFromJwt(prof.jwt, prof.instance) - .then((u) => u) - .catch((err) => { - toast({ content: err as any, type: 'error' }) - }) - instance.set(prof.instance) - prof.user = user?.user - prof.avatar = user?.user.local_user_view.person.avatar - site.set(user?.site) - } - - profile.update(() => prof ?? getDefaultProfile()) - - return prof + return get(profile) } export function moveProfile(id: number, up: boolean) { @@ -369,11 +324,7 @@ export function moveProfile(id: number, up: boolean) { } } -const getNotificationCount = async ( - jwt: string, - mod: boolean, - admin: boolean -) => { +async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { const unreads = await getClient().getUnreadCount() let reports: number = 0 @@ -427,11 +378,6 @@ async function checkInbox() { applications: notifs.applications, reports: notifs.reports, } - - profile.update((p) => ({ - ...p!, - user: user, - })) } setInterval(checkInbox, 4 * 60 * 1000) diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index b4d3ba05..09298d49 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -48,7 +48,7 @@ async function customFetch( if (auth && url) url.searchParams.set('auth', auth) const res = await f(url ?? input, init) - if (!res.ok) error(res.status, await res.text()); + if (!res.ok) error(res.status, await res.text()) return res } @@ -101,27 +101,33 @@ export async function validateInstance(instance: string): Promise { } } -export function mayBeIncompatible(minVersion: string, availableVersion: string) { - if (minVersion.valueOf() === availableVersion.valueOf()) return false; +export function mayBeIncompatible( + minVersion: string, + availableVersion: string +) { + if (minVersion.valueOf() === availableVersion.valueOf()) return false - const versionFormatter = /[\d.]/; - if (!minVersion.match(versionFormatter) || !availableVersion.match(availableVersion)) { - return true; + const versionFormatter = /[\d.]/ + if ( + !minVersion.match(versionFormatter) || + !availableVersion.match(availableVersion) + ) { + return true } - const splitMinVersion = minVersion.split('.'); - const splitAvailableVersion = availableVersion.split('.'); + const splitMinVersion = minVersion.split('.') + const splitAvailableVersion = availableVersion.split('.') - if (splitMinVersion.length !== splitAvailableVersion.length) return true; + if (splitMinVersion.length !== splitAvailableVersion.length) return true for (let i = 0; i < splitMinVersion.length; ++i) { - const minVersionDigit = parseInt(splitMinVersion[i]); - const availableVersionDigit = parseInt(splitAvailableVersion[i]); + const minVersionDigit = parseInt(splitMinVersion[i]) + const availableVersionDigit = parseInt(splitAvailableVersion[i]) - if (availableVersionDigit === undefined) return true; - if (minVersionDigit < availableVersionDigit) return false; - if (availableVersionDigit < minVersionDigit) return true; + if (availableVersionDigit === undefined) return true + if (minVersionDigit < availableVersionDigit) return false + if (availableVersionDigit < minVersionDigit) return true } - return false; + return false } diff --git a/src/lib/util.ts b/src/lib/util.ts index 99de8775..4411c00b 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -5,7 +5,8 @@ import { get } from 'svelte/store' import type { SubscribedType } from 'lemmy-js-client' import { page } from '$app/stores' import { feature } from '$lib/version.js' -import { client, site } from '$lib/lemmy.js' +import { client } from '$lib/lemmy.js' +import { site } from './lemmy' export const findClosestNumber = (numbers: number[], target: number): number => numbers.reduce((prev, curr) => diff --git a/src/routes/admin/+layout.ts b/src/routes/admin/+layout.ts index 4f057387..8e370c83 100644 --- a/src/routes/admin/+layout.ts +++ b/src/routes/admin/+layout.ts @@ -1,6 +1,7 @@ import { profile } from '$lib/auth.js' import { get } from 'svelte/store' -import { getClient, site as siteStore } from '$lib/lemmy.js' +import { getClient } from '$lib/lemmy.js' +import { site as siteStore } from '$lib/site' export async function load({ fetch }) { if (!get(profile)) return diff --git a/src/routes/login/guest/+page.svelte b/src/routes/login/guest/+page.svelte index 112f45cb..a41b1373 100644 --- a/src/routes/login/guest/+page.svelte +++ b/src/routes/login/guest/+page.svelte @@ -34,10 +34,6 @@ username: form.username, } - profile.set({ - ...newProfile, - }) - return { profile: id, profiles: [...pd.profiles, newProfile],