add subscribe to subreddit feature

This commit is contained in:
teddit 2021-01-08 21:39:46 +01:00
parent ace5b2587c
commit f368b40dda
6 changed files with 227 additions and 59 deletions

View File

@ -29,6 +29,7 @@ Community instances:
## TODO ## TODO
* Import/export preferences
* User trophies * User trophies
* "other discussions" feature * "other discussions" feature
* "Open on reddit" links * "Open on reddit" links

View File

@ -23,7 +23,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
res.clearCookie('theme') res.clearCookie('theme')
res.clearCookie('flairs') res.clearCookie('flairs')
res.clearCookie('nsfw_enabled') res.clearCookie('nsfw_enabled')
res.clearCookie("highlight_controversial") res.clearCookie('highlight_controversial')
res.clearCookie('subbed_subreddits')
return res.redirect('/preferences') return res.redirect('/preferences')
}) })
@ -31,6 +32,84 @@ module.exports = (app, redis, fetch, RedditAPI) => {
return res.render('privacypolicy', { user_preferences: req.cookies }) return res.render('privacypolicy', { user_preferences: req.cookies })
}) })
app.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, '+')
return res.redirect(back)
}
})
app.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)
}
})
app.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, '+')
return res.redirect(back)
}
})
app.get('/search', (req, res, next) => { app.get('/search', (req, res, next) => {
let q = req.query.q let q = req.query.q
@ -118,6 +197,15 @@ module.exports = (app, redis, fetch, RedditAPI) => {
api_req = false api_req = false
let key = `/after:${after}:before:${before}:sort:${sortby}:past:${past}` let key = `/after:${after}:before:${before}:sort:${sortby}:past:${past}`
let subbed_subreddits = req.cookies.subbed_subreddits
let get_subbed_subreddits = false
if(subbed_subreddits && Array.isArray(subbed_subreddits)) {
get_subbed_subreddits = true
subbed_subreddits = subbed_subreddits.join('+')
key = `${subbed_subreddits.toLowerCase()}:${after}:${before}:sort:${sortby}:past:${past}`
}
redis.get(key, (error, json) => { redis.get(key, (error, json) => {
if(error) { if(error) {
console.error('Error getting the frontpage key from redis.', error) console.error('Error getting the frontpage key from redis.', error)
@ -140,10 +228,17 @@ module.exports = (app, redis, fetch, RedditAPI) => {
})() })()
} else { } else {
let url = '' let url = ''
if(config.use_reddit_oauth) if(config.use_reddit_oauth) {
if(get_subbed_subreddits)
url = `https://oauth.reddit.com/r/${subbed_subreddits}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}`
else
url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}` url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}`
} else {
if(get_subbed_subreddits)
url = `https://reddit.com/r/${subbed_subreddits}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}`
else else
url = `https://reddit.com/${sortby}.json?g=GLOBAL&t=${past}${d}` url = `https://reddit.com/${sortby}.json?g=GLOBAL&t=${past}${d}`
}
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then(result => {
if(result.status === 200) { if(result.status === 200) {

View File

@ -311,6 +311,11 @@ form legend {
border-bottom: 1px solid #e3e3e3; border-bottom: 1px solid #e3e3e3;
margin-bottom: 10px; margin-bottom: 10px;
padding-bottom: 10px; padding-bottom: 10px;
margin-top: 40px;
font-weight: bold;
}
form legend:first-child {
margin-top: 0px;
} }
form .setting { form .setting {
margin: 10px 0px; margin: 10px 0px;
@ -398,10 +403,6 @@ header .tabmenu li.active a {
border-radius: 3px; border-radius: 3px;
font-weight: bold; font-weight: bold;
} }
.subreddit-listing {
margin-left: 15px;
margin-top: 8px;
}
.green { .green {
color: green !important; color: green !important;
} }
@ -1157,9 +1158,35 @@ body.dark .flair {
font-size: smaller; font-size: smaller;
padding-right: 15px; padding-right: 15px;
} }
.subreddit-listing {
margin: 8px 0px;
list-style: none;
}
.subreddit-listing li {
margin: 15px 0px;
}
#sidebar .content .description { #sidebar .content .description {
margin-top: 38px; margin-top: 38px;
} }
a.sub-to-subreddit {
color: #f9f9f9;
background: #007900;
font-size: var(--sm-font);
padding: 6px 8px 6px 8px;
margin: 0px 5px 0px 0px;
}
a.sub-to-subreddit:hover,
a.sub-to-subreddit:focus {
color: white !important;
}
a.sub-to-subreddit.gray {
background: gray;
}
.subscribe {
margin: 0px 0px 30px 0px;
width: 100%;
float: left;
}
/* SEARCH */ /* SEARCH */
#search { #search {
margin-bottom: 50px; margin-bottom: 50px;

View File

@ -8,6 +8,10 @@ div#topbar
.icon-container .icon-container
a(href="/preferences") [preferences] a(href="/preferences") [preferences]
.top-links .top-links
if user_preferences.subbed_subreddits && Array.isArray(user_preferences.subbed_subreddits)
each subreddit in user_preferences.subbed_subreddits
a(href="/r/" + subreddit) #{subreddit}
else
a(href="/r/all") All a(href="/r/all") All
a(href="/r/AskReddit") AskReddit a(href="/r/AskReddit") AskReddit
a(href="/r/pics") pics a(href="/r/pics") pics

View File

@ -37,6 +37,19 @@ html
input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled") input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled")
else else
input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled", checked="checked") input(type="checkbox", name="nsfw_enabled", id="nsfw_enabled", checked="checked")
legend Subscribed subreddits
.setting
details
summary
span Show subscribed subreddits
if user_preferences.subbed_subreddits && Array.isArray(user_preferences.subbed_subreddits)
ul.subreddit-listing
each subreddit in user_preferences.subbed_subreddits
li
a(href="/unsubscribe/" + subreddit + "/?b=/preferences", class="sub-to-subreddit gray", title="subscriptions are saved in your browser's cookies") unsubscribe
a(href="/r/" + subreddit) #{subreddit}
else
small no subscribed subreddits
small(class="notice") Preferences are stored client-side using cookies without any personal information. small(class="notice") Preferences are stored client-side using cookies without any personal information.
input(type="submit", value="Save preferences") input(type="submit", value="Save preferences")
a(href="/resetprefs", class="btn") Reset preferences a(href="/resetprefs", class="btn") Reset preferences

View File

@ -150,6 +150,20 @@ html
input(type="checkbox", name="nsfw", id="nsfw", checked="checked") input(type="checkbox", name="nsfw", id="nsfw", checked="checked")
input(type="submit", value="search") input(type="submit", value="search")
if subreddit_about if subreddit_about
.subscribe
-
let subbed_to_this_subreddit = false
let subbed = []
if(user_preferences.subbed_subreddits && Array.isArray(user_preferences.subbed_subreddits))
subbed = user_preferences.subbed_subreddits
for(let i = 0; i < subbed.length; i++) {
if(subbed[i] === subreddit)
subbed_to_this_subreddit = true
}
if subbed_to_this_subreddit
a(href="/unsubscribe/" + subreddit + "", class="sub-to-subreddit gray", title="subscriptions are saved in your browser's cookies") unsubscribe
else
a(href="/subscribe/" + subreddit + "", class="sub-to-subreddit", title="subscriptions are saved in your browser's cookies") subscribe
if subreddit_about.subscribers if subreddit_about.subscribers
.content .content
p subscribers: #{subreddit_about.subscribers.toLocaleString()} p subscribers: #{subreddit_about.subscribers.toLocaleString()}
@ -169,6 +183,20 @@ html
let subreddits = subreddit.split('+') let subreddits = subreddit.split('+')
ul(class="subreddit-listing") ul(class="subreddit-listing")
each subreddit in subreddits each subreddit in subreddits
-
let subbed_to_this_subreddit = false
let subbed = user_preferences.subbed_subreddits
for(let i = 0; i < subbed.length; i++) {
if(subbed[i] === subreddit)
subbed_to_this_subreddit = true
}
li li
if subbed_to_this_subreddit
a(href="/unsubscribe/" + subreddit + "?b=/r/" + subreddits + "", class="sub-to-subreddit gray", title="subscriptions are saved in your browser's cookies") unsubscribe
a(href="/r/" + subreddit + "") #{subreddit} a(href="/r/" + subreddit + "") #{subreddit}
else
a(href="/subscribe/" + subreddit + "?b=/r/" + subreddits + "", class="sub-to-subreddit", title="subscriptions are saved in your browser's cookies") subscribe
a(href="/r/" + subreddit + "") #{subreddit}
- joined_subreddits = subreddits.join("+")
a(href="/import_subscriptions/" + joined_subreddits + "?b=/r/" + joined_subreddits) subscribe to all of these subreddits
include includes/footer.pug include includes/footer.pug