diff --git a/src/lib/components/PostList.svelte b/src/lib/components/PostList.svelte index 7b263c9..2c7e6da 100644 --- a/src/lib/components/PostList.svelte +++ b/src/lib/components/PostList.svelte @@ -7,8 +7,9 @@ </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> + <Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','hot')}}>Hot</Button> + <Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','rating')}}>Top</Button> + <Button clickFunc={() => { window.location.search = setLocation(window.location,'sort','time')}}>Recent</Button> </p> {#if data && data.postJson && data.postJson.data} diff --git a/src/lib/db/db.js b/src/lib/db/db.js index e27f71e..80f52ec 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -9,10 +9,11 @@ const AUTH_ACTIONS = [ 'follow' ]; -const LEGAL_SORTS = [ - 'time', - 'rating' -] +const LEGAL_SORTS = { + 'time': 'time', + 'rating': 'rating', + 'hot': `rating / (%d - time + 24000)` +} const FILE_SIZE_LIMIT = 1024*1024*16; @@ -226,7 +227,12 @@ backend.postBulk = async ({page, id, user, cookies, sort, type}) => { var userAuth = (await backend.token({cookies})).data || ''; - sort = (LEGAL_SORTS.indexOf(sort + '') == -1) ? 'rating' : sort; + sort = (LEGAL_SORTS[sort]) || 'rating'; + + if (sort + '' != sort) sort = 'rating'; + + sort = sort.replaceAll('%d',Math.floor(new Date() * 1000)); + if (type == 'all') { posts = await db.all('SELECT * from post ORDER BY '+sort+' DESC LIMIT ?, ?', [ diff --git a/src/routes/+page.js b/src/routes/+page.js index a391fe7..8d57236 100644 --- a/src/routes/+page.js +++ b/src/routes/+page.js @@ -4,7 +4,7 @@ export async function load({ fetch, params, url }) { var id = search.get('page') * 1; - var sort = search.get('sort') || 'rating'; + var sort = search.get('sort') || 'hot'; var type = search.get('type') || 'all';