Added replies

This commit is contained in:
Xodrium 2023-02-12 04:50:15 -05:00
parent 1c75af5349
commit e9723303d8
6 changed files with 54 additions and 14 deletions

View file

@ -80,12 +80,12 @@
<span slot="footer"> <span slot="footer">
<PostButton <PostButton
clickFunc={() => vote('up')} clickFunc={() => vote('up')}
data={upvotes * 1 + ' Yes'} data={upvotes * 1}
icon='/upvote.svg' icon='/upvote.svg'
/> />
<PostButton <PostButton
clickFunc={() => vote('down')} clickFunc={() => vote('down')}
data={downvotes * 1 + ' No'} data={downvotes * 1 }
icon='/downvote.svg' icon='/downvote.svg'
/> />
{#if isAuthor} {#if isAuthor}
@ -102,6 +102,11 @@
icon='/view.svg' icon='/view.svg'
/> />
{/if} {/if}
<PostButton
href='/new_post?init=%23{id}'
data={'Reply'}
icon='/view.svg'
/>
</span> </span>
</Area> </Area>
{/if} {/if}

View file

@ -18,7 +18,7 @@ import { open } from 'sqlite'
import { hash, compare } from 'bcrypt' import { hash, compare } from 'bcrypt'
import { randomBytes, createHash } from 'node:crypto'; import { randomBytes, createHash } from 'node:crypto';
import { writeFile } from 'node:fs/promises'; import { writeFile } from 'node:fs/promises';
import { calcVote, checkLength, checkRegex, safePath } from '../util.js'; import { calcVote, checkLength, checkRegex, safePath, formatPost } from '../util.js';
var db; var db;
async function initDb() { async function initDb() {
@ -29,7 +29,7 @@ async function initDb() {
await db.run('CREATE TABLE IF NOT EXISTS auth (username CHAR(64), password CHAR(1024))'); await db.run('CREATE TABLE IF NOT EXISTS auth (username CHAR(64), password CHAR(1024))');
await db.run('CREATE TABLE IF NOT EXISTS token (username CHAR(64), token CHAR(1024))'); 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 post (username CHAR(64), id CHAR(64), content CHAR(10240), upvotes INTEGER, downvotes INTEGER, rating REAL, reply CHAR(64))');
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 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)'); await db.run('CREATE TABLE IF NOT EXISTS bio (username CHAR(64), content CHAR(10240), roles INTEGER)');
@ -149,11 +149,18 @@ backend.postCreate = async ({content, user}) => {
var id = randomBytes(10).toString('hex'); var id = randomBytes(10).toString('hex');
await db.run('INSERT INTO post (username, id, content, rating) VALUES (?, ?, ?, ?)', [ var postFlatten = formatPost(content).flat();
var reply = postFlatten[postFlatten.findIndex(x => x.subtype == 'post')];
if (reply)
reply = reply.url.split('/').pop();
await db.run('INSERT INTO post (username, id, content, rating, reply) VALUES (?, ?, ?, ?, ?)', [
user, user,
id, id,
content, content,
calcVote(0,0) calcVote(0,0),
reply || ''
]) ])
return {'success': 'Your post has been broadcasted!', 'href': `/post/${id}` }; return {'success': 'Your post has been broadcasted!', 'href': `/post/${id}` };
@ -206,7 +213,16 @@ backend.postBulk = async ({page, id, user, cookies}) => {
} else if (id) { } else if (id) {
posts = await db.all('SELECT * from post WHERE id = ?', [ posts = await db.all('SELECT * from post WHERE id = ?', [
id id
]) ]);
if (posts.length == 0) posts.push({});
posts.push(...(await db.all('SELECT * from post WHERE reply = ? ORDER BY rating DESC LIMIT ?, ?', [
id,
page*ROW_COUNT,
ROW_COUNT
])))
} else { } else {
posts = await db.all('SELECT * from post WHERE username = ? ORDER BY rating DESC LIMIT ?, ?', [ posts = await db.all('SELECT * from post WHERE username = ? ORDER BY rating DESC LIMIT ?, ?', [
user, user,

View file

@ -83,7 +83,7 @@ let formatPost = function(post) {
var type = (subPost[0] == '@') ? 'user' : 'post'; var type = (subPost[0] == '@') ? 'user' : 'post';
splitPost = {'type': 'link', 'display': subPost, 'url': `/${type}/${subPostIn}`}; splitPost = {'type': 'link', 'display': subPost, 'subtype': type, 'url': `/${type}/${subPostIn}`};
return splitPost; return splitPost;
} }

View file

@ -1,17 +1,19 @@
<script> <script>
import { page } from '$app/stores'
import Area from '$lib/components/Area.svelte'; import Area from '$lib/components/Area.svelte';
import FileUpload from '$lib/components/FileUpload.svelte'; import FileUpload from '$lib/components/FileUpload.svelte';
import PostBody from '$lib/components/PostBody.svelte'; import PostBody from '$lib/components/PostBody.svelte';
import Button from '$lib/components/Button.svelte'; import Button from '$lib/components/Button.svelte';
import { handleSubmit, formatPost } from '$lib/util.js';; import { handleSubmit, formatPost } from '$lib/util.js';
/** @type {import('./$types').ActionData} */ /** @type {import('./$types').ActionData} */
export let form; export let form;
let uploadForm = {}; let uploadForm = {};
let formContent = ''; let formContent = $page.url.searchParams.get('init') || '';
let formBody; let formBody;
let currentState = 'editor'; let currentState = 'editor';
@ -48,8 +50,7 @@
method='POST' method='POST'
on:submit|preventDefault={async e => form = JSON.parse(await handleSubmit(e)) } on:submit|preventDefault={async e => form = JSON.parse(await handleSubmit(e)) }
> >
<textarea name='content' style='display: none;' value={formContent}></textarea <textarea name='content' style='display: none;' value={formContent}></textarea>
</form>
<input formaction="?/create" type='submit' value='Post' bind:this={formBody} style='display: none;'> <input formaction="?/create" type='submit' value='Post' bind:this={formBody} style='display: none;'>
{#if currentState == 'editor'} {#if currentState == 'editor'}
<p> <p>
@ -60,6 +61,7 @@
{:else} {:else}
<FileUpload bind:form={uploadForm} /> <FileUpload bind:form={uploadForm} />
{/if} {/if}
</form>
</div> </div>
<span slot="footer"> <span slot="footer">
<div class='wrapper'> <div class='wrapper'>

View file

@ -4,11 +4,12 @@ import { redirect } from '@sveltejs/kit';
export async function load({ fetch, params, url }) { export async function load({ fetch, params, url }) {
var id = params.post; var id = params.post;
var page = url.searchParams.get('page') * 1;
await new Promise(resolve => setTimeout(resolve, 100)); await new Promise(resolve => setTimeout(resolve, 100));
const res = await fetch(`/api/postBulk?id=${id}`); const res = await fetch(`/api/postBulk?id=${id}&page=${page}`);
const postJson = (await res.json()); const postJson = (await res.json());
return {postJson}; return {postJson, id: page};
} }

View file

@ -1,8 +1,24 @@
<script> <script>
import PostList from '$lib/components/PostList.svelte'; import PostList from '$lib/components/PostList.svelte';
import Post from '$lib/components/Post.svelte';
/** @type {import('./$types').PageData} */ /** @type {import('./$types').PageData} */
export let data; export let data;
let firstEntry = data.postJson.data.shift();
</script> </script>
{#if firstEntry.username}
<Post
success={firstEntry.success}
username={firstEntry.username}
content={firstEntry.content}
upvotes={firstEntry.upvotes}
downvotes={firstEntry.downvotes}
id={firstEntry.id}
isAuthor={firstEntry.isAuthor}
></Post>
{/if}
<h2>Replies</h2>
<PostList data={data} /> <PostList data={data} />