From 8998db333aaec228433d0db0d76fc7507b5f4ef6 Mon Sep 17 00:00:00 2001 From: takatalk Date: Sun, 24 Jan 2021 17:24:02 -0500 Subject: [PATCH 01/13] expandable image full screen width for mobile --- static/css/styles.css | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/static/css/styles.css b/static/css/styles.css index f25b966..8ef0496 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -497,6 +497,12 @@ footer a { #links .link .entry details .line:first-child { margin-top: 0; } +@media screen and (max-width: 600px) { + #links .link .entry details[open] .preview { + width: 100vw; + transform: translateX(-150px); + } +} /* COMMENTS */ .comment { font-size: 0.83rem; From 9ee2deb4605d9cbc068a961b8edb4a0fdd5b70a4 Mon Sep 17 00:00:00 2001 From: bopol Date: Wed, 27 Jan 2021 17:39:12 +0100 Subject: [PATCH 02/13] add auto theme switches between white and dark, based on browser's theme --- views/about.pug | 2 +- views/includes/head.pug | 2 ++ views/index.pug | 2 +- views/post.pug | 2 +- views/preferences.pug | 10 +++++++++- views/privacypolicy.pug | 2 +- views/search.pug | 2 +- views/subreddit.pug | 2 +- views/subreddits_explore.pug | 2 +- views/user.pug | 2 +- 10 files changed, 19 insertions(+), 9 deletions(-) diff --git a/views/about.pug b/views/about.pug index aba4be0..1bf9c17 100644 --- a/views/about.pug +++ b/views/about.pug @@ -3,7 +3,7 @@ html head title about - teddit include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug .container .content diff --git a/views/includes/head.pug b/views/includes/head.pug index 3d497b3..19c0854 100644 --- a/views/includes/head.pug +++ b/views/includes/head.pug @@ -1,3 +1,5 @@ +if(user_preferences.theme === 'auto') + link(rel="stylesheet", type="text/css", href="/css/dark.css", media="(prefers-color-scheme: dark)") if(user_preferences.theme === 'dark') link(rel="stylesheet", type="text/css", href="/css/dark.css") if(user_preferences.theme === 'sepia') diff --git a/views/index.pug b/views/index.pug index a5c5db1..0768728 100644 --- a/views/index.pug +++ b/views/index.pug @@ -3,7 +3,7 @@ html head title teddit include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug if json === null h2 error diff --git a/views/post.pug b/views/post.pug index e88fa74..83f723c 100644 --- a/views/post.pug +++ b/views/post.pug @@ -3,7 +3,7 @@ html head title #{cleanTitle(post.title)} : #{subreddit} include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug if post === null h1 Error occured diff --git a/views/preferences.pug b/views/preferences.pug index 9d173a7..4d1169a 100644 --- a/views/preferences.pug +++ b/views/preferences.pug @@ -3,7 +3,7 @@ html head title preferences - teddit include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug .container .content @@ -14,17 +14,25 @@ html label(for="theme") Theme: select(id="theme", name="theme") if(!user_preferences.theme || user_preferences.theme == '') + option(value="auto") Auto option(value="", selected="selected") White option(value="dark") Dark option(value="sepia") Sepia if(user_preferences.theme === 'dark') + option(value="auto") Auto option(value="") White option(value="dark", selected="selected") Dark option(value="sepia") Sepia if(user_preferences.theme === 'sepia') + option(value="auto") Auto option(value="") White option(value="dark") Dark option(value="sepia", selected="selected") Sepia + if(user_preferences.theme === 'auto') + option(value="auto", selected="selected") Auto + option(value="") White + option(value="dark") Dark + option(value="sepia") Sepia .setting label(for="flairs") Show flairs: if(!user_preferences.flairs || user_preferences.flairs == 'true') diff --git a/views/privacypolicy.pug b/views/privacypolicy.pug index 803a70c..20bf2f9 100644 --- a/views/privacypolicy.pug +++ b/views/privacypolicy.pug @@ -3,7 +3,7 @@ html head title privacy policy - teddit include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug .container .content diff --git a/views/search.pug b/views/search.pug index 1abaab7..bb2ace4 100644 --- a/views/search.pug +++ b/views/search.pug @@ -6,7 +6,7 @@ html else title search results for #{q} include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug #search.sr.search-page form(action="/r/" + subreddit + "/search", method="GET") diff --git a/views/subreddit.pug b/views/subreddit.pug index 08b81bc..683b003 100644 --- a/views/subreddit.pug +++ b/views/subreddit.pug @@ -3,7 +3,7 @@ html head title /r/#{subreddit} include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug - let show_nsfw_warning = false; diff --git a/views/subreddits_explore.pug b/views/subreddits_explore.pug index fe23008..9823a1f 100644 --- a/views/subreddits_explore.pug +++ b/views/subreddits_explore.pug @@ -3,7 +3,7 @@ html head title subreddits - explore include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug if json === null h1 Error occured diff --git a/views/user.pug b/views/user.pug index 6a6c65e..94c3b6c 100644 --- a/views/user.pug +++ b/views/user.pug @@ -3,7 +3,7 @@ html head title overview for #{data.username} include includes/head.pug - body(class=""+ user_preferences.theme +"") + body(class=""+ (user_preferences.theme === 'auto' ? 'dark' : user_preferences.theme) + "") include includes/topbar.pug if user === null h1 Error occured From cbc06fd3c94e538f988b7f20079e339b613f5b58 Mon Sep 17 00:00:00 2001 From: teddit Date: Wed, 27 Jan 2021 18:52:18 +0100 Subject: [PATCH 03/13] fix post selftext when expanding it on subreddit view, fixes #137 --- static/css/styles.css | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/static/css/styles.css b/static/css/styles.css index 8ef0496..031e9a0 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -498,10 +498,6 @@ footer a { margin-top: 0; } @media screen and (max-width: 600px) { - #links .link .entry details[open] .preview { - width: 100vw; - transform: translateX(-150px); - } } /* COMMENTS */ .comment { @@ -1548,4 +1544,13 @@ code { .explore#links .link .entry { width: calc(100% - 20px); } + #links .link .entry details[open] .preview { + width: 100vw; + transform: translateX(-150px); + } + #links .link .entry .selftext { + width: calc(100vw - 40px); + transform: translateX(-150px); + margin-left: 10px; + } } From 7555a3c75e7e39dede4f21f95ca97e7c1067af57 Mon Sep 17 00:00:00 2001 From: juzz Date: Thu, 28 Jan 2021 08:41:56 +1100 Subject: [PATCH 04/13] 131 post kind on user page without oauth --- app.js | 1 - inc/processJsonUser.js | 8 ++++++-- routes.js | 29 +++++++++++++++++++++++++---- views/user.pug | 12 ++++++------ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index 2ae20d8..872d944 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,6 @@ const express = require('express') const cookieParser = require('cookie-parser') const r = require('redis') - const redis = (() => { if (!config.redis_enabled) { // Stub Redis if disabled diff --git a/inc/processJsonUser.js b/inc/processJsonUser.js index 05ae464..f80ed78 100644 --- a/inc/processJsonUser.js +++ b/inc/processJsonUser.js @@ -1,6 +1,6 @@ module.exports = function() { const config = require('../config'); - this.processJsonUser = function(json, parsed, after, before, user_preferences) { + this.processJsonUser = function(json, parsed, after, before, user_preferences, kind, post_type) { return new Promise(resolve => { (async () => { if(!parsed) { @@ -40,7 +40,10 @@ module.exports = function() { let post_id = post.permalink.split('/').slice(-2)[0] + '/' let url = post.permalink.replace(post_id, '') - + + if(type !== kind && kind) + continue + if(post.over_18) if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') continue @@ -103,6 +106,7 @@ module.exports = function() { comment_karma: about.comment_karma, view_more_posts: view_more_posts, user_front: user_front, + post_type: post_type, before: before, after: after, posts: posts diff --git a/routes.js b/routes.js index 2764ce2..af486fe 100644 --- a/routes.js +++ b/routes.js @@ -1005,7 +1005,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { res.redirect(`/u/${req.params.user}`) }) - app.get('/u/:user/:sort?', (req, res, next) => { + app.get('/u/:user/:kind?/:sort?', (req, res, next) => { let user = req.params.user let after = req.query.after let before = req.query.before @@ -1029,6 +1029,27 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(before) { d = `&before=${before}` } + + let post_type = req.params.kind + let kind = post_type + + if(!config.use_reddit_oauth) { + post_type = `/${post_type}` + switch(post_type) { + case '/comments': + kind = 't1' + break; + case '/submitted': + kind = 't3' + break; + default: + post_type = '' + kind = '' + } + } else { + post_type = '' + kind = '' + } let sortby = req.query.sort let past = req.query.t @@ -1071,7 +1092,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiUser(json, req, res, 'redis', api_type, api_target, user, after, before) } else { - let processed_json = await processJsonUser(json, false, after, before, req.cookies) + let processed_json = await processJsonUser(json, false, after, before, req.cookies, kind, post_type) return res.render('user', { data: processed_json, sortby: sortby, @@ -1096,7 +1117,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(config.use_reddit_oauth) url = `https://oauth.reddit.com/user/${user}/overview?limit=26${d}&sort=${sortby}&t=${past}` else - url = `https://reddit.com/user/${user}.json?limit=26${d}&sort=${sortby}&t=${past}` + url = `https://reddit.com/user/${user}${post_type}.json?limit=26${d}&sort=${sortby}&t=${past}` fetch(encodeURI(url), redditApiGETHeaders()) .then(result => { if(result.status === 200) { @@ -1112,7 +1133,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(api_req) { return handleTedditApiUser(user_data, req, res, 'online', api_type, api_target, user, after, before) } else { - let processed_json = await processJsonUser(user_data, true, after, before, req.cookies) + let processed_json = await processJsonUser(user_data, true, after, before, req.cookies, kind, post_type) return res.render('user', { data: processed_json, sortby: sortby, diff --git a/views/user.pug b/views/user.pug index 6a6c65e..f384e2b 100644 --- a/views/user.pug +++ b/views/user.pug @@ -23,13 +23,13 @@ html span sorted by: controversial ul li(class=!sortby || sortby == 'new' ? 'active' : '') - a(href="/u/" + data.username) new + a(href="/u/" + data.username + data.post_type) new li(class=sortby === 'hot' ? 'active' : '') - a(href="/u/" + data.username + "?sort=hot") hot + a(href="/u/" + data.username + data.post_type + "?sort=hot") hot li(class=sortby === 'top' ? 'active' : '') - a(href="/u/" + data.username + "?sort=top&t=" + past + "") top + a(href="/u/" + data.username + data.post_type + "?sort=top&t=" + past + "") top li(class=sortby === 'controversial' ? 'active' : '') - a(href="/u/" + data.username + "?sort=controversial&t=" + past + "") controversial + a(href="/u/" + data.username + data.post_type + "?sort=controversial&t=" + past + "") controversial if sortby === 'top' || sortby === 'controversial' details summary @@ -132,7 +132,7 @@ html if data.before || data.after p view more: if data.before && !data.user_front - a(href="/u/" + data.username + "?before=" + data.before + "") ‹ prev + a(href="/u/" + data.username + data.post_type + "?before=" + data.before + "") ‹ prev if data.after - a(href="/u/" + data.username + "?after=" + data.after + "") next › + a(href="/u/" + data.username + data.post_type + "?after=" + data.after + "") next › include includes/footer.pug From 7e951dbd528ba19808b37bedb32011c480901014 Mon Sep 17 00:00:00 2001 From: teddit Date: Thu, 28 Jan 2021 16:19:53 +0100 Subject: [PATCH 05/13] make expandable post image in subreddit views max-height 600px --- static/css/styles.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static/css/styles.css b/static/css/styles.css index 031e9a0..8109a74 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -497,7 +497,8 @@ footer a { #links .link .entry details .line:first-child { margin-top: 0; } -@media screen and (max-width: 600px) { +#links .link .entry details.preview-container img { + max-height: 600px !important; } /* COMMENTS */ .comment { From 5b813d3f8f290d60b112f5145b50580b93da8ca2 Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 29 Jan 2021 16:26:06 +0100 Subject: [PATCH 06/13] add zaggy's instance --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6bf7cd8..0500f53 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Community instances: * [https://teddit.ggc-project.de](https://teddit.ggc-project.de) * [https://teddit.kavin.rocks](https://teddit.kavin.rocks) +* [https://teddit.zaggy.nl](https://teddit.zaggy.nl) ## TODO From 388dca548c0d58915ed205236db6b49334460453 Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 29 Jan 2021 16:33:48 +0100 Subject: [PATCH 07/13] fix expandable image overflowing with sepia theme --- static/css/sepia.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/static/css/sepia.css b/static/css/sepia.css index c8dbac5..e4e4a6a 100644 --- a/static/css/sepia.css +++ b/static/css/sepia.css @@ -223,4 +223,7 @@ body.sepia .comments > form button { body.sepia #sidebar { width: 100%; } + body.sepia #links .link .entry details[open] .preview { + width: calc(100vw - 30px); + } } From ae90d4fd79eb113d37528163e2851f254a176ac9 Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 29 Jan 2021 18:32:08 +0100 Subject: [PATCH 08/13] fine-tune expandables for sepia theme --- static/css/sepia.css | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/static/css/sepia.css b/static/css/sepia.css index e4e4a6a..928bf4b 100644 --- a/static/css/sepia.css +++ b/static/css/sepia.css @@ -224,6 +224,10 @@ body.sepia .comments > form button { width: 100%; } body.sepia #links .link .entry details[open] .preview { - width: calc(100vw - 30px); + width: calc(100vw - 34px); + margin-left: 3px; + } + body.sepia #links .link .entry .selftext { + width: calc(100vw - 70px); } } From d6e1a98187c17119ecb333bb611661b3dc282be6 Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 29 Jan 2021 19:41:32 +0100 Subject: [PATCH 09/13] add fixes for #139 --- routes.js | 50 +++++++++++++++++++++---------------------- static/css/styles.css | 3 +++ views/user.pug | 11 ++++++++++ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/routes.js b/routes.js index af486fe..2348cd6 100644 --- a/routes.js +++ b/routes.js @@ -1009,6 +1009,8 @@ module.exports = (app, redis, fetch, RedditAPI) => { let user = req.params.user let after = req.query.after let before = req.query.before + let post_type = req.params.kind + let kind = post_type let user_data = {} let api_req = req.query.api let api_type = req.query.type @@ -1030,25 +1032,17 @@ module.exports = (app, redis, fetch, RedditAPI) => { d = `&before=${before}` } - let post_type = req.params.kind - let kind = post_type - - if(!config.use_reddit_oauth) { - post_type = `/${post_type}` - switch(post_type) { - case '/comments': - kind = 't1' - break; - case '/submitted': - kind = 't3' - break; - default: - post_type = '' - kind = '' - } - } else { - post_type = '' - kind = '' + post_type = `/${post_type}` + switch(post_type) { + case '/comments': + kind = 't1' + break; + case '/submitted': + kind = 't3' + break; + default: + post_type = '' + kind = '' } let sortby = req.query.sort @@ -1080,7 +1074,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { } } - let key = `${user}:${after}:${before}:sort:${sortby}:past:${past}` + let key = `${user}:${after}:${before}:sort:${sortby}:past:${past}:post_type:${post_type}` redis.get(key, (error, json) => { if(error) { console.error(`Error getting the user ${key} key from redis.`, error) @@ -1089,7 +1083,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { if(json) { console.log(`Got user ${user} key from redis.`); (async () => { - if(api_req) { + if(api_req) { return handleTedditApiUser(json, req, res, 'redis', api_type, api_target, user, after, before) } else { let processed_json = await processJsonUser(json, false, after, before, req.cookies, kind, post_type) @@ -1114,10 +1108,14 @@ module.exports = (app, redis, fetch, RedditAPI) => { .then(json => { user_data.about = json let url = '' - if(config.use_reddit_oauth) - url = `https://oauth.reddit.com/user/${user}/overview?limit=26${d}&sort=${sortby}&t=${past}` - else + if(config.use_reddit_oauth) { + let endpoint = '/overview' + if(post_type !== '') + endpoint = post_type + url = `https://oauth.reddit.com/user/${user}${post_type}?limit=26${d}&sort=${sortby}&t=${past}` + } else { url = `https://reddit.com/user/${user}${post_type}.json?limit=26${d}&sort=${sortby}&t=${past}` + } fetch(encodeURI(url), redditApiGETHeaders()) .then(result => { if(result.status === 200) { @@ -1130,7 +1128,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { return res.render('index', { post: null, user_preferences: req.cookies }) } else { (async () => { - if(api_req) { + if(api_req) { return handleTedditApiUser(user_data, req, res, 'online', api_type, api_target, user, after, before) } else { let processed_json = await processJsonUser(user_data, true, after, before, req.cookies, kind, post_type) @@ -1165,7 +1163,7 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) } else { if(result.status === 404) { - console.log('404 – User not found') + console.log('404 – User not found') } else { console.error(`Something went wrong while fetching data from Reddit. ${result.status} – ${result.statusText}`) console.error(config.reddit_api_error_text) diff --git a/static/css/styles.css b/static/css/styles.css index 8109a74..158cc43 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -183,6 +183,9 @@ header a { header a.main { margin-left: 12px; } +header h3.username { + margin: 0px 15px 0px 0px; +} header .bottom { float: left; overflow: hidden; diff --git a/views/user.pug b/views/user.pug index 2017c66..284193b 100644 --- a/views/user.pug +++ b/views/user.pug @@ -10,6 +10,17 @@ html p #{JSON.stringify(error_data)} else #user + header + .bottom + a(href="/u/" + data.username + "") + h3.username user: #{data.username} + ul.tabmenu + li(class=!data.post_type || data.post_type == '' ? 'active' : '') + a(href="/u/" + data.username) overview + li(class=data.post_type === '/comments' ? 'active' : '') + a(href="/u/" + data.username + "/comments") comments + li(class=data.post_type === '/submitted' ? 'active' : '') + a(href="/u/" + data.username + "/submitted") submitted #links details summary From cf2fe7f9f11844da9e73da54cb4f6574a57b4cbe Mon Sep 17 00:00:00 2001 From: teddit Date: Fri, 29 Jan 2021 20:09:26 +0100 Subject: [PATCH 10/13] fix /user/ redirect route --- routes.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/routes.js b/routes.js index 2348cd6..75aee11 100644 --- a/routes.js +++ b/routes.js @@ -1001,11 +1001,20 @@ module.exports = (app, redis, fetch, RedditAPI) => { }) }) - app.get('/user/:user', (req, res, next) => { - res.redirect(`/u/${req.params.user}`) + app.get('/user/:user/:kind?', (req, res, next) => { + let kind = '' + if(req.params.kind) + kind = `/${req.params.kind}` + let q = '' + if(req.query.sort) + q += `?sort=${req.query.sort}&` + if(req.query.t) + q += `t=${req.query.t}` + + res.redirect(`/u/${req.params.user}${kind}${q}`) }) - app.get('/u/:user/:kind?/:sort?', (req, res, next) => { + app.get('/u/:user/:kind?', (req, res, next) => { let user = req.params.user let after = req.query.after let before = req.query.before From bc0aff9481cd25c991759fa36f1758c783197cee Mon Sep 17 00:00:00 2001 From: teddit Date: Sat, 30 Jan 2021 15:23:26 +0100 Subject: [PATCH 11/13] add 'pubDate' for RSS feeds --- inc/teddit_api/handleSubreddit.js | 1 + inc/teddit_api/handleUser.js | 1 + 2 files changed, 2 insertions(+) diff --git a/inc/teddit_api/handleSubreddit.js b/inc/teddit_api/handleSubreddit.js index 261cf7e..058d783 100644 --- a/inc/teddit_api/handleSubreddit.js +++ b/inc/teddit_api/handleSubreddit.js @@ -69,6 +69,7 @@ module.exports = function() { ${link.title} ${link.author} ${link.created} + ${new Date(link.created_utc*1000).toGMTString()} ${link.domain} ${link.id} ${thumbnail} diff --git a/inc/teddit_api/handleUser.js b/inc/teddit_api/handleUser.js index 110f52f..2aba285 100644 --- a/inc/teddit_api/handleUser.js +++ b/inc/teddit_api/handleUser.js @@ -74,6 +74,7 @@ module.exports = function() { ${kind} ${post.subreddit} ${post.created_utc} + ${new Date(post.created_utc*1000).toGMTString()} ${post.ups} ${permalink} ${post.edited} From 6eb6c73b180d9a62bbe2bf990bb40910229dae70 Mon Sep 17 00:00:00 2001 From: teddit Date: Sat, 30 Jan 2021 22:16:56 +0100 Subject: [PATCH 12/13] add import/export feature for preferences --- README.md | 1 - routes.js | 49 +++++++++++++++++++++++++++++++++++++++++++ static/css/styles.css | 6 ++++++ views/preferences.pug | 23 ++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0500f53..ce15d1f 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,6 @@ Community instances: ## TODO -* Import/export preferences * User trophies * "other discussions" feature * "Open on reddit" links diff --git a/routes.js b/routes.js index 75aee11..bc4b978 100644 --- a/routes.js +++ b/routes.js @@ -28,6 +28,42 @@ module.exports = (app, redis, fetch, RedditAPI) => { res.clearCookie('subbed_subreddits') return res.redirect('/preferences') }) + + app.get('/import_prefs/:key', (req, res, next) => { + let key = req.params.key + if(!key) + return res.redirect('/') + if(key.length !== 10) + return res.redirect('/') + + key = `prefs_key:${key}` + redis.get(key, (error, json) => { + if(error) { + console.error(`Error getting the preferences import key ${key} from redis.`, error) + return res.render('index', { json: null, user_preferences: req.cookies }) + } + if(json) { + try { + let prefs = JSON.parse(json) + let subbed_subreddits_is_set = false + for(var setting in prefs) { + if(prefs.hasOwnProperty(setting)) { + res.cookie(setting, prefs[setting], { maxAge: 365 * 24 * 60 * 60 * 1000, httpOnly: true }) + if(setting === 'subbed_subreddits') + subbed_subreddits_is_set = true + } + } + if(!subbed_subreddits_is_set) + res.clearCookie('subbed_subreddits') + return res.redirect('/preferences') + } catch(e) { + console.error(`Error setting imported preferences to the cookies. Key: ${key}.`, error) + } + } else { + return res.redirect('/preferences') + } + }) + }) app.get('/privacy', (req, res, next) => { return res.render('privacypolicy', { user_preferences: req.cookies }) @@ -1224,6 +1260,19 @@ module.exports = (app, redis, fetch, RedditAPI) => { return res.redirect('/preferences') }) + + app.post('/export_prefs', (req, res, next) => { + let r = `${(Math.random().toString(36)+'00000000000000000').slice(2, 10+2).toUpperCase()}` + let key = `prefs_key:${r}` + redis.set(key, JSON.stringify(req.cookies), (error) => { + if(error) { + console.error(`Error saving preferences to redis.`, error) + return res.redirect('/preferences') + } else { + return res.render('preferences', { user_preferences: req.cookies, instance_config: config, preferences_key: r }) + } + }) + }) app.post('/r/:subreddit/comments/:id/:snippet', (req, res, next) => { /* morechildren route */ diff --git a/static/css/styles.css b/static/css/styles.css index 158cc43..ca23764 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -150,6 +150,12 @@ form .setting { margin: 10px 0; width: 100%; } +#export-form { + margin: 60px 0px 0px 0px; +} +#export-form input { + margin: 10px 0px 10px 0px; +} .container .content small.notice { padding-top: 20px; padding-bottom: 5px; diff --git a/views/preferences.pug b/views/preferences.pug index 4d1169a..1ec6fe2 100644 --- a/views/preferences.pug +++ b/views/preferences.pug @@ -67,4 +67,27 @@ html 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 + form(action="/export_prefs", method="POST", id="export-form") + if preferences_key + details(open) + summary + span Export preferences + .setting + small By exporting your preferences you can transfer your subscribed subreddits and preferences to another device. Or you could create a bookmark if you tend to delete your cookies frequently. + br + input(type="submit", value="Export preferences") + if preferences_key + - var protocol = 'http' + if instance_config.https_enabled === true + - var protocol = 'https' + p Visit this URL to import your preferences: + a(href=`${protocol}://${instance_config.domain}/import_prefs/${preferences_key}`) #{protocol}://#{instance_config.domain}/import_prefs/#{preferences_key} + else + details + summary + span Export preferences + .setting + small By exporting your preferences you can transfer your subscribed subreddits and preferences to another device. Or you could create a bookmark if you tend to delete your cookies frequently. + br + input(type="submit", value="Export preferences") include includes/footer.pug From f27b2e2c50d5fb48eebcc6ca5c3bdeb131c7983c Mon Sep 17 00:00:00 2001 From: teddit Date: Sat, 30 Jan 2021 22:25:20 +0100 Subject: [PATCH 13/13] we now have sepia, so technically we can remove this. themes are still warmly welcome! --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ce15d1f..333439d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,6 @@ Community instances: ## Roadmap -* More themes, not just white or dark * HLS video streaming? (Would require browser JavaScript) * Onion site * User login, so people can use their Reddit account through teddit to comment and up/downvote posts etc.