mirror of
https://codeberg.org/tacerus/teddit.git
synced 2025-01-08 12:22:33 +01:00
add nsfw warnings for subreddits too
This commit is contained in:
parent
5f8e8eb7a3
commit
f220b37012
@ -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
|
||||||
|
73
inc/processSubredditAbout.js
Normal file
73
inc/processSubredditAbout.js
Normal 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)
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
16
routes.js
16
routes.js
@ -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
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
@ -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;
|
||||||
|
@ -5,12 +5,30 @@ 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
|
||||||
if json.error_data.reason === "private"
|
if json.error_data.reason === "private"
|
||||||
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
|
||||||
|
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, <code>nsfw_enabled</code> cookie preference will be automatically set to <code>true</code>.
|
||||||
else
|
else
|
||||||
header
|
header
|
||||||
a(href="/", class="main")
|
a(href="/", class="main")
|
||||||
@ -128,18 +146,18 @@ html
|
|||||||
label(for="nsfw") include NSFW results
|
label(for="nsfw") include NSFW results
|
||||||
input(type="checkbox", name="nsfw", id="nsfw", checked="checked")
|
input(type="checkbox", name="nsfw", id="nsfw", checked="checked")
|
||||||
input(type="submit", value="search")
|
input(type="submit", value="search")
|
||||||
if sidebar_data
|
if subreddit_about
|
||||||
if sidebar_data.subscribers
|
if subreddit_about.subscribers
|
||||||
.content
|
.content
|
||||||
p subscribers: #{sidebar_data.subscribers.toLocaleString()}
|
p subscribers: #{subreddit_about.subscribers.toLocaleString()}
|
||||||
p users here right now: #{sidebar_data.active_user_count.toLocaleString()}
|
p users here right now: #{subreddit_about.active_user_count.toLocaleString()}
|
||||||
br
|
br
|
||||||
.heading
|
.heading
|
||||||
p.title #{sidebar_data.title}
|
p.title #{subreddit_about.title}
|
||||||
.short-description
|
.short-description
|
||||||
!= unescape(sidebar_data.public_description_html)
|
!= unescape(subreddit_about.public_description_html)
|
||||||
.description
|
.description
|
||||||
!= unescape(sidebar_data.description_html)
|
!= unescape(subreddit_about.description_html)
|
||||||
else
|
else
|
||||||
if subreddit.includes('+')
|
if subreddit.includes('+')
|
||||||
.content
|
.content
|
||||||
|
Loading…
Reference in New Issue
Block a user