From 3e3778dddb562d811af661e18bf8f8142f6418f6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 6 Dec 2024 23:39:42 +0000 Subject: add post search, also fix filters, combine old and new parse logic --- src/routes/index.js | 45 +++++++++++++++++++-------------------------- src/views/post-search.pug | 2 +- src/views/search.pug | 10 ++++------ src/views/sub-search.pug | 2 +- 4 files changed, 25 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/routes/index.js b/src/routes/index.js index 604cabb..f4c0fc9 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -112,20 +112,9 @@ router.get("/sub-search", authenticateToken, async (req, res) => { if (!req.query || !req.query.q) { res.render("sub-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 { q, options } = parseQuery(req.query.q); const { items, after } = await G.searchSubreddits(q, { - include_over_18: options.includes("nsfw"), + include_over_18: (options.nsfw ?? "no") === "yes", }); const subs = db .query("SELECT subreddit FROM subscriptions WHERE user_id = $id") @@ -151,20 +140,9 @@ 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 { q, options } = parseQuery(req.query.q); const { items, after } = await G.searchSubmissions(q, { - include_over_18: options.includes("nsfw"), + include_over_18: (options.nsfw ?? "no") === "yes", }); const message = items.length === 0 @@ -180,6 +158,21 @@ router.get("/post-search", authenticateToken, async (req, res) => { } }); +function parseQuery(q) { + return q.split(/\s+/).reduce( + (acc, word) => { + if (word.includes(":")) { + const [key, val] = word.split(":"); + acc.options[key] = val; + } else { + acc.q += `${word} `; + } + return acc; + }, + { options: [], 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 index 441d939..54c1cca 100644 --- a/src/views/post-search.pug +++ b/src/views/post-search.pug @@ -13,7 +13,7 @@ html 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 + input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input button(type="submit").search-button go if message div.search-message diff --git a/src/views/search.pug b/src/views/search.pug index 216875f..ef9b53e 100644 --- a/src/views/search.pug +++ b/src/views/search.pug @@ -13,19 +13,17 @@ html 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 + input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input button(type="submit").search-button go + hr + 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 + input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input button(type="submit").search-button go - - hr - - h3 tips p | you can narrow search results using filters: br diff --git a/src/views/sub-search.pug b/src/views/sub-search.pug index 76d72a8..f2402f7 100644 --- a/src/views/sub-search.pug +++ b/src/views/sub-search.pug @@ -12,7 +12,7 @@ html 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 + input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input button(type="submit").search-button go if message div.search-message -- cgit v1.2.3