From 2df4f3420ffa3d89841dc128a13949c723bd9583 Mon Sep 17 00:00:00 2001 From: json Date: Thu, 2 Sep 2021 20:24:22 +0100 Subject: [PATCH] separate subscription routes --- routes/index.js | 2 ++ routes/subscription.js | 82 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 routes/subscription.js diff --git a/routes/index.js b/routes/index.js index 5bb72ef..1733d0b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,6 +2,7 @@ const overridingRoutes = require('./overides'); const preferenceRoutes = require('./preferences'); const staticRoutes = require('./static'); const subredditRoutes = require('./subreddit'); +const subscriptionRoutes = require('./subscription'); const userRoutes = require('./user'); const allRoutes = require('express').Router(); @@ -11,5 +12,6 @@ allRoutes.use(staticRoutes); allRoutes.use(preferenceRoutes); allRoutes.use(subredditRoutes); allRoutes.use(userRoutes); +allRoutes.use(subscriptionRoutes); module.exports = allRoutes; diff --git a/routes/subscription.js b/routes/subscription.js new file mode 100644 index 0000000..deb8bf1 --- /dev/null +++ b/routes/subscription.js @@ -0,0 +1,82 @@ +const subscriptionRoutes = require('express').Router(); + +subscriptionRoutes.get('/subscribe/:subreddit', (req, res, next) => { + let subreddit = req.params.subreddit; + let subbed = req.cookies.subbed_subreddits; + let back = req.query.b; + + if (!subreddit) return res.redirect('/'); + + if (!subbed || !Array.isArray(subbed)) subbed = []; + + if (!subbed.includes(subreddit)) subbed.push(subreddit); + + res.cookie('subbed_subreddits', subbed, { + maxAge: 365 * 24 * 60 * 60 * 1000, + httpOnly: true, + }); + + if (!back) return res.redirect('/r/' + subreddit); + else { + back = back.replace(/,/g, '+').replace(/§1/g, '&'); + return res.redirect(back); + } +}); + +subscriptionRoutes.get( + '/import_subscriptions/:subreddits', + (req, res, next) => { + let subreddits = req.params.subreddits; + let subbed = req.cookies.subbed_subreddits; + let back = req.query.b; + + if (!subreddits) return res.redirect('/'); + + if (!subbed || !Array.isArray(subbed)) subbed = []; + + subreddits = subreddits.split('+'); + for (var i = 0; i < subreddits.length; i++) { + if (!subbed.includes(subreddits[i])) subbed.push(subreddits[i]); + } + + res.cookie('subbed_subreddits', subbed, { + maxAge: 365 * 24 * 60 * 60 * 1000, + httpOnly: true, + }); + + if (!back) return res.redirect('/r/' + subreddits); + else { + back = back.replace(/,/g, '+').replace(/ /g, '+'); + return res.redirect(back); + } + } +); + +subscriptionRoutes.get('/unsubscribe/:subreddit', (req, res, next) => { + let subreddit = req.params.subreddit; + let subbed = req.cookies.subbed_subreddits; + let back = req.query.b; + + if (!subreddit || !subbed || !Array.isArray(subbed)) { + res.clearCookie('subbed_subreddits'); + return res.redirect('/'); + } + + var index = subbed.indexOf(subreddit); + if (index !== -1) subbed.splice(index, 1); + + if (subbed.length <= 0) res.clearCookie('subbed_subreddits'); + else + res.cookie('subbed_subreddits', subbed, { + maxAge: 365 * 24 * 60 * 60 * 1000, + httpOnly: true, + }); + + if (!back) return res.redirect('/r/' + subreddit); + else { + back = back.replace(/,/g, '+').replace(/§1/g, '&'); + return res.redirect(back); + } +}); + +module.exports = subscriptionRoutes;