More sorting options + timestamps

This commit is contained in:
Xodrium 2023-02-12 12:06:19 -05:00
parent 010bccc89d
commit 5972139552
9 changed files with 57 additions and 18 deletions

View file

@ -41,7 +41,6 @@
fData.append('last',i == (images.length - 1));
fData.append('id',rId);
method: 'POST',
form = await fetch(`/api/fileCreate`, {
method: 'POST',
body: fData,

View file

@ -3,7 +3,12 @@
import PostButton from '$lib/components/PostButton.svelte';
import PostBody from '$lib/components/PostBody.svelte';
export let success, username, content, upvotes, downvotes, id, isAuthor;
export let success, username, content, upvotes, downvotes, id, isAuthor, time;
let date = 'Time unknown';
if (time)
date = new Date(time / 1000);
let query = (id) ? `/post/${id}` : '';
@ -53,6 +58,12 @@
img {
max-width: 250px;
}
.date {
font-size: 0.8rem;
font-style: italic;
font-weight: normal;
}
</style>
{#if success}
@ -73,6 +84,9 @@
<a href='/user/{username}'>
{username}
</a>
<span class='date'>
{date}
</span>
</span>
<span slot="main">
<PostBody content={content} />

View file

@ -1,10 +1,16 @@
<script>
import Post from '$lib/components/Post.svelte';
import Button from '$lib/components/Button.svelte'
import Button from '$lib/components/Button.svelte';
import {setLocation} from '$lib/util.js';
export let data;
</script>
<p>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','rating')}}>Sort by rating</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','time')}}>Sort by time</Button>
</p>
{#if data && data.postJson && data.postJson.data}
{#each data.postJson.data as post}
<Post
@ -15,11 +21,12 @@
downvotes={post.downvotes}
id={post.id}
isAuthor={post.isAuthor}
time={post.time}
></Post>
{/each}
{/if}
<p>
<Button clickFunc={() => { window.location.search = 'page=' + ((data.id)-1) }}>Previous page</Button>
<Button clickFunc={() => { window.location.search = 'page=' + ((data.id)+1) }}>Next page</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'page',((data.id)-1)) }}>Previous page</Button>
<Button clickFunc={() => { window.location.search = setLocation(window.location,'page',((data.id)+1)) }}>Next page</Button>
</p>

View file

@ -7,6 +7,11 @@ const AUTH_ACTIONS = [
'postDelete'
];
const LEGAL_SORTS = [
'time',
'rating'
]
const FILE_SIZE_LIMIT = 1024*1024*16;
const VALID_EXTENSIONS = ['png','jpg','jpeg','gif','svg']
@ -29,7 +34,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, rating REAL, reply CHAR(64))');
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), time 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 bio (username CHAR(64), content CHAR(10240), roles INTEGER)');
@ -155,12 +160,13 @@ backend.postCreate = async ({content, user}) => {
if (reply)
reply = reply.url.split('/').pop();
await db.run('INSERT INTO post (username, id, content, rating, reply) VALUES (?, ?, ?, ?, ?)', [
await db.run('INSERT INTO post (username, id, content, rating, reply, time) VALUES (?, ?, ?, ?, ?, ?)', [
user,
id,
content,
calcVote(0,0),
reply || ''
reply || '',
Math.floor(new Date() * 1000)
])
return {'success': 'Your post has been broadcasted!', 'href': `/post/${id}` };
@ -200,13 +206,15 @@ backend.userBio = async ({user}) => {
return {data: posts[0]};
}
backend.postBulk = async ({page, id, user, cookies}) => {
backend.postBulk = async ({page, id, user, cookies, sort}) => {
var posts;
var userAuth = (await backend.token({cookies})).data;
sort = (LEGAL_SORTS.indexOf(sort) == -1) ? 'rating' : sort;
if (!user && !id) {
posts = await db.all('SELECT * from post ORDER BY rating DESC LIMIT ?, ?', [
posts = await db.all('SELECT * from post ORDER BY '+sort+' DESC LIMIT ?, ?', [
page*ROW_COUNT,
ROW_COUNT
])
@ -217,14 +225,14 @@ backend.postBulk = async ({page, id, user, cookies}) => {
if (posts.length == 0) posts.push({});
posts.push(...(await db.all('SELECT * from post WHERE reply = ? ORDER BY rating DESC LIMIT ?, ?', [
posts.push(...(await db.all('SELECT * from post WHERE reply = ? ORDER BY '+sort+' DESC LIMIT ?, ?', [
id,
page*ROW_COUNT,
ROW_COUNT
])))
} 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 '+sort+' DESC LIMIT ?, ?', [
user,
page*ROW_COUNT,
ROW_COUNT

View file

@ -105,6 +105,13 @@ let safePath = function(path) {
return path.replace(/[\/]+/g, '')
}
let setLocation = function(location, key, value) {
var loc = new URL(location).searchParams;
loc.set(key,value);
return loc.toString();
}
export {
checkLength,
checkRegex,
@ -112,5 +119,6 @@ export {
handleSubmit,
formatPost,
block,
safePath
safePath,
setLocation
};

View file

@ -2,13 +2,13 @@
export async function load({ fetch, params, url }) {
var search = url.searchParams;
var voteType = search.get('vote');
var id = search.get('page') * 1;
var sort = search.get('sort') || 'rating';
await new Promise(resolve => setTimeout(resolve, 100));
const res = await fetch(`/api/postBulk?page=${id}`);
const res = await fetch(`/api/postBulk?page=${id}&sort=${sort}`);
const postJson = await res.json();
return { postJson, id };

View file

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

View file

@ -17,6 +17,7 @@
downvotes={firstEntry.downvotes}
id={firstEntry.id}
isAuthor={firstEntry.isAuthor}
time={firstEntry.time}
></Post>
{/if}

View file

@ -3,6 +3,7 @@ export async function load({ fetch, params, url }) {
var search = url.searchParams;
var voteType = search.get('vote');
var sort = search.get('sort');
var id = search.get('page') * 1;
@ -10,7 +11,7 @@ export async function load({ fetch, params, url }) {
await new Promise(resolve => setTimeout(resolve, 100));
const res = await fetch(`/api/postBulk?user=${user}&page=${id}`);
const res = await fetch(`/api/postBulk?user=${user}&page=${id}&sort=${sort}`);
const postJson = await res.json();
const resUser = await fetch(`/api/userGet?user=${user}`);