diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 6617c494..6e0b54b3 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' @@ -57,12 +58,12 @@ interface ProfileData { defaultInstance?: string } -interface PersonData extends MyUserInfo { - notifications: { - inbox: number - reports: number - applications: number - } +interface PersonData extends MyUserInfo {} + +interface Notifications { + inbox: number + reports: number + applications: number } const getCookie = (key: string): string | undefined => { @@ -91,19 +92,75 @@ export let profileData = writable( } ) -// stupid hack to get dev server working -// why does this always happen to me +let fetchingUser = false -let initialInstance = get(profileData).defaultInstance +export let profile = derived, Profile>( + profileData, + (pd) => { + const profile = + pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() + + if (profile?.jwt) { + if (!profile.user && !fetchingUser) { + site.set(undefined) + + fetchingUser = true + + notifications.set({ applications: 0, inbox: 0, reports: 0 }) + + 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 + profile.avatar = res?.user.local_user_view.person.avatar + + checkInbox() + + fetchingUser = false + }) + .catch((e) => { + fetchingUser = false + toast({ content: e, type: 'error' }) + }) + } + } else { + site.set(undefined) + client({ instanceURL: profile.instance }) + .getSite() + .then((res) => site.set(res)) + } + + instance.set(profile.instance) + + return profile + } +) + +export let notifications = writable({ + applications: 0, + inbox: 0, + reports: 0, +}) 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 +202,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 +237,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], @@ -261,32 +276,12 @@ async function userFromJwt( if (!myUser) return undefined return { - user: { - notifications: { - applications: 0, - inbox: 0, - reports: 0, - }, - ...myUser, - }, + user: myUser, site: site, } } -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 +303,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 +321,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 +339,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 @@ -419,26 +385,14 @@ async function checkInbox() { const notifs = await getNotificationCount( jwt, amModOfAny(user) ?? false, - isAdmin(user) + user ? isAdmin(user) : false ) - user.notifications = { + notifications.set({ inbox: notifs.unreads, applications: notifs.applications, reports: notifs.reports, - } - - profile.update((p) => ({ - ...p!, - user: user, - })) + }) } setInterval(checkInbox, 4 * 60 * 1000) - -let prevId: number | undefined = -2 -profile.subscribe((p) => { - if (p?.id == prevId || !p?.user) return - prevId = p?.id - if (p.id != -1) checkInbox() -}) diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index 0d9f2f5d..894b7f3d 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -55,6 +55,11 @@ } + + {#if embed_title && !compact}
diff --git a/src/lib/components/lemmy/post/PostMeta.svelte b/src/lib/components/lemmy/post/PostMeta.svelte index 363e91aa..1e8dbeb4 100644 --- a/src/lib/components/lemmy/post/PostMeta.svelte +++ b/src/lib/components/lemmy/post/PostMeta.svelte @@ -45,6 +45,10 @@ } +
+{:else} +
{/if}