diff --git a/app.js b/app.js index 4e74f0a..bd7dd5b 100644 --- a/app.js +++ b/app.js @@ -93,20 +93,27 @@ if(config.use_compression) { app.use(cookieParser()) -const themeMiddleware = (req, res, next) => { +const preferencesMiddleware = (req, res, next) => { let themeOverride = req.query.theme - if( themeOverride) { + if(themeOverride) { // 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 req.cookies.theme = themeOverride // 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) { app.set('view cache', true) diff --git a/dist/css/styles.css b/dist/css/styles.css index 1aa24ca..aa239d8 100644 --- a/dist/css/styles.css +++ b/dist/css/styles.css @@ -288,6 +288,10 @@ form legend { margin-bottom: 10px; padding-bottom: 10px; } +form .setting { + margin: 10px 0px; + width: 100%; +} .container .content p.notice { padding-top: 20px; padding-bottom: 20px; @@ -861,7 +865,10 @@ input[type="submit"]:hover, #user .entries .entry .meta { 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; } #user .comment { diff --git a/inc/processJsonPost.js b/inc/processJsonPost.js index baa503c..ad0fb8f 100644 --- a/inc/processJsonPost.js +++ b/inc/processJsonPost.js @@ -1,7 +1,7 @@ module.exports = function(fetch) { var compilePostComments = require('./compilePostComments.js')(); var procPostMedia = require('./processPostMedia.js')(); - this.processJsonPost = (json, parsed) => { + this.processJsonPost = (json, parsed, user_preferences) => { return new Promise(resolve => { (async () => { if(!parsed) { @@ -34,8 +34,8 @@ module.exports = function(fetch) { images: null, crosspost: false, selftext: unescape(post.selftext_html), - link_flair: await formatLinkFlair(post), - user_flair: await formatUserFlair(post) + link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''), + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') } let validEmbedDomains = ['gfycat.com', 'youtube.com'] @@ -89,7 +89,7 @@ module.exports = function(fetch) { selftext: unescape(post.selftext_html), selftext_crosspost: unescape(post.crosspost.selftext_html), 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, replies: [], depth: 0, - user_flair: await formatUserFlair(comment) + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : '') } } else { obj = { @@ -165,7 +165,7 @@ module.exports = function(fetch) { if(comment.replies && kind !== 'more') { if(comment.replies.data) { 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 } } - this.processReplies = async (data, post_id, depth) => { + this.processReplies = async (data, post_id, depth, user_preferences) => { let return_replies = [] for(var i = 0; i < data.length; i++) { let kind = data[i].kind @@ -227,7 +227,7 @@ module.exports = function(fetch) { edited: reply.edited, replies: [], depth: depth, - user_flair: await formatUserFlair(reply) + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(reply) : '') } } else { obj = { @@ -263,7 +263,7 @@ module.exports = function(fetch) { distinguished: comment.edited, replies: [], depth: depth + 1, - user_flair: await formatUserFlair(comment) + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(comment) : '') } } else { objct = { @@ -285,7 +285,7 @@ module.exports = function(fetch) { if(comment.replies) { if(comment.replies.data) { 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) } } } diff --git a/inc/processJsonSubreddit.js b/inc/processJsonSubreddit.js index a108dd7..6b05760 100644 --- a/inc/processJsonSubreddit.js +++ b/inc/processJsonSubreddit.js @@ -1,6 +1,6 @@ module.exports = function() { const config = require('../config'); - this.processJsonSubreddit = (json, from, subreddit_front) => { + this.processJsonSubreddit = (json, from, subreddit_front, user_preferences) => { return new Promise(resolve => { (async () => { if(from === 'redis') { @@ -75,8 +75,8 @@ module.exports = function() { stickied: data.stickied, is_self_link: is_self_link, subreddit_front: subreddit_front, - link_flair: await formatLinkFlair(data), - user_flair: await formatUserFlair(data) + link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(data) : ''), + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(data) : '') } ret.links.push(obj) } diff --git a/inc/processJsonUser.js b/inc/processJsonUser.js index f0c0687..cc101f4 100644 --- a/inc/processJsonUser.js +++ b/inc/processJsonUser.js @@ -1,5 +1,5 @@ module.exports = function() { - this.processJsonUser = function(json, parsed, after, before) { + this.processJsonUser = function(json, parsed, after, before, user_preferences) { return new Promise(resolve => { (async () => { if(!parsed) { @@ -64,7 +64,7 @@ module.exports = function() { num_comments: post.num_comments, over_18: post.over_18, permalink: post.permalink, - user_flair: await formatUserFlair(post) + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') } } if(type === 't1') { @@ -83,7 +83,7 @@ module.exports = function() { permalink: post.permalink, link_author: post.link_author, link_title: post.link_title, - user_flair: await formatUserFlair(post) + user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '') } } posts.push(obj) diff --git a/inc/teddit_api/handleSubreddit.js b/inc/teddit_api/handleSubreddit.js index 0ea5f5b..f2b7c63 100644 --- a/inc/teddit_api/handleSubreddit.js +++ b/inc/teddit_api/handleSubreddit.js @@ -96,7 +96,7 @@ module.exports = function() { if(api_target === 'reddit') { return res.end(JSON.stringify(json)) } 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') for(var i = 0; i < processed_json.links.length; i++) { diff --git a/routes.js b/routes.js index 60024c0..eb392e4 100644 --- a/routes.js +++ b/routes.js @@ -110,7 +110,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, '/') } else { - let processed_json = await processJsonSubreddit(json, 'redis') + let processed_json = await processJsonSubreddit(json, 'redis', null, req.cookies) return res.render('index', { json: processed_json, sortby: sortby, @@ -135,7 +135,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, '/') } else { - let processed_json = await processJsonSubreddit(json, 'from_online') + let processed_json = await processJsonSubreddit(json, 'from_online', null, req.cookies) return res.render('index', { json: processed_json, sortby: sortby, @@ -381,7 +381,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiSubreddit(json, req, res, 'redis', api_type, api_target, subreddit) } 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) if(!processed_json.error) { return res.render('subreddit', { @@ -419,7 +419,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiSubreddit(json, req, res, 'from_online', api_type, api_target, subreddit) } 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) return res.render('subreddit', { json: processed_json, @@ -492,7 +492,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { console.log(`Got ${comments_url} key from redis.`); (async () => { 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) return res.render('post', { post: finalized_json.post_data, @@ -523,7 +523,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { post_json = JSON.parse(post_json) json = JSON.parse(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) return res.render('post', { @@ -557,7 +557,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } else { console.log(`Fetched the JSON from reddit.com${comments_url}.`); (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) return res.render('post', { post: finalized_json.post_data, @@ -651,7 +651,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(json) { console.log(`Got user ${user} key from redis.`); (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', { data: processed_json, sortby: sortby, @@ -678,7 +678,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { return res.render('index', { post: null, user_preferences: req.cookies }) } else { (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', { data: processed_json, sortby: sortby, @@ -735,7 +735,15 @@ module.exports = (app, redis, fetch, RedditAPI) => { app.post('/saveprefs', (req, res, next) => { let theme = req.body.theme + let flairs = req.body.flairs + 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') }) diff --git a/views/preferences.pug b/views/preferences.pug index ac54806..054dfc5 100644 --- a/views/preferences.pug +++ b/views/preferences.pug @@ -10,14 +10,21 @@ html h1 Preferences form(action="/saveprefs", method="POST") legend Display - label(for="theme") Theme: - select(id="theme", name="theme") - if(!user_preferences.theme || user_preferences.theme == '') - option(value="", selected="selected") White - option(value="dark") Dark - if(user_preferences.theme === 'dark') - option(value="") White - option(value="dark", selected="selected") Dark + .setting + label(for="theme") Theme: + select(id="theme", name="theme") + if(!user_preferences.theme || user_preferences.theme == '') + option(value="", selected="selected") White + option(value="dark") Dark + if(user_preferences.theme === '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. input(type="submit", value="Save preferences") a(href="/resetprefs", class="btn") Reset preferences