Add Random link and basic query (#1306)

* Add Random link and basic query

* Use random

* refine random sort query

* make vote threshold higher

---------

Co-authored-by: k00b <k00b@stacker.news>
This commit is contained in:
Tom 2024-08-20 22:50:55 +01:00 committed by GitHub
parent 17199e8f91
commit 4fd23a3694
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 59 additions and 4 deletions

View File

@ -80,6 +80,8 @@ const orderByClause = (by, me, models, type) => {
return 'ORDER BY "Item".msats DESC' return 'ORDER BY "Item".msats DESC'
case 'zaprank': case 'zaprank':
return topOrderByWeightedSats(me, models) return topOrderByWeightedSats(me, models)
case 'random':
return 'ORDER BY RANDOM()'
default: default:
return `ORDER BY ${type === 'bookmarks' ? '"bookmarkCreatedAt"' : '"Item".created_at'} DESC` return `ORDER BY ${type === 'bookmarks' ? '"bookmarkCreatedAt"' : '"Item".created_at'} DESC`
} }
@ -400,6 +402,27 @@ export default {
orderBy: orderByClause(by || 'zaprank', me, models, type) orderBy: orderByClause(by || 'zaprank', me, models, type)
}, ...whenRange(when, from, to || decodedCursor.time), decodedCursor.offset, limit, ...subArr) }, ...whenRange(when, from, to || decodedCursor.time), decodedCursor.offset, limit, ...subArr)
break break
case 'random':
items = await itemQueryWithMeta({
me,
models,
query: `
${selectClause(type)}
${relationClause(type)}
${whereClause(
'"Item"."deletedAt" IS NULL',
'"Item"."weightedVotes" - "Item"."weightedDownVotes" > 2',
type === 'posts' && '"Item"."subName" IS NOT NULL',
subClause(sub, 3, subClauseTable(type), me, showNsfw),
typeClause(type),
await filterClause(me, models, type),
muteClause(me))}
${orderByClause('random', me, models, type)}
OFFSET $1
LIMIT $2`,
orderBy: orderByClause('random', me, models, type)
}, decodedCursor.offset, limit, ...subArr)
break
default: default:
// sub so we know the default ranking // sub so we know the default ranking
if (sub) { if (sub) {

View File

@ -51,7 +51,7 @@ export default function Items ({ ssrData, variables = {}, query, destructureData
<> <>
<div className={styles.grid}> <div className={styles.grid}>
{itemsWithPins.filter(filter).map((item, i) => ( {itemsWithPins.filter(filter).map((item, i) => (
<ListItem key={item.id} item={item} rank={rank && i + 1} itemClassName={variables.includeComments ? 'py-2' : ''} pinnable={isHome ? false : pins?.length > 0} /> <ListItem key={`${item.id}-${i + 1}`} item={item} rank={rank && i + 1} itemClassName={variables.includeComments ? 'py-2' : ''} pinnable={isHome ? false : pins?.length > 0} />
))} ))}
</div> </div>
<Foooter <Foooter

View File

@ -326,11 +326,11 @@ export function Sorts ({ sub, prefix, className }) {
<Nav.Link eventKey='recent' className={styles.navLink}>recent</Nav.Link> <Nav.Link eventKey='recent' className={styles.navLink}>recent</Nav.Link>
</Link> </Link>
</Nav.Item> </Nav.Item>
{/* <Nav.Item className={className}> <Nav.Item className={className}>
<Link href={prefix + '/random'} passHref legacyBehavior> <Link href={prefix + '/random'} passHref legacyBehavior>
<Nav.Link eventKey='random' className={styles.navLink}>random</Nav.Link> <Nav.Link eventKey='random' className={styles.navLink}>random</Nav.Link>
</Link> </Link>
</Nav.Item> */} </Nav.Item>
{sub !== 'jobs' && {sub !== 'jobs' &&
<Nav.Item className={className}> <Nav.Item className={className}>
<Link <Link

View File

@ -177,7 +177,7 @@ module.exports = withPlausibleProxy()({
source: '/~:sub/:slug*', source: '/~:sub/:slug*',
destination: '/~/:slug*?sub=:sub' destination: '/~/:slug*?sub=:sub'
}, },
...['/', '/post', '/rss', '/recent/:slug*', '/top/:slug*'].map(source => ({ source, destination: '/~' + source })) ...['/', '/post', '/rss', '/random', '/recent/:slug*', '/top/:slug*'].map(source => ({ source, destination: '/~' + source }))
] ]
}, },
async redirects () { async redirects () {

31
pages/~/random/index.js Normal file
View File

@ -0,0 +1,31 @@
import Layout from '@/components/layout'
import Items from '@/components/items'
import { getGetServerSideProps } from '@/api/ssrApollo'
import { SUB_ITEMS } from '@/fragments/subs'
import { useRouter } from 'next/router'
const staticVariables = { sort: 'random' }
const variablesFunc = vars =>
({ ...staticVariables, ...vars })
export const getServerSideProps = getGetServerSideProps({
query: SUB_ITEMS,
variables: variablesFunc,
notFound: (data, vars) => vars.sub && !data.sub
})
export default function Index ({ ssrData }) {
const router = useRouter()
const variables = variablesFunc(router.query)
const sub = ssrData?.sub?.name || variables.sub
return (
<Layout sub={sub}>
<Items
ssrData={ssrData}
query={SUB_ITEMS}
variables={variables}
noMoreText='NO MORE'
/>
</Layout>
)
}

View File

@ -11,6 +11,7 @@ import PageLoading from '@/components/page-loading'
const staticVariables = { sort: 'recent' } const staticVariables = { sort: 'recent' }
const variablesFunc = vars => const variablesFunc = vars =>
({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars }) ({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars })
export const getServerSideProps = getGetServerSideProps({ export const getServerSideProps = getGetServerSideProps({
query: SUB_ITEMS, query: SUB_ITEMS,
variables: variablesFunc, variables: variablesFunc,