From f220b37012b37ab5896465f15a96902f398fdbf4 Mon Sep 17 00:00:00 2001 From: teddit Date: Mon, 28 Dec 2020 00:16:45 +0100 Subject: [PATCH] add nsfw warnings for subreddits too --- inc/processJsonSubreddit.js | 1 - inc/processSubredditAbout.js | 73 +++++++++ routes.js | 16 +- static/css/styles.css | 48 +++--- views/subreddit.pug | 292 +++++++++++++++++++---------------- 5 files changed, 261 insertions(+), 169 deletions(-) create mode 100644 inc/processSubredditAbout.js diff --git a/inc/processJsonSubreddit.js b/inc/processJsonSubreddit.js index 4597ded..8c3d0fb 100644 --- a/inc/processJsonSubreddit.js +++ b/inc/processJsonSubreddit.js @@ -28,7 +28,6 @@ module.exports = function() { let is_self_link = false let valid_reddit_self_domains = ['reddit.com'] - if(data.over_18) if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') continue diff --git a/inc/processSubredditAbout.js b/inc/processSubredditAbout.js new file mode 100644 index 0000000..acca18c --- /dev/null +++ b/inc/processSubredditAbout.js @@ -0,0 +1,73 @@ +module.exports = function() { + const config = require('../config') + this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => { + return new Promise(resolve => { + (async () => { + if(subreddit && !subreddit.includes('+')) { + let key = `${subreddit}:sidebar` + redis.get(key, (error, json) => { + if(error) { + console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error) + resolve(null) + } + if(json) { + json = JSON.parse(json) + let obj = { + title: json.data.title, + public_description_html: json.data.public_description_html, + active_user_count: json.data.active_user_count, + subscribers: json.data.subscribers, + created_utc: json.data.created_utc, + over18: json.data.over18, + description_html: json.data.description_html + } + resolve(obj) + } else { + if(subreddit !== 'all') { + fetch(encodeURI(`https://oauth.reddit.com/r/${subreddit}/about`), redditApiGETHeaders()) + .then(result => { + if(result.status === 200) { + result.json() + .then(json => { + redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { + if(error) { + console.error('Error setting the sidebar key to redis.', error) + return res.render('index', { json: null, user_preferences: req.cookies }) + } else { + console.log('Fetched the sidebar from reddit API.'); + (async () => { + let obj = { + title: json.data.title, + public_description_html: json.data.public_description_html, + active_user_count: json.data.active_user_count, + subscribers: json.data.subscribers, + created_utc: json.data.created_utc, + over18: json.data.over18, + description_html: json.data.description_html + } + resolve(obj) + })() + } + }) + }) + } else { + console.error(`Something went wrong while fetching data from reddit API. ${result.status} – ${result.statusText}`) + console.error(config.reddit_api_error_text) + resolve(null) + } + }).catch(error => { + console.error('Error fetching the sidebar.', error) + resolve(null) + }) + } else { + resolve(null) + } + } + }) + } else { + resolve(null) + } + })() + }) + } +} diff --git a/routes.js b/routes.js index 9d50095..172039b 100644 --- a/routes.js +++ b/routes.js @@ -7,7 +7,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { let processPost = require('./inc/processJsonPost.js')(); let processUser = require('./inc/processJsonUser.js')(); let processSearches = require('./inc/processSearchResults.js')(); - let processSidebar = require('./inc/processSubredditSidebar.js')(); + let processAbout = require('./inc/processSubredditAbout.js')(); let tedditApiSubreddit = require('./inc/teddit_api/handleSubreddit.js')(); app.get('/about', (req, res, next) => { @@ -384,16 +384,17 @@ module.exports = (app, redis, fetch, RedditAPI) => { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) - let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI) + let subreddit_about = await processSubredditAbout(subreddit, redis, fetch, RedditAPI) if(!processed_json.error) { return res.render('subreddit', { json: processed_json, subreddit: subreddit, - sidebar_data: sidebar_data, + subreddit_about: subreddit_about, subreddit_front: (!before && !after) ? true : false, sortby: sortby, past: past, - user_preferences: req.cookies + user_preferences: req.cookies, + instance_nsfw_enabled: config.nsfw_enabled }) } else { return res.render('subreddit', { @@ -422,15 +423,16 @@ module.exports = (app, redis, fetch, RedditAPI) => { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, subreddit) } else { let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) - let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI) + let subreddit_about = await processSubredditAbout(subreddit, redis, fetch, RedditAPI) return res.render('subreddit', { json: processed_json, subreddit: subreddit, - sidebar_data: sidebar_data, + subreddit_about: subreddit_about, subreddit_front: (!before && !after) ? true : false, sortby: sortby, past: past, - user_preferences: req.cookies + user_preferences: req.cookies, + instance_nsfw_enabled: config.nsfw_enabled }) } })() diff --git a/static/css/styles.css b/static/css/styles.css index c1dcfa2..11ff476 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -402,6 +402,30 @@ header .tabmenu li.active a { border-color: #d10023; color: #d10023; } +.nsfw-warning { + text-align: center; + float: left; + width: 100%; + margin: 40px 0px; +} +.nsfw-warning span { + font-size: 3rem; + background: #ff575b; + border-radius: 130px; + display: inline-block; + padding: 39px 20px 39px 20px; + color: white; +} +.nsfw-warning h2 { + margin: 20px 0px; +} +.nsfw-warning a { + margin: 20px; + display: inline-block; + background: #4f86b5; + color: white; + padding: 16px; +} input[type="submit"], .btn { padding: 3px; @@ -852,30 +876,6 @@ input[type="submit"]:hover, #post .source-url { overflow-wrap: anywhere; } -#post .nsfw-warning { - text-align: center; - float: left; - width: 100%; - margin: 40px 0px; -} -#post .nsfw-warning span { - font-size: 3rem; - background: #ff575b; - border-radius: 130px; - display: inline-block; - padding: 39px 20px 39px 20px; - color: white; -} -#post .nsfw-warning h2 { - margin: 20px 0px; -} -#post .nsfw-warning a { - margin: 20px; - display: inline-block; - background: #4f86b5; - color: white; - padding: 16px; -} /* USER */ #user .entries { float: left; diff --git a/views/subreddit.pug b/views/subreddit.pug index aaf9905..12d68f1 100644 --- a/views/subreddit.pug +++ b/views/subreddit.pug @@ -5,6 +5,15 @@ html include includes/head.pug body(class=""+ user_preferences.theme +"") include includes/topbar.pug + - + let show_nsfw_warning = false; + if(subreddit_about) { + if(subreddit_about.over18) { + if((instance_nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') { + show_nsfw_warning = true; + } + } + } if json === null h1 Error occured if error @@ -12,142 +21,151 @@ html h2 This is a private subreddit. p Error: #{JSON.stringify(json.error_data)} else - header - a(href="/", class="main") - h1 teddit - .bottom - if !subreddit.includes('+') - a(href="/r/" + subreddit + "", class="subreddit") - h2 #{subreddit} - ul.tabmenu - li(class=!sortby || sortby == 'hot' ? 'active' : '') - a(href="/r/" + subreddit) hot - li(class=sortby === 'new' ? 'active' : '') - a(href="/r/" + subreddit + "/new") new - li(class=sortby === 'rising' ? 'active' : '') - a(href="/r/" + subreddit + "/rising") rising - li(class=sortby === 'controversial' ? 'active' : '') - a(href="/r/" + subreddit + "/controversial") controversial - li(class=sortby === 'top' ? 'active' : '') - a(href="/r/" + subreddit + "/top") top - #links.sr - if sortby === 'top' || sortby === 'controversial' - details - summary - if past === 'hour' - span links from: past hour - if past === 'day' - span links from: past 24 hours - if past === 'week' - span links from: past week - if past === 'month' - span links from: past month - if past === 'year' - span links from: past year - if past === 'all' - span links from: all time - ul - li(class=past === 'hour' ? 'active' : '') - a(href="?t=hour") past hour - li(class=past === 'day' ? 'active' : '') - a(href="?t=day") past 24 hours - li(class=past === 'week' ? 'active' : '') - a(href="?t=week") past week - li(class=past === 'month' ? 'active' : '') - a(href="?t=month") past month - li(class=past === 'year' ? 'active' : '') - a(href="?t=year") past year - li(class=past === 'all' ? 'active' : '') - a(href="?t=all") all time - if json.links.length === 0 - p nothing here - else - each link in json.links - .link - .upvotes - .arrow - span #{kFormatter(link.ups)} - .arrow.down - .image - if link.images - if link.is_self_link - a(href="" + link.permalink + "") - img(src="" + link.images.thumb + "", alt="") - else - a(href=""+ link.url +"") - img(src="" + link.images.thumb + "", alt="") - else - a(href="" + link.permalink + "") - .no-image - .entry - .title - if link.is_self_link - a(href="" + link.permalink + "") - h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - != link.link_flair - span (#{link.domain}) - else - a(href="" + link.url + "") - h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - != link.link_flair - span (#{link.domain}) - .meta - p.submitted submitted - span(title="" + toUTCString(link.created) + "") #{timeDifference(link.created)} by - if link.author === '[deleted]' - span(class="deleted") [deleted] + if show_nsfw_warning === true + .nsfw-warning + span 18+ + h2 You must be 18+ to view this community + p You must be at least eighteen years old to view this content. Are you over eighteen and willing to see adult content? + a(href="/") No thank you + a(href="?nsfw_enabled=true") Continue + p If you continue, nsfw_enabled cookie preference will be automatically set to true. + else + header + a(href="/", class="main") + h1 teddit + .bottom + if !subreddit.includes('+') + a(href="/r/" + subreddit + "", class="subreddit") + h2 #{subreddit} + ul.tabmenu + li(class=!sortby || sortby == 'hot' ? 'active' : '') + a(href="/r/" + subreddit) hot + li(class=sortby === 'new' ? 'active' : '') + a(href="/r/" + subreddit + "/new") new + li(class=sortby === 'rising' ? 'active' : '') + a(href="/r/" + subreddit + "/rising") rising + li(class=sortby === 'controversial' ? 'active' : '') + a(href="/r/" + subreddit + "/controversial") controversial + li(class=sortby === 'top' ? 'active' : '') + a(href="/r/" + subreddit + "/top") top + #links.sr + if sortby === 'top' || sortby === 'controversial' + details + summary + if past === 'hour' + span links from: past hour + if past === 'day' + span links from: past 24 hours + if past === 'week' + span links from: past week + if past === 'month' + span links from: past month + if past === 'year' + span links from: past year + if past === 'all' + span links from: all time + ul + li(class=past === 'hour' ? 'active' : '') + a(href="?t=hour") past hour + li(class=past === 'day' ? 'active' : '') + a(href="?t=day") past 24 hours + li(class=past === 'week' ? 'active' : '') + a(href="?t=week") past week + li(class=past === 'month' ? 'active' : '') + a(href="?t=month") past month + li(class=past === 'year' ? 'active' : '') + a(href="?t=year") past year + li(class=past === 'all' ? 'active' : '') + a(href="?t=all") all time + if json.links.length === 0 + p nothing here + else + each link in json.links + .link + .upvotes + .arrow + span #{kFormatter(link.ups)} + .arrow.down + .image + if link.images + if link.is_self_link + a(href="" + link.permalink + "") + img(src="" + link.images.thumb + "", alt="") else - a(href="/u/" + link.author + "") - | #{link.author} - != link.user_flair - p.to to - a(href="/r/" + link.subreddit + "") - | #{link.subreddit} - if link.stickied - span(class="green") stickied - .links - if link.over_18 - span.tag.nsfw NSFW - a(href="" + link.permalink + "", class="comments") - | #{link.num_comments} comments - if json.info.before || json.info.after - .view-more-inks - if json.info.before && !subreddit_front - a(href="/r/" + subreddit + "/" + sortby + "?t="+ (past ? past : '') +"&before=" + json.info.before + "") ‹ prev - if json.info.after - a(href="/r/" + subreddit + "/" + sortby + "?t=" + (past ? past : '') + "&after=" + json.info.after + "") next › - #sidebar - #search.sr - p search - form(action="/r/" + subreddit + "/search", method="GET") - input(type="text", name="q", id="q", placeholder="search") - div - label(for="restrict_sr") limit my search to r/#{subreddit} - input(type="checkbox", name="restrict_sr", id="restrict_sr", checked="checked") - div - label(for="nsfw") include NSFW results - input(type="checkbox", name="nsfw", id="nsfw", checked="checked") - input(type="submit", value="search") - if sidebar_data - if sidebar_data.subscribers - .content - p subscribers: #{sidebar_data.subscribers.toLocaleString()} - p users here right now: #{sidebar_data.active_user_count.toLocaleString()} - br - .heading - p.title #{sidebar_data.title} - .short-description - != unescape(sidebar_data.public_description_html) - .description - != unescape(sidebar_data.description_html) - else - if subreddit.includes('+') - .content - p These subreddits - - - let subreddits = subreddit.split('+') - ul(class="subreddit-listing") - each subreddit in subreddits - li - a(href="/r/" + subreddit + "") #{subreddit} + a(href=""+ link.url +"") + img(src="" + link.images.thumb + "", alt="") + else + a(href="" + link.permalink + "") + .no-image + .entry + .title + if link.is_self_link + a(href="" + link.permalink + "") + h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} + != link.link_flair + span (#{link.domain}) + else + a(href="" + link.url + "") + h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} + != link.link_flair + span (#{link.domain}) + .meta + p.submitted submitted + span(title="" + toUTCString(link.created) + "") #{timeDifference(link.created)} by + if link.author === '[deleted]' + span(class="deleted") [deleted] + else + a(href="/u/" + link.author + "") + | #{link.author} + != link.user_flair + p.to to + a(href="/r/" + link.subreddit + "") + | #{link.subreddit} + if link.stickied + span(class="green") stickied + .links + if link.over_18 + span.tag.nsfw NSFW + a(href="" + link.permalink + "", class="comments") + | #{link.num_comments} comments + if json.info.before || json.info.after + .view-more-inks + if json.info.before && !subreddit_front + a(href="/r/" + subreddit + "/" + sortby + "?t="+ (past ? past : '') +"&before=" + json.info.before + "") ‹ prev + if json.info.after + a(href="/r/" + subreddit + "/" + sortby + "?t=" + (past ? past : '') + "&after=" + json.info.after + "") next › + #sidebar + #search.sr + p search + form(action="/r/" + subreddit + "/search", method="GET") + input(type="text", name="q", id="q", placeholder="search") + div + label(for="restrict_sr") limit my search to r/#{subreddit} + input(type="checkbox", name="restrict_sr", id="restrict_sr", checked="checked") + div + label(for="nsfw") include NSFW results + input(type="checkbox", name="nsfw", id="nsfw", checked="checked") + input(type="submit", value="search") + if subreddit_about + if subreddit_about.subscribers + .content + p subscribers: #{subreddit_about.subscribers.toLocaleString()} + p users here right now: #{subreddit_about.active_user_count.toLocaleString()} + br + .heading + p.title #{subreddit_about.title} + .short-description + != unescape(subreddit_about.public_description_html) + .description + != unescape(subreddit_about.description_html) + else + if subreddit.includes('+') + .content + p These subreddits + - + let subreddits = subreddit.split('+') + ul(class="subreddit-listing") + each subreddit in subreddits + li + a(href="/r/" + subreddit + "") #{subreddit}