auth: update profile class to contain profileData as well
This commit is contained in:
13 files changed
+211
-238
No files matched your search
+155
-174
@@ -20,11 +20,6 @@ import { site } from './lemmy.svelte'
|
|||||||
import { errorMessage } from './lemmy/error'
|
import { errorMessage } from './lemmy/error'
|
||||||
import { publishedToDate } from './components/util/date'
|
import { publishedToDate } from './components/util/date'
|
||||||
|
|
||||||
const getDefaultProfile = (): Profile => ({
|
|
||||||
id: -1,
|
|
||||||
instance: profileData.defaultInstance ?? DEFAULT_INSTANCE_URL,
|
|
||||||
})
|
|
||||||
|
|
||||||
function getFromStorage<T>(key: string): T | undefined {
|
function getFromStorage<T>(key: string): T | undefined {
|
||||||
if (typeof localStorage == 'undefined') return undefined
|
if (typeof localStorage == 'undefined') return undefined
|
||||||
const lc = localStorage.getItem(key)
|
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)
|
return localStorage.setItem(key, stringify ? JSON.stringify(item) : item)
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Profile {
|
export interface ProfileInfo {
|
||||||
id: number
|
id: number
|
||||||
instance: string
|
instance: string
|
||||||
jwt?: string
|
jwt?: string
|
||||||
@@ -54,9 +49,9 @@ export interface Profile {
|
|||||||
* What gets stored in localstorage.
|
* What gets stored in localstorage.
|
||||||
*/
|
*/
|
||||||
interface ProfileData {
|
interface ProfileData {
|
||||||
profiles: Profile[]
|
profiles: ProfileInfo[]
|
||||||
|
// should be named currentId
|
||||||
profile: number
|
profile: number
|
||||||
defaultInstance?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Notifications {
|
interface Notifications {
|
||||||
@@ -68,8 +63,6 @@ interface Notifications {
|
|||||||
const getCookie = (key: string): string | undefined => {
|
const getCookie = (key: string): string | undefined => {
|
||||||
if (!browser) return 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
|
return document?.cookie
|
||||||
?.split(';')
|
?.split(';')
|
||||||
.map(c => c.trim())
|
.map(c => c.trim())
|
||||||
@@ -77,7 +70,8 @@ const getCookie = (key: string): string | undefined => {
|
|||||||
?.split('=')?.[1]
|
?.split('=')?.[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const profileData = $state<ProfileData>(
|
class Profile {
|
||||||
|
meta = $state<ProfileData>(
|
||||||
getFromStorage<ProfileData>('profileData') ?? {
|
getFromStorage<ProfileData>('profileData') ?? {
|
||||||
profiles: [
|
profiles: [
|
||||||
{
|
{
|
||||||
@@ -89,21 +83,88 @@ export const profileData = $state<ProfileData>(
|
|||||||
],
|
],
|
||||||
profile: 1,
|
profile: 1,
|
||||||
},
|
},
|
||||||
)
|
|
||||||
|
|
||||||
class CurrentProfile {
|
|
||||||
#current = $derived(
|
|
||||||
profileData.profiles.find(i => i.id == profileData.profile) ??
|
|
||||||
getDefaultProfile(),
|
|
||||||
)
|
)
|
||||||
|
#current = $derived(
|
||||||
|
this.meta.profiles.find(i => i.id == this.meta.profile) ??
|
||||||
|
this.getDefaultProfile(),
|
||||||
|
)
|
||||||
|
|
||||||
|
getDefaultProfile(): ProfileInfo {
|
||||||
|
return {
|
||||||
|
id: -1,
|
||||||
|
instance: DEFAULT_INSTANCE_URL,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
get current() {
|
||||||
return this.#current
|
return this.#current
|
||||||
}
|
}
|
||||||
set current(value) {
|
set current(value) {
|
||||||
if (!value) return
|
if (!value) return
|
||||||
const index = profileData.profiles.findIndex(i => i.id == value?.id)
|
const index = this.meta.profiles.findIndex(i => i.id == value?.id)
|
||||||
profileData.profiles[index] = value
|
this.meta.profiles[index] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchUserData() {
|
async fetchUserData() {
|
||||||
@@ -143,76 +204,26 @@ class CurrentProfile {
|
|||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export const profile = new CurrentProfile()
|
async checkInbox() {
|
||||||
|
const { user, jwt } = profile.current
|
||||||
|
if (!user || !jwt) throw new Error('checkInbox() called with invalid user')
|
||||||
|
|
||||||
export const notifications = writable<Notifications>({
|
const notifs = await getNotificationCount(
|
||||||
applications: 0,
|
jwt,
|
||||||
inbox: 0,
|
amModOfAny(user) ?? false,
|
||||||
reports: 0,
|
user ? isAdmin(user) : false,
|
||||||
})
|
)
|
||||||
|
|
||||||
$effect.root(() => {
|
return {
|
||||||
$effect(() => {
|
inbox: notifs.unreads,
|
||||||
const serialized = {
|
applications: notifs.applications,
|
||||||
...profileData,
|
reports: notifs.reports,
|
||||||
profiles: profileData.profiles.map(p => serializeUser(p)),
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setFromStorage('profileData', serialized)
|
async add(jwt: string, instance: string) {
|
||||||
if (serialized.profiles.length == 0) {
|
const user = await userFromJwt(jwt, instance)
|
||||||
profileData.profiles = [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
instance: DEFAULT_INSTANCE_URL,
|
|
||||||
username: t.get('account.guest') || 'Guest',
|
|
||||||
},
|
|
||||||
]
|
|
||||||
profileData.profile = 1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
if (profile.current.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)
|
.then(u => u)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
toast({ content: errorMessage(err as string), type: 'error' })
|
toast({ content: errorMessage(err as string), type: 'error' })
|
||||||
@@ -224,19 +235,74 @@ export async function setUser(jwt: string, inst: string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = Math.max(...profileData.profiles.map(p => p.id)) + 1
|
const id = Math.max(...this.meta.profiles.map(p => p.id)) + 1
|
||||||
profileData.profile = id
|
this.meta.profile = id
|
||||||
profileData.profiles.push({
|
this.meta.profiles.push({
|
||||||
id: id,
|
id: id,
|
||||||
instance: inst,
|
instance: instance,
|
||||||
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 user
|
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 Profile()
|
||||||
|
|
||||||
|
export const notifications = writable<Notifications>({
|
||||||
|
applications: 0,
|
||||||
|
inbox: 0,
|
||||||
|
reports: 0,
|
||||||
|
})
|
||||||
|
|
||||||
async function userFromJwt(
|
async function userFromJwt(
|
||||||
jwt: string,
|
jwt: string,
|
||||||
instance: string,
|
instance: string,
|
||||||
@@ -281,44 +347,13 @@ async function userFromJwt(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resetProfile() {
|
function serializeUser(user: ProfileInfo): ProfileInfo {
|
||||||
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 {
|
|
||||||
return {
|
return {
|
||||||
...user,
|
...user,
|
||||||
user: undefined,
|
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) {
|
async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) {
|
||||||
const unreadsPromise = getClient()
|
const unreadsPromise = getClient()
|
||||||
.getUnreadCount()
|
.getUnreadCount()
|
||||||
@@ -356,57 +391,3 @@ async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) {
|
|||||||
applications: applications,
|
applications: applications,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkInbox() {
|
|
||||||
const { user, jwt } = profile.current
|
|
||||||
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.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)
|
|
||||||
}
|
|
||||||
|
|
||||||
init()
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
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 { t } from '$lib/i18n/translations'
|
||||||
import { resumables } from '$lib/lemmy/item'
|
import { resumables } from '$lib/lemmy/item'
|
||||||
import { theme } from '$lib/ui/colors.svelte'
|
import { theme } from '$lib/ui/colors.svelte'
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
groups = getGroups(
|
groups = getGroups(
|
||||||
$resumables,
|
$resumables,
|
||||||
profile.current,
|
profile.current,
|
||||||
profileData.profiles,
|
profile.meta.profiles,
|
||||||
theme.data,
|
theme.data,
|
||||||
page.data.contextual?.actions,
|
page.data.contextual?.actions,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { setUserID, type Profile } from '$lib/auth.svelte'
|
import { profile as currentProfile, type ProfileInfo } from '$lib/auth.svelte'
|
||||||
import {
|
import {
|
||||||
amModOfAny,
|
amModOfAny,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
@@ -64,8 +64,8 @@ export interface Action {
|
|||||||
|
|
||||||
export function getGroups(
|
export function getGroups(
|
||||||
resumables: ResumableItem[],
|
resumables: ResumableItem[],
|
||||||
profile: Profile,
|
profile: ProfileInfo,
|
||||||
profiles: Profile[],
|
profiles: ProfileInfo[],
|
||||||
td: ThemeData,
|
td: ThemeData,
|
||||||
contextual?: Action[],
|
contextual?: Action[],
|
||||||
) {
|
) {
|
||||||
@@ -311,7 +311,7 @@ export function getGroups(
|
|||||||
detail: p.instance,
|
detail: p.instance,
|
||||||
handle: async () => {
|
handle: async () => {
|
||||||
if (profile.id != p.id) {
|
if (profile.id != p.id) {
|
||||||
await setUserID(p.id)
|
currentProfile.meta.profile = p.id
|
||||||
}
|
}
|
||||||
|
|
||||||
await goto(page.url, {
|
await goto(page.url, {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { profile, setUserID, type Profile } from '$lib/auth.svelte.js'
|
import { profile, type ProfileInfo } from '$lib/auth.svelte'
|
||||||
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||||
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
|
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
|
||||||
import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte'
|
import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte'
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
let switching: boolean = $state(false)
|
let switching: boolean = $state(false)
|
||||||
interface Props {
|
interface Props {
|
||||||
prof: Profile
|
prof: ProfileInfo
|
||||||
index: number
|
index: number
|
||||||
guest?: boolean
|
guest?: boolean
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
switching = true
|
switching = true
|
||||||
|
|
||||||
if (profile.current?.id != prof.id) {
|
if (profile.current?.id != prof.id) {
|
||||||
await setUserID(prof.id)
|
profile.meta.profile = prof.id
|
||||||
}
|
}
|
||||||
|
|
||||||
await goto(page.url, {
|
await goto(page.url, {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { notifications, profile, profileData } from '$lib/auth.svelte.js'
|
import { notifications, profile } from '$lib/auth.svelte.js'
|
||||||
import Expandable from '$lib/components/ui/Expandable.svelte'
|
import Expandable from '$lib/components/ui/Expandable.svelte'
|
||||||
import CommunityList from '$lib/components/ui/sidebar/CommunityList.svelte'
|
import CommunityList from '$lib/components/ui/sidebar/CommunityList.svelte'
|
||||||
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
{/snippet}
|
{/snippet}
|
||||||
</SidebarButton>
|
</SidebarButton>
|
||||||
{/if}
|
{/if}
|
||||||
{#if profileData.profiles.length >= 1}
|
{#if profile.meta.profiles.length >= 1}
|
||||||
<hr class="border-slate-200 dark:border-zinc-900 my-1" />
|
<hr class="border-slate-200 dark:border-zinc-900 my-1" />
|
||||||
|
|
||||||
<Expandable
|
<Expandable
|
||||||
@@ -101,14 +101,14 @@
|
|||||||
{$t('account.accounts')}
|
{$t('account.accounts')}
|
||||||
{#snippet action()}
|
{#snippet action()}
|
||||||
<span class="dark:text-white text-black">
|
<span class="dark:text-white text-black">
|
||||||
{profileData.profiles.length}
|
{profile.meta.profiles.length}
|
||||||
</span>
|
</span>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
</span>
|
</span>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
<div class="w-full flex gap-px flex-col">
|
<div class="w-full flex gap-px flex-col">
|
||||||
{#each [...profileData.profiles] as prof, index}
|
{#each [...profile.meta.profiles] as prof, index}
|
||||||
<div class="w-full">
|
<div class="w-full">
|
||||||
<ProfileButton {index} {prof} />
|
<ProfileButton {index} {prof} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import {
|
import { profile, type ProfileInfo } from '$lib/auth.svelte.js'
|
||||||
profile as currentProfile,
|
|
||||||
deleteProfile,
|
|
||||||
moveProfile,
|
|
||||||
type Profile,
|
|
||||||
profile,
|
|
||||||
profileData,
|
|
||||||
setUserID,
|
|
||||||
} from '$lib/auth.svelte.js'
|
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import DebugObject from '$lib/components/util/debug/DebugObject.svelte'
|
import DebugObject from '$lib/components/util/debug/DebugObject.svelte'
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
@@ -29,11 +21,11 @@
|
|||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
|
|
||||||
let debugging = $state(false)
|
let debugging = $state(false)
|
||||||
let debugProfile: Profile | undefined = $state(undefined)
|
let debugProfile: ProfileInfo | undefined = $state(undefined)
|
||||||
|
|
||||||
let removing = $state({
|
let removing = $state({
|
||||||
shown: false,
|
shown: false,
|
||||||
account: undefined as Profile | undefined,
|
account: undefined as ProfileInfo | undefined,
|
||||||
})
|
})
|
||||||
|
|
||||||
let switching = $state(-1)
|
let switching = $state(-1)
|
||||||
@@ -41,7 +33,7 @@
|
|||||||
let radioSelected = $state(profile.current.id)
|
let radioSelected = $state(profile.current.id)
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
switching = radioSelected
|
switching = radioSelected
|
||||||
setUserID(radioSelected)
|
profile.meta.profile = radioSelected
|
||||||
})
|
})
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
@@ -94,7 +86,7 @@
|
|||||||
<Button
|
<Button
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
removing.shown = false
|
removing.shown = false
|
||||||
if (removing.account) deleteProfile(removing.account.id)
|
if (removing.account) profile.remove(removing.account.id)
|
||||||
}}
|
}}
|
||||||
size="lg"
|
size="lg"
|
||||||
class="flex-1"
|
class="flex-1"
|
||||||
@@ -141,13 +133,13 @@
|
|||||||
{/snippet}
|
{/snippet}
|
||||||
</Header>
|
</Header>
|
||||||
<form class="accounts-grid mt-4 sm:mt-6 gap-4">
|
<form class="accounts-grid mt-4 sm:mt-6 gap-4">
|
||||||
{#each profileData.profiles as profile, index (profile.id)}
|
{#each profile.meta.profiles as p, index (p.id)}
|
||||||
<label>
|
<label>
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
id={profile.id.toString()}
|
id={p.id.toString()}
|
||||||
name="profile"
|
name="profile"
|
||||||
value={profile.id}
|
value={p.id}
|
||||||
class="hidden peer"
|
class="hidden peer"
|
||||||
bind:group={radioSelected}
|
bind:group={radioSelected}
|
||||||
/>
|
/>
|
||||||
@@ -158,26 +150,26 @@
|
|||||||
class={[
|
class={[
|
||||||
'flex flex-row items-center px-3 py-3 gap-2 transition-all duration-75 cursor-pointer relative',
|
'flex flex-row items-center px-3 py-3 gap-2 transition-all duration-75 cursor-pointer relative',
|
||||||
'peer-checked:ring-2',
|
'peer-checked:ring-2',
|
||||||
switching == profile.id && 'ring-slate-400 dark:ring-zinc-600',
|
switching == p.id && 'ring-slate-400 dark:ring-zinc-600',
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
<ProfileAvatar
|
<ProfileAvatar
|
||||||
{profile}
|
profile={p}
|
||||||
{index}
|
{index}
|
||||||
selected={currentProfile?.data.id == profile.id}
|
selected={profile?.current.id == p.id}
|
||||||
size={40}
|
size={40}
|
||||||
/>
|
/>
|
||||||
<div class="flex flex-col overflow-hidden">
|
<div class="flex flex-col overflow-hidden">
|
||||||
<span class="break-words font-medium text-base">
|
<span class="break-words font-medium text-base">
|
||||||
{profile.username}
|
{p.username}
|
||||||
{#if !profile.jwt}
|
{#if !p.jwt}
|
||||||
<Badge class="inline-grid w-6 h-6 p-0! place-items-center">
|
<Badge class="inline-grid w-6 h-6 p-0! place-items-center">
|
||||||
<Icon src={QuestionMarkCircle} size="16" micro />
|
<Icon src={QuestionMarkCircle} size="16" micro />
|
||||||
</Badge>
|
</Badge>
|
||||||
{/if}
|
{/if}
|
||||||
</span>
|
</span>
|
||||||
<span class="text-sm text-slate-600 dark:text-zinc-400">
|
<span class="text-sm text-slate-600 dark:text-zinc-400">
|
||||||
{profile.instance}
|
{p.instance}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<Menu placement="bottom-end">
|
<Menu placement="bottom-end">
|
||||||
@@ -203,7 +195,7 @@
|
|||||||
size="square-md"
|
size="square-md"
|
||||||
color="secondary"
|
color="secondary"
|
||||||
title={$t('account.moveUp')}
|
title={$t('account.moveUp')}
|
||||||
onclick={() => moveProfile(profile.id, true)}
|
onclick={() => profile.move(p.id, true)}
|
||||||
>
|
>
|
||||||
{#snippet prefix()}
|
{#snippet prefix()}
|
||||||
<Icon src={ChevronUp} size="16" mini />
|
<Icon src={ChevronUp} size="16" mini />
|
||||||
@@ -213,7 +205,7 @@
|
|||||||
size="square-md"
|
size="square-md"
|
||||||
color="secondary"
|
color="secondary"
|
||||||
title={$t('account.moveDown')}
|
title={$t('account.moveDown')}
|
||||||
onclick={() => moveProfile(profile.id, false)}
|
onclick={() => profile.move(p.id, false)}
|
||||||
>
|
>
|
||||||
{#snippet prefix()}
|
{#snippet prefix()}
|
||||||
<Icon src={ChevronDown} size="16" mini />
|
<Icon src={ChevronDown} size="16" mini />
|
||||||
@@ -223,7 +215,7 @@
|
|||||||
{#if settings.debugInfo}
|
{#if settings.debugInfo}
|
||||||
<MenuButton
|
<MenuButton
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
debugProfile = profile
|
debugProfile = p
|
||||||
debugging = !debugging
|
debugging = !debugging
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -233,10 +225,10 @@
|
|||||||
{$t('common.debug')}
|
{$t('common.debug')}
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
{/if}
|
{/if}
|
||||||
{#if !LINKED_INSTANCE_URL || profile.user}
|
{#if !LINKED_INSTANCE_URL || p.user}
|
||||||
<MenuButton
|
<MenuButton
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
removing.account = profile
|
removing.account = p
|
||||||
removing.shown = !removing.shown
|
removing.shown = !removing.shown
|
||||||
}}
|
}}
|
||||||
color="danger-subtle"
|
color="danger-subtle"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { resolveRoute } from '$app/paths'
|
import { resolveRoute } from '$app/paths'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { profile, profileData } from '$lib/auth.svelte'
|
import { profile } from '$lib/auth.svelte'
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
||||||
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||||
@@ -74,8 +74,8 @@
|
|||||||
{$t('routes.postRedirect.actions.back')}
|
{$t('routes.postRedirect.actions.back')}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{#if profileData.profiles}
|
{#if profile.meta.profiles}
|
||||||
{@const filtered = profileData.profiles.filter(
|
{@const filtered = profile.meta.profiles.filter(
|
||||||
i => i.instance == page.params.instance,
|
i => i.instance == page.params.instance,
|
||||||
)}
|
)}
|
||||||
<div class="space-y-1">
|
<div class="space-y-1">
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { setUser } from '$lib/auth.svelte.js'
|
|
||||||
import ErrorContainer, {
|
import ErrorContainer, {
|
||||||
clearErrorScope,
|
clearErrorScope,
|
||||||
pushError,
|
pushError,
|
||||||
@@ -25,6 +24,7 @@
|
|||||||
QuestionMarkCircle,
|
QuestionMarkCircle,
|
||||||
UserCircle,
|
UserCircle,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
|
import { profile } from '$lib/auth.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
ref?: string
|
ref?: string
|
||||||
@@ -57,7 +57,7 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (response?.jwt) {
|
if (response?.jwt) {
|
||||||
const result = await setUser(response.jwt, data.instance, data.username)
|
const result = await profile.add(response.jwt, data.instance)
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
toast({ content: $t('toast.logIn'), type: 'success' })
|
toast({ content: $t('toast.logIn'), type: 'success' })
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { profileData } from '$lib/auth.svelte'
|
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
|
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
|
||||||
@@ -12,6 +11,7 @@
|
|||||||
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte'
|
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte'
|
||||||
import { MINIMUM_VERSION } from '$lib/version'
|
import { MINIMUM_VERSION } from '$lib/version'
|
||||||
import { Button, Note, TextInput, toast } from 'mono-svelte'
|
import { Button, Note, TextInput, toast } from 'mono-svelte'
|
||||||
|
import { profile } from '$lib/auth.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
ref?: string
|
ref?: string
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
let form = $state({
|
let form = $state({
|
||||||
instance: '',
|
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,
|
loading: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -35,13 +35,13 @@
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = Math.max(...profileData.profiles.map(i => i.id)) + 1
|
const id = Math.max(...profile.meta.profiles.map(i => i.id)) + 1
|
||||||
profileData.profiles.push({
|
profile.meta.profiles.push({
|
||||||
id: id,
|
id: id,
|
||||||
instance: form.instance,
|
instance: form.instance,
|
||||||
username: form.username,
|
username: form.username,
|
||||||
})
|
})
|
||||||
profileData.profile = id
|
profile.meta.profile = id
|
||||||
|
|
||||||
toast({ content: $t('toast.addAccount'), type: 'success' })
|
toast({ content: $t('toast.addAccount'), type: 'success' })
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { resolveRoute } from '$app/paths'
|
import { resolveRoute } from '$app/paths'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { profile, profileData } from '$lib/auth.svelte'
|
import { profile } from '$lib/auth.svelte'
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
import ProfileButton from '$lib/components/ui/sidebar/ProfileButton.svelte'
|
||||||
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||||
@@ -74,8 +74,8 @@
|
|||||||
{$t('routes.postRedirect.actions.back')}
|
{$t('routes.postRedirect.actions.back')}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{#if profileData.profiles}
|
{#if profile.meta.profiles}
|
||||||
{@const filtered = profileData.profiles.filter(
|
{@const filtered = profile.meta.profiles.filter(
|
||||||
i => i.instance == page.params.instance,
|
i => i.instance == page.params.instance,
|
||||||
)}
|
)}
|
||||||
<div class="space-y-1">
|
<div class="space-y-1">
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { preventDefault } from 'svelte/legacy'
|
import { preventDefault } from 'svelte/legacy'
|
||||||
|
|
||||||
import { deleteProfile, profile, setUser } from '$lib/auth.svelte'
|
|
||||||
import { client } from '$lib/lemmy.svelte'
|
import { client } from '$lib/lemmy.svelte'
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
import { Button, TextInput, toast } from 'mono-svelte'
|
import { Button, TextInput, toast } from 'mono-svelte'
|
||||||
import { errorMessage } from '$lib/lemmy/error'
|
import { errorMessage } from '$lib/lemmy/error'
|
||||||
|
import { profile } from '$lib/auth.svelte'
|
||||||
|
|
||||||
let oldPassword = $state(''),
|
let oldPassword = $state(''),
|
||||||
newPassword = $state(''),
|
newPassword = $state(''),
|
||||||
@@ -21,9 +21,9 @@
|
|||||||
old_password: oldPassword,
|
old_password: oldPassword,
|
||||||
})
|
})
|
||||||
if (res?.jwt) {
|
if (res?.jwt) {
|
||||||
const { instance, username } = profile.current
|
const { instance } = profile.current
|
||||||
deleteProfile(profile.current.id)
|
profile.remove(profile.current.id)
|
||||||
await setUser(res.jwt, instance, username!)
|
await profile.add(res.jwt, instance)
|
||||||
|
|
||||||
toast({ content: $t('toast.loginRefresh'), type: 'success' })
|
toast({ content: $t('toast.loginRefresh'), type: 'success' })
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { profileData, setUserID } from '$lib/auth.svelte'
|
|
||||||
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
import { client } from '$lib/lemmy.svelte'
|
import { client } from '$lib/lemmy.svelte'
|
||||||
import { t } from '$lib/i18n/translations'
|
import { t } from '$lib/i18n/translations'
|
||||||
@@ -14,6 +13,7 @@
|
|||||||
TextInput,
|
TextInput,
|
||||||
toast,
|
toast,
|
||||||
} from 'mono-svelte'
|
} from 'mono-svelte'
|
||||||
|
import { profile } from '$lib/auth.svelte'
|
||||||
|
|
||||||
let deletion = $state({
|
let deletion = $state({
|
||||||
modal: false,
|
modal: false,
|
||||||
@@ -70,11 +70,11 @@
|
|||||||
delete_content: deletion.deleteContent,
|
delete_content: deletion.deleteContent,
|
||||||
})
|
})
|
||||||
|
|
||||||
profileData.profiles.splice(
|
profile.meta.profiles.splice(
|
||||||
profileData.profiles.findIndex(p => profileData.profile == p.id),
|
profile.meta.profiles.findIndex(p => profile.meta.profile == p.id),
|
||||||
)
|
)
|
||||||
|
|
||||||
setUserID(-1)
|
profile.meta.profile = -1
|
||||||
toast({
|
toast({
|
||||||
content: $t('toast.deleted'),
|
content: $t('toast.deleted'),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/state'
|
import { page } from '$app/state'
|
||||||
import { setUser } from '$lib/auth.svelte.js'
|
|
||||||
import ErrorContainer, {
|
import ErrorContainer, {
|
||||||
clearErrorScope,
|
clearErrorScope,
|
||||||
pushError,
|
pushError,
|
||||||
@@ -37,6 +36,7 @@
|
|||||||
QuestionMarkCircle,
|
QuestionMarkCircle,
|
||||||
XCircle,
|
XCircle,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
|
import { profile } from '$lib/auth.svelte.js'
|
||||||
|
|
||||||
let { data } = $props()
|
let { data } = $props()
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
const registrationMode = data.site_view.local_site.registration_mode
|
const registrationMode = data.site_view.local_site.registration_mode
|
||||||
|
|
||||||
if (res?.jwt) {
|
if (res?.jwt) {
|
||||||
await setUser(res.jwt, page.params.instance)
|
await profile.add(res.jwt, page.params.instance)
|
||||||
|
|
||||||
toast({ content: $t('toast.logIn'), type: 'success' })
|
toast({ content: $t('toast.logIn'), type: 'success' })
|
||||||
goto('/')
|
goto('/')
|
||||||
@@ -127,7 +127,7 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (res.jwt) {
|
if (res.jwt) {
|
||||||
await setUser(res.jwt, page.params.instance)
|
await profile.add(res.jwt, page.params.instance)
|
||||||
goto('/')
|
goto('/')
|
||||||
} else if (res.verify_email_sent) {
|
} else if (res.verify_email_sent) {
|
||||||
pushError({
|
pushError({
|
||||||
|
|||||||
Reference in new issue
Block a user