diff --git a/src/lib/components/FileUpload.svelte b/src/lib/components/FileUpload.svelte index 9a8d5e8..8b28ac6 100644 --- a/src/lib/components/FileUpload.svelte +++ b/src/lib/components/FileUpload.svelte @@ -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, diff --git a/src/lib/components/Post.svelte b/src/lib/components/Post.svelte index f17033a..f9ef00a 100644 --- a/src/lib/components/Post.svelte +++ b/src/lib/components/Post.svelte @@ -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; + } {#if success} @@ -73,6 +84,9 @@ {username} + + {date} + diff --git a/src/lib/components/PostList.svelte b/src/lib/components/PostList.svelte index a060303..2ef7356 100644 --- a/src/lib/components/PostList.svelte +++ b/src/lib/components/PostList.svelte @@ -1,10 +1,16 @@ +

+ + +

+ {#if data && data.postJson && data.postJson.data} {#each data.postJson.data as post} {/each} {/if}

- - + +

\ No newline at end of file diff --git a/src/lib/db/db.js b/src/lib/db/db.js index dc3ceb7..a6e04d1 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -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 diff --git a/src/lib/util.js b/src/lib/util.js index c986a7d..b9c2dd9 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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 }; \ No newline at end of file diff --git a/src/routes/+page.js b/src/routes/+page.js index f684945..3c26ebb 100644 --- a/src/routes/+page.js +++ b/src/routes/+page.js @@ -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 }; diff --git a/src/routes/post/[post]/+page.js b/src/routes/post/[post]/+page.js index 3fbaa80..4de9b03 100644 --- a/src/routes/post/[post]/+page.js +++ b/src/routes/post/[post]/+page.js @@ -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}; diff --git a/src/routes/post/[post]/+page.svelte b/src/routes/post/[post]/+page.svelte index 2e56f1f..f4cf854 100644 --- a/src/routes/post/[post]/+page.svelte +++ b/src/routes/post/[post]/+page.svelte @@ -17,6 +17,7 @@ downvotes={firstEntry.downvotes} id={firstEntry.id} isAuthor={firstEntry.isAuthor} + time={firstEntry.time} > {/if} diff --git a/src/routes/user/[user]/+page.js b/src/routes/user/[user]/+page.js index bdb904f..5b1da46 100644 --- a/src/routes/user/[user]/+page.js +++ b/src/routes/user/[user]/+page.js @@ -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}`);