add flairs to preferences

This commit is contained in:
teddit 2020-12-24 22:13:08 +01:00
parent 0ce28e6d05
commit 8a5334c9ac
8 changed files with 70 additions and 41 deletions

19
app.js
View File

@ -93,20 +93,27 @@ if(config.use_compression) {
app.use(cookieParser()) app.use(cookieParser())
const themeMiddleware = (req, res, next) => { const preferencesMiddleware = (req, res, next) => {
let themeOverride = req.query.theme let themeOverride = req.query.theme
if( themeOverride) { if(themeOverride) {
// Convert Dark to dark since the stylesheet has it lower case // Convert Dark to dark since the stylesheet has it lower case
themeOverride = themeOverride.toLowerCase(); themeOverride = themeOverride.toLowerCase()
// This override here will set it for the current request // This override here will set it for the current request
req.cookies.theme = themeOverride req.cookies.theme = themeOverride
// this will set it for future requests // this will set it for future requests
res.cookie('theme', themeOverride, {maxAge: 31536000, httpOnly: true}) res.cookie('theme', themeOverride, { maxAge: 31536000, httpOnly: true })
} }
next();
let flairsOverride = req.query.flairs
if(flairsOverride) {
req.cookies.flairs = flairsOverride
res.cookie('flairs', flairsOverride, { maxAge: 31536000, httpOnly: true })
}
next()
} }
app.use(themeMiddleware) app.use(preferencesMiddleware)
if(config.use_view_cache) { if(config.use_view_cache) {
app.set('view cache', true) app.set('view cache', true)

9
dist/css/styles.css vendored
View File

@ -288,6 +288,10 @@ form legend {
margin-bottom: 10px; margin-bottom: 10px;
padding-bottom: 10px; padding-bottom: 10px;
} }
form .setting {
margin: 10px 0px;
width: 100%;
}
.container .content p.notice { .container .content p.notice {
padding-top: 20px; padding-top: 20px;
padding-bottom: 20px; padding-bottom: 20px;
@ -861,7 +865,10 @@ input[type="submit"]:hover,
#user .entries .entry .meta { #user .entries .entry .meta {
float: left; float: left;
} }
#user .entries .entry .meta .title,#user .entries .entry .meta .author,#user .entries .entry .meta .subreddit { #user .entries .entry .meta .title,
#user .entries .entry .meta .author,
#user .entries .entry .meta .subreddit,
#user .entries .entry .meta .flair {
float: left; float: left;
} }
#user .comment { #user .comment {

View File

