add suggested subreddits for search pages (ref #104)

This commit is contained in:
teddit 2021-04-22 20:44:23 +02:00
parent 6bcb181cb1
commit 80b67fa3ab
4 changed files with 98 additions and 22 deletions

View File

@ -15,6 +15,15 @@ module.exports = function() {
search_firstpage = true search_firstpage = true
} }
let suggested_subreddits = false
if(json.suggested_subreddits) {
if(json.suggested_subreddits.data) {
if(json.suggested_subreddits.data.children.length > 0) {
suggested_subreddits = json.suggested_subreddits.data.children
}
}
}
if(json.data.children) { if(json.data.children) {
let view_more_posts = false let view_more_posts = false
let posts_limit = 25 let posts_limit = 25
@ -90,7 +99,8 @@ module.exports = function() {
search_firstpage: search_firstpage, search_firstpage: search_firstpage,
before: before, before: before,
after: after, after: after,
posts: posts posts: posts,
suggested_subreddits: suggested_subreddits,
} }
resolve(obj) resolve(obj)

View File

@ -916,28 +916,43 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(result.status === 200) { if(result.status === 200) {
result.json() result.json()
.then(json => { .then(json => {
redis.setex(key, config.setexs.searches, JSON.stringify(json), (error) => { (async () => {
if(error) { /**
console.error('Error setting the searches key to redis.', error) * Fetch suggested subreddits when the restrict_sr option is
return res.render('index', { json: null, user_preferences: req.cookies }) * turned off ("limit my search to") and we are on the first search
} else { * page (just like in Reddit).
console.log('Fetched search results from Reddit.'); */
(async () => { json.suggested_subreddits = {}
let processed_json = await processSearchResults(json, true, after, before, req.cookies) if(restrict_sr === 'off' && before == '' && after == '') {
return res.render('search', { let url = `https://reddit.com/subreddits/search.json?q=${q}&include_over_18=${nsfw}&limit=3`
no_query: false, const response = await fetch(url)
json: processed_json, const data = await response.json()
q: q, json.suggested_subreddits = data
restrict_sr: restrict_sr,
nsfw: nsfw,
subreddit: subreddit,
sortby: sortby,
past: past,
user_preferences: req.cookies
})
})()
} }
})
redis.setex(key, config.setexs.searches, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the searches key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched search results from Reddit.');
(async () => {
let processed_json = await processSearchResults(json, true, after, before, req.cookies)
return res.render('search', {
no_query: false,
json: processed_json,
q: q,
restrict_sr: restrict_sr,
nsfw: nsfw,
subreddit: subreddit,
sortby: sortby,
past: past,
user_preferences: req.cookies
})
})()
}
})
})()
}) })
} else { } else {
console.error(`Something went wrong while fetching data from Reddit. ${result.status} ${result.statusText}`) console.error(`Something went wrong while fetching data from Reddit. ${result.status} ${result.statusText}`)

View File

@ -1228,6 +1228,23 @@ a.sub-to-subreddit.gray {
border-radius: 0; border-radius: 0;
margin-bottom: 11px; margin-bottom: 11px;
} }
/* SUGGESTED SUBREDDITS ON SEARCH PAGES */
.suggested-subreddits {
margin: 0px 0px 30px 1%;
}
.suggested-subreddits h3 {
border-bottom: 1px solid #7d7d7d;
max-width: 820px;
margin: 0px 0px 16px 10px;
padding: 0px 0px 5px 0px;
font-size: 0.9rem;
}
.suggested-subreddits .sub-button {
margin: 0px 0px 7px 0px;
}
.suggested-subreddits .description {
font-size: 0.8rem;
}
/* REDDIT STYLES */ /* REDDIT STYLES */
.md .md-spoiler-text { .md .md-spoiler-text {
border-radius:2px; border-radius:2px;

View File

@ -100,6 +100,40 @@ html
if json.posts.length === 0 && !no_query if json.posts.length === 0 && !no_query
p no results p no results
else else
if json.suggested_subreddits
.suggested-subreddits
h3 suggested subreddits
each suggested_subreddit in json.suggested_subreddits
.link
.entry
-
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].toLowerCase() === suggested_subreddit.data.display_name.toLowerCase())
subbed_to_this_subreddit = true
}
.content
.title
a(href="" + suggested_subreddit.data.url + "", rel="noopener noreferrer")
h2 #{suggested_subreddit.data.display_name_prefixed}: #{cleanTitle(suggested_subreddit.data.title)}
.sub-button
if subbed_to_this_subreddit
a(href="/unsubscribe/" + suggested_subreddit.data.display_name + "?b=/r/" + subreddit + "?q=" + q + "§nsfw=" + nsfw + "", class="sub-to-subreddit gray", title="subscriptions are saved in your browser's cookies") unsubscribe
else
a(href="/subscribe/" + suggested_subreddit.data.display_name + "?b=/r/" + subreddit + "?q=" + q + "§nsfw=" + nsfw + "", class="sub-to-subreddit", title="subscriptions are saved in your browser's cookies") subscribe
.description
p #{cleanTitle(suggested_subreddit.data.public_description)}
.meta
p.subscribers #{kFormatter(suggested_subreddit.data.subscribers)} subscribers,
p.submitted &nbsp; created
span(title="" + toUTCString(suggested_subreddit.data.created) + "") #{timeDifference(suggested_subreddit.data.created)}
.links
if suggested_subreddit.data.over_18
span.tag.nsfw NSFW
a(href="/subreddits/search?q="+ q +"", class="btn") show more similar subreddits
each link in json.posts each link in json.posts
.link .link
.upvotes .upvotes