feat: switch to new account system

This commit is contained in:
Xyphyn committed 2023-08-01 13:00:06 -07:00
1 parent 54ab6fdc82
commit 2da415b670
38 files changed
+222 -362

No files matched your search

+16 -9
View File
@@ -1,9 +1,15 @@
import { env } from '$env/dynamic/public'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js'
import { getClient, instance } from '$lib/lemmy.js'
import { userSettings } from '$lib/settings.js'
import type { MyUserInfo } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
const LINKED_INSTANCE_URL = env.PUBLIC_INSTANCE_URL
const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml'
function getFromStorage<T>(key: string): T | undefined {
if (typeof localStorage == 'undefined') return undefined
const lc = localStorage.getItem(key)
if (!lc) return undefined
@@ -11,6 +17,7 @@ function getFromStorage<T>(key: string): T | undefined {
}
function setFromStorage(key: string, item: any) {
if (typeof localStorage == 'undefined') return
return localStorage.setItem(key, JSON.stringify(item))
}
@@ -46,15 +53,13 @@ profileData.subscribe((pd) => {
export const profile = writable<Profile | undefined>(getProfile())
profile.subscribe(async (p) => {
if (p?.instance) {
instance.set(p.instance)
}
if (!p || !p.jwt) {
profileData.update((pd) => ({ ...pd, profile: -1 }))
return
}
if (p.user) return
instance.set(p.instance)
// fetch the user because p.user is undefined
const user = await userFromJwt(p.jwt, p.instance)
@@ -134,11 +139,13 @@ function getProfile() {
return pd.profiles.find((p) => p.id == id)
}
const getDefaultProfile = () => ({
id: -1,
instance: get(instance),
})
export function resetProfile() {
profile.set({
id: -1,
instance: DEFAULT_INSTANCE_URL,
})
profile.set(getDefaultProfile())
profileData.update((p) => ({ ...p, profile: -1 }))
}
@@ -168,7 +175,7 @@ export function setUserID(id: number) {
}
const prof = pd.profiles.find((p) => p.id == id)
profile.set(prof)
profile.set(prof ?? getDefaultProfile())
profileData.update((p) => ({ ...p, profile: id }))
return prof
@@ -11,16 +11,13 @@
import type { CommentNodeI } from './comments'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import CommentForm from './CommentForm.svelte'
import { page } from '$app/stores'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte'
import { createEventDispatcher, getContext } from 'svelte'
import type { CommentView } from 'lemmy-js-client'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { authData, getClient } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import TextArea from '$lib/components/input/TextArea.svelte'
import { profile } from '$lib/auth.js'
export let node: CommentNodeI
export let postId: number
@@ -39,11 +36,11 @@
bind:open={editing}
action="Save"
on:action={async () => {
if (!$authData || newComment.length <= 0) return
if (!$profile?.jwt || newComment.length <= 0) return
try {
await getClient().editComment({
auth: $authData.token,
auth: $profile.jwt,
comment_id: node.comment_view.comment.id,
content: newComment,
})
@@ -15,7 +15,7 @@
PencilSquare,
Trash,
} from 'svelte-hero-icons'
import { authData, getClient, user } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
@@ -25,12 +25,13 @@
import { amMod, report } from '$lib/components/lemmy/moderation/moderation.js'
import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte'
import CommentModerationMenu from '$lib/components/lemmy/moderation/CommentModerationMenu.svelte'
import { profile } from '$lib/auth.js'
export let comment: CommentView
export let replying: boolean = false
async function save() {
if (!$authData) return
if (!$profile?.jwt) return
const saved = comment.saved
@@ -38,7 +39,7 @@
try {
await getClient().saveComment({
auth: $authData.token,
auth: $profile.jwt,
comment_id: comment.comment.id,
save: !saved,
})
@@ -70,7 +71,7 @@
<Icon src={ArrowUturnLeft} width={14} height={14} mini />
<span class="text-xs">Reply</span>
</Button>
{#if $user && amMod($user, comment.community)}
{#if $profile?.user && amMod($profile?.user, comment.community)}
<CommentModerationMenu item={comment} />
{/if}
<Menu
@@ -89,8 +90,8 @@
<Icon src={EllipsisHorizontal} width={16} height={16} mini slot="icon" />
</Button>
<span class="text-xs opacity-80 py-1 my-1 px-4">Comment actions</span>
{#if $authData?.token}
{#if comment.creator.id == $user?.local_user_view.person.id}
{#if $profile?.jwt}
{#if comment.creator.id == $profile.user?.local_user_view.person.id}
<MenuButton on:click={() => dispatcher('edit', comment)}>
<Icon src={PencilSquare} mini size="16" />
<span>Edit</span>
@@ -100,14 +101,14 @@
<Icon src={comment.saved ? BookmarkSlash : Bookmark} mini size="16" />
<span>{comment.saved ? 'Unsave' : 'Save'}</span>
</MenuButton>
{#if $user && $authData && isCommentMutable(comment, $user.local_user_view)}
{#if $profile?.user && $profile.jwt && isCommentMutable(comment, $profile.user.local_user_view)}
<MenuButton
color="dangerSecondary"
on:click={async () => {
if (!$authData) return
if (!$profile?.jwt) return
try {
await getClient().deleteComment({
auth: $authData.token,
auth: $profile.jwt,
comment_id: comment.comment.id,
deleted: true,
})
@@ -127,7 +128,7 @@
<span>Delete</span>
</MenuButton>
{/if}
{#if $authData && $user?.local_user_view.person.id != comment.creator.id}
{#if $profile.jwt && $profile.user?.local_user_view.person.id != comment.creator.id}
<MenuButton on:click={() => report(comment)} color="dangerSecondary">
<Icon src={Flag} mini size="16" />
<span>Report</span>
@@ -2,12 +2,13 @@
import Button from '$lib/components/input/Button.svelte'
import { Color } from '$lib/ui/colors.js'
import type { CommentResponse } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import { createEventDispatcher } from 'svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import TextArea from '$lib/components/input/TextArea.svelte'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import { profile } from '$lib/auth.js'
export let postId: number
export let parentId: number | undefined = undefined
@@ -25,13 +26,13 @@
let preview = false
async function submit() {
if (!$user || !$authData || value == '') return
if (!$profile?.user || !$profile?.jwt || value == '') return
loading = true
try {
const response = await getClient().createComment({
auth: $authData.token,
auth: $profile.jwt,
content: value,
post_id: postId,
parent_id: parentId,
@@ -1,15 +1,16 @@
<script lang="ts">
import { Color } from '$lib/ui/colors'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
import { authData, getClient } from '$lib/lemmy'
import { getClient } from '$lib/lemmy'
import { userSettings } from '$lib/settings'
import { profile } from '$lib/auth.js'
export let vote: number = 0
export let score: number
export let commentId: number
async function upvote() {
if (!$authData) return
if (!$profile?.jwt) return
const upvoted = vote == 1
@@ -26,14 +27,14 @@
await getClient()
.likeComment({
score: upvoted ? 0 : 1,
auth: $authData.token,
auth: $profile.jwt,
comment_id: commentId,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
if (!$profile?.jwt) return
const upvoted = vote == -1
@@ -50,7 +51,7 @@
await getClient()
.likeComment({
score: upvoted ? 0 : -1,
auth: $authData.token,
auth: $profile.jwt,
comment_id: commentId,
})
.catch((_) => undefined)
@@ -5,12 +5,11 @@
import { onMount, setContext } from 'svelte'
import Button from '$lib/components/input/Button.svelte'
import { ChevronDown, Icon } from 'svelte-hero-icons'
import { authData, getClient } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import type { CommentView, Post } from 'lemmy-js-client'
import { fly } from 'svelte/transition'
import { toast, ToastType } from '$lib/components/ui/toasts/toasts.js'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import { profile } from '$lib/auth.js'
export let nodes: CommentNodeI[]
export let isParent: boolean
@@ -44,7 +43,7 @@
parent.loading = true
const newComments = await getClient($page.params.instance).getComments({
auth: $authData?.token,
auth: $profile?.jwt,
max_depth: 5,
parent_id: parent.comment_view.comment.id,
type_: 'All',
@@ -1,4 +1,5 @@
<script lang="ts">
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
@@ -7,8 +8,7 @@
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { authData, getClient, user } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import { getClient } from '$lib/lemmy.js'
import type { CommunityView } from 'lemmy-js-client'
import {
Calendar,
@@ -26,7 +26,7 @@
}
async function subscribe() {
if (!$authData) return
if (!$profile?.jwt) return
loading.subscribing = true
const subscribed =
community_view.subscribed == 'Subscribed' ||
@@ -34,7 +34,7 @@
try {
await getClient().followCommunity({
auth: $authData.token,
auth: $profile.jwt,
community_id: community_view.community.id,
follow: !subscribed,
})
@@ -47,13 +47,13 @@
loading.subscribing = false
}
async function block() {
if (!$authData) return
if (!$profile?.jwt) return
loading.blocking = true
const blocked = community_view.blocked
try {
await getClient().blockCommunity({
auth: $authData.token,
auth: $profile.jwt,
community_id: community_view.community.id,
block: !blocked,
})
@@ -100,7 +100,7 @@
<FormattedNumber number={community_view.counts.comments} />
</span>
</div>
{#if $authData}
{#if $profile?.jwt}
<div class="w-full mt-2 flex flex-col gap-2">
<Button
disabled={loading.subscribing}
@@ -5,9 +5,10 @@
import Avatar from '$lib/components/ui/Avatar.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import type { Community, Person, PersonView } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import TextInput from '$lib/components/input/TextInput.svelte'
import { profile } from '$lib/auth.js'
export let open = false
let item: Person | undefined
@@ -27,7 +28,7 @@
$: if (item) resetReason()
async function submit() {
if (!item || !$user || !community || !$authData) return
if (!item || !$profile?.user || !community || !$profile?.jwt) return
loading = true
@@ -59,7 +60,7 @@
}
await getClient().banFromCommunity({
auth: $authData.token,
auth: $profile.jwt,
ban: !banned,
community_id: community.id,
person_id: item.id,
@@ -1,5 +1,5 @@
<script lang="ts">
import { authData, getClient, user } from '$lib/lemmy'
import { getClient } from '$lib/lemmy'
import type {
CommentView,
Community,
@@ -13,6 +13,7 @@
import { Icon, ShieldExclamation, Trash } from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment } from '$lib/lemmy/item.js'
import { profile } from '$lib/auth.js'
export let item: PostView | CommentView
</script>
@@ -53,7 +54,7 @@
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
{#if $user && $user.local_user_view.person.id != item.creator.id}
{#if $profile.user && $profile.user?.local_user_view.person.id != item.creator.id}
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
User
</span>
@@ -1,11 +1,6 @@
<script lang="ts">
import { authData, getClient, user } from '$lib/lemmy'
import type {
CommentView,
Community,
CommunityView,
PostView,
} from 'lemmy-js-client'
import { getClient } from '$lib/lemmy'
import type { CommentView, Community, PostView } from 'lemmy-js-client'
import { amMod, ban, remove } from './moderation'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import Button from '$lib/components/input/Button.svelte'
@@ -21,6 +16,7 @@
import { Color } from '$lib/ui/colors.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
export let item: PostView | CommentView
@@ -30,12 +26,12 @@
$: acting = locking || pinning
async function lock(lock: boolean) {
if (!$authData || !isPost(item)) return
if (!$profile?.jwt || !isPost(item)) return
locking = true
try {
await getClient().lockPost({
auth: $authData.token,
auth: $profile.jwt,
locked: lock,
post_id: item.post.id,
})
@@ -59,14 +55,14 @@
}
async function pin(pinned: boolean) {
if (!$authData || !isPost(item)) return
if (!$profile?.jwt || !isPost(item)) return
pinning = true
try {
await getClient().featurePost({
feature_type: 'Community',
auth: $authData.token,
auth: $profile.jwt,
featured: pinned,
post_id: item.post.id,
})
@@ -149,7 +145,7 @@
{item.post.removed ? 'Restore' : 'Remove'}
{/if}
</MenuButton>
{#if $user && $user.local_user_view.person.id != item.creator.id}
{#if $profile?.user && $profile.user.local_user_view.person.id != item.creator.id}
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
User
</span>
@@ -5,9 +5,10 @@
import Post from '$lib/components/lemmy/post/Post.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { authData, getClient } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { profile } from '$lib/auth.js'
export let open: boolean
export let item: PostView | CommentView | undefined = undefined
@@ -23,14 +24,14 @@
async function remove() {
if (!item) return
if (!$authData) throw new Error('Unauthenticated')
if (!$profile?.jwt) throw new Error('Unauthenticated')
loading = true
try {
if (isComment(item)) {
await getClient().removeComment({
auth: $authData.token,
auth: $profile.jwt,
comment_id: item.comment.id,
removed: !removed,
reason: reason || undefined,
@@ -38,7 +39,7 @@
item.comment.removed = !removed
} else if (isPost(item)) {
await getClient().removePost({
auth: $authData.token,
auth: $profile.jwt,
post_id: item.post.id,
removed: !removed,
reason: reason || undefined,
@@ -5,8 +5,9 @@
import Post from '$lib/components/lemmy/post/Post.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { authData, getClient } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { profile } from '$lib/auth.js'
export let open: boolean
export let item: PostView | CommentView | undefined = undefined
@@ -21,19 +22,19 @@
let reason = ''
async function report() {
if (!item || !$authData || reason == '') return
if (!item || !$profile?.jwt || reason == '') return
loading = true
try {
if (isComment(item)) {
await getClient().createCommentReport({
auth: $authData.token,
auth: $profile.jwt,
comment_id: item.comment.id,
reason: reason,
})
} else if (isPost(item)) {
await getClient().createPostReport({
auth: $authData.token,
auth: $profile.jwt,
post_id: item.post.id,
reason: reason,
})
@@ -2,8 +2,6 @@
import type { PostView } from 'lemmy-js-client'
import PostVote from './PostVote.svelte'
import { getClient, getInstance } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import Link from '$lib/components/input/Link.svelte'
import {
Bookmark,
BookmarkSlash,
@@ -22,25 +20,24 @@
import Button from '$lib/components/input/Button.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { page } from '$app/stores'
import { user, authData } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { isMutable } from '$lib/components/lemmy/post/helpers.js'
import { createEventDispatcher } from 'svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import PostForm from '$lib/components/lemmy/post/PostForm.svelte'
import { amMod, report } from '$lib/components/lemmy/moderation/moderation.js'
import ModerationMenu from '$lib/components/lemmy/moderation/ModerationMenu.svelte'
import { profile } from '$lib/auth.js'
export let post: PostView
async function deletePost() {
if (!$authData) return
if (!$profile?.jwt) return
const deleted = post.post.deleted
try {
await getClient().deletePost({
auth: $authData.token,
auth: $profile.jwt,
deleted: !deleted,
post_id: post.post.id,
})
@@ -58,7 +55,7 @@
}
async function save(post: PostView) {
if (!$authData) return
if (!$profile?.jwt) return
const saved = post.saved
@@ -66,7 +63,7 @@
try {
await getClient().savePost({
auth: $authData.token,
auth: $profile.jwt,
post_id: post.post.id,
save: !saved,
})
@@ -117,7 +114,7 @@
<FormattedNumber number={post.counts.comments} />
</Button>
<div class="ml-auto" />
{#if $user && amMod($user, post.community)}
{#if $profile?.user && amMod($profile.user, post.community)}
<ModerationMenu item={post} community={post.community} />
{/if}
<Menu alignment="bottom-right" class="overflow-auto" let:toggleOpen>
@@ -148,7 +145,7 @@
</MenuButton>
<hr class="w-[90%] mx-auto opacity-100 dark:opacity-10 my-2" />
<li class="mx-4 text-xs opacity-80 text-left my-1 py-1">Actions</li>
{#if $user && $user.local_user_view.person.id == post.creator.id}
{#if $profile?.user && $profile.user.local_user_view.person.id == post.creator.id}
<MenuButton on:click={() => (editing = true)}>
<Icon src={PencilSquare} width={16} mini />
Edit
@@ -164,18 +161,18 @@
<Icon src={Square2Stack} width={16} mini />
Copy Link
</MenuButton>
{#if $authData?.token}
{#if $profile?.jwt}
<MenuButton on:click={() => save(post)}>
<Icon src={post.saved ? BookmarkSlash : Bookmark} width={16} mini />
{post.saved ? 'Unsave' : 'Save'}
</MenuButton>
{#if $user && post.creator.id == $user.local_user_view.person.id}
{#if $profile.user && post.creator.id == $profile.user.local_user_view.person.id}
<MenuButton on:click={deletePost} color="dangerSecondary">
<Icon src={Trash} width={16} mini />
{post.post.deleted ? 'Restore' : 'Delete'}
</MenuButton>
{/if}
{#if $user?.local_user_view.person.id != post.creator.id}
{#if $profile.user?.local_user_view.person.id != post.creator.id}
<MenuButton on:click={() => report(post)} color="dangerSecondary">
<Icon src={Flag} width={16} mini />
Report
@@ -1,17 +1,15 @@
<script lang="ts">
import { createEventDispatcher, onMount } from 'svelte'
import { getClient, authData, uploadImage } from '$lib/lemmy.js'
import { getClient, uploadImage } from '$lib/lemmy.js'
import type { Community, Post, PostView } from 'lemmy-js-client'
import TextInput from '$lib/components/input/TextInput.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
import Dots from '$lib/components/ui/loader/Dots.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import Button from '$lib/components/input/Button.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { goto } from '$app/navigation'
import SearchInput from '$lib/components/input/SearchInput.svelte'
import { Check, Icon } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
export let edit = false
@@ -67,7 +65,7 @@
}
const list = await getClient().listCommunities({
auth: $authData?.token,
auth: $profile?.jwt,
type_: 'Subscribed',
sort: 'Active',
limit: 40,
@@ -84,7 +82,7 @@
})
return
}
if (!data.title || !$authData) return
if (!data.title || !$profile?.jwt) return
data.loading = true
@@ -95,7 +93,7 @@
}
const post = await getClient().editPost({
auth: $authData.token,
auth: $profile.jwt,
name: data.title,
body: data.body,
url: data.url || undefined,
@@ -111,7 +109,7 @@
let image = data.image ? await uploadImage(data.image[0]) : undefined
data.url = image || data.url || undefined
const post = await getClient().createPost({
auth: $authData.token,
auth: $profile.jwt,
community_id: data.community!,
name: data.title,
body: data.body,
@@ -158,7 +156,7 @@
on:search={async () => {
const results = await getClient().search({
q: communitySearch,
auth: $authData?.token,
auth: $profile?.jwt,
type_: 'Communities',
limit: 20,
listing_type: 'Subscribed',
@@ -1,18 +1,19 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { authData, getClient } from '$lib/lemmy'
import { getClient } from '$lib/lemmy'
import { userSettings } from '$lib/settings.js'
import { Color } from '$lib/ui/colors.js'
import type { Post } from 'lemmy-js-client'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
export let post: Post
export let vote: number = 0
export let score: number
async function upvote() {
if (!$authData) return
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
@@ -31,14 +32,14 @@
await getClient()
.likePost({
score: upvoted ? 0 : 1,
auth: $authData.token,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
if (!$profile?.jwt) return
if (vote == undefined) vote = 0
@@ -57,7 +58,7 @@
await getClient()
.likePost({
score: upvoted ? 0 : -1,
auth: $authData.token,
auth: $profile.jwt,
post_id: post.id,
})
.catch((_) => undefined)
+18 -27
View File
@@ -9,13 +9,7 @@
import Spinner from '$lib/components/ui/loader/Spinner.svelte'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import {
LINKED_INSTANCE_URL,
authData,
instance,
site,
user,
} from '$lib/lemmy.js'
import { LINKED_INSTANCE_URL, instance, site } from '$lib/lemmy.js'
import {
ArrowLeftOnRectangle,
ArrowRightOnRectangle,
@@ -87,7 +81,7 @@
</a>
</div>
<div class="flex flex-row gap-2 py-2 px-2">
{#if $user && amModOfAny($user)}
{#if amModOfAny($profile?.user)}
<Button
href="/moderation"
label="Search"
@@ -95,7 +89,7 @@
dark:text-zinc-300 text-slate-700 hover:text-inherit
hover:bg-slate-200 hover:dark:bg-zinc-900 relative"
>
{#if $user.reports ?? 0 > 0}
{#if $profile?.user?.reports ?? 0 > 0}
<div
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
/>
@@ -134,11 +128,15 @@
<span class="hidden md:inline">Create</span>
</Button>
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1">Create</li>
<MenuButton link href="/create/post" disabled={$authData == undefined}>
<MenuButton
link
href="/create/post"
disabled={$profile?.jwt == undefined}
>
<Icon src={PencilSquare} mini width={16} />
Post
</MenuButton>
{#if !$authData}
{#if !$profile?.jwt}
<span class="text-sm mx-4 my-1 py-1">
<Link highlight href="/login">Log in</Link> to create content.
</span>
@@ -193,22 +191,15 @@
<Icon src={Bookmark} mini width={16} /> Saved
</MenuButton>
{/if}
{#if !$profile?.user && $profileData.profiles.length == 0}
<MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Log in
</MenuButton>
{:else}
<MenuButton link href="/accounts">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
Switch account
<span
class="text-xs font-bold bg-slate-100 dark:bg-zinc-700 px-2 py-0.5 rounded-md ml-auto"
>
{$profileData.profiles.length}
</span>
</MenuButton>
{/if}
<MenuButton link href="/accounts">
<Icon src={ArrowLeftOnRectangle} mini width={16} />
{$profileData.profiles.length == 0 ? 'Log in' : 'Switch accounts'}
<span
class="text-xs font-bold bg-slate-100 dark:bg-zinc-700 px-2 py-0.5 rounded-md ml-auto"
>
{$profileData.profiles.length}
</span>
</MenuButton>
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
<li class="text-xs px-4 py-1 my-1 opacity-80">App</li>
<MenuButton link href="/settings">
+11 -5
View File
@@ -9,7 +9,7 @@
} from 'svelte-hero-icons'
import Button from '../input/Button.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import { user } from '$lib/lemmy.js'
import { profile } from '$lib/auth.js'
let expanded = true
</script>
@@ -50,8 +50,8 @@
<span class:hidden={!expanded}>Settings</span>
</Button>
<hr class="border-slate-300 dark:border-zinc-800 my-1" />
{#if $user}
{#each $user.follows
{#if $profile?.user}
{#each $profile.user.follows
.map((f) => f.community)
.sort((a, b) => a.title.localeCompare(b.title)) as follow}
<Button
@@ -60,11 +60,17 @@
alignment="left"
href="/c/{follow.name}@{new URL(follow.actor_id).hostname}"
>
<Avatar url={follow.icon} alt={follow.name} title={follow.title} width={20} slot="icon" />
<Avatar
url={follow.icon}
alt={follow.name}
title={follow.title}
width={20}
slot="icon"
/>
<span class:hidden={!expanded}>{follow.title}</span>
</Button>
{/each}
{#if $user.follows.length == 0}
{#if $profile.user.follows.length == 0}
<Button
class="hover:bg-slate-200 {expanded ? '' : '!p-1.5'}"
href="/communities"
+4 -88
View File
@@ -5,10 +5,8 @@ import {
type PersonView,
} from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { userSettings } from '$lib/settings.js'
import { env } from '$env/dynamic/public'
import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js'
import { profile } from '$lib/auth.js'
export const LINKED_INSTANCE_URL = env.PUBLIC_INSTANCE_URL
export const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml'
@@ -24,21 +22,6 @@ export function getClient(instanceURL?: string): LemmyHttp {
export const getInstance = () => get(instance)
export interface AuthData {
token: string
username: string
instance: string
}
export interface UserData {
unreads: number
reports?: number
}
export const authData = writable<AuthData | undefined>()
export const user = writable<(MyUserInfo & UserData) | undefined | null>(
undefined
)
export const site = writable<GetSiteResponse | undefined>(undefined)
if (LINKED_INSTANCE_URL) {
@@ -47,73 +30,6 @@ if (LINKED_INSTANCE_URL) {
.then((s) => site.set(s))
}
setInterval(async () => {
// check for unread messages
if (!get(authData) || !get(user)) return
const response = await getClient().getUnreadCount({
auth: get(authData)!.token,
})
const u = get(user)
if (!u) return
u.unreads = response.mentions + response.private_messages + response.replies
if (amModOfAny(u)) {
const reports = await getClient().getReportCount({
auth: get(authData)!.token,
})
u.reports =
reports.comment_reports +
reports.post_reports +
(reports.private_message_reports ?? 0)
}
user.set(u)
}, 30 * 1000)
if (typeof localStorage != 'undefined') {
if (localStorage.getItem('user')) {
try {
authData.set(JSON.parse(localStorage.getItem('user') ?? ''))
} catch (error) {
// keep authdata until we can verify that the user is infact invalid
}
}
}
authData.subscribe(async (data) => {
instance.set(
data?.instance ?? get(userSettings).instance ?? DEFAULT_INSTANCE_URL
)
if (!data?.token) {
user.set(null)
return
}
try {
const site = await getClient().getSite({
auth: data.token,
})
if (!site?.my_user) {
user.set(null)
throw Error('Missing user')
}
user.set({ ...site.my_user, unreads: 0 })
if (typeof localStorage != 'undefined') {
localStorage.setItem('user', JSON.stringify(data))
}
} catch (error) {
toast({
content: 'Failed to fetch your user. Is your instance down?',
type: ToastType.error,
})
console.error(error)
authData.set(undefined)
user.set(null)
}
})
export async function validateInstance(instance: string): Promise<boolean> {
if (instance == '') return false
@@ -129,7 +45,7 @@ export async function validateInstance(instance: string): Promise<boolean> {
export async function uploadImage(
image: File | null | undefined
): Promise<string | undefined> {
if (!image || !get(authData)) return
if (!image || !get(profile)?.jwt) return
const formData = new FormData()
formData.append('images[]', image)
@@ -138,7 +54,7 @@ export async function uploadImage(
`${
window.location.origin
}/cors/${getInstance()}/pictrs/image?${new URLSearchParams({
auth: get(authData)!.token,
auth: get(profile)!.jwt!,
})}`,
{
method: 'POST',
@@ -149,7 +65,7 @@ export async function uploadImage(
const json = await response.json()
if (json.msg == 'ok') {
return `https://${get(authData)?.instance}/pictrs/image/${
return `https://${get(profile)?.instance}/pictrs/image/${
json.files?.[0]?.file
}`
}
+3 -2
View File
@@ -1,9 +1,10 @@
import { authData, getClient } from '$lib/lemmy.js'
import { profile } from '$lib/auth.js'
import { getClient } from '$lib/lemmy.js'
import type { MyUserInfo, PersonView } from 'lemmy-js-client'
import { get } from 'svelte/store'
export const blockUser = async (block: boolean, id: number) => {
const auth = get(authData)?.token
const auth = get(profile)?.jwt
if (!auth) throw new Error('Unauthorized')