aboutsummaryrefslogtreecommitdiff
path: root/src/routes/index.js
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-11-05 17:42:41 +0000
committerAkshay <[email protected]>2024-11-05 17:42:41 +0000
commit16139326f2fb5aea23614ac779171e31390b8f72 (patch)
tree851ab235a6cbbab4f27ee6fd92f584501873e030 /src/routes/index.js
parentff789b09c45e0a9dcc4f39cdf5948e3c589a765a (diff)
add biome
Diffstat (limited to 'src/routes/index.js')
-rw-r--r--src/routes/index.js96
1 files changed, 49 insertions, 47 deletions
diff --git a/src/routes/index.js b/src/routes/index.js
index 54a5b18..01c17fa 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -1,79 +1,81 @@
1const express = require('express'); 1const express = require("express");
2const he = require('he'); 2const he = require("he");
3const router = express.Router(); 3const router = express.Router();
4const geddit = require('../geddit.js'); 4const geddit = require("../geddit.js");
5const G = new geddit.Geddit(); 5const G = new geddit.Geddit();
6 6
7
8// GET / 7// GET /
9router.get('/', async (req, res) => { 8router.get("/", async (req, res) => {
10 res.redirect("/r/all") 9 res.redirect("/r/all");
11}); 10});
12 11
13// GET /r/:id 12// GET /r/:id
14router.get('/r/:subreddit', async (req, res) => { 13router.get("/r/:subreddit", async (req, res) => {
15 var subreddit = req.params.subreddit; 14 var subreddit = req.params.subreddit;
16 var query = req.query? req.query : {}; 15 var query = req.query ? req.query : {};
17 if (!query.sort) { 16 if (!query.sort) {
18 query.sort = 'hot'; 17 query.sort = "hot";
19 } 18 }
20 19
21 var postsReq = G.getSubmissions(query.sort, `${subreddit}`, query); 20 var postsReq = G.getSubmissions(query.sort, `${subreddit}`, query);
22 var aboutReq = G.getSubreddit(`${subreddit}`); 21 var aboutReq = G.getSubreddit(`${subreddit}`);
23 22
24 var [posts, about] = await Promise.all([postsReq, aboutReq]); 23 var [posts, about] = await Promise.all([postsReq, aboutReq]);
25 24
26 res.render('index', { subreddit, posts, about, query }); 25 res.render("index", { subreddit, posts, about, query });
27}); 26});
28 27
29// GET /comments/:id 28// GET /comments/:id
30router.get('/comments/:id', async (req, res) => { 29router.get("/comments/:id", async (req, res) => {
31 var id = req.params.id; 30 var id = req.params.id;
32 31
33 response = await G.getSubmissionComments(id); 32 var params = {
33 limit: 50,
34 };
35 response = await G.getSubmissionComments(id, params);
34 36
35 res.render('comments', unescape_submission(response)); 37 res.render("comments", unescape_submission(response));
36}); 38});
37 39
38// GET /subs 40// GET /subs
39router.get('/subs', async (req, res) => { 41router.get("/subs", async (req, res) => {
40 res.render('subs'); 42 res.render("subs");
41}); 43});
42 44
43// GET /media 45// GET /media
44router.get('/media/*', async (req, res) => { 46router.get("/media/*", async (req, res) => {
45 var url = req.params[0]; 47 var url = req.params[0];
46 console.log(`making request to ${url}`); 48 console.log(`making request to ${url}`);
47 return await fetch(url, { 49 return await fetch(url, {
48 headers: { 50 headers: {
49 Accept: "*/*", 51 Accept: "*/*",
50 } 52 },
51 }); 53 });
52}); 54});
53 55
54module.exports = router; 56module.exports = router;
55 57
56function unescape_submission(response) { 58function unescape_submission(response) {
57 var post = response.submission.data; 59 var post = response.submission.data;
58 var comments = response.comments; 60 var comments = response.comments;
59 61
60 if (post.selftext_html) { 62 if (post.selftext_html) {
61 post.selftext_html = he.decode(post.selftext_html); 63 post.selftext_html = he.decode(post.selftext_html);
62 } 64 }
63 comments.forEach(unescape_comment); 65 comments.forEach(unescape_comment);
64 66
65 return { post, comments }; 67 return { post, comments };
66} 68}
67 69
68function unescape_comment(comment) { 70function unescape_comment(comment) {
69 if (comment.data.body_html) { 71 if (comment.data.body_html) {
70 comment.data.body_html = he.decode(comment.data.body_html); 72 comment.data.body_html = he.decode(comment.data.body_html);
71 } 73 }
72 if (comment.data.replies) { 74 if (comment.data.replies) {
73 if(comment.data.replies.data) { 75 if (comment.data.replies.data) {
74 if(comment.data.replies.data.children) { 76 if (comment.data.replies.data.children) {
75 comment.data.replies.data.children.forEach(unescape_comment); 77 comment.data.replies.data.children.forEach(unescape_comment);
76 } 78 }
77 } 79 }
78 } 80 }
79} 81}