From 6a203f61920418fbdd38c03a6c1d0199117d4fbe Mon Sep 17 00:00:00 2001 From: teddit Date: Mon, 26 Apr 2021 18:34:56 +0200 Subject: [PATCH] teddify reddit URLs after the URL #193 --- routes.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/routes.js b/routes.js index 8eca712..db6ae36 100644 --- a/routes.js +++ b/routes.js @@ -82,6 +82,22 @@ module.exports = (app, redis, fetch, RedditAPI) => { res.cookie('domain_instagram', domainInstagram, { maxAge: 31536000, httpOnly: true }) } + const valid_reddit_starts = ['/https://old.reddit.com', '/https://reddit.com', '/https://www.reddit.com', '/old.reddit.com', '/reddit.com', '/www.reddit.com'] + for(var i = 0; i < valid_reddit_starts.length; i++) { + if(req.url.startsWith(valid_reddit_starts[i])) { + req.url = req.url.substring(1) + const redditRegex = /([A-z.]+\.)?(reddit(\.com))/gm; + let teddified_url = req.url.replace(redditRegex, '') + if(teddified_url.includes('://')) { + teddified_url = teddified_url.split('://')[1] + } + if(teddified_url == '') { + teddified_url = '/' + } + return res.redirect(teddified_url) + } + } + if(!config.rate_limiting) { return next() }