From e368b99775274150df5001a4dde2c46725d3e9b5 Mon Sep 17 00:00:00 2001 From: Xodrium <118943715+malloc62@users.noreply.github.com> Date: Sat, 11 Feb 2023 19:42:10 -0500 Subject: [PATCH] Better formatting --- src/lib/components/PostBody.svelte | 30 ++++++++++++--- src/lib/db/db.js | 17 ++++++++- src/lib/util.js | 37 +++++++++++++++---- src/routes/new_post/+page.svelte | 6 +++ src/routes/user/[user]/+page.js | 2 +- src/routes/user/[user]/+page.svelte | 57 ++++++++++++++++++----------- 6 files changed, 111 insertions(+), 38 deletions(-) diff --git a/src/lib/components/PostBody.svelte b/src/lib/components/PostBody.svelte index ea14f4a..75d1493 100644 --- a/src/lib/components/PostBody.svelte +++ b/src/lib/components/PostBody.svelte @@ -1,7 +1,15 @@ @@ -18,10 +26,20 @@ {#each contentSplit as line} - {#if line && line.type == 'img'} - Image preview - {:else} -

{line}

- {/if} +

+ {#each line as elem} + {#if elem && elem.type == 'img'} + {#if line.filter(x => x.type == 'img').length < 2} + Image preview + {:else} + Image preview + {/if} + {:else if elem.type == 'link'} + {elem.display + ' '} + {:else} + {elem + ' '} + {/if} + {/each} +

{/each}
\ No newline at end of file diff --git a/src/lib/db/db.js b/src/lib/db/db.js index 20d848d..c1d4c83 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -29,7 +29,8 @@ async function initDb() { await db.run('CREATE TABLE IF NOT EXISTS token (username CHAR(64), token CHAR(1024))'); await db.run('CREATE TABLE IF NOT EXISTS post (username CHAR(64), id CHAR(64), content CHAR(10240), upvotes INTEGER, downvotes INTEGER, rating REAL)'); await db.run('CREATE TABLE IF NOT EXISTS vote (id CHAR(64), username CHAR(64), type INTEGER)'); - await db.run('CREATE TABLE IF NOT EXISTS user (username CHAR(64), followers INTEGER, following INTEGER, upvotes INTEGER, downvotes INTEGER, reputation REAL)'); + await db.run('CREATE TABLE IF NOT EXISTS user (username CHAR(64), followers INTEGER, following INTEGER, upvotes INTEGER, downvotes INTEGER, reputation REAL)'); + await db.run('CREATE TABLE IF NOT EXISTS bio (username CHAR(64), content CHAR(10240), roles INTEGER)'); } let backendProxy = async ({route, backendParams}) => { @@ -189,7 +190,19 @@ backend.userGet = async ({user}) => { ]) if (!posts || posts.length < 1) { - return {'success': 'Post does not exist.'} + return {'success': 'User does not exist.'} + } + + return {data: posts[0]}; +} + +backend.userBio = async ({user}) => { + var posts = await db.all('SELECT * from bio WHERE username = ?', [ + user + ]) + + if (!posts || posts.length < 1) { + return {'success': 'Bio does not exist.'} } return {data: posts[0]}; diff --git a/src/lib/util.js b/src/lib/util.js index 8341f94..1858365 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -63,19 +63,40 @@ let formatPost = function(post) { post = post.split('\n'); post = post.map(subPost => { - var splitPost = subPost.split('||'); + return subPost.split(' '); + }); - if (splitPost.length > 1) { - var cap1 = splitPost[0]; + post = post.map(line => { + line = line.map(subPost => { + var splitPost = subPost.split('||'); - if (cap1 == 'img') { - var matchCleaned = splitPost[1].replace(/(\s+)/g, '\\$1'); - splitPost = {'type': 'img', 'url': `/img/${matchCleaned}`}; + if (splitPost.length > 1) { + var cap1 = splitPost[0]; + + if (cap1 == 'img') { + var matchCleaned = splitPost[1].replace(/(\s+)/g, '\\$1'); + splitPost = {'type': 'img', 'url': `/img/${matchCleaned}`}; + + return splitPost; + } + } else if (subPost[0] == '@') { + var subPostIn = subPost.substring(0).replaceAll(/[^A-Za-z0-9\-\_]/g, ''); + + splitPost = {'type': 'link', 'display': subPost, 'url': `/user/${subPostIn}`}; + + return splitPost; + + } else if (subPost[0] == '#') { + var subPostIn = subPost.substring(0).replaceAll(/[^A-Za-z0-9]/g, ''); + + splitPost = {'type': 'link', 'display': subPost, 'url': `/post/${subPostIn}`}; return splitPost; } - } - return subPost; + + return subPost; + }) + return line; }); return post; diff --git a/src/routes/new_post/+page.svelte b/src/routes/new_post/+page.svelte index 291d0f5..ba52050 100644 --- a/src/routes/new_post/+page.svelte +++ b/src/routes/new_post/+page.svelte @@ -82,5 +82,11 @@

img||filename.blah embeds a user-uploaded file in this site

+

+ @user mentions a user +

+

+ #post replies to a post by ID +

\ No newline at end of file diff --git a/src/routes/user/[user]/+page.js b/src/routes/user/[user]/+page.js index 0818dd1..9fb0dad 100644 --- a/src/routes/user/[user]/+page.js +++ b/src/routes/user/[user]/+page.js @@ -14,7 +14,7 @@ export async function load({ fetch, params, url }) { const postJson = await res.json(); const resUser = await fetch(`/api/userGet?user=${user}`); - const postJsonUser = await resUser.json(); + const postJsonUser = (await resUser.json()) || {}; return { postJson, id, postJsonUser }; } \ No newline at end of file diff --git a/src/routes/user/[user]/+page.svelte b/src/routes/user/[user]/+page.svelte index 64b6db3..2fe86ce 100644 --- a/src/routes/user/[user]/+page.svelte +++ b/src/routes/user/[user]/+page.svelte @@ -8,27 +8,42 @@ let userData = data.postJsonUser.data; - - - - {userData.username} - - - -

- Reputation: {userData.reputation} -

-

- Upvotes: {userData.upvotes} -

-

- Downvotes: {userData.downvotes} -

-
- - - - +{#if userData} + + + + {userData.username} + + + +

+ Reputation: {userData.reputation} +

+

+ Upvotes: {userData.upvotes} +

+

+ Downvotes: {userData.downvotes} +

+
+ + + + +{:else} + + + + Error + + + + + + This user does not exist. + + +{/if}

Posts