@ -1,7 +1,7 @@
module.exports = function(fetch) { module.exports = function(fetch) {
var compilePostComments = require('./compilePostComments.js')(); var compilePostComments = require('./compilePostComments.js')();
var procPostMedia = require('./processPostMedia.js')(); var procPostMedia = require('./processPostMedia.js')();
this.processJsonPost = (json, parsed) => { this.processJsonPost = (json, parsed, user_preferences) => {
return new Promise(resolve => { return new Promise(resolve => {
(async () => { (async () => {
if(!parsed) { if(!parsed) {
@ -34,8 +34,8 @@ module.exports = function(fetch) {
images: null, images: null,
crosspost: false, crosspost: false,
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
link_flair: await formatLinkFlair(post), link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''),
user_flair: await formatUserFlair(post) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
} }
let validEmbedDomains = ['gfycat.com', 'youtube.com'] let validEmbedDomains = ['gfycat.com', 'youtube.com']
@ -89,7 +89,7 @@ module.exports = function(fetch) {
selftext: unescape(post.selftext_html), selftext: unescape(post.selftext_html),
selftext_crosspost: unescape(post.crosspost.selftext_html), selftext_crosspost: unescape(post.crosspost.selftext_html),
is_crosspost: true, is_crosspost: true,
user_flair: await formatUserFlair(post) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
} }
} }
@ -149,7 +149,7 @@ module.exports = function(fetch) {
edited: comment.edited, edited: comment.edited,
replies: [], replies: [],
depth: 0, depth: 0,
user_flair: await formatUserFlair(comment) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : '')
} }
} else { } else {
obj = { obj = {
@ -165,7 +165,7 @@ module.exports = function(fetch) {
if(comment.replies && kind !== 'more') { if(comment.replies && kind !== 'more') {
if(comment.replies.data) { if(comment.replies.data) {
if(comment.replies.data.children.length > 0) { if(comment.replies.data.children.length > 0) {
obj.replies = await processReplies(comment.replies.data.children, post_id, 1) obj.replies = await processReplies(comment.replies.data.children, post_id, 1, user_preferences)
} }
} }
} }
@ -204,7 +204,7 @@ module.exports = function(fetch) {
return { post_data: post_data, comments: comments_html } return { post_data: post_data, comments: comments_html }
} }
this.processReplies = async (data, post_id, depth) => { this.processReplies = async (data, post_id, depth, user_preferences) => {
let return_replies = [] let return_replies = []
for(var i = 0; i < data.length; i++) { for(var i = 0; i < data.length; i++) {
let kind = data[i].kind let kind = data[i].kind
@ -227,7 +227,7 @@ module.exports = function(fetch) {
edited: reply.edited, edited: reply.edited,
replies: [], replies: [],
depth: depth, depth: depth,
user_flair: await formatUserFlair(reply) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(reply) : '')
} }
} else { } else {
obj = { obj = {
@ -263,7 +263,7 @@ module.exports = function(fetch) {
distinguished: comment.edited, distinguished: comment.edited,
replies: [], replies: [],
depth: depth + 1, depth: depth + 1,
user_flair: await formatUserFlair(comment) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : '')
} }
} else { } else {
objct = { objct = {
@ -285,7 +285,7 @@ module.exports = function(fetch) {
if(comment.replies) { if(comment.replies) {
if(comment.replies.data) { if(comment.replies.data) {
if(comment.replies.data.children.length > 0) { if(comment.replies.data.children.length > 0) {
objct.replies = await processReplies(comment.replies.data.children, post_id, depth ) objct.replies = await processReplies(comment.replies.data.children, post_id, depth, user_preferences)
} }
} }
} }

View File

@ -1,6 +1,6 @@
module.exports = function() { module.exports = function() {
const config = require('../config'); const config = require('../config');
this.processJsonSubreddit = (json, from, subreddit_front) => { this.processJsonSubreddit = (json, from, subreddit_front, user_preferences) => {
return new Promise(resolve => { return new Promise(resolve => {
(async () => { (async () => {
if(from === 'redis') { if(from === 'redis') {
@ -75,8 +75,8 @@ module.exports = function() {
stickied: data.stickied, stickied: data.stickied,
is_self_link: is_self_link, is_self_link: is_self_link,
subreddit_front: subreddit_front, subreddit_front: subreddit_front,
link_flair: await formatLinkFlair(data), link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''),
user_flair: await formatUserFlair(data) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '')
} }
ret.links.push(obj) ret.links.push(obj)
} }

View File

@ -1,5 +1,5 @@
module.exports = function() { module.exports = function() {
this.processJsonUser = function(json, parsed, after, before) { this.processJsonUser = function(json, parsed, after, before, user_preferences) {
return new Promise(resolve => { return new Promise(resolve => {
(async () => { (async () => {
if(!parsed) { if(!parsed) {
@ -64,7 +64,7 @@ module.exports = function() {
num_comments: post.num_comments, num_comments: post.num_comments,
over_18: post.over_18, over_18: post.over_18,
permalink: post.permalink, permalink: post.permalink,
user_flair: await formatUserFlair(post) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
} }
} }
if(type === 't1') { if(type === 't1') {
@ -83,7 +83,7 @@ module.exports = function() {
permalink: post.permalink, permalink: post.permalink,
link_author: post.link_author, link_author: post.link_author,
link_title: post.link_title, link_title: post.link_title,
user_flair: await formatUserFlair(post) user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
} }
} }
posts.push(obj) posts.push(obj)

View File

@ -96,7 +96,7 @@ module.exports = function() {
if(api_target === 'reddit') { if(api_target === 'reddit') {
return res.end(JSON.stringify(json)) return res.end(JSON.stringify(json))
} else { } else {
let processed_json = await processJsonSubreddit(_json, from) let processed_json = await processJsonSubreddit(_json, from, null, req.cookies)
let protocol = (config.https_enabled ? 'https' : 'http') let protocol = (config.https_enabled ? 'https' : 'http')
for(var i = 0; i < processed_json.links.length; i++) { for(var i = 0; i < processed_json.links.length; i++) {

View File

@ -110,7 +110,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(api_req) { if(api_req) {
return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, '/') return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, '/')
} else { } else {
let processed_json = await processJsonSubreddit(json, 'redis') let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies)
return res.render('index', { return res.render('index', {
json: processed_json, json: processed_json,
sortby: sortby, sortby: sortby,
@ -135,7 +135,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(api_req) { if(api_req) {
return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, '/') return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, '/')
} else { } else {
let processed_json = await processJsonSubreddit(json, 'from_online') let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies)
return res.render('index', { return res.render('index', {
json: processed_json, json: processed_json,
sortby: sortby, sortby: sortby,
@ -381,7 +381,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(api_req) { if(api_req) {
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') let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies)
let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI) let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI)
if(!processed_json.error) { if(!processed_json.error) {
return res.render('subreddit', { return res.render('subreddit', {
@ -419,7 +419,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(api_req) { if(api_req) {
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') let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies)
let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI) let sidebar_data = await processSubredditSidebar(subreddit, redis, fetch, RedditAPI)
return res.render('subreddit', { return res.render('subreddit', {
json: processed_json, json: processed_json,
@ -492,7 +492,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
console.log(`Got ${comments_url} key from redis.`); console.log(`Got ${comments_url} key from redis.`);
(async () => { (async () => {
if(!more_comments_cursor) { if(!more_comments_cursor) {
let processed_json = await processJsonPost(json, false) 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)
return res.render('post', { return res.render('post', {
post: finalized_json.post_data, post: finalized_json.post_data,
@ -523,7 +523,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
post_json = JSON.parse(post_json) post_json = JSON.parse(post_json)
json = JSON.parse(json) json = JSON.parse(json)
post_json[1].data.children = json post_json[1].data.children = json
let processed_json = await processJsonPost(post_json, true) let processed_json = await processJsonPost(post_json, true, req.cookies)
let finalized_json = await finalizeJsonPost(processed_json, id, post_url, morechildren_ids) let finalized_json = await finalizeJsonPost(processed_json, id, post_url, morechildren_ids)
return res.render('post', { return res.render('post', {
@ -557,7 +557,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} else { } else {
console.log(`Fetched the JSON from reddit.com${comments_url}.`); console.log(`Fetched the JSON from reddit.com${comments_url}.`);
(async () => { (async () => {
let processed_json = await processJsonPost(json, true) let processed_json = await processJsonPost(json, true, 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)
return res.render('post', { return res.render('post', {
post: finalized_json.post_data, post: finalized_json.post_data,
@ -651,7 +651,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(json) { if(json) {
console.log(`Got user ${user} key from redis.`); console.log(`Got user ${user} key from redis.`);
(async () => { (async () => {
let processed_json = await processJsonUser(json, false, after, before) let processed_json = await processJsonUser(json, false, after, before, req.cookies)
return res.render('user', { return res.render('user', {
data: processed_json, data: processed_json,
sortby: sortby, sortby: sortby,
@ -678,7 +678,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
return res.render('index', { post: null, user_preferences: req.cookies }) return res.render('index', { post: null, user_preferences: req.cookies })
} else { } else {
(async () => { (async () => {
let processed_json = await processJsonUser(user_data, true, after, before) let processed_json = await processJsonUser(user_data, true, after, before, req.cookies)
return res.render('user', { return res.render('user', {
data: processed_json, data: processed_json,
sortby: sortby, sortby: sortby,
@ -735,7 +735,15 @@ module.exports = (app, redis, fetch, RedditAPI) => {
app.post('/saveprefs', (req, res, next) => { app.post('/saveprefs', (req, res, next) => {
let theme = req.body.theme let theme = req.body.theme
let flairs = req.body.flairs
res.cookie('theme', theme, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) res.cookie('theme', theme, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
if(flairs === 'on')
flairs = 'true'
else
flairs = 'false'
res.cookie('flairs', flairs, { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true })
return res.redirect('/preferences') return res.redirect('/preferences')
}) })

View File

@ -10,14 +10,21 @@ html
h1 Preferences h1 Preferences
form(action="/saveprefs", method="POST") form(action="/saveprefs", method="POST")
legend Display legend Display
label(for="theme") Theme: .setting
select(id="theme", name="theme") label(for="theme") Theme:
if(!user_preferences.theme || user_preferences.theme == '') select(id="theme", name="theme")
option(value="", selected="selected") White if(!user_preferences.theme || user_preferences.theme == '')
option(value="dark") Dark option(value="", selected="selected") White
if(user_preferences.theme === 'dark') option(value="dark") Dark
option(value="") White if(user_preferences.theme === 'dark')
option(value="dark", selected="selected") Dark option(value="") White
option(value="dark", selected="selected") Dark
.setting
label(for="flairs") Show flairs:
if(!user_preferences.flairs || user_preferences.flairs == 'true')
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. p(class="notice") Preferences are stored client-side using cookies without any personal information.
input(type="submit", value="Save preferences") input(type="submit", value="Save preferences")
a(href="/resetprefs", class="btn") Reset preferences a(href="/resetprefs", class="btn") Reset preferences