aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-12-21 15:23:23 +0000
committerAkshay <[email protected]>2024-12-21 15:23:23 +0000
commit9b09e3141ef725e3887c5dd79101a6349bb171dd (patch)
tree240800596ea064951d3fc0dd9bd3104a158792eb
parentf8bef76385f8af54b2f3de24879c1fe04d277c11 (diff)
fix search with args
-rw-r--r--src/geddit.js7
-rw-r--r--src/routes/index.js25
2 files changed, 3 insertions, 29 deletions
diff --git a/src/geddit.js b/src/geddit.js
index d81cedf..87a000b 100644
--- a/src/geddit.js
+++ b/src/geddit.js
@@ -241,13 +241,8 @@ class Geddit {
241 options.q = query; 241 options.q = query;
242 options.type = "link"; 242 options.type = "link";
243 243
244 const params = {
245 limit: 25,
246 include_over_18: true,
247 };
248
249 return await fetch( 244 return await fetch(
250 `${this.host}/search.json?${new URLSearchParams(Object.assign(params, options))}`, 245 `${this.host}/search.json?${new URLSearchParams(options)}`,
251 ) 246 )
252 .then((res) => res.json()) 247 .then((res) => res.json())
253 .then((json) => json.data) 248 .then((json) => json.data)
diff --git a/src/routes/index.js b/src/routes/index.js
index 34a5e52..a743c7f 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -117,10 +117,7 @@ router.get("/sub-search", authenticateToken, async (req, res) => {
117 if (!req.query || !req.query.q) { 117 if (!req.query || !req.query.q) {
118 res.render("sub-search", { user: req.user }); 118 res.render("sub-search", { user: req.user });
119 } else { 119 } else {
120 const { q, options } = parseQuery(req.query.q); 120 const { items, after } = await G.searchSubreddits(q);
121 const { items, after } = await G.searchSubreddits(q, {
122 include_over_18: (options.nsfw ?? "no") === "yes",
123 });
124 const subs = db 121 const subs = db
125 .query("SELECT subreddit FROM subscriptions WHERE user_id = $id") 122 .query("SELECT subreddit FROM subscriptions WHERE user_id = $id")
126 .all({ id: req.user.id }) 123 .all({ id: req.user.id })
@@ -145,10 +142,7 @@ router.get("/post-search", authenticateToken, async (req, res) => {
145 if (!req.query || !req.query.q) { 142 if (!req.query || !req.query.q) {
146 res.render("post-search", { user: req.user }); 143 res.render("post-search", { user: req.user });
147 } else { 144 } else {
148 const { q, options } = parseQuery(req.query.q); 145 const { items, after } = await G.searchSubmissions(req.query.q);
149 const { items, after } = await G.searchSubmissions(q, {
150 include_over_18: (options.nsfw ?? "no") === "yes",
151 });
152 const message = 146 const message =
153 items.length === 0 147 items.length === 0
154 ? "no results found" 148 ? "no results found"
@@ -164,21 +158,6 @@ router.get("/post-search", authenticateToken, async (req, res) => {
164 } 158 }
165}); 159});
166 160
167function parseQuery(q) {
168 return q.split(/\s+/).reduce(
169 (acc, word) => {
170 if (word.includes(":")) {
171 const [key, val] = word.split(":");
172 acc.options[key] = val;
173 } else {
174 acc.q += `${word} `;
175 }
176 return acc;
177 },
178 { options: [], q: "" },
179 );
180}
181
182// GET /dashboard 161// GET /dashboard
183router.get("/dashboard", authenticateToken, async (req, res) => { 162router.get("/dashboard", authenticateToken, async (req, res) => {
184 let invites = null; 163 let invites = null;