misc: update username on refreshed login
This commit is contained in:
1 parent
87af6183d0
commit
4dcbea4603
1 file changed
+27
-15
+27
-15
@@ -13,7 +13,13 @@ import {
|
|||||||
type MyUserInfo,
|
type MyUserInfo,
|
||||||
type Community,
|
type Community,
|
||||||
} from 'lemmy-js-client'
|
} from 'lemmy-js-client'
|
||||||
import { derived, get, writable, type Readable, type Writable } from 'svelte/store'
|
import {
|
||||||
|
derived,
|
||||||
|
get,
|
||||||
|
writable,
|
||||||
|
type Readable,
|
||||||
|
type Writable,
|
||||||
|
} from 'svelte/store'
|
||||||
import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js'
|
import { MINIMUM_VERSION, versionIsSupported } from '$lib/version.js'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
import { env } from '$env/dynamic/public'
|
||||||
@@ -57,7 +63,7 @@ interface ProfileData {
|
|||||||
defaultInstance?: string
|
defaultInstance?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PersonData extends MyUserInfo { }
|
interface PersonData extends MyUserInfo {}
|
||||||
|
|
||||||
interface Notifications {
|
interface Notifications {
|
||||||
inbox: number
|
inbox: number
|
||||||
@@ -93,7 +99,7 @@ export let profileData = writable<ProfileData>(
|
|||||||
|
|
||||||
let fetchUser = {
|
let fetchUser = {
|
||||||
loading: false,
|
loading: false,
|
||||||
prevProfile: -1
|
prevProfile: -1,
|
||||||
}
|
}
|
||||||
|
|
||||||
export let profile: Readable<Profile> = derived(
|
export let profile: Readable<Profile> = derived(
|
||||||
@@ -106,7 +112,7 @@ export let profile: Readable<Profile> = derived(
|
|||||||
set(profile)
|
set(profile)
|
||||||
|
|
||||||
if (profile?.jwt) {
|
if (profile?.jwt) {
|
||||||
if ((profile.id != fetchUser.prevProfile) && !fetchUser.loading) {
|
if (profile.id != fetchUser.prevProfile && !fetchUser.loading) {
|
||||||
site.set(undefined)
|
site.set(undefined)
|
||||||
|
|
||||||
fetchUser.loading = true
|
fetchUser.loading = true
|
||||||
@@ -125,6 +131,7 @@ export let profile: Readable<Profile> = derived(
|
|||||||
|
|
||||||
profile.user = res?.user
|
profile.user = res?.user
|
||||||
profile.avatar = res?.user?.local_user_view.person.avatar
|
profile.avatar = res?.user?.local_user_view.person.avatar
|
||||||
|
profile.username = res?.user?.local_user_view.person.name
|
||||||
|
|
||||||
fetchUser.loading = false
|
fetchUser.loading = false
|
||||||
fetchUser.prevProfile = profile.id
|
fetchUser.prevProfile = profile.id
|
||||||
@@ -146,7 +153,6 @@ export let profile: Readable<Profile> = derived(
|
|||||||
fetchUser.loading = false
|
fetchUser.loading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -223,7 +229,7 @@ export async function setUser(jwt: string, inst: string, username?: string) {
|
|||||||
})
|
})
|
||||||
if (!user?.user) {
|
if (!user?.user) {
|
||||||
toast({
|
toast({
|
||||||
content: 'Your instance\'s API did not return your user data.',
|
content: "Your instance's API did not return your user data.",
|
||||||
type: 'error',
|
type: 'error',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -232,14 +238,14 @@ export async function setUser(jwt: string, inst: string, username?: string) {
|
|||||||
|
|
||||||
profileData.update((pd) => {
|
profileData.update((pd) => {
|
||||||
// too lazy to make a decent system
|
// too lazy to make a decent system
|
||||||
const id = Math.max(...pd.profiles.map(p => p.id)) + 1
|
const id = Math.max(...pd.profiles.map((p) => p.id)) + 1
|
||||||
|
|
||||||
const newProfile: Profile = {
|
const newProfile: Profile = {
|
||||||
id: id,
|
id: id,
|
||||||
instance: inst,
|
instance: inst,
|
||||||
jwt: jwt,
|
jwt: jwt,
|
||||||
username: user?.user?.local_user_view.person.name,
|
username: user?.user?.local_user_view.person.name,
|
||||||
avatar: user?.user?.local_user_view.person.avatar
|
avatar: user?.user?.local_user_view.person.avatar,
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -254,7 +260,9 @@ export async function setUser(jwt: string, inst: string, username?: string) {
|
|||||||
async function userFromJwt(
|
async function userFromJwt(
|
||||||
jwt: string,
|
jwt: string,
|
||||||
instance: string
|
instance: string
|
||||||
): Promise<{ user: PersonData | undefined; site: GetSiteResponse } | undefined> {
|
): Promise<
|
||||||
|
{ user: PersonData | undefined; site: GetSiteResponse } | undefined
|
||||||
|
> {
|
||||||
const sitePromise = client({ instanceURL: instance, auth: jwt }).getSite()
|
const sitePromise = client({ instanceURL: instance, auth: jwt }).getSite()
|
||||||
|
|
||||||
let timer = setTimeout(
|
let timer = setTimeout(
|
||||||
@@ -262,15 +270,19 @@ async function userFromJwt(
|
|||||||
toast({
|
toast({
|
||||||
content: `Still loading your user data...`,
|
content: `Still loading your user data...`,
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
loading: true
|
loading: true,
|
||||||
}),
|
}),
|
||||||
5000
|
5000
|
||||||
)
|
)
|
||||||
|
|
||||||
const site = await sitePromise.then((r) => {
|
const site = await sitePromise
|
||||||
clearTimeout(timer)
|
.then((r) => {
|
||||||
return r
|
clearTimeout(timer)
|
||||||
}).catch((e) => { toast({ content: `Failed to contact the instance. ${e}` }) })
|
return r
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
toast({ content: `Failed to contact the instance. ${e}` })
|
||||||
|
})
|
||||||
|
|
||||||
if (!site) return
|
if (!site) return
|
||||||
|
|
||||||
@@ -319,7 +331,7 @@ instance.subscribe(async (i) => {
|
|||||||
const s = await new LemmyHttp(`https://${i}`).getSite()
|
const s = await new LemmyHttp(`https://${i}`).getSite()
|
||||||
|
|
||||||
site.set(s)
|
site.set(s)
|
||||||
} catch (e) { }
|
} catch (e) {}
|
||||||
})
|
})
|
||||||
|
|
||||||
export async function setUserID(id: number) {
|
export async function setUserID(id: number) {
|
||||||
|
|||||||
Reference in new issue
Block a user