diff options
author | Akshay <[email protected]> | 2024-12-06 23:23:15 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2024-12-06 23:23:15 +0000 |
commit | e3b134223ec87ab4a0471eb4e90c20bf739e404e (patch) | |
tree | 30f5c1acac526a9bd02aacc34064ae6bcf67abf1 /src | |
parent | 8dba8a8375ac69b2547a31c9db41a71964ed186c (diff) |
allow post search
Diffstat (limited to 'src')
-rw-r--r-- | src/routes/index.js | 41 | ||||
-rw-r--r-- | src/views/post-search.pug | 23 | ||||
-rw-r--r-- | src/views/search.pug | 54 | ||||
-rw-r--r-- | src/views/sub-search.pug | 4 |
4 files changed, 119 insertions, 3 deletions
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) => { | |||
102 | res.render("subs", { subs, user: req.user }); | 102 | res.render("subs", { subs, user: req.user }); |
103 | }); | 103 | }); |
104 | 104 | ||
105 | // GET /search-subreddits | 105 | // GET /search |
106 | router.get("/search", authenticateToken, async (req, res) => { | 106 | router.get("/search", authenticateToken, async (req, res) => { |
107 | res.render("search", { user: req.user }); | ||
108 | }); | ||
109 | |||
110 | // GET /sub-search | ||
111 | router.get("/sub-search", authenticateToken, async (req, res) => { | ||
107 | if (!req.query || !req.query.q) { | 112 | if (!req.query || !req.query.q) { |
108 | res.render("sub-search", { user: req.user }); | 113 | res.render("sub-search", { user: req.user }); |
109 | } else { | 114 | } else { |
@@ -141,6 +146,40 @@ router.get("/search", authenticateToken, async (req, res) => { | |||
141 | } | 146 | } |
142 | }); | 147 | }); |
143 | 148 | ||
149 | // GET /post-search | ||
150 | router.get("/post-search", authenticateToken, async (req, res) => { | ||
151 | if (!req.query || !req.query.q) { | ||
152 | res.render("post-search", { user: req.user }); | ||
153 | } else { | ||
154 | const { q, options } = req.query.q.split(/\s+/).reduce( | ||
155 | (acc, word) => { | ||
156 | if (word.startsWith("+")) { | ||
157 | acc.options.push(word.slice(1)); | ||
158 | } else { | ||
159 | acc.q += `${word} `; | ||
160 | } | ||
161 | return acc; | ||
162 | }, | ||
163 | { options: [], q: "" }, | ||
164 | ); | ||
165 | |||
166 | const { items, after } = await G.searchSubmissions(q, { | ||
167 | include_over_18: options.includes("nsfw"), | ||
168 | }); | ||
169 | const message = | ||
170 | items.length === 0 | ||
171 | ? "no results found" | ||
172 | : `showing ${items.length} results`; | ||
173 | res.render("post-search", { | ||
174 | items, | ||
175 | after, | ||
176 | message, | ||
177 | user: req.user, | ||
178 | original_query: req.query.q, | ||
179 | }); | ||
180 | } | ||
181 | }); | ||
182 | |||
144 | // GET /dashboard | 183 | // GET /dashboard |
145 | router.get("/dashboard", authenticateToken, async (req, res) => { | 184 | router.get("/dashboard", authenticateToken, async (req, res) => { |
146 | let invites = null; | 185 | 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 @@ | |||
1 | include ../mixins/post | ||
2 | include ../mixins/header | ||
3 | include ../mixins/head | ||
4 | |||
5 | doctype html | ||
6 | html | ||
7 | +head("search posts") | ||
8 | include ../mixins/subUtils | ||
9 | body | ||
10 | main#content | ||
11 | +header(user) | ||
12 | div.hero | ||
13 | h1 search posts | ||
14 | form(action="/post-search" method="get").search-bar | ||
15 | - var prefill = original_query ?? ""; | ||
16 | input(type="text" name="q" placeholder="search posts" value=prefill required).search-input | ||
17 | button(type="submit").search-button go | ||
18 | if message | ||
19 | div.search-message | ||
20 | i #{message} | ||
21 | if items | ||
22 | each item in items | ||
23 | +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 @@ | |||
1 | include ../mixins/header | ||
2 | include ../mixins/head | ||
3 | |||
4 | doctype html | ||
5 | html | ||
6 | +head("search subreddits") | ||
7 | include ../mixins/subUtils | ||
8 | body | ||
9 | main#content | ||
10 | +header(user) | ||
11 | div.hero | ||
12 | h1 search subreddits | ||
13 | |||
14 | form(action="/sub-search" method="get").search-bar | ||
15 | - var prefill = original_query ?? ""; | ||
16 | input(type="text" name="q" placeholder="search subreddits" value=prefill required).search-input | ||
17 | button(type="submit").search-button go | ||
18 | |||
19 | h1 search posts | ||
20 | |||
21 | form(action="/post-search" method="get").search-bar | ||
22 | - var prefill = original_query ?? ""; | ||
23 | input(type="text" name="q" placeholder="search posts" value=prefill required).search-input | ||
24 | button(type="submit").search-button go | ||
25 | |||
26 | hr | ||
27 | |||
28 | h3 tips | ||
29 | p | ||
30 | | you can narrow search results using filters: | ||
31 | br | ||
32 | - | ||
33 | var triples = [ | ||
34 | ["subreddit", "ohio", "find submissions in 'r/ohio'"], | ||
35 | ["author", "spez", "find submissions by 'u/spez'"], | ||
36 | ["site", "x.com", "find submissions from 'x.com'"], | ||
37 | ["url", "text", "search for 'text' in url"], | ||
38 | ["selftext", "text", "search for 'text' in post contents"], | ||
39 | ["self", "yes/no", "include/exclude selftext posts"], | ||
40 | ["nsfw", "yes/no", "include/exclude over-18 posts"], | ||
41 | ] | ||
42 | |||
43 | ul | ||
44 | each triple in triples | ||
45 | li | ||
46 | strong | ||
47 | | #{triple[0]}: | ||
48 | em #{triple[1]} | ||
49 | | #{triple[2]} | ||
50 | | example: | ||
51 | br | ||
52 | | | ||
53 | code subreddit:iowa site:x.com elections | ||
54 | |||
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 | |||
10 | +header(user) | 10 | +header(user) |
11 | div.hero | 11 | div.hero |
12 | h1 search subreddits | 12 | h1 search subreddits |
13 | form(action="/search" method="get").search-bar | 13 | form(action="/sub-search" method="get").search-bar |
14 | - var prefill = original_query ?? ""; | 14 | - var prefill = original_query ?? ""; |
15 | input(type="text" name="q" placeholder="search subreddits (add +nsfw to include over-18 results)" value=prefill required).search-input | 15 | input(type="text" name="q" placeholder="search subreddits" value=prefill required).search-input |
16 | button(type="submit").search-button go | 16 | button(type="submit").search-button go |
17 | if message | 17 | if message |
18 | div.search-message | 18 | div.search-message |