Added a sort for trending posts

This commit is contained in:
tdgmdev 2023-03-08 18:56:28 -05:00
parent 2bb1c9725d
commit e2a3f560c1
3 changed files with 15 additions and 8 deletions

View file

@ -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}

View file

@ -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 ?, ?', [

View file

@ -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';