add nsfw warnings for subreddits too

This commit is contained in:
teddit 2020-12-28 00:16:45 +01:00
parent 5f8e8eb7a3
commit f220b37012
5 changed files with 261 additions and 169 deletions

View File

@ -28,7 +28,6 @@ module.exports = function() {
let is_self_link = false let is_self_link = false
let valid_reddit_self_domains = ['reddit.com'] let valid_reddit_self_domains = ['reddit.com']
if(data.over_18) if(data.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue continue

View File

@ -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)
}
})()
})
}
}

View File

@ -7,7 +7,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
let processPost = require('./inc/processJsonPost.js')(); let processPost = require('./inc/processJsonPost.js')();
let processUser = require('./inc/processJsonUser.js')(); let processUser = require('./inc/processJsonUser.js')();
let processSearches = require('./inc/processSearchResults.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')(); let tedditApiSubreddit = require('./inc/teddit_api/handleSubreddit.js')();
app.get('/about', (req, res, next) => { 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) return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, subreddit)
} else { } else {
let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) 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) { if(!processed_json.error) {
return res.render('subreddit', { return res.render('subreddit', {
json: processed_json, json: processed_json,
subreddit: subreddit, subreddit: subreddit,
sidebar_data: sidebar_data, subreddit_about: subreddit_about,
subreddit_front: (!before && !after) ? true : false, subreddit_front: (!before && !after) ? true : false,
sortby: sortby, sortby: sortby,
past: past, past: past,
user_preferences: req.cookies user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
}) })
} else { } else {
return res.render('subreddit', { 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) return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, subreddit)
} else { } else {
let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) 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', { return res.render('subreddit', {
json: processed_json, json: processed_json,
subreddit: subreddit, subreddit: subreddit,
sidebar_data: sidebar_data, subreddit_about: subreddit_about,
subreddit_front: (!before && !after) ? true : false, subreddit_front: (!before && !after) ? true : false,
sortby: sortby, sortby: sortby,
past: past, past: past,
user_preferences: req.cookies user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
}) })
} }
})() })()

View File

