From bd4bc75298bfedc38e1484092fb0e682bcfe3ea9 Mon Sep 17 00:00:00 2001 From: teddit Date: Tue, 16 Mar 2021 18:07:27 +0100 Subject: [PATCH] add a feature to the preferences where users can choose to collapse child comments automatically --- app.js | 6 +++++ inc/compilePostComments.js | 11 ++++++--- inc/processJsonPost.js | 4 +-- routes.js | 50 ++++++++++++++++++++++---------------- views/preferences.pug | 6 +++++ 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/app.js b/app.js index 004465f..bfdd898 100644 --- a/app.js +++ b/app.js @@ -131,6 +131,12 @@ const preferencesMiddleware = (req, res, next) => { } } + let collapseChildComments = req.query.collapse_child_comments + if(collapseChildComments) { + req.cookies.collapse_child_comments = collapseChildComments + res.cookie('collapse_child_comments', collapseChildComments, { maxAge: 31536000, httpOnly: true }) + } + next() } diff --git a/inc/compilePostComments.js b/inc/compilePostComments.js index 1f0da40..2f21e79 100644 --- a/inc/compilePostComments.js +++ b/inc/compilePostComments.js @@ -1,5 +1,5 @@ module.exports = function() { - this.compilePostCommentsHtml = (comments, next_comment, post_id, post_url, morechildren_ids, post_author, viewing_comment) => { + this.compilePostCommentsHtml = (comments, next_comment, post_id, post_url, morechildren_ids, post_author, viewing_comment, user_preferences) => { return new Promise((resolve, reject) => { (async () => { let comments_html @@ -10,6 +10,9 @@ module.exports = function() { else return `${comment.author}${submitter || ''}${moderator || ''}` } + + if(!user_preferences) + user_preferences = {} if(comments.author !== undefined && comments.body_html !== undefined) { let classlist = [] @@ -42,7 +45,7 @@ module.exports = function() { } comments_html = `
-
+
0 ? '' : 'open'}>

${commentAuthor(comments, classlist, submitter && submitter_link, moderator && moderator_badge)}

${ups}

@@ -138,7 +141,7 @@ module.exports = function() { } comments_html += `
-
+

${commentAuthor(comment, classlist, submitter && submitter_link, moderator && moderator_badge)}

${ups}

@@ -164,7 +167,7 @@ module.exports = function() { if(comment.replies[j+1]) { next_reply = comment.replies[j+1] } - replies_html += await compilePostCommentsHtml(comment.replies[j], next_reply, post_id, post_url, null, post_author, viewing_comment) + replies_html += await compilePostCommentsHtml(comment.replies[j], next_reply, post_id, post_url, null, post_author, viewing_comment, user_preferences) } } } diff --git a/inc/processJsonPost.js b/inc/processJsonPost.js index 9906181..0909246 100644 --- a/inc/processJsonPost.js +++ b/inc/processJsonPost.js @@ -198,7 +198,7 @@ module.exports = function(fetch) { }) } - this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment) => { + this.finalizeJsonPost = async (processed_json, post_id, post_url, morechildren_ids, viewing_comment, user_preferences) => { let comments_html = `
` let comments = processed_json.comments for(var i = 0; i < comments.length; i++) { @@ -206,7 +206,7 @@ module.exports = function(fetch) { if(comments[i+1]) { next_comment = comments[i+1] } - comments_html += await compilePostCommentsHtml(comments[i], next_comment, post_id, post_url, morechildren_ids, processed_json.author, viewing_comment) + comments_html += await compilePostCommentsHtml(comments[i], next_comment, post_id, post_url, morechildren_ids, processed_json.author, viewing_comment, user_preferences) } comments_html += `
` diff --git a/routes.js b/routes.js index 43f2ffe..92dce1a 100644 --- a/routes.js +++ b/routes.js @@ -133,7 +133,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } if(json) { json = JSON.parse(json) - if(fetched === 'true' || redis_key.includes('/comments/')) + if(fetched === 'true' || redis_key.includes('/comments/')) json = json[0] let post_to_save = false @@ -235,7 +235,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { return res.redirect(back) } }) - } else { + } else { return res.redirect(`/saved`) } } else { @@ -264,7 +264,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { d = `&before=${before}` } - if(nsfw !== 'on') { + if(nsfw !== 'on') { nsfw = 'off' } @@ -361,7 +361,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Subreddits not found') + console.log('404 – Subreddits not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -486,11 +486,11 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(!before) { before = '' } - if(restrict_sr !== 'on') { + if(restrict_sr !== 'on') { restrict_sr = 'off' } - if(nsfw !== 'on') { + if(nsfw !== 'on') { nsfw = 'off' } let d = `&after=${after}` @@ -561,7 +561,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(json) { console.log('Got frontpage key from redis.'); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, '/') } else { let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) @@ -599,7 +599,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } else { console.log('Fetched the frontpage from Reddit.'); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, '/') } else { let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) @@ -736,11 +736,11 @@ module.exports = (app, redis, fetch, RedditAPI) => { d = `&before=${before}` } - if(restrict_sr !== 'on') { + if(restrict_sr !== 'on') { restrict_sr = 'off' } - if(nsfw !== 'on') { + if(nsfw !== 'on') { nsfw = 'off' } @@ -864,7 +864,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Subreddit wiki not found') + console.log('404 – Subreddit wiki not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -922,7 +922,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Subreddit not found') + console.log('404 – Subreddit not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -991,7 +991,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(json) { console.log(`Got /r/${subreddit} key from redis.`); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) @@ -1038,7 +1038,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } else { console.log(`Fetched the JSON from reddit.com/r/${subreddit}.`); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) @@ -1063,7 +1063,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Subreddit not found') + console.log('404 – Subreddit not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -1119,7 +1119,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { (async () => { if(!more_comments_cursor) { let processed_json = await processJsonPost(json, false, req.cookies) - let finalized_json = await finalizeJsonPost(processed_json, id, post_url, null, viewing_comment) + let finalized_json = await finalizeJsonPost(processed_json, id, post_url, null, viewing_comment, req.cookies) return res.render('post', { post: finalized_json.post_data, comments: finalized_json.comments, @@ -1215,7 +1215,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Post not found') + console.log('404 – Post not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -1483,7 +1483,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(json) { console.log(`Got /u/${user} custom_feed key from redis.`); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) @@ -1529,7 +1529,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } else { console.log(`Fetched the JSON from reddit.com/r/${subreddit}.`); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) @@ -1553,7 +1553,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – Subreddit not found') + console.log('404 – Subreddit not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) @@ -1581,6 +1581,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { let nsfw_enabled = req.body.nsfw_enabled let highlight_controversial = req.body.highlight_controversial let post_media_max_height = req.body.post_media_max_height + let collapse_child_comments = req.body.collapse_child_comments res.cookie('theme', theme, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) @@ -1604,7 +1605,13 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(config.post_media_max_heights.hasOwnProperty(post_media_max_height) || !isNaN(post_media_max_height)) res.cookie('post_media_max_height', post_media_max_height, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) - + + if(collapse_child_comments === 'on') + collapse_child_comments = 'true' + else + collapse_child_comments = 'false' + res.cookie('collapse_child_comments', collapse_child_comments, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) + return res.redirect('/preferences') }) @@ -1708,3 +1715,4 @@ module.exports = (app, redis, fetch, RedditAPI) => { } }) } + diff --git a/views/preferences.pug b/views/preferences.pug index 5431d92..65f348c 100644 --- a/views/preferences.pug +++ b/views/preferences.pug @@ -65,6 +65,12 @@ html max_heights_html += `` } != max_heights_html + .setting + label(for="collapse_child_comments") Collapse child comments automatically: + if(user_preferences.collapse_child_comments == 'true') + input(type="checkbox", name="collapse_child_comments", id="collapse_child_comments", checked="checked") + else + input(type="checkbox", name="collapse_child_comments", id="collapse_child_comments") legend Subscribed subreddits .setting details