added everything

.
This commit is contained in:
Xodrium 2023-02-01 19:22:43 -05:00
parent dcae12453c
commit fcd279884c
15 changed files with 309 additions and 136 deletions

View file

@ -26,15 +26,26 @@
#main {
min-height: 250px;
}
#main.tiny {
min-height: initial;
height: 100px;
overflow: hidden;
}
</style>
<script>
export let tiny = false;
</script>
<div id='content'>
<div id='header'>
<slot name="header">
Header
</slot>
</div>
<div id='main'>
<div id='main' class='{tiny ? "tiny" : ""}'>
<slot name="main">
<p>Content</p>
</slot>

View file

@ -1,15 +0,0 @@
<script>
import Area from '$lib/Area.svelte';
</script>
<Area>
<p slot="header">
Welcome to SvelteKit
</p>
<p slot="main">
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
</p>
<p slot="footer">
</p>
</Area>

View file

@ -1,15 +1,75 @@
<script>
import Area from '$lib/Area.svelte';
export let success, username, content, upvotes, downvotes, id;
let query = (id) ? `/post/${id}` : '';
let contentSplit = content.split('\n');
</script>
<Area>
<p slot="header">
Welcome to SvelteKit
</p>
<p slot="main">
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
</p>
<p slot="footer">
</p>
</Area>
<style>
.button {
width: auto;
height: 35px;
}
.votes {
font-weight: bold;
font-size: 1.5rem;
}
.vote-area {
margin-right: 30px;
}
</style>
{#if success}
<Area>
<p slot="header">
Error
</p>
<p slot="main">
{success}
</p>
<p slot="footer">
Failed to get post.
</p>
</Area>
{:else}
<Area tiny='{!!id}'>
<span slot="header">
<a href='/users/{username}'>
{username}
</a>
</span>
<span slot="main">
{#each contentSplit as line}
<p>{line}</p>
{/each}
</span>
<span slot="footer">
<span class='vote-area'>
<a data-sveltekit-reload href='{query}?vote=up'>
<img src='/upvote.svg' class='button' alt='Upvote'>
</a>
<span class='votes'>
{upvotes + 0}
</span>
</span>
<span class='vote-area'>
<a data-sveltekit-reload href='{query}?vote=down'>
<img src='/downvote.svg' class='button' alt='Downvote'>
</a>
<span class='votes'>
{downvotes + 0}
</span>
</span>
{#if id}
<span class='vote-area'>
<a href='/post/{id}'>
<img src='/view.svg' class='button' alt='View'>
</a>
</span>
{/if}
</span>
</Area>
{/if}

View file

@ -1,6 +1,7 @@
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
import { hash, compare } from 'bcrypt'
import { calcVote } from './util.js';
const {
randomBytes
@ -15,7 +16,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 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)');
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)');
}
@ -67,10 +68,11 @@ async function loginBackend({user, pass}) {
async function postCreateBackend({user, content}) {
var id = randomBytes(10).toString('hex');
await db.run('INSERT INTO post (username, id, content) VALUES (?, ?, ?)', [
await db.run('INSERT INTO post (username, id, content, rating) VALUES (?, ?, ?, ?)', [
user,
id,
content
content,
calcVote(0,0)
])
}
@ -86,6 +88,46 @@ async function postGetBackend({id}) {
return posts[0];
}
async function postGetBulkBackend({page, rows}) {
var posts = await db.all('SELECT * from post ORDER BY rating DESC LIMIT ?, ?', [
page*rows,
rows
])
return posts;
}
async function voteBackend({user, id, vote}) {
if (!user || !id || user == '' || (vote != 'down' && vote != 'up')) return {};
await db.run('DELETE FROM vote WHERE username = ? AND id = ?', [
user,
id
]);
await db.run('INSERT INTO vote (id, username, type) VALUES (?,?,?)', [
id,
user,
vote == 'up' ? 1 : 2
]);
var votes = await db.all('SELECT type from vote WHERE id = ?', [
id
]) || [];
var up = votes.filter(x => x.type == 1).length;
var down = votes.filter(x => x.type == 2).length;
await db.run('UPDATE post SET upvotes = ?, downvotes = ?, rating = ? WHERE id = ?', [
up,
down,
calcVote(up,down),
id
]);
return {};
}
async function tokenBackend({token}) {
if (!db) await initDb();
@ -104,5 +146,7 @@ export {
loginBackend,
tokenBackend,
postCreateBackend,
postGetBackend
postGetBackend,
voteBackend,
postGetBulkBackend
}

View file

@ -20,7 +20,23 @@ let checkRegex = function(string, field, regex) {
return false;
}
let calcVote = function(up,down) {
var upPadded = up + 3;
var downPadded = down + 3;
var totalPadded = Math.max(up + down, 3);
var rating = -Math.log((1 / ((((upPadded - downPadded) / (upPadded + downPadded)) + 1) / 2)) - 1) / Math.log(Math.E);
rating = Math.min(rating,10);
rating = Math.max(rating,-10);
rating = (rating + 10) / 2;
return rating * Math.log(totalPadded);
}
export {
checkLength,
checkRegex
checkRegex,
calcVote
};