i forgot whatever i've been doing, i think i added an inbox

This commit is contained in:
Xyphyn committed 2023-07-04 18:45:15 -07:00
1 parent ef037349f0
commit 04704749b7
24 files changed
+307 -86

No files matched your search

+26 -14
View File
@@ -10,6 +10,12 @@ function buildUrl(inputUrl: URL): URL {
return new URL(`${withoutCors}?${inputUrl.searchParams.toString()}`)
}
export async function handleError() {
return {
message: 'The proxy failed to fetch from that URL',
}
}
export async function handle({ event, resolve }) {
// annoying hack to fix lemmy's CORS
if (event.url.pathname.startsWith('/cors')) {
@@ -27,10 +33,20 @@ export async function handle({ event, resolve }) {
// @ts-ignore this works, idk why typescript complains
duplex: 'half',
signal: AbortSignal.timeout(20 * 1000),
})
}).catch((_) => undefined)
if (!data) {
return new Response(
JSON.stringify({
message: 'proxy failed to fetch',
}),
{
status: 500,
}
)
}
if (!data.ok) {
await resolve(event)
return new Response(
JSON.stringify({
message: await data.text(),
@@ -44,23 +60,18 @@ export async function handle({ event, resolve }) {
try {
const json = await data.json()
await resolve(event)
return new Response(JSON.stringify(json), {
status: data.status,
})
} catch (err) {
await resolve(event)
return new Response(
JSON.stringify({
message: 'Failed to fetch',
}),
{
status: data.status,
}
)
} catch (error) {
console.log('Caught the thing')
return new Response(JSON.stringify({ message: 'i hate everything' }), {
status: 500,
})
}
} catch (error) {
await resolve(event)
console.log('caught it')
return new Response(
JSON.stringify({
message: 'the proxy failed to fetch from server',
@@ -71,6 +82,7 @@ export async function handle({ event, resolve }) {
)
}
}
const response = await resolve(event)
return response
}
@@ -19,6 +19,8 @@
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
import { authData, getClient } from '$lib/lemmy.js'
import PostVote from '$lib/components/lemmy/PostVote.svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
let postRes: PostView
export { postRes as post }
@@ -96,55 +98,22 @@
{/if}
</div>
<div class="flex flex-row gap-2 pb-4 px-4">
<div
class="flex flex-row items-center gap-1 rounded-md transition-colors cursor-pointer
{// upvote
vote == 1
? 'bg-orange-600/20 text-orange-500'
: // downvote
vote == -1
? 'bg-blue-600/40 text-blue-400'
: // no vote
'bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200 dark:hover:bg-zinc-700'}"
>
<button
aria-label="Upvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == 1 ? 0 : 1
upvote()
}}
>
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">
{Intl.NumberFormat('en', { notation: 'compact' }).format(
postRes.counts.score
)}
</span>
<button
aria-label="Downvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == -1 ? 0 : -1
downvote()
}}
>
<Icon src={ChevronDown} mini width={20} height={20} />
</button>
</div>
<PostVote
post={postRes.post}
vote={postRes.my_vote}
score={postRes.counts.score}
/>
<a
class="flex flex-row items-center gap-1 p-1.5 px-3 rounded-md
bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200
dark:hover:bg-zinc-700 transition-colors"
bg-slate-100 dark:bg-zinc-900 hover:bg-slate-200
dark:hover:bg-zinc-700 transition-colors border border-slate-200
dark:border-zinc-800"
href="/post/{postRes.post.id}"
>
<Icon src={ChatBubbleOvalLeft} mini width={16} height={16} />
<span class="text-sm">
{Intl.NumberFormat('en', { notation: 'compact' }).format(
postRes.counts.comments
)}
<FormattedNumber number={postRes.counts.comments} />
</span>
</a>
</div>
+75
View File
@@ -0,0 +1,75 @@
<script lang="ts">
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { authData, getClient } from '$lib/lemmy'
import type { Post } from 'lemmy-js-client'
import { ChevronDown, ChevronUp, Icon } from 'svelte-hero-icons'
export let post: Post
export let vote: number | undefined
export let score: number
async function upvote() {
if (!$authData) return
const upvoted = vote == 1
await getClient()
.likePost({
score: upvoted ? 0 : 1,
auth: $authData.token,
post_id: post.id,
})
.catch((_) => undefined)
}
async function downvote() {
if (!$authData) return
const upvoted = vote == -1
await getClient()
.likePost({
score: upvoted ? 0 : -1,
auth: $authData.token,
post_id: post.id,
})
.catch((_) => undefined)
}
</script>
<div
class="flex flex-row items-center gap-1 rounded-md transition-colors
cursor-pointer border border-slate-200 dark:border-zinc-800
{// upvote
vote == 1
? 'bg-orange-600/20 text-orange-500'
: // downvote
vote == -1
? 'bg-blue-600/40 text-blue-400'
: // no vote
'bg-slate-100 dark:bg-zinc-900 hover:bg-slate-200 dark:hover:bg-zinc-700'}"
>
<button
aria-label="Upvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == 1 ? 0 : 1
upvote()
}}
>
<Icon src={ChevronUp} mini width={20} height={20} />
</button>
<span class="text-sm">
<FormattedNumber number={score} />
</span>
<button
aria-label="Downvote"
class="p-1 px-1.5"
on:click={() => {
vote = vote == -1 ? 0 : -1
downvote()
}}
>
<Icon src={ChevronDown} mini width={20} height={20} />
</button>
</div>
@@ -7,13 +7,14 @@
Minus,
Plus,
} from 'svelte-hero-icons'
import Avatar from '../../../../lib/components/ui/Avatar.svelte'
import type { CommentNodeI } from '../comments.js'
import Avatar from '$lib/components/ui/Avatar.svelte'
import type { CommentNodeI } from './comments'
import SvelteMarkdown from 'svelte-markdown'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { getClient, authData } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import CommentForm from './CommentForm.svelte'
import { page } from '$app/stores'
export let node: CommentNodeI
@@ -55,7 +56,12 @@
}
</script>
<li class="py-3">
<li
class="py-3 {$page.url.hash == `#${node.comment_view.comment.id}`
? 'bg-slate-300 dark:bg-zinc-800'
: ''}"
id="#{node.comment_view.comment.id.toString()}"
>
<details bind:open class="flex flex-col gap-1">
<summary
class="flex flex-row cursor-pointer arrow gap-2 items-center group"
@@ -66,7 +72,7 @@
width={24}
/>
<span class="text-sm">{node.comment_view.creator.name}</span>
<span class="text-sm opacity-60">
<span class="text-sm opacity-60 md:inline hidden">
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
1
? ''
@@ -139,12 +145,14 @@
{postId}
parentId={node.comment_view.comment.id}
on:comment={(e) => {
node.children.unshift(e.detail)
node.children = [e.detail, ...node.children]
replying = false
}}
/>
</div>
{/if}
<slot />
<div class="bg-transparent dark:bg-transparent">
<slot />
</div>
</details>
</li>
@@ -50,7 +50,7 @@
focus:dark:border-white transition-colors rounded-md"
bind:value={newComment}
/>
<div class="w-24 ml-auto">
<div class="w-24 ml-auto flex flex-row gap-2">
<Button
large
on:click={submit}
@@ -1,13 +1,23 @@
<script lang="ts">
import Comment from './Comment.svelte'
import type { CommentNodeI } from '../comments.js'
import type { CommentNodeI } from './comments'
import type { Post } from 'lemmy-js-client'
import { page } from '$app/stores'
import { onMount } from 'svelte'
const maxComments = 250
export let nodes: CommentNodeI[]
export let isParent: boolean
export let post: Post
onMount(() => {
if (isParent && $page.url.hash) {
document.getElementById($page.url.hash)?.scrollIntoView({
behavior: 'smooth',
})
}
})
</script>
<ul
+1 -1
View File
@@ -6,7 +6,7 @@
<div
class="bg-white border border-slate-200
dark:border-zinc-800 dark:bg-zinc-900 break-words
flex-col gap-4 p-4 max-w-lg rounded-md {clazz}"
flex-col gap-4 p-4 rounded-md {clazz}"
>
<slot />
</div>
+4
View File
@@ -16,6 +16,7 @@
ArrowRightOnRectangle,
Cog6Tooth,
Icon,
Inbox,
} from 'svelte-hero-icons'
</script>
@@ -64,6 +65,9 @@
<Icon src={ArrowLeftOnRectangle} mini width={16} /> Log in
</MenuButton>
{:else}
<MenuButton link href="/inbox">
<Icon src={Inbox} mini width={16} /> Inbox
</MenuButton>
<MenuButton
on:click={() => {
localStorage.removeItem('user')
+1 -1
View File
@@ -48,7 +48,7 @@
{top ? 'bottom-[100%] origin-bottom-left' : 'top-[100%]'}
rounded-md py-2 min-w-[16rem] my-2 flex flex-col shadow-lg {itemsClass
? itemsClass
: 'bg-white dark:bg-zinc-900'}"
: 'bg-white dark:bg-zinc-900 border border-slate-200 dark:border-zinc-800'}"
>
{#if open}
<slot />
+1 -1
View File
@@ -6,7 +6,7 @@ import {
import { get, writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
export const DEFAULT_INSTANCE_URL = 'lemmy.world'
export const DEFAULT_INSTANCE_URL = 'lemm.ee'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export function getClient(instance?: string): LemmyHttp {
+22
View File
@@ -0,0 +1,22 @@
import type {
CommentReplyView,
PersonMentionView,
PrivateMessageView,
} from 'lemmy-js-client'
export type InboxItemView =
| PersonMentionView
| CommentReplyView
| PrivateMessageView
export function getInboxItemPublished(item: InboxItemView): string {
if ('comment_reply' in item) {
return item.comment_reply.published
}
if ('private_message' in item) {
return item.private_message.published
}
return item.person_mention.published
}
+1 -2
View File
@@ -1,7 +1,6 @@
<script lang="ts">
import { page } from '$app/stores'
import Link from '$lib/components/input/Link.svelte'
import { addToast } from '$lib/components/ui/toasts/toasts.js'
</script>
<div class="flex flex-col mx-auto gap-4 justify-center items-center">
@@ -10,7 +9,7 @@
Failed to fetch page. The lemmy servers are having quite a lot of issues
right now, this is likely on their end.
</p>
<code class="bg-zinc-800 px-2 py-1 rounded-md">
<code class="bg-slate-100 dark:bg-zinc-800 px-2 py-1 rounded-md">
{JSON.stringify($page.error)}
</code>
<Link href={$page.url.toString()}>Retry</Link>
+2 -2
View File
@@ -14,8 +14,8 @@
PencilSquare,
UserGroup,
} from 'svelte-hero-icons'
import Avatar from '../lib/components/ui/Avatar.svelte'
import Post from './post/[id]/Post.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Post from '$lib/components/lemmy/Post.svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
+1 -1
View File
@@ -2,7 +2,7 @@ import { authData, getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
export async function load({ url }) {
export async function load({ url, params }) {
const page = Number(url.searchParams.get('page') || 1) || 1
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
+1 -1
View File
@@ -4,7 +4,7 @@
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import SvelteMarkdown from 'svelte-markdown'
import Avatar from '../../../lib/components/ui/Avatar.svelte'
import Post from '../../post/[id]/Post.svelte'
import Post from '../../../lib/components/lemmy/Post.svelte'
import { goto } from '$app/navigation'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import {
+1 -1
View File
@@ -9,7 +9,7 @@ export function load(req: any) {
(req.url.searchParams.get('sort') as SortType) || 'Active'
return {
posts: getClient().getPosts({
posts: getClient(req.params).getPosts({
limit: 40,
community_name: req.params.name,
page: page,
+18
View File
@@ -0,0 +1,18 @@
<script lang="ts">
import InboxItem from './InboxItem.svelte'
export let data
</script>
<h1 class="font-bold text-3xl">Inbox</h1>
<div class="flex flex-col gap-4 list-none my-4">
{#if !data.data || (data.data?.length ?? 0) == 0}
<p class="text-center opacity-60 text-lg mx-4">
Wow, it's quite empty in here.
</p>
{:else}
{#each data.data as item}
<InboxItem {item} />
{/each}
{/if}
</div>
+36
View File
@@ -0,0 +1,36 @@
import { authData, getClient } from '$lib/lemmy.js'
import { getInboxItemPublished } from '$lib/lemmy/inbox.js'
import { get } from 'svelte/store'
export async function load() {
const auth = get(authData)
if (!auth) return
const client = getClient()
const params = {
limit: 50,
page: 1,
auth: auth.token,
}
const [replies, mentions, privateMessages] = await Promise.all([
client.getReplies({
...params,
sort: 'New',
}),
client.getPersonMentions({
...params,
sort: 'New',
}),
client.getPrivateMessages(params),
])
const data = [...replies.replies, ...mentions.mentions].sort(
(a, b) =>
Date.parse(getInboxItemPublished(b)) -
Date.parse(getInboxItemPublished(a))
)
return { data: data }
}
+39
View File
@@ -0,0 +1,39 @@
<script lang="ts">
import Link from '$lib/components/input/Link.svelte'
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Card from '$lib/components/ui/Card.svelte'
import type { CommentReplyView, PersonMentionView } from 'lemmy-js-client'
export let item: CommentReplyView | PersonMentionView
</script>
<Card class="flex flex-col bg-white rounded-md p-4 max-w-full">
<div class="flex flex-row items-center">
<div
class="text-sm max-w-[80ch] whitespace-nowrap text-ellipsis overflow-hidden"
>
<span class="font-bold">
{item.creator.display_name ?? item.creator.name}
</span>
replied to you in
<a
href="/post/{item.post.id}"
class="font-bold max-w-[48ch] overflow-hidden text-ellipsis
whitespace-nowrap inline hover:underline"
>
{item.post.name}
</a>
</div>
<div class="w-max ml-auto">
<Link href="/post/{item.post.id}#{item.comment.id}">Jump</Link>
</div>
</div>
<div>
<Comment
postId={item.post.id}
node={{ children: [], comment_view: item, depth: 1 }}
replying={false}
/>
</div>
</Card>
+18
View File
@@ -0,0 +1,18 @@
import { DEFAULT_INSTANCE_URL, authData } from '$lib/lemmy.js'
import { error, redirect } from '@sveltejs/kit'
import { get } from 'svelte/store'
export function load({ params, url }) {
if (Number(params.instance)) {
const split = url.pathname.split('/')
split.splice(2, 0, `${get(authData)?.instance ?? DEFAULT_INSTANCE_URL}`)
const newUrl = new URL(url)
newUrl.pathname = split.join('/')
throw redirect(300, newUrl.toString())
}
throw error(404, 'Not found')
}
@@ -1,18 +1,16 @@
<script lang="ts">
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import type {
GetCommentsResponse,
GetPostResponse,
Comment,
CommentView,
} from 'lemmy-js-client'
import type { CommentNodeI } from './comments.js'
import Comments from './comment/Comments.svelte'
import type { Comment, CommentView } from 'lemmy-js-client'
import type { CommentNodeI } from '$lib/components/lemmy/comment/comments.js'
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
import SvelteMarkdown from 'svelte-markdown'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
import { user } from '$lib/lemmy.js'
import CommentForm from './comment/CommentForm.svelte'
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
import PostVote from '$lib/components/lemmy/PostVote.svelte'
import { page } from '$app/stores'
import { onMount } from 'svelte'
export let data
@@ -117,6 +115,13 @@
<SvelteMarkdown source={post.body} />
</p>
{/if}
<div class="w-max">
<PostVote
post={data.post.post_view.post}
score={data.post.post_view.counts.score}
vote={data.post.post_view.my_vote}
/>
</div>
</div>
<div class="mt-4 flex flex-col gap-2">
<div class="font-bold text-lg">
@@ -141,7 +146,8 @@
{#if $user}
<CommentForm
postId={post.id}
on:comment={(comment) => comments.comments.unshift(comment.detail)}
on:comment={(comment) =>
(comments.comments = [...comment.detail, comments.comments])}
/>
{/if}
<MultiSelect
@@ -3,12 +3,12 @@ import { get } from 'svelte/store'
export async function load({ params }) {
return {
post: getClient().getPost({
post: getClient(params.instance).getPost({
id: Number(params.id),
auth: get(authData)?.token,
}),
streamed: {
comments: getClient().getComments({
comments: getClient(params.instance).getComments({
post_id: Number(params.id),
type_: 'All',
limit: 250,
+5
View File
@@ -26,6 +26,11 @@ pre {
border-slate-200 border dark:bg-zinc-900 dark:border-zinc-800 text-sm;
}
code {
@apply bg-slate-200 border border-slate-300 dark:bg-zinc-800
dark:border-zinc-700 rounded-md px-1 py-0.5;
}
ol {
@apply list-decimal;
}