diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 047123c9..87050853 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -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 } diff --git a/src/routes/post/[id]/Post.svelte b/src/lib/components/lemmy/Post.svelte similarity index 69% rename from src/routes/post/[id]/Post.svelte rename to src/lib/components/lemmy/Post.svelte index 1c928b99..99a04316 100644 --- a/src/routes/post/[id]/Post.svelte +++ b/src/lib/components/lemmy/Post.svelte @@ -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}
-
- - - {Intl.NumberFormat('en', { notation: 'compact' }).format( - postRes.counts.score - )} - - -
+ - {Intl.NumberFormat('en', { notation: 'compact' }).format( - postRes.counts.comments - )} +
diff --git a/src/lib/components/lemmy/PostVote.svelte b/src/lib/components/lemmy/PostVote.svelte new file mode 100644 index 00000000..ed03ef5c --- /dev/null +++ b/src/lib/components/lemmy/PostVote.svelte @@ -0,0 +1,75 @@ + + +
+ + + + + +
diff --git a/src/routes/post/[id]/comment/Comment.svelte b/src/lib/components/lemmy/comment/Comment.svelte similarity index 89% rename from src/routes/post/[id]/comment/Comment.svelte rename to src/lib/components/lemmy/comment/Comment.svelte index af862b72..57b6a231 100644 --- a/src/routes/post/[id]/comment/Comment.svelte +++ b/src/lib/components/lemmy/comment/Comment.svelte @@ -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 @@ } -
  • +
  • {node.comment_view.creator.name} - +
  • diff --git a/src/routes/post/[id]/comment/CommentForm.svelte b/src/lib/components/lemmy/comment/CommentForm.svelte similarity index 97% rename from src/routes/post/[id]/comment/CommentForm.svelte rename to src/lib/components/lemmy/comment/CommentForm.svelte index f571ad22..b595dc47 100644 --- a/src/routes/post/[id]/comment/CommentForm.svelte +++ b/src/lib/components/lemmy/comment/CommentForm.svelte @@ -50,7 +50,7 @@ focus:dark:border-white transition-colors rounded-md" bind:value={newComment} /> -
    +
    diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index ee868d62..7c080bc9 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -16,6 +16,7 @@ ArrowRightOnRectangle, Cog6Tooth, Icon, + Inbox, } from 'svelte-hero-icons' @@ -64,6 +65,9 @@ Log in {:else} + + Inbox + { localStorage.removeItem('user') diff --git a/src/lib/components/ui/menu/Menu.svelte b/src/lib/components/ui/menu/Menu.svelte index 59eba320..c91addfa 100644 --- a/src/lib/components/ui/menu/Menu.svelte +++ b/src/lib/components/ui/menu/Menu.svelte @@ -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} diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index 78d41853..3d19ef81 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -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 { diff --git a/src/lib/lemmy/inbox.ts b/src/lib/lemmy/inbox.ts new file mode 100644 index 00000000..a1a73310 --- /dev/null +++ b/src/lib/lemmy/inbox.ts @@ -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 +} diff --git a/src/routes/+error.svelte b/src/routes/+error.svelte index 2d2be96c..98438c16 100644 --- a/src/routes/+error.svelte +++ b/src/routes/+error.svelte @@ -1,7 +1,6 @@
    @@ -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.

    - + {JSON.stringify($page.error)} Retry diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 7407b4d8..957073bb 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -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' diff --git a/src/routes/+page.ts b/src/routes/+page.ts index c37ec4e8..8b3db2b6 100644 --- a/src/routes/+page.ts +++ b/src/routes/+page.ts @@ -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' diff --git a/src/routes/c/[name]/+page.svelte b/src/routes/c/[name]/+page.svelte index 307b746e..3532ffad 100644 --- a/src/routes/c/[name]/+page.svelte +++ b/src/routes/c/[name]/+page.svelte @@ -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 { diff --git a/src/routes/c/[name]/+page.ts b/src/routes/c/[name]/+page.ts index cd1842cf..1d2293f1 100644 --- a/src/routes/c/[name]/+page.ts +++ b/src/routes/c/[name]/+page.ts @@ -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, diff --git a/src/routes/inbox/+page.svelte b/src/routes/inbox/+page.svelte new file mode 100644 index 00000000..a7b4541a --- /dev/null +++ b/src/routes/inbox/+page.svelte @@ -0,0 +1,18 @@ + + +

    Inbox

    +
    + {#if !data.data || (data.data?.length ?? 0) == 0} +

    + Wow, it's quite empty in here. +

    + {:else} + {#each data.data as item} + + {/each} + {/if} +
    diff --git a/src/routes/inbox/+page.ts b/src/routes/inbox/+page.ts new file mode 100644 index 00000000..c1535d1e --- /dev/null +++ b/src/routes/inbox/+page.ts @@ -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 } +} diff --git a/src/routes/inbox/InboxItem.svelte b/src/routes/inbox/InboxItem.svelte new file mode 100644 index 00000000..1b3788ca --- /dev/null +++ b/src/routes/inbox/InboxItem.svelte @@ -0,0 +1,39 @@ + + + +
    +
    + + {item.creator.display_name ?? item.creator.name} + + replied to you in + + {item.post.name} + +
    + +
    + Jump +
    +
    +
    + +
    +
    diff --git a/src/routes/post/[instance]/+page.ts b/src/routes/post/[instance]/+page.ts new file mode 100644 index 00000000..2bb76f19 --- /dev/null +++ b/src/routes/post/[instance]/+page.ts @@ -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') +} diff --git a/src/routes/post/[id]/+page.svelte b/src/routes/post/[instance]/[id]/+page.svelte similarity index 85% rename from src/routes/post/[id]/+page.svelte rename to src/routes/post/[instance]/[id]/+page.svelte index b3d95787..7dc71197 100644 --- a/src/routes/post/[id]/+page.svelte +++ b/src/routes/post/[instance]/[id]/+page.svelte @@ -1,18 +1,16 @@