separate subscription routes

This commit is contained in:
json 2021-09-02 20:24:22 +01:00
parent fe19ec0ba6
commit 2df4f3420f
2 changed files with 84 additions and 0 deletions

View File

@ -2,6 +2,7 @@ const overridingRoutes = require('./overides');
const preferenceRoutes = require('./preferences'); const preferenceRoutes = require('./preferences');
const staticRoutes = require('./static'); const staticRoutes = require('./static');
const subredditRoutes = require('./subreddit'); const subredditRoutes = require('./subreddit');
const subscriptionRoutes = require('./subscription');
const userRoutes = require('./user'); const userRoutes = require('./user');
const allRoutes = require('express').Router(); const allRoutes = require('express').Router();
@ -11,5 +12,6 @@ allRoutes.use(staticRoutes);
allRoutes.use(preferenceRoutes); allRoutes.use(preferenceRoutes);
allRoutes.use(subredditRoutes); allRoutes.use(subredditRoutes);
allRoutes.use(userRoutes); allRoutes.use(userRoutes);
allRoutes.use(subscriptionRoutes);
module.exports = allRoutes; module.exports = allRoutes;

82
routes/subscription.js Normal file
View File

@ -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;