aboutsummaryrefslogtreecommitdiff
path: root/src/routes/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/index.js')
-rw-r--r--src/routes/index.js43
1 files changed, 35 insertions, 8 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index 117b728..9415607 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -16,11 +16,14 @@ router.get("/", authenticateToken, async (req, res) => {
16 const subs = db 16 const subs = db
17 .query("SELECT * FROM subscriptions WHERE user_id = $id") 17 .query("SELECT * FROM subscriptions WHERE user_id = $id")
18 .all({ id: req.user.id }); 18 .all({ id: req.user.id });
19
20 const qs = req.query ? ('?' + new URLSearchParams(req.query).toString()) : '';
21
19 if (subs.length === 0) { 22 if (subs.length === 0) {
20 res.redirect("/r/all"); 23 res.redirect(`/r/all${qs}`);
21 } else { 24 } else {
22 const p = subs.map((s) => s.subreddit).join("+"); 25 const p = subs.map((s) => s.subreddit).join("+");
23 res.redirect(`/r/${p}`); 26 res.redirect(`/r/${p}${qs}`);
24 } 27 }
25}); 28});
26 29
@@ -32,6 +35,9 @@ router.get("/r/:subreddit", authenticateToken, async (req, res) => {
32 if (!query.sort) { 35 if (!query.sort) {
33 query.sort = "hot"; 36 query.sort = "hot";
34 } 37 }
38 if (!query.view) {
39 query.view = "compact";
40 }
35 41
36 let isSubbed = false; 42 let isSubbed = false;
37 if (!isMulti) { 43 if (!isMulti) {
@@ -47,6 +53,10 @@ router.get("/r/:subreddit", authenticateToken, async (req, res) => {
47 53
48 const [posts, about] = await Promise.all([postsReq, aboutReq]); 54 const [posts, about] = await Promise.all([postsReq, aboutReq]);
49 55
56 if (query.view == 'card' && posts && posts.posts) {
57 posts.posts.forEach(unescape_selftext);
58 }
59
50 res.render("index", { 60 res.render("index", {
51 subreddit, 61 subreddit,
52 posts, 62 posts,
@@ -71,6 +81,7 @@ router.get("/comments/:id", authenticateToken, async (req, res) => {
71 data: unescape_submission(response), 81 data: unescape_submission(response),
72 user: req.user, 82 user: req.user,
73 from: req.query.from, 83 from: req.query.from,
84 query: req.query,
74 }); 85 });
75}); 86});
76 87
@@ -104,12 +115,12 @@ router.get("/subs", authenticateToken, async (req, res) => {
104 ) 115 )
105 .all({ id: req.user.id }); 116 .all({ id: req.user.id });
106 117
107 res.render("subs", { subs, user: req.user }); 118 res.render("subs", { subs, user: req.user, query: req.query });
108}); 119});
109 120
110// GET /search 121// GET /search
111router.get("/search", authenticateToken, async (req, res) => { 122router.get("/search", authenticateToken, async (req, res) => {
112 res.render("search", { user: req.user }); 123 res.render("search", { user: req.user, query: req.query });
113}); 124});
114 125
115// GET /sub-search 126// GET /sub-search
@@ -133,6 +144,7 @@ router.get("/sub-search", authenticateToken, async (req, res) => {
133 message, 144 message,
134 user: req.user, 145 user: req.user,
135 original_query: req.query.q, 146 original_query: req.query.q,
147 query: req.query,
136 }); 148 });
137 } 149 }
138}); 150});
@@ -147,6 +159,11 @@ router.get("/post-search", authenticateToken, async (req, res) => {
147 items.length === 0 159 items.length === 0
148 ? "no results found" 160 ? "no results found"
149 : `showing ${items.length} results`; 161 : `showing ${items.length} results`;
162
163 if (req.query.view == 'card' && items) {
164 items.forEach(unescape_selftext);
165 }
166
150 res.render("post-search", { 167 res.render("post-search", {
151 items, 168 items,
152 after, 169 after,
@@ -154,6 +171,7 @@ router.get("/post-search", authenticateToken, async (req, res) => {
154 user: req.user, 171 user: req.user,
155 original_query: req.query.q, 172 original_query: req.query.q,
156 currentUrl: req.url, 173 currentUrl: req.url,
174 query: req.query,
157 }); 175 });
158 } 176 }
159}); 177});
@@ -176,7 +194,7 @@ router.get("/dashboard", authenticateToken, async (req, res) => {
176 usedAt: Date.parse(inv.usedAt), 194 usedAt: Date.parse(inv.usedAt),
177 })); 195 }));
178 } 196 }
179 res.render("dashboard", { invites, isAdmin, user: req.user }); 197 res.render("dashboard", { invites, isAdmin, user: req.user, query: req.query });
180}); 198});
181 199
182router.get("/create-invite", authenticateAdmin, async (req, res) => { 200router.get("/create-invite", authenticateAdmin, async (req, res) => {
@@ -359,14 +377,23 @@ function unescape_submission(response) {
359 const post = response.submission.data; 377 const post = response.submission.data;
360 const comments = response.comments; 378 const comments = response.comments;
361 379
362 if (post.selftext_html) { 380 unescape_selftext(post);
363 post.selftext_html = he.decode(post.selftext_html);
364 }
365 comments.forEach(unescape_comment); 381 comments.forEach(unescape_comment);
366 382
367 return { post, comments }; 383 return { post, comments };
368} 384}
369 385
386function unescape_selftext(post) {
387 // If called after getSubmissions
388 if (post.data && post.data.selftext_html) {
389 post.data.selftext_html = he.decode(post.data.selftext_html);
390 }
391 // If called after getSubmissionComments
392 if (post.selftext_html) {
393 post.selftext_html = he.decode(post.selftext_html);
394 }
395}
396
370function unescape_comment(comment) { 397function unescape_comment(comment) {
371 if (comment.data.body_html) { 398 if (comment.data.body_html) {
372 comment.data.body_html = he.decode(comment.data.body_html); 399 comment.data.body_html = he.decode(comment.data.body_html);