From e3b134223ec87ab4a0471eb4e90c20bf739e404e Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 6 Dec 2024 23:23:15 +0000 Subject: allow post search --- src/routes/index.js | 41 ++++++++++++++++++++++++++++++++++- src/views/post-search.pug | 23 ++++++++++++++++++++ src/views/search.pug | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/views/sub-search.pug | 4 ++-- 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/views/post-search.pug create mode 100644 src/views/search.pug (limited to 'src') diff --git a/src/routes/index.js b/src/routes/index.js index 0dafd3e..604cabb 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -102,8 +102,13 @@ router.get("/subs", authenticateToken, async (req, res) => { res.render("subs", { subs, user: req.user }); }); -// GET /search-subreddits +// GET /search router.get("/search", authenticateToken, async (req, res) => { + res.render("search", { user: req.user }); +}); + +// GET /sub-search +router.get("/sub-search", authenticateToken, async (req, res) => { if (!req.query || !req.query.q) { res.render("sub-search", { user: req.user }); } else { @@ -141,6 +146,40 @@ router.get("/search", authenticateToken, async (req, res) => { } }); +// GET /post-search +router.get("/post-search", authenticateToken, async (req, res) => { + if (!req.query || !req.query.q) { + res.render("post-search", { user: req.user }); + } else { + const { q, options } = req.query.q.split(/\s+/).reduce( + (acc, word) => { + if (word.startsWith("+")) { + acc.options.push(word.slice(1)); + } else { + acc.q += `${word} `; + } + return acc; + }, + { options: [], q: "" }, + ); + + const { items, after } = await G.searchSubmissions(q, { + include_over_18: options.includes("nsfw"), + }); + const message = + items.length === 0 + ? "no results found" + : `showing ${items.length} results`; + res.render("post-search", { + items, + after, + message, + user: req.user, + original_query: req.query.q, + }); + } +}); + // GET /dashboard router.get("/dashboard", authenticateToken, async (req, res) => { let invites = null; diff --git a/src/views/post-search.pug b/src/views/post-search.pug new file mode 100644 index 0000000..441d939 --- /dev/null +++ b/src/views/post-search.pug @@ -0,0 +1,23 @@ +include ../mixins/post +include ../mixins/header +include ../mixins/head + +doctype html +html + +head("search posts") + include ../mixins/subUtils + body + main#content + +header(user) + div.hero + h1 search posts + form(action="/post-search" method="get").search-bar + - var prefill = original_query ?? ""; + input(type="text" name="q" placeholder="search posts" value=prefill required).search-input + button(type="submit").search-button go + if message + div.search-message + i #{message} + if items + each item in items + +post(item.data) diff --git a/src/views/search.pug b/src/views/search.pug new file mode 100644 index 0000000..216875f --- /dev/null +++ b/src/views/search.pug @@ -0,0 +1,54 @@ +include ../mixins/header +include ../mixins/head + +doctype html +html + +head("search subreddits") + include ../mixins/subUtils + body + main#content + +header(user) + div.hero + h1 search subreddits + + form(action="/sub-search" method="get").search-bar + - var prefill = original_query ?? ""; + input(type="text" name="q" placeholder="search subreddits" value=prefill required).search-input + button(type="submit").search-button go + + h1 search posts + + form(action="/post-search" method="get").search-bar + - var prefill = original_query ?? ""; + input(type="text" name="q" placeholder="search posts" value=prefill required).search-input + button(type="submit").search-button go + + hr + + h3 tips + p + | you can narrow search results using filters: + br + - + var triples = [ + ["subreddit", "ohio", "find submissions in 'r/ohio'"], + ["author", "spez", "find submissions by 'u/spez'"], + ["site", "x.com", "find submissions from 'x.com'"], + ["url", "text", "search for 'text' in url"], + ["selftext", "text", "search for 'text' in post contents"], + ["self", "yes/no", "include/exclude selftext posts"], + ["nsfw", "yes/no", "include/exclude over-18 posts"], + ] + + ul + each triple in triples + li + strong + | #{triple[0]}: + em #{triple[1]} + |     #{triple[2]} + | example: + br + |     + code subreddit:iowa site:x.com elections + diff --git a/src/views/sub-search.pug b/src/views/sub-search.pug index bf0d402..76d72a8 100644 --- a/src/views/sub-search.pug +++ b/src/views/sub-search.pug @@ -10,9 +10,9 @@ html +header(user) div.hero h1 search subreddits - form(action="/search" method="get").search-bar + form(action="/sub-search" method="get").search-bar - var prefill = original_query ?? ""; - input(type="text" name="q" placeholder="search subreddits (add +nsfw to include over-18 results)" value=prefill required).search-input + input(type="text" name="q" placeholder="search subreddits" value=prefill required).search-input button(type="submit").search-button go if message div.search-message -- cgit v1.2.3