diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/routes/index.js | 45 | ||||
-rw-r--r-- | src/views/post-search.pug | 2 | ||||
-rw-r--r-- | src/views/search.pug | 10 | ||||
-rw-r--r-- | src/views/sub-search.pug | 2 |
4 files changed, 25 insertions, 34 deletions
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) => { | |||
112 | if (!req.query || !req.query.q) { | 112 | if (!req.query || !req.query.q) { |
113 | res.render("sub-search", { user: req.user }); | 113 | res.render("sub-search", { user: req.user }); |
114 | } else { | 114 | } else { |
115 | const { q, options } = req.query.q.split(/\s+/).reduce( | 115 | const { q, options } = parseQuery(req.query.q); |
116 | (acc, word) => { | ||
117 | if (word.startsWith("+")) { | ||
118 | acc.options.push(word.slice(1)); | ||
119 | } else { | ||
120 | acc.q += `${word} `; | ||
121 | } | ||
122 | return acc; | ||
123 | }, | ||
124 | { options: [], q: "" }, | ||
125 | ); | ||
126 | |||
127 | const { items, after } = await G.searchSubreddits(q, { | 116 | const { items, after } = await G.searchSubreddits(q, { |
128 | include_over_18: options.includes("nsfw"), | 117 | include_over_18: (options.nsfw ?? "no") === "yes", |
129 | }); | 118 | }); |
130 | const subs = db | 119 | const subs = db |
131 | .query("SELECT subreddit FROM subscriptions WHERE user_id = $id") | 120 | .query("SELECT subreddit FROM subscriptions WHERE user_id = $id") |
@@ -151,20 +140,9 @@ router.get("/post-search", authenticateToken, async (req, res) => { | |||
151 | if (!req.query || !req.query.q) { | 140 | if (!req.query || !req.query.q) { |
152 | res.render("post-search", { user: req.user }); | 141 | res.render("post-search", { user: req.user }); |
153 | } else { | 142 | } else { |
154 | const { q, options } = req.query.q.split(/\s+/).reduce( | 143 | const { q, options } = parseQuery(req.query.q); |
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, { | 144 | const { items, after } = await G.searchSubmissions(q, { |
167 | include_over_18: options.includes("nsfw"), | 145 | include_over_18: (options.nsfw ?? "no") === "yes", |
168 | }); | 146 | }); |
169 | const message = | 147 | const message = |
170 | items.length === 0 | 148 | items.length === 0 |
@@ -180,6 +158,21 @@ router.get("/post-search", authenticateToken, async (req, res) => { | |||
180 | } | 158 | } |
181 | }); | 159 | }); |
182 | 160 | ||
161 | function parseQuery(q) { | ||
162 | return q.split(/\s+/).reduce( | ||
163 | (acc, word) => { | ||
164 | if (word.includes(":")) { | ||
165 | const [key, val] = word.split(":"); | ||
166 | acc.options[key] = val; | ||
167 | } else { | ||
168 | acc.q += `${word} `; | ||
169 | } | ||
170 | return acc; | ||
171 | }, | ||
172 | { options: [], q: "" }, | ||
173 | ); | ||
174 | } | ||
175 | |||
183 | // GET /dashboard | 176 | // GET /dashboard |
184 | router.get("/dashboard", authenticateToken, async (req, res) => { | 177 | router.get("/dashboard", authenticateToken, async (req, res) => { |
185 | let invites = null; | 178 | 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 | |||
13 | h1 search posts | 13 | h1 search posts |
14 | form(action="/post-search" method="get").search-bar | 14 | form(action="/post-search" method="get").search-bar |
15 | - var prefill = original_query ?? ""; | 15 | - var prefill = original_query ?? ""; |
16 | input(type="text" name="q" placeholder="search posts" value=prefill required).search-input | 16 | input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input |
17 | button(type="submit").search-button go | 17 | button(type="submit").search-button go |
18 | if message | 18 | if message |
19 | div.search-message | 19 | 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 | |||
13 | 13 | ||
14 | form(action="/sub-search" method="get").search-bar | 14 | form(action="/sub-search" method="get").search-bar |
15 | - var prefill = original_query ?? ""; | 15 | - var prefill = original_query ?? ""; |
16 | input(type="text" name="q" placeholder="search subreddits" value=prefill required).search-input | 16 | input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input |
17 | button(type="submit").search-button go | 17 | button(type="submit").search-button go |
18 | 18 | ||
19 | hr | ||
20 | |||
19 | h1 search posts | 21 | h1 search posts |
20 | 22 | ||
21 | form(action="/post-search" method="get").search-bar | 23 | form(action="/post-search" method="get").search-bar |
22 | - var prefill = original_query ?? ""; | 24 | - var prefill = original_query ?? ""; |
23 | input(type="text" name="q" placeholder="search posts" value=prefill required).search-input | 25 | input(type="text" name="q" placeholder="type in a search term..." value=prefill required).search-input |
24 | button(type="submit").search-button go | 26 | button(type="submit").search-button go |
25 | |||
26 | hr | ||
27 | |||
28 | h3 tips | ||
29 | p | 27 | p |
30 | | you can narrow search results using filters: | 28 | | you can narrow search results using filters: |
31 | br | 29 | 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 | |||
12 | h1 search subreddits | 12 | h1 search subreddits |
13 | form(action="/sub-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" value=prefill required).search-input | 15 | input(type="text" name="q" placeholder="type in a search term..." 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 |