From 4fd23a3694ec5ecf4d84341d769a9964b1dc6377 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 20 Aug 2024 22:50:55 +0100 Subject: [PATCH] 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 --- api/resolvers/item.js | 23 +++++++++++++++++++++++ components/items.js | 2 +- components/nav/common.js | 4 ++-- next.config.js | 2 +- pages/~/random/index.js | 31 +++++++++++++++++++++++++++++++ pages/~/recent/[type].js | 1 + 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 pages/~/random/index.js diff --git a/api/resolvers/item.js b/api/resolvers/item.js index ca45bbe7..bd2f5a72 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -80,6 +80,8 @@ const orderByClause = (by, me, models, type) => { return 'ORDER BY "Item".msats DESC' case 'zaprank': return topOrderByWeightedSats(me, models) + case 'random': + return 'ORDER BY RANDOM()' default: return `ORDER BY ${type === 'bookmarks' ? '"bookmarkCreatedAt"' : '"Item".created_at'} DESC` } @@ -400,6 +402,27 @@ export default { orderBy: orderByClause(by || 'zaprank', me, models, type) }, ...whenRange(when, from, to || decodedCursor.time), decodedCursor.offset, limit, ...subArr) 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: // sub so we know the default ranking if (sub) { diff --git a/components/items.js b/components/items.js index 6a2064c7..3d4e90de 100644 --- a/components/items.js +++ b/components/items.js @@ -51,7 +51,7 @@ export default function Items ({ ssrData, variables = {}, query, destructureData <>
{itemsWithPins.filter(filter).map((item, i) => ( - 0} /> + 0} /> ))}
recent - {/* + random - */} + {sub !== 'jobs' && ({ source, destination: '/~' + source })) + ...['/', '/post', '/rss', '/random', '/recent/:slug*', '/top/:slug*'].map(source => ({ source, destination: '/~' + source })) ] }, async redirects () { diff --git a/pages/~/random/index.js b/pages/~/random/index.js new file mode 100644 index 00000000..25bfd355 --- /dev/null +++ b/pages/~/random/index.js @@ -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 ( + + + + ) +} diff --git a/pages/~/recent/[type].js b/pages/~/recent/[type].js index 3bf7f60f..7e09afb6 100644 --- a/pages/~/recent/[type].js +++ b/pages/~/recent/[type].js @@ -11,6 +11,7 @@ import PageLoading from '@/components/page-loading' const staticVariables = { sort: 'recent' } const variablesFunc = vars => ({ includeComments: COMMENT_TYPE_QUERY.includes(vars.type), ...staticVariables, ...vars }) + export const getServerSideProps = getGetServerSideProps({ query: SUB_ITEMS, variables: variablesFunc,