Merge pull request #58 from Xyphyn/new-auth
feat: add multi account system, add #55
This commit is contained in:
43 files changed
+663
-377
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "photon-lemmy",
|
||||
"version": "0.4.17",
|
||||
"version": "0.4.19",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js'
|
||||
import type { MyUserInfo } from 'lemmy-js-client'
|
||||
import { get, writable } from 'svelte/store'
|
||||
|
||||
function getFromStorage<T>(key: string): T | undefined {
|
||||
if (typeof localStorage == 'undefined') return undefined
|
||||
const lc = localStorage.getItem(key)
|
||||
if (!lc) return undefined
|
||||
|
||||
return JSON.parse(lc)
|
||||
}
|
||||
|
||||
function setFromStorage(key: string, item: any) {
|
||||
if (typeof localStorage == 'undefined') return
|
||||
return localStorage.setItem(key, JSON.stringify(item))
|
||||
}
|
||||
|
||||
interface Profile {
|
||||
id: number
|
||||
instance: string
|
||||
jwt?: string
|
||||
user?: PersonData
|
||||
username?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* What gets stored in localstorage.
|
||||
*/
|
||||
interface ProfileData {
|
||||
profiles: Profile[]
|
||||
profile: number
|
||||
defaultInstance?: string
|
||||
}
|
||||
|
||||
interface PersonData extends MyUserInfo {
|
||||
unreads: number
|
||||
reports: number
|
||||
}
|
||||
|
||||
export const profileData = writable<ProfileData>(
|
||||
getFromStorage<ProfileData>('profileData') ?? { profiles: [], profile: -1 }
|
||||
)
|
||||
|
||||
let initialInstance = get(profileData).defaultInstance
|
||||
|
||||
profileData.subscribe(async (pd) => {
|
||||
setFromStorage('profileData', pd)
|
||||
|
||||
if (pd.profile == -1 && pd.defaultInstance != initialInstance) {
|
||||
initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL
|
||||
instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||
}
|
||||
})
|
||||
|
||||
export const profile = writable<Profile | undefined>(getProfile())
|
||||
|
||||
profile.subscribe(async (p) => {
|
||||
if (p?.id == -1) {
|
||||
instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||
}
|
||||
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)
|
||||
|
||||
profile.update(() => ({
|
||||
...p,
|
||||
user: user,
|
||||
}))
|
||||
})
|
||||
|
||||
export async function setUser(jwt: string, instance: string, username: string) {
|
||||
try {
|
||||
new URL(`https://${instance}`)
|
||||
} catch (err) {
|
||||
return
|
||||
}
|
||||
|
||||
const user = await userFromJwt(jwt, instance)
|
||||
if (!user) {
|
||||
toast({
|
||||
content: 'Failed to fetch your user. Is your instance down?',
|
||||
type: ToastType.error,
|
||||
})
|
||||
}
|
||||
|
||||
profileData.update((pd) => {
|
||||
// too lazy to make a decent system
|
||||
const id = Math.floor(Math.random() * 100000)
|
||||
|
||||
const newProfile: Profile = {
|
||||
id: id,
|
||||
instance: instance,
|
||||
jwt: jwt,
|
||||
username: username,
|
||||
}
|
||||
|
||||
profile.set({
|
||||
...newProfile,
|
||||
user: user,
|
||||
})
|
||||
|
||||
return {
|
||||
profile: id,
|
||||
profiles: [...pd.profiles, newProfile],
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function userFromJwt(
|
||||
jwt: string,
|
||||
instance: string
|
||||
): Promise<PersonData | undefined> {
|
||||
const myUser = (await getClient(instance).getSite({ auth: jwt })).my_user
|
||||
if (!myUser) return undefined
|
||||
|
||||
return {
|
||||
unreads: 0,
|
||||
reports: 0,
|
||||
...myUser,
|
||||
}
|
||||
}
|
||||
|
||||
const userToPersonData = (user: MyUserInfo): PersonData => ({
|
||||
...user,
|
||||
unreads: 0,
|
||||
reports: 0,
|
||||
})
|
||||
|
||||
function getProfile() {
|
||||
const id = get(profileData).profile
|
||||
|
||||
if (id == -1) {
|
||||
return
|
||||
}
|
||||
|
||||
const pd = get(profileData)
|
||||
|
||||
return pd.profiles.find((p) => p.id == id)
|
||||
}
|
||||
|
||||
const getDefaultProfile = () => ({
|
||||
id: -1,
|
||||
instance: get(instance),
|
||||
})
|
||||
|
||||
export function resetProfile() {
|
||||
profile.set(getDefaultProfile())
|
||||
profileData.update((p) => ({ ...p, profile: -1 }))
|
||||
}
|
||||
|
||||
export function deleteProfile(id: number) {
|
||||
const pd = get(profileData)
|
||||
|
||||
const index = pd.profiles.findIndex((p) => p.id == id)
|
||||
if (index <= -1) return
|
||||
|
||||
pd.profiles.splice(index, 1)
|
||||
|
||||
profileData.update((p) => ({
|
||||
...p,
|
||||
profiles: pd.profiles,
|
||||
}))
|
||||
|
||||
if (id == get(profile)?.id) {
|
||||
resetProfile()
|
||||
}
|
||||
}
|
||||
|
||||
export function setUserID(id: number) {
|
||||
const pd = get(profileData)
|
||||
if (id == -1) {
|
||||
resetProfile()
|
||||
return
|
||||
}
|
||||
const prof = pd.profiles.find((p) => p.id == id)
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { profile, profileData, setUserID } from '$lib/auth.js'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import Link from '$lib/components/input/Link.svelte'
|
||||
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
|
||||
@@ -8,16 +9,11 @@
|
||||
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,
|
||||
Beaker,
|
||||
Bookmark,
|
||||
Cog6Tooth,
|
||||
GlobeAlt,
|
||||
@@ -85,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"
|
||||
@@ -93,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"
|
||||
/>
|
||||
@@ -132,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>
|
||||
@@ -151,20 +151,15 @@
|
||||
slot="button"
|
||||
on:click={toggleOpen}
|
||||
>
|
||||
{#if $user === undefined}
|
||||
<div class="w-full h-full grid place-items-center">
|
||||
<Spinner width={20} />
|
||||
</div>
|
||||
{/if}
|
||||
{#if $user}
|
||||
{#if $profile?.user}
|
||||
<div class="w-8 h-8 aspect-square object-cover rounded-full">
|
||||
<Avatar
|
||||
url={$user.local_user_view.person.avatar}
|
||||
url={$profile.user.local_user_view.person.avatar}
|
||||
width={32}
|
||||
alt={$user.local_user_view.person.name}
|
||||
alt={$profile.user.local_user_view.person.name}
|
||||
/>
|
||||
</div>
|
||||
{#if $user.unreads > 0}
|
||||
{#if $profile.user.unreads > 0}
|
||||
<div
|
||||
class="rounded-full w-2 h-2 bg-red-500 absolute top-0 left-0 z-10"
|
||||
/>
|
||||
@@ -172,18 +167,19 @@
|
||||
{/if}
|
||||
</button>
|
||||
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1">
|
||||
{$user ? $user.local_user_view.person.name : 'Profile'}
|
||||
{$profile?.user ? $profile.user.local_user_view.person.name : 'Profile'}
|
||||
</li>
|
||||
{#if $user && $authData}
|
||||
{#if $profile?.user}
|
||||
<MenuButton
|
||||
link
|
||||
href="/u/{$user.local_user_view.person.name}@{$authData.instance}"
|
||||
href="/u/{$profile.user.local_user_view.person
|
||||
.name}@{$profile.instance}"
|
||||
>
|
||||
<Icon src={UserCircle} mini width={16} /> Profile
|
||||
</MenuButton>
|
||||
<MenuButton link href="/inbox">
|
||||
<div class="relative">
|
||||
{#if $user.unreads > 0}
|
||||
{#if $profile.user.unreads > 0}
|
||||
<div
|
||||
class="rounded-full w-2 h-2 bg-red-500 absolute -top-1 -left-1"
|
||||
/>
|
||||
@@ -194,23 +190,16 @@
|
||||
<MenuButton link href="/saved">
|
||||
<Icon src={Bookmark} mini width={16} /> Saved
|
||||
</MenuButton>
|
||||
<MenuButton
|
||||
on:click={() => {
|
||||
localStorage.removeItem('user')
|
||||
user.set(undefined)
|
||||
authData.set(undefined)
|
||||
}}
|
||||
color="dangerSecondary"
|
||||
{/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"
|
||||
>
|
||||
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if !$user || !$authData}
|
||||
<MenuButton link href="/login">
|
||||
<Icon src={ArrowLeftOnRectangle} mini width={16} />
|
||||
Log in
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{$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">
|
||||
|
||||
@@ -6,10 +6,13 @@
|
||||
Home,
|
||||
Icon,
|
||||
Plus,
|
||||
UserCircle,
|
||||
} 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, profileData, setUserID } from '$lib/auth.js'
|
||||
import { goto } from '$app/navigation'
|
||||
import { page } from '$app/stores'
|
||||
|
||||
let expanded = true
|
||||
</script>
|
||||
@@ -29,6 +32,7 @@
|
||||
mini
|
||||
size="16"
|
||||
class="transition-transform {expanded ? '' : 'rotate-180'}"
|
||||
title="Toggle expanded"
|
||||
/>
|
||||
</Button>
|
||||
<Button
|
||||
@@ -37,7 +41,7 @@
|
||||
color="tertiary"
|
||||
alignment="left"
|
||||
>
|
||||
<Icon src={Home} solid size="20" />
|
||||
<Icon src={Home} solid size="20" title="Frontpage" />
|
||||
<span class:hidden={!expanded}>Frontpage</span>
|
||||
</Button>
|
||||
<Button
|
||||
@@ -46,12 +50,48 @@
|
||||
color="tertiary"
|
||||
alignment="left"
|
||||
>
|
||||
<Icon src={Cog6Tooth} solid size="20" />
|
||||
<Icon src={Cog6Tooth} solid size="20" title="Settings" />
|
||||
<span class:hidden={!expanded}>Settings</span>
|
||||
</Button>
|
||||
{#if $profileData.profiles.length >= 1}
|
||||
<hr class="border-slate-300 dark:border-zinc-800 my-1" />
|
||||
{#each $profileData.profiles as prof, index}
|
||||
<Button
|
||||
class="hover:bg-slate-200 {expanded ? '' : '!p-1.5'} {$profile?.id ==
|
||||
prof.id
|
||||
? expanded
|
||||
? 'font-bold'
|
||||
: 'text-sky-500'
|
||||
: ''}"
|
||||
color="tertiary"
|
||||
alignment="left"
|
||||
on:click={() => {
|
||||
setUserID(prof.id)
|
||||
goto($page.url, {
|
||||
invalidateAll: true,
|
||||
})
|
||||
}}
|
||||
>
|
||||
<Icon
|
||||
src={UserCircle}
|
||||
mini={$profile?.id == prof.id}
|
||||
size="20"
|
||||
title={prof.username}
|
||||
class="text-blue-500"
|
||||
style="filter: hue-rotate({index * 50}deg)"
|
||||
/>
|
||||
<span class:hidden={!expanded} class="flex flex-col gap-0">
|
||||
{prof.username}
|
||||
<span class="text-slate-500 dark:text-zinc-500 font-normal text-xs">
|
||||
{prof.instance}
|
||||
</span>
|
||||
</span>
|
||||
</Button>
|
||||
{/each}
|
||||
{/if}
|
||||
<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 +100,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"
|
||||
@@ -78,7 +124,7 @@
|
||||
{:else}
|
||||
<Button
|
||||
class="hover:bg-slate-200 {expanded ? '' : '!p-1.5'}"
|
||||
href="/login"
|
||||
href="/accounts"
|
||||
color="tertiary"
|
||||
alignment="left"
|
||||
>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
|
||||
type T = $$Generic
|
||||
|
||||
const dispatcher = createEventDispatcher<{ action: any }>()
|
||||
|
||||
function action(detail: any) {
|
||||
dispatcher('action', detail)
|
||||
}
|
||||
</script>
|
||||
|
||||
<ul class="divide-y divide-slate-200 dark:divide-zinc-900 flex flex-col">
|
||||
<slot {action} />
|
||||
</ul>
|
||||
@@ -0,0 +1,3 @@
|
||||
<div class="p-2 flex flex-row">
|
||||
<slot />
|
||||
</div>
|
||||
+4
-88
@@ -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
|
||||
}`
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
import SiteCard from '$lib/components/lemmy/SiteCard.svelte'
|
||||
import { fly } from 'svelte/transition'
|
||||
import Spinner from '$lib/components/ui/loader/Spinner.svelte'
|
||||
import { authData } from '$lib/lemmy.js'
|
||||
import { ArchiveBox, Icon, Plus } from 'svelte-hero-icons'
|
||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
>
|
||||
<MultiSelect
|
||||
options={['Subscribed', 'Local', 'All']}
|
||||
disabled={[$authData?.token == undefined]}
|
||||
disabled={[$profile?.jwt == undefined]}
|
||||
selected={data.listingType}
|
||||
on:select={(e) => {
|
||||
$page.url.searchParams.set('type', e.detail)
|
||||
|
||||
+3
-2
@@ -1,8 +1,9 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
import { error } from '@sveltejs/kit'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export async function load({ url }) {
|
||||
const page = Number(url.searchParams.get('page') || 1) || 1
|
||||
@@ -24,7 +25,7 @@ export async function load({ url }) {
|
||||
page: page,
|
||||
sort: sort,
|
||||
type_: listingType,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
}),
|
||||
streamed: {
|
||||
site: getClient().getSite({}),
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
profileData,
|
||||
setUserID,
|
||||
profile as currentProfile,
|
||||
resetProfile,
|
||||
deleteProfile,
|
||||
} from '$lib/auth.js'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { instance, validateInstance } from '$lib/lemmy.js'
|
||||
import { ArrowLeftOnRectangle, Icon, Plus, Trash } from 'svelte-hero-icons'
|
||||
import { flip } from 'svelte/animate'
|
||||
|
||||
let newInstance: string = $instance
|
||||
let loading = false
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Accounts</title>
|
||||
</svelte:head>
|
||||
|
||||
{#if $profileData.profiles.length == 0}
|
||||
<div class="h-full flex items-center justify-center">
|
||||
<div
|
||||
class="text-slate-600 dark:text-zinc-400 flex flex-col justify-center items-center py-8 gap-4"
|
||||
>
|
||||
<div class="flex flex-col items-center">
|
||||
<Icon src={ArrowLeftOnRectangle} size="48" solid />
|
||||
<h1 class="font-bold text-3xl">No accounts</h1>
|
||||
</div>
|
||||
<Button href="/login" size="lg">
|
||||
<Icon slot="icon" src={ArrowLeftOnRectangle} size="16" mini />
|
||||
Log in
|
||||
</Button>
|
||||
<div class="flex flex-row font-normal gap-2">
|
||||
<TextInput
|
||||
label="Guest instance"
|
||||
on:change={async () => {
|
||||
loading = true
|
||||
try {
|
||||
const valid = await validateInstance(newInstance.trim())
|
||||
|
||||
if (!valid) {
|
||||
throw new Error('invalid instance')
|
||||
}
|
||||
|
||||
toast({
|
||||
content: 'Changed guest instance.',
|
||||
type: ToastType.success,
|
||||
})
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: 'Failed to contact that instance URL. Is it down?',
|
||||
type: ToastType.error,
|
||||
})
|
||||
|
||||
loading = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
$profileData.defaultInstance = newInstance
|
||||
if ($currentProfile && $currentProfile.id == -1) {
|
||||
$instance = newInstance
|
||||
}
|
||||
loading = false
|
||||
}}
|
||||
placeholder="Instance URL"
|
||||
bind:value={newInstance}
|
||||
/>
|
||||
<Button
|
||||
color="primary"
|
||||
class="h-max self-end"
|
||||
size="lg"
|
||||
{loading}
|
||||
disabled={loading}
|
||||
>
|
||||
Change
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="flex flex-col h-full gap-4">
|
||||
<div class="flex flex-row justify-between">
|
||||
<h1 class="text-2xl font-bold">Accounts</h1>
|
||||
<Button
|
||||
on:click={() => {
|
||||
$profileData.profiles = []
|
||||
resetProfile()
|
||||
}}
|
||||
>
|
||||
Delete all
|
||||
</Button>
|
||||
</div>
|
||||
<EditableList
|
||||
on:action={(id) => {
|
||||
deleteProfile(id.detail)
|
||||
}}
|
||||
let:action
|
||||
>
|
||||
{#each $profileData.profiles as profile (profile.id)}
|
||||
<div
|
||||
class="flex flex-row gap-4 items-center py-4"
|
||||
animate:flip={{ duration: 250 }}
|
||||
>
|
||||
<div class="flex flex-col">
|
||||
<span class="font-bold">{profile.username}</span>
|
||||
<span class="text-sm text-slate-600 dark:text-zinc-400">
|
||||
{profile.instance}
|
||||
</span>
|
||||
</div>
|
||||
<div class="ml-auto" />
|
||||
<Button
|
||||
on:click={() => {
|
||||
if (profile.id == $currentProfile?.id) {
|
||||
setUserID(-1)
|
||||
} else {
|
||||
setUserID(profile.id)
|
||||
}
|
||||
}}
|
||||
color={profile.id == $currentProfile?.id ? 'primary' : 'secondary'}
|
||||
>
|
||||
{profile.id == $currentProfile?.id ? 'Current' : 'Switch to'}
|
||||
</Button>
|
||||
<Button
|
||||
on:click={() => action(profile.id)}
|
||||
class="!p-2"
|
||||
color="danger"
|
||||
>
|
||||
<Icon slot="icon" src={Trash} size="16" mini />
|
||||
</Button>
|
||||
</div>
|
||||
{/each}
|
||||
<div class="flex flex-row gap-4 items-center py-4">
|
||||
<span class="font-bold flex flex-col">
|
||||
Guest
|
||||
<div class="flex flex-row font-normal gap-2">
|
||||
<TextInput
|
||||
on:change={async () => {
|
||||
loading = true
|
||||
try {
|
||||
const valid = await validateInstance(newInstance.trim())
|
||||
|
||||
if (!valid) {
|
||||
throw new Error('invalid instance')
|
||||
}
|
||||
|
||||
toast({
|
||||
content: 'Changed guest instance.',
|
||||
type: ToastType.success,
|
||||
})
|
||||
} catch (err) {
|
||||
toast({
|
||||
content: 'Failed to contact that instance URL. Is it down?',
|
||||
type: ToastType.error,
|
||||
})
|
||||
}
|
||||
|
||||
$profileData.defaultInstance = newInstance
|
||||
if ($currentProfile && $currentProfile.id == -1) {
|
||||
$instance = newInstance
|
||||
}
|
||||
loading = false
|
||||
}}
|
||||
placeholder="Instance URL"
|
||||
bind:value={newInstance}
|
||||
/>
|
||||
<Button color="primary" {loading} disabled={loading}>Change</Button>
|
||||
</div>
|
||||
</span>
|
||||
<div class="ml-auto" />
|
||||
|
||||
<Button
|
||||
on:click={() => {
|
||||
setUserID(-1)
|
||||
}}
|
||||
color={$currentProfile?.id == -1 ? 'primary' : 'secondary'}
|
||||
>
|
||||
{$currentProfile?.id == -1 ? 'Current' : 'Switch to'}
|
||||
</Button>
|
||||
</div>
|
||||
</EditableList>
|
||||
<Button href="/login" size="lg" class="mt-auto">
|
||||
<Icon slot="icon" src={Plus} size="16" mini />
|
||||
Add more
|
||||
</Button>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
@@ -18,12 +19,12 @@ export async function load(req: any) {
|
||||
community_name: req.params.name,
|
||||
page: page,
|
||||
sort: sort,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
}),
|
||||
community: (
|
||||
await getClient().getCommunity({
|
||||
name: req.params.name,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
})
|
||||
).community_view,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { ListingType } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
@@ -15,7 +16,7 @@ export async function load({ url }) {
|
||||
type_: 'Communities',
|
||||
listing_type: type,
|
||||
q: query,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
})
|
||||
} else {
|
||||
return await getClient().listCommunities({
|
||||
@@ -23,7 +24,7 @@ export async function load({ url }) {
|
||||
page: page,
|
||||
sort: 'TopAll',
|
||||
type_: type,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,21 @@
|
||||
<script lang="ts">
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type {
|
||||
Community,
|
||||
CommunityView,
|
||||
SubscribedType,
|
||||
} from 'lemmy-js-client'
|
||||
import { authData } from '$lib/lemmy.js'
|
||||
import type { CommunityView } from 'lemmy-js-client'
|
||||
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let community: CommunityView
|
||||
|
||||
let subscribing = false
|
||||
|
||||
async function subscribe() {
|
||||
if (!$authData?.token) return
|
||||
if (!$profile?.jwt) return
|
||||
|
||||
subscribing = true
|
||||
|
||||
try {
|
||||
const res = await getClient().followCommunity({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
community_id: community.community.id,
|
||||
follow: community.subscribed == 'NotSubscribed',
|
||||
})
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
import { goto } from '$app/navigation'
|
||||
import PostForm from '$lib/components/lemmy/post/PostForm.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
import { authData } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
onMount(() => {
|
||||
if (!$authData) {
|
||||
if (!$profile?.jwt) {
|
||||
goto('/login')
|
||||
}
|
||||
})
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
import InboxItem from './InboxItem.svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import { Check, EnvelopeOpen, Icon } from 'svelte-hero-icons'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { goto } from '$app/navigation'
|
||||
import { page } from '$app/stores'
|
||||
import { isRead } from '$lib/lemmy/inbox.js'
|
||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let data
|
||||
|
||||
let markingAsRead = false
|
||||
|
||||
async function markAllAsRead() {
|
||||
if (!$user) {
|
||||
if (!$profile?.user) {
|
||||
goto('/login')
|
||||
return
|
||||
}
|
||||
@@ -22,10 +23,10 @@
|
||||
markingAsRead = true
|
||||
|
||||
const response = await getClient().markAllAsRead({
|
||||
auth: $authData!.token,
|
||||
auth: $profile.jwt!,
|
||||
})
|
||||
|
||||
$user!.unreads = 0
|
||||
$profile.user.unreads = 0
|
||||
|
||||
goto($page.url, {
|
||||
invalidateAll: true,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { getInboxItemPublished } from '$lib/lemmy/inbox.js'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
type InboxFeedType = 'replies' | 'mentions' | 'messages' | 'all'
|
||||
|
||||
export async function load({ url }) {
|
||||
const auth = get(authData)
|
||||
if (!auth) return
|
||||
const auth = get(profile)
|
||||
if (!auth?.jwt) return
|
||||
|
||||
const type: InboxFeedType =
|
||||
(url.searchParams.get('type') as InboxFeedType) || 'all'
|
||||
@@ -18,7 +19,7 @@ export async function load({ url }) {
|
||||
const params = {
|
||||
limit: 50,
|
||||
page: page,
|
||||
auth: auth.token,
|
||||
auth: auth.jwt,
|
||||
unread_only: unreadOnly,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import Link from '$lib/components/input/Link.svelte'
|
||||
import TextArea from '$lib/components/input/TextArea.svelte'
|
||||
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import { Color } from '$lib/ui/colors.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import type {
|
||||
CommentReplyView,
|
||||
PersonMentionView,
|
||||
@@ -34,13 +33,13 @@
|
||||
async function replyToMessage(
|
||||
message: PrivateMessageView | CommentReplyView | PersonMentionView
|
||||
) {
|
||||
if (!$authData) return
|
||||
if (!$profile?.jwt) return
|
||||
|
||||
loading = true
|
||||
|
||||
try {
|
||||
await getClient().createPrivateMessage({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
content: reply,
|
||||
recipient_id: message.creator.id,
|
||||
})
|
||||
@@ -105,7 +104,7 @@
|
||||
overflow-hidden flex flex-row items-center gap-1 {read ? 'opacity-80' : ''}"
|
||||
>
|
||||
<span class="font-bold flex items-center">
|
||||
{#if item.creator.id == $user?.local_user_view.person.id}
|
||||
{#if item.creator.id == $profile?.user?.local_user_view.person.id}
|
||||
You
|
||||
{:else}
|
||||
<UserLink avatar user={item.creator} />
|
||||
@@ -113,7 +112,7 @@
|
||||
</span>
|
||||
<span>messaged</span>
|
||||
<span class="font-bold flex items-center">
|
||||
{#if item.recipient.id == $user?.local_user_view.person.id}
|
||||
{#if item.recipient.id == $profile?.user?.local_user_view.person.id}
|
||||
You
|
||||
{:else}
|
||||
<UserLink avatar user={item.recipient} />
|
||||
@@ -124,7 +123,7 @@
|
||||
<p class="text-sm py-2">
|
||||
<Markdown source={item.private_message.content} />
|
||||
</p>
|
||||
{#if item.recipient.id == $user?.local_user_view.person.id}
|
||||
{#if item.recipient.id == $profile?.user?.local_user_view.person.id}
|
||||
<div class="flex flex-row gap-2">
|
||||
<Button color="ghost" on:click={() => (replying = !replying)}>
|
||||
<Icon mini src={ChatBubbleOvalLeft} width={16} />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation'
|
||||
import { profileData, setUser } from '$lib/auth.js'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
@@ -11,9 +12,7 @@
|
||||
import {
|
||||
DEFAULT_INSTANCE_URL,
|
||||
LINKED_INSTANCE_URL,
|
||||
authData,
|
||||
getClient,
|
||||
user,
|
||||
validateInstance,
|
||||
} from '$lib/lemmy.js'
|
||||
|
||||
@@ -27,6 +26,7 @@
|
||||
|
||||
async function logIn() {
|
||||
data.loading = true
|
||||
|
||||
try {
|
||||
if (!(await validateInstance(data.instance))) {
|
||||
throw new Error('Failed to contact that instance. Is it down?')
|
||||
@@ -39,12 +39,7 @@
|
||||
})
|
||||
|
||||
if (response?.jwt) {
|
||||
user.set(undefined)
|
||||
authData.set({
|
||||
instance: data.instance,
|
||||
token: response.jwt,
|
||||
username: data.username,
|
||||
})
|
||||
setUser(response.jwt, data.instance, data.username)
|
||||
|
||||
toast({ content: 'Successfully logged in.', type: ToastType.success })
|
||||
goto('/')
|
||||
@@ -56,7 +51,6 @@
|
||||
content: error as any,
|
||||
type: ToastType.error,
|
||||
})
|
||||
user.set(null)
|
||||
}
|
||||
data.loading = false
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { getItemPublished } from '$lib/lemmy/item.js'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
@@ -12,11 +13,11 @@ export async function load({ url }) {
|
||||
const filter: ReportFilter =
|
||||
(url.searchParams.get('filter') as ReportFilter) || 'all'
|
||||
|
||||
const jwt = get(authData)?.token
|
||||
const jwt = get(profile)?.jwt
|
||||
|
||||
if (!jwt) return { type: type, page: page }
|
||||
|
||||
// can they like
|
||||
// can they please
|
||||
// make a sane, consistent, and simple API for once
|
||||
// 3 separate requests? really?
|
||||
// and this time instead of get() it's list(). Why?!
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import CommentItem from '$lib/components/lemmy/comment/CommentItem.svelte'
|
||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { isCommentReport, isPostReport } from '$lib/lemmy/item.js'
|
||||
import type {
|
||||
CommentReportView,
|
||||
@@ -11,7 +11,7 @@
|
||||
PrivateMessageReportView,
|
||||
} from 'lemmy-js-client'
|
||||
import { Check, Icon } from 'svelte-hero-icons'
|
||||
import { get } from 'svelte/store'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let item: PostReportView | CommentReportView | PrivateMessageReportView
|
||||
|
||||
@@ -23,13 +23,13 @@
|
||||
|
||||
let resolving = false
|
||||
async function resolve() {
|
||||
if (!$authData || !$user) return
|
||||
if (!$profile?.jwt || !$profile.user) return
|
||||
resolving = true
|
||||
|
||||
try {
|
||||
if (isCommentReport(item)) {
|
||||
await getClient().resolveCommentReport({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
report_id: item.comment_report.id,
|
||||
resolved: !resolved,
|
||||
})
|
||||
@@ -42,7 +42,7 @@
|
||||
})
|
||||
} else if (isPostReport(item)) {
|
||||
await getClient().resolvePostReport({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
report_id: item.post_report.id,
|
||||
resolved: !resolved,
|
||||
})
|
||||
@@ -55,10 +55,10 @@
|
||||
}
|
||||
|
||||
const reports = await getClient().getReportCount({
|
||||
auth: get(authData)!.token,
|
||||
auth: $profile?.jwt,
|
||||
})
|
||||
|
||||
$user.reports =
|
||||
$profile.user.reports =
|
||||
reports.comment_reports +
|
||||
reports.post_reports +
|
||||
(reports.private_message_reports ?? 0)
|
||||
|
||||
@@ -4,26 +4,15 @@
|
||||
buildCommentsTreeAsync,
|
||||
} from '$lib/components/lemmy/comment/comments.js'
|
||||
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import { isImage } from '$lib/ui/image.js'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
|
||||
import { onMount } from 'svelte'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import Markdown from '$lib/components/markdown/Markdown.svelte'
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import { page } from '$app/stores'
|
||||
import PostActions from '$lib/components/lemmy/post/PostActions.svelte'
|
||||
import Badge from '$lib/components/ui/Badge.svelte'
|
||||
import {
|
||||
Bookmark,
|
||||
ExclamationTriangle,
|
||||
Icon,
|
||||
InformationCircle,
|
||||
Trash,
|
||||
} from 'svelte-hero-icons'
|
||||
import { ExclamationTriangle, Icon } from 'svelte-hero-icons'
|
||||
import Link from '$lib/components/input/Link.svelte'
|
||||
import Spinner from '$lib/components/ui/loader/Spinner.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
@@ -32,6 +21,7 @@
|
||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import type { CommentSortType } from 'lemmy-js-client'
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let data
|
||||
|
||||
@@ -39,9 +29,9 @@
|
||||
const post = postData.post
|
||||
|
||||
onMount(async () => {
|
||||
if (!postData.read && $authData) {
|
||||
if (!postData.read && $profile?.jwt) {
|
||||
getClient().markPostAsRead({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
read: true,
|
||||
post_id: post.id,
|
||||
})
|
||||
@@ -58,7 +48,7 @@
|
||||
commentsPage = 1
|
||||
|
||||
data.streamed.comments = getClient().getComments({
|
||||
auth: $authData?.token,
|
||||
auth: $profile?.jwt,
|
||||
max_depth: 3,
|
||||
page: commentsPage,
|
||||
limit: 25,
|
||||
@@ -82,7 +72,7 @@
|
||||
</svelte:head>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
{#if $authData && $page.params.instance.toLowerCase() != $authData.instance.toLowerCase()}
|
||||
{#if $profile && $page.params.instance.toLowerCase() != $profile.instance.toLowerCase()}
|
||||
<Card cardColor="warning" class="p-4 flex flex-col gap-1">
|
||||
<Icon
|
||||
src={ExclamationTriangle}
|
||||
@@ -183,7 +173,7 @@
|
||||
<Spinner width={24} />
|
||||
</div>
|
||||
{:then comments}
|
||||
{#if $user}
|
||||
{#if $profile?.user}
|
||||
<CommentForm
|
||||
postId={post.id}
|
||||
on:comment={(comment) =>
|
||||
@@ -211,7 +201,7 @@
|
||||
loading = true
|
||||
|
||||
const loadedComments = await getClient().getComments({
|
||||
auth: $authData?.token,
|
||||
auth: $profile?.jwt,
|
||||
max_depth: 3,
|
||||
page: ++commentsPage,
|
||||
limit: 25,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
export async function load({ params, url }) {
|
||||
const post = await getClient(params.instance.toLowerCase()).getPost({
|
||||
id: Number(params.id),
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
})
|
||||
|
||||
let max_depth = post.post_view.counts.comments > 100 ? 1 : 3
|
||||
@@ -36,7 +37,7 @@ export async function load({ params, url }) {
|
||||
max_depth: max_depth,
|
||||
saved_only: false,
|
||||
sort: 'Hot',
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
parent_id: parentId,
|
||||
}),
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { CommentView, PostView } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
@@ -11,7 +12,7 @@ function getSavedItemPublished(item: PostView | CommentView) {
|
||||
}
|
||||
|
||||
export async function load({ url }) {
|
||||
if (!get(authData)) return { posts: [] }
|
||||
if (!get(profile)) return { posts: [] }
|
||||
const page = Number(url.searchParams.get('page')) || 1
|
||||
|
||||
const client = getClient()
|
||||
@@ -19,7 +20,7 @@ export async function load({ url }) {
|
||||
const [posts, comments] = await Promise.all([
|
||||
client.getPosts({
|
||||
saved_only: true,
|
||||
auth: get(authData)!.token,
|
||||
auth: get(profile)!.jwt,
|
||||
limit: 20,
|
||||
page: page,
|
||||
sort: 'New',
|
||||
@@ -27,7 +28,7 @@ export async function load({ url }) {
|
||||
|
||||
client.getComments({
|
||||
saved_only: true,
|
||||
auth: get(authData)!.token,
|
||||
auth: get(profile)!.jwt,
|
||||
limit: 20,
|
||||
page: page,
|
||||
sort: 'New',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { getItemPublished } from '$lib/lemmy/item.js'
|
||||
import type {
|
||||
CommentView,
|
||||
@@ -21,7 +22,7 @@ export async function load({ url }) {
|
||||
if (query) {
|
||||
const results = await getClient().search({
|
||||
q: query,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
community_name: community ?? undefined,
|
||||
limit: 40,
|
||||
page: page,
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
import { userSettings } from '$lib/settings'
|
||||
import {
|
||||
LINKED_INSTANCE_URL,
|
||||
authData,
|
||||
instance,
|
||||
user,
|
||||
validateInstance,
|
||||
} from '$lib/lemmy.js'
|
||||
import { Color } from '$lib/ui/colors.js'
|
||||
@@ -17,34 +15,8 @@
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
|
||||
let data = {
|
||||
newInstance: $instance,
|
||||
loading: false,
|
||||
}
|
||||
|
||||
async function changeInstance() {
|
||||
data.loading = true
|
||||
try {
|
||||
authData.set(undefined)
|
||||
user.set(undefined)
|
||||
if (typeof localStorage != 'undefined') {
|
||||
localStorage.removeItem('user')
|
||||
}
|
||||
|
||||
if (!(await validateInstance(data.newInstance))) {
|
||||
throw new Error('Failed to contact that instance URL')
|
||||
}
|
||||
|
||||
instance.set(data.newInstance)
|
||||
|
||||
$userSettings.instance = data.newInstance
|
||||
} catch (error) {
|
||||
toast({
|
||||
content: error as any,
|
||||
type: ToastType.error,
|
||||
})
|
||||
}
|
||||
data.loading = false
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -54,31 +26,6 @@
|
||||
<div class="flex flex-col">
|
||||
<h1 class="text-3xl font-bold">Settings</h1>
|
||||
<h2 class="uppercase font-bold opacity-80 text-sm mt-4">General</h2>
|
||||
{#if !LINKED_INSTANCE_URL}
|
||||
<Setting>
|
||||
<span slot="title">Instance</span>
|
||||
<span slot="description">Changing this will log you out.</span>
|
||||
<div class="flex gap-2">
|
||||
<TextInput
|
||||
bind:value={data.newInstance}
|
||||
pattern={'(?!-)[A-Za-z0-9-]+([-.]{1}[a-z0-9]+)*.[A-Za-z]{2,6}'}
|
||||
class="flex-1"
|
||||
/>
|
||||
<div class="w-24">
|
||||
<Button
|
||||
color="primary"
|
||||
loading={data.loading}
|
||||
disabled={data.loading}
|
||||
on:click={changeInstance}
|
||||
size="lg"
|
||||
class="h-full"
|
||||
>
|
||||
Change
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Setting>
|
||||
{/if}
|
||||
<Setting>
|
||||
<span slot="title">Default sort</span>
|
||||
<span slot="description">The default sort to use for feeds.</span>
|
||||
|
||||
@@ -19,34 +19,37 @@
|
||||
import { page } from '$app/stores'
|
||||
import { goto } from '$app/navigation'
|
||||
import { isComment } from '$lib/lemmy/item.js'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { isBlocked } from '$lib/lemmy/user.js'
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
||||
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
|
||||
import TextArea from '$lib/components/input/TextArea.svelte'
|
||||
import { profile } from '$lib/auth.js'
|
||||
|
||||
export let data
|
||||
|
||||
let blocking = false
|
||||
|
||||
async function blockUser(block: number) {
|
||||
if (!$user || !$authData) throw new Error('Unauthenticated')
|
||||
if (!$profile?.user || !$profile?.jwt) throw new Error('Unauthenticated')
|
||||
|
||||
blocking = true
|
||||
try {
|
||||
const blocked = isBlocked($user, block)
|
||||
const blocked = isBlocked($profile.user, block)
|
||||
|
||||
await getClient().blockPerson({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
block: !blocked,
|
||||
person_id: block,
|
||||
})
|
||||
|
||||
if (blocked) {
|
||||
const index = $user.person_blocks.map((p) => p.target.id).indexOf(block)
|
||||
$user.person_blocks.splice(index, 1)
|
||||
const index = $profile.user.person_blocks
|
||||
.map((p) => p.target.id)
|
||||
.indexOf(block)
|
||||
$profile.user.person_blocks.splice(index, 1)
|
||||
}
|
||||
|
||||
toast({
|
||||
@@ -71,13 +74,13 @@
|
||||
let message = ''
|
||||
|
||||
async function sendMessage() {
|
||||
if (!$authData || message == '') return
|
||||
if (!$profile?.jwt || message == '') return
|
||||
|
||||
loadingMessage = true
|
||||
|
||||
try {
|
||||
await getClient().createPrivateMessage({
|
||||
auth: $authData.token,
|
||||
auth: $profile.jwt,
|
||||
content: message,
|
||||
recipient_id: data.person_view.person.id,
|
||||
})
|
||||
@@ -103,7 +106,7 @@
|
||||
<title>{data.person_view.person.name}</title>
|
||||
</svelte:head>
|
||||
|
||||
{#if $authData}
|
||||
{#if $profile?.user}
|
||||
<Modal bind:open={messaging}>
|
||||
<h1 class="text-2xl font-bold" slot="title">Message</h1>
|
||||
<form on:submit|preventDefault={sendMessage} class="flex flex-col gap-4">
|
||||
@@ -198,7 +201,7 @@
|
||||
<FormattedNumber number={data.person_view.counts.comment_count} />
|
||||
</span>
|
||||
</div>
|
||||
{#if $user && $authData && data.person_view.person.id != $user.local_user_view.person.id}
|
||||
{#if $profile?.user && $profile.jwt && data.person_view.person.id != $profile.user.local_user_view.person.id}
|
||||
<div class="flex flex-col gap-2">
|
||||
<Button
|
||||
size="lg"
|
||||
@@ -216,7 +219,9 @@
|
||||
on:click={() => blockUser(data.person_view.person.id)}
|
||||
>
|
||||
<Icon slot="icon" mini size="16" src={NoSymbol} />
|
||||
{isBlocked($user, data.person_view.person.id) ? 'Unblock' : 'Block'}
|
||||
{isBlocked($profile.user, data.person_view.person.id)
|
||||
? 'Unblock'
|
||||
: 'Block'}
|
||||
</Button>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import { profile } from '$lib/auth.js'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import { getItemPublished } from '$lib/lemmy/item.js'
|
||||
import type { SortType } from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
@@ -14,7 +15,7 @@ export async function load({ params, url }) {
|
||||
page: page,
|
||||
username: params.name,
|
||||
sort: sort,
|
||||
auth: get(authData)?.token,
|
||||
auth: get(profile)?.jwt,
|
||||
})
|
||||
|
||||
const items = [...user.posts, ...user.comments]
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// @ts-ignore
|
||||
import { build, files } from '$service-worker'
|
||||
|
||||
const version = '0.4.17'
|
||||
const version = '0.4.19'
|
||||
const timestamp = new Date().toLocaleString('en')
|
||||
const worker = self as unknown as ServiceWorkerGlobalScope
|
||||
const FILES = `cache${timestamp}`
|
||||
|
||||
Reference in new issue
Block a user