From fff9c2d5b98c532341acb5dcf0bdd1ef229e80fb Mon Sep 17 00:00:00 2001 From: teddit Date: Sat, 16 Jan 2021 21:14:12 +0100 Subject: [PATCH] convert reddit.com links to instance domain --- config.js.template | 6 ++++++ inc/commons.js | 24 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/config.js.template b/config.js.template index be8cac8..59a007d 100644 --- a/config.js.template +++ b/config.js.template @@ -40,6 +40,12 @@ const config = { shorts: 60 * 60 * 24 * 31, wikis: 60* 60 * 24 * 7 }, + convert_urls: { + /** + * When enabled, all the domains in the content (e.g. comments, sidebars, wikis etc.) will be replaced with a privacy respecting version. + */ + reddit: true, // old.reddit.com and reddit.com --> instance domain (config.domain) + }, 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.` }; diff --git a/inc/commons.js b/inc/commons.js index c532d2f..3e88ece 100644 --- a/inc/commons.js +++ b/inc/commons.js @@ -125,12 +125,15 @@ module.exports = function(request, fs) { } this.toUTCString = (time) => { - let d = new Date(); - d.setTime(time*1000); - return d.toUTCString(); + let d = new Date() + d.setTime(time*1000) + return d.toUTCString() } this.unescape = (s) => { + /* It would make much more sense to rename this function to something + * like "formatter". + */ if(s) { var re = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; var unescaped = { @@ -145,9 +148,18 @@ module.exports = function(request, fs) { '"': '"', '"': '"' } - return s.replace(re, (m) => { - return unescaped[m] - }) + if(config.convert_urls.reddit) { + let str = s.replace(re, (m) => { + return unescaped[m] + }) + let r = new RegExp('((www|old)\.)?reddit.com', 'g') + let result = str.replace(r, config.domain) + return result + } else { + return s.replace(re, (m) => { + return unescaped[m] + }) + } } else { return '' }