@ -402,6 +402,30 @@ header .tabmenu li.active a {
border-color: #d10023; border-color: #d10023;
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"], input[type="submit"],
.btn { .btn {
padding: 3px; padding: 3px;
@ -852,30 +876,6 @@ input[type="submit"]:hover,
#post .source-url { #post .source-url {
overflow-wrap: anywhere; 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 */
#user .entries { #user .entries {
float: left; float: left;

View File

@ -5,6 +5,15 @@ html
include includes/head.pug include includes/head.pug
body(class=""+ user_preferences.theme +"") body(class=""+ user_preferences.theme +"")
include includes/topbar.pug 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 if json === null
h1 Error occured h1 Error occured
if error if error
@ -12,142 +21,151 @@ html
h2 This is a private subreddit. h2 This is a private subreddit.
p Error: #{JSON.stringify(json.error_data)} p Error: #{JSON.stringify(json.error_data)}
else else
header if show_nsfw_warning === true
a(href="/", class="main") .nsfw-warning
h1 teddit span 18+
.bottom h2 You must be 18+ to view this community
if !subreddit.includes('+') 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="/r/" + subreddit + "", class="subreddit") a(href="/") No thank you
h2 #{subreddit} a(href="?nsfw_enabled=true") Continue
ul.tabmenu p If you continue, <code>nsfw_enabled</code> cookie preference will be automatically set to <code>true</code>.
li(class=!sortby || sortby == 'hot' ? 'active' : '') else
a(href="/r/" + subreddit) hot header
li(class=sortby === 'new' ? 'active' : '') a(href="/", class="main")
a(href="/r/" + subreddit + "/new") new h1 teddit
li(class=sortby === 'rising' ? 'active' : '') .bottom
a(href="/r/" + subreddit + "/rising") rising if !subreddit.includes('+')
li(class=sortby === 'controversial' ? 'active' : '') a(href="/r/" + subreddit + "", class="subreddit")
a(href="/r/" + subreddit + "/controversial") controversial h2 #{subreddit}
li(class=sortby === 'top' ? 'active' : '') ul.tabmenu
a(href="/r/" + subreddit + "/top") top li(class=!sortby || sortby == 'hot' ? 'active' : '')
#links.sr a(href="/r/" + subreddit) hot
if sortby === 'top' || sortby === 'controversial' li(class=sortby === 'new' ? 'active' : '')
details a(href="/r/" + subreddit + "/new") new
summary li(class=sortby === 'rising' ? 'active' : '')
if past === 'hour' a(href="/r/" + subreddit + "/rising") rising
span links from: past hour li(class=sortby === 'controversial' ? 'active' : '')
if past === 'day' a(href="/r/" + subreddit + "/controversial") controversial
span links from: past 24 hours li(class=sortby === 'top' ? 'active' : '')
if past === 'week' a(href="/r/" + subreddit + "/top") top
span links from: past week #links.sr
if past === 'month' if sortby === 'top' || sortby === 'controversial'
span links from: past month details
if past === 'year' summary
span links from: past year if past === 'hour'
if past === 'all' span links from: past hour
span links from: all time if past === 'day'
ul span links from: past 24 hours
li(class=past === 'hour' ? 'active' : '') if past === 'week'
a(href="?t=hour") past hour span links from: past week
li(class=past === 'day' ? 'active' : '') if past === 'month'
a(href="?t=day") past 24 hours span links from: past month
li(class=past === 'week' ? 'active' : '') if past === 'year'
a(href="?t=week") past week span links from: past year
li(class=past === 'month' ? 'active' : '') if past === 'all'
a(href="?t=month") past month span links from: all time
li(class=past === 'year' ? 'active' : '') ul
a(href="?t=year") past year li(class=past === 'hour' ? 'active' : '')
li(class=past === 'all' ? 'active' : '') a(href="?t=hour") past hour
a(href="?t=all") all time li(class=past === 'day' ? 'active' : '')
if json.links.length === 0 a(href="?t=day") past 24 hours
p nothing here li(class=past === 'week' ? 'active' : '')
else a(href="?t=week") past week
each link in json.links li(class=past === 'month' ? 'active' : '')
.link a(href="?t=month") past month
.upvotes li(class=past === 'year' ? 'active' : '')
.arrow a(href="?t=year") past year
span #{kFormatter(link.ups)} li(class=past === 'all' ? 'active' : '')
.arrow.down a(href="?t=all") all time
.image if json.links.length === 0
if link.images p nothing here
if link.is_self_link else
a(href="" + link.permalink + "") each link in json.links
img(src="" + link.images.thumb + "", alt="") .link
else .upvotes
a(href=""+ link.url +"") .arrow
img(src="" + link.images.thumb + "", alt="") span #{kFormatter(link.ups)}
else .arrow.down
a(href="" + link.permalink + "") .image
.no-image if link.images
.entry if link.is_self_link
.title a(href="" + link.permalink + "")
if link.is_self_link img(src="" + link.images.thumb + "", alt="")
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 else
a(href="/u/" + link.author + "") a(href=""+ link.url +"")
| #{link.author} img(src="" + link.images.thumb + "", alt="")
!= link.user_flair else
p.to to a(href="" + link.permalink + "")
a(href="/r/" + link.subreddit + "") .no-image
| #{link.subreddit} .entry
if link.stickied .title
span(class="green") stickied if link.is_self_link
.links a(href="" + link.permalink + "")
if link.over_18 h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)}
span.tag.nsfw NSFW != link.link_flair
a(href="" + link.permalink + "", class="comments") span (#{link.domain})
| #{link.num_comments} comments else
if json.info.before || json.info.after a(href="" + link.url + "")
.view-more-inks h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)}
if json.info.before && !subreddit_front != link.link_flair
a(href="/r/" + subreddit + "/" + sortby + "?t="+ (past ? past : '') +"&before=" + json.info.before + "") prev span (#{link.domain})
if json.info.after .meta
a(href="/r/" + subreddit + "/" + sortby + "?t=" + (past ? past : '') + "&after=" + json.info.after + "") next p.submitted submitted
#sidebar span(title="" + toUTCString(link.created) + "") #{timeDifference(link.created)} by
#search.sr if link.author === '[deleted]'
p search span(class="deleted") [deleted]
form(action="/r/" + subreddit + "/search", method="GET") else
input(type="text", name="q", id="q", placeholder="search") a(href="/u/" + link.author + "")
div | #{link.author}
label(for="restrict_sr") limit my search to r/#{subreddit} != link.user_flair
input(type="checkbox", name="restrict_sr", id="restrict_sr", checked="checked") p.to to
div a(href="/r/" + link.subreddit + "")
label(for="nsfw") include NSFW results | #{link.subreddit}
input(type="checkbox", name="nsfw", id="nsfw", checked="checked") if link.stickied
input(type="submit", value="search") span(class="green") stickied
if sidebar_data .links
if sidebar_data.subscribers if link.over_18
.content span.tag.nsfw NSFW
p subscribers: #{sidebar_data.subscribers.toLocaleString()} a(href="" + link.permalink + "", class="comments")
p users here right now: #{sidebar_data.active_user_count.toLocaleString()} | #{link.num_comments} comments
br if json.info.before || json.info.after
.heading .view-more-inks
p.title #{sidebar_data.title} if json.info.before && !subreddit_front
.short-description a(href="/r/" + subreddit + "/" + sortby + "?t="+ (past ? past : '') +"&before=" + json.info.before + "") prev
!= unescape(sidebar_data.public_description_html) if json.info.after
.description a(href="/r/" + subreddit + "/" + sortby + "?t=" + (past ? past : '') + "&after=" + json.info.after + "") next
!= unescape(sidebar_data.description_html) #sidebar
else #search.sr
if subreddit.includes('+') p search
.content form(action="/r/" + subreddit + "/search", method="GET")
p These subreddits input(type="text", name="q", id="q", placeholder="search")
- div
let subreddits = subreddit.split('+') label(for="restrict_sr") limit my search to r/#{subreddit}
ul(class="subreddit-listing") input(type="checkbox", name="restrict_sr", id="restrict_sr", checked="checked")
each subreddit in subreddits div
li label(for="nsfw") include NSFW results
a(href="/r/" + subreddit + "") #{subreddit} 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}