add nsfw warning pages to posts when nsfw content is disabled

This commit is contained in:
teddit 2020-12-27 23:25:39 +01:00
parent 357c6402cd
commit 5f8e8eb7a3
8 changed files with 224 additions and 154 deletions

6
app.js
View File

@ -110,6 +110,12 @@ const preferencesMiddleware = (req, res, next) => {
res.cookie('flairs', flairsOverride, { maxAge: 31536000, httpOnly: true })
}
let nsfwEnabledOverride = req.query.nsfw_enabled
if(nsfwEnabledOverride) {
req.cookies.nsfw_enabled = nsfwEnabledOverride
res.cookie('nsfw_enabled', nsfwEnabledOverride, { maxAge: 31536000, httpOnly: true })
}
next()
}

View File

@ -1,6 +1,6 @@
const config = {
domain: process.env.DOMAIN || '127.0.0.1', // Or for example 'teddit.net'
reddit_app_id: process.env.REDDIT_APP_ID || 'H6-HjZ5pUPjaFQ', // You should obtain your own Reddit app ID. For testing purposes it's okay to use this project's default app ID. Create your Reddit app here: https://old.reddit.com/prefs/apps/. Make sure to create an "installed app" type of app.
reddit_app_id: process.env.REDDIT_APP_ID || 'ABfYqdDc9qPh1w', // You should obtain your own Reddit app ID. For testing purposes it's okay to use this project's default app ID. Create your Reddit app here: https://old.reddit.com/prefs/apps/. Make sure to create an "installed app" type of app.
cert_dir: process.env.CERT_DIR || '', // For example '/home/teddit/letsencrypt/live/teddit.net', if you are using https. No trailing slash.
flairs_enabled: process.env.FLAIRS_ENABLED !== "true" || true, // Enables the rendering of user and link flairs on teddit
api_enabled: process.env.API_ENABLED !== "true" || true, // Teddit API feature. Might increase loads significantly on your instance.
@ -20,6 +20,8 @@ const config = {
use_helmet_hsts: process.env.USE_HELMET_HSTS === "true" || false, // Recommended to be true when using https
trust_proxy: process.env.TRUST_PROXY === "true" || false, // Enable trust_proxy if you are using reverse proxy like nginx
trust_proxy_address: process.env.TRUST_PROXY_ADDRESS || '127.0.0.1',
nsfw_enabled: process.env.NSFW_ENABLED !== "true" || true, // Enable NSFW (over 18) content. If false, a warning is shown to the user before opening any NSFW post. When the NFSW content is disabled, NSFW posts are hidden from subreddits and from user page feeds. Note: Users can set this to true or false from their preferences.
post_comments_sort: process.env.POST_COMMENTS_SORT || 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live.
setexs: {
/**,
* Redis cache expiration values (in seconds).
@ -34,7 +36,6 @@ const config = {
sidebar: 60 * 60 * 24 * 7, // 7 days
shorts: 60 * 60 * 24 * 31
},
post_comments_sort: 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live.
valid_media_domains: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'emoji.redditmedia.com', 'thumbs.gfycat.com', 'i.ytimg.com'],
reddit_api_error_text: `Seems like your instance is either blocked (e.g. due to API rate limiting), reddit is currently down, or your API key is expired and not renewd properly. This can also happen for other reasons.`
};

View File

@ -28,6 +28,11 @@ 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
if(data.domain) {
let tld = data.domain.split('self.')
if(tld.length > 1) {

View File

@ -1,4 +1,5 @@
module.exports = function() {
const config = require('../config');
this.processJsonUser = function(json, parsed, after, before, user_preferences) {
return new Promise(resolve => {
(async () => {
@ -40,6 +41,10 @@ module.exports = function() {
let post_id = post.permalink.split('/').slice(-2)[0] + '/'
let url = post.permalink.replace(post_id, '')
if(post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
if(type === 't3') {
let duration = null
if(post.media) {

View File

@ -15,11 +15,13 @@ module.exports = (app, redis, fetch, RedditAPI) => {
})
app.get('/preferences', (req, res, next) => {
return res.render('preferences', { user_preferences: req.cookies })
return res.render('preferences', { user_preferences: req.cookies, instance_config: config })
})
app.get('/resetprefs', (req, res, next) => {
res.clearCookie('theme')
res.clearCookie('flairs')
res.clearCookie('nsfw_enabled')
return res.redirect('/preferences')
})
@ -501,7 +503,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
post_url: post_url,
subreddit: subreddit,
sortby: sortby,
user_preferences: req.cookies
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
})
} else {
let key = `morechildren:${post_url};1`
@ -534,7 +537,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
subreddit: req.params.subreddit,
sortby: sortby,
more_comments_page: 1,
user_preferences: req.cookies
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
})
})()
})
@ -566,7 +570,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
post_url: post_url,
subreddit: subreddit,
sortby: sortby,
user_preferences: req.cookies
user_preferences: req.cookies,
instance_nsfw_enabled: config.nsfw_enabled
})
})()
}
@ -736,6 +741,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
app.post('/saveprefs', (req, res, next) => {
let theme = req.body.theme
let flairs = req.body.flairs
let nsfw_enabled = req.body.nsfw_enabled
res.cookie('theme', theme, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
@ -744,6 +750,13 @@ module.exports = (app, redis, fetch, RedditAPI) => {
else
flairs = 'false'
res.cookie('flairs', flairs, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
if(nsfw_enabled === 'on')
nsfw_enabled = 'true'
else
nsfw_enabled = 'false'
res.cookie('nsfw_enabled', nsfw_enabled, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
return res.redirect('/preferences')
})

View File

@ -300,9 +300,10 @@ form .setting {
margin: 10px 0px;
width: 100%;
}
.container .content p.notice {
.container .content small.notice {
padding-top: 20px;
padding-bottom: 20px;
padding-bottom: 5px;
display: inline-block;
}
.container .content p.version {
text-align: right;
@ -851,6 +852,30 @@ 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;

View File

@ -10,6 +10,15 @@ html
p #{JSON.stringify(error_data)}
else
#post
if post.over_18 && (instance_nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false'
.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, <code>nsfw_enabled</code> cookie preference will be automatically set to <code>true</code>.
else
header
div
p subreddit:

View File

@ -25,6 +25,12 @@ html
input(type="checkbox", name="flairs", id="flairs", checked="checked")
else
input(type="checkbox", name="flairs", id="flairs")
p(class="notice") Preferences are stored client-side using cookies without any personal information.
.setting
label(for="nsfw_enabled") Show NSFW content:
if (instance_config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false'
input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled")
else
input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled", checked="checked")
small(class="notice") Preferences are stored client-side using cookies without any personal information.
input(type="submit", value="Save preferences")
a(href="/resetprefs", class="btn") Reset preferences