Basic post display

This commit is contained in:
2023-04-01 14:31:29 +02:00
parent dccb94a792
commit d2f2214d65
13 changed files with 4325 additions and 2891 deletions

View File

@ -1,2 +1,41 @@
<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
<script lang="ts">
import { onMount } from "svelte";
import type { PageData } from './$types';
import type { Post } from '$lib/mastodon/response';
import { PUBLIC_REFRESH_INTERVAL } from '$env/static/public';
import PostComponent from '$lib/components/PostComponent.svelte';
export let data: PageData;
let interval: NodeJS.Timer | null = null;
onMount(async () => {
interval = setInterval(async () => {
const params = new URLSearchParams();
if (data.posts.length > 0) {
params.set('since', data.posts[0].created_at);
}
await fetch(`/api/posts?${params}`)
.then(r => r.json())
.then((resp: Post[]) => {
if (resp.length > 0) {
data.posts = resp.concat(data.posts);
console.log('updated data', resp, data.posts);
}
})
.catch(e => {
// TODO: Show error in UI
console.error('Error loading newest posts', e);
});
}, parseInt(PUBLIC_REFRESH_INTERVAL));
return () => {
if (interval !== null) {
clearInterval(interval)
}
}
})
</script>
{#each data.posts as post (post.id)}
<PostComponent {post}></PostComponent>
{/each}

10
src/routes/+page.ts Normal file
View File

@ -0,0 +1,10 @@
import type { Post } from '$lib/mastodon/response';
import type { PageLoad } from './$types';
export const load = (async ({ fetch }) => {
const p = await fetch('/');
//const posts: Post[] = await p.json();
return {
posts: await p.json() as Post[]
};
}) satisfies PageLoad;

8
src/routes/+server.ts Normal file
View File

@ -0,0 +1,8 @@
import { TimelineReader } from '$lib/server/timeline';
import type { RequestHandler } from './$types';
TimelineReader.init();
export const GET = (async ({ fetch }) => {
return await fetch('api/posts');
}) satisfies RequestHandler;

View File

@ -0,0 +1,15 @@
import { getPosts } from '$lib/server/db';
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
export const GET = (async ({ url }) => {
const since = url.searchParams.get('since');
let count = Number.parseInt(url.searchParams.get('count') || '');
if (isNaN(count)) {
count = 20;
}
count = Math.min(count, 100);
const posts = await getPosts(since, count);
return json(posts);
}) satisfies RequestHandler;