add sorting for search

This commit is contained in:
teddit 2020-11-19 20:53:29 +01:00
parent a96bbc25dd
commit 7c2446c4b3
2 changed files with 120 additions and 24 deletions

View File

@ -83,6 +83,34 @@ module.exports = (app, redis, fetch, RedditAPI) => {
app.get('/preferences', (req, res, next) => { app.get('/preferences', (req, res, next) => {
return res.render('preferences') return res.render('preferences')
}) })
app.get('/search', (req, res, next) => {
let q = req.query.q
let restrict_sr = req.query.restrict_sr
let nsfw = req.query.nsfw
let sortby = req.query.sort
let past = req.query.t
let after = req.query.after
let before = req.query.before
if(!after) {
after = ''
}
if(!before) {
before = ''
}
if(restrict_sr !== 'on') {
restrict_sr = 'off'
}
if(nsfw !== 'on') {
nsfw = 'off'
}
let d = `&after=${after}`
if(before) {
d = `&before=${before}`
}
return res.redirect(`/r/all/search?q=${q}&restrict_sr=${restrict_sr}&nsfw=${nsfw}&sort=${sortby}&t=${past}${d}`)
})
app.get('/:sort', (req, res, next) => { app.get('/:sort', (req, res, next) => {
let sortby = req.params.sort let sortby = req.params.sort
@ -169,15 +197,13 @@ module.exports = (app, redis, fetch, RedditAPI) => {
}) })
}) })
app.get('/r/:subreddit/search', (req, res, next) => { app.get('/r/:subreddit/search', (req, res, next) => {
let subreddit = req.params.subreddit let subreddit = req.params.subreddit
let q = req.query.q let q = req.query.q
let restrict_sr = req.query.restrict_sr let restrict_sr = req.query.restrict_sr
let nsfw = req.query.nsfw let nsfw = req.query.nsfw
let sortby = 'relevance' let sortby = req.query.sort
let past = 'all' let past = req.query.t
let after = req.query.after let after = req.query.after
let before = req.query.before let before = req.query.before
if(!after) { if(!after) {

View File

@ -20,28 +20,98 @@ html
input(type="checkbox", name="nsfw", id="nsfw", checked="checked") input(type="checkbox", name="nsfw", id="nsfw", checked="checked")
else else
input(type="checkbox", name="nsfw", id="nsfw") input(type="checkbox", name="nsfw", id="nsfw")
div
//- Let me know if there's a better way to add selected attribute!
label(for="sort") sorted by:
select(name="sort", id="sort")
if sortby === 'relevance' || !sortby
option(value="relevance", selected="selected") relevance
option(value="top") top
option(value="new") new
option(value="comments") comments
if sortby === 'top'
option(value="relevance") relevance
option(value="top", selected="selected") top
option(value="new") new
option(value="comments") comments
if sortby === 'new'
option(value="relevance") relevance
option(value="top") top
option(value="new", selected="selected") new
option(value="comments") comments
if sortby === 'comments'
option(value="relevance") relevance
option(value="top") top
option(value="new") new
option(value="comments", selected="selected") comments
div
//- Let me know if there's a better way to add selected attribute!
label(for="t") links from:
select(name="t", id="t")
if past === 'hour'
option(value="hour", selected="selected") hour
option(value="day") 24 hours
option(value="week") week
option(value="month") month
option(value="year") year
option(value="all") all time
if past === 'day'
option(value="hour") hour
option(value="day", selected="selected") 24 hours
option(value="week") week
option(value="month") month
option(value="year") year
option(value="all") all time
if past === 'week'
option(value="hour") hour
option(value="day") 24 hours
option(value="week", selected="selected") week
option(value="month") month
option(value="year") year
option(value="all") all time
if past === 'month'
option(value="hour") hour
option(value="day") 24 hours
option(value="week") week
option(value="month", selected="selected") month
option(value="year") year
option(value="all") all time
if past === 'year'
option(value="hour") hour
option(value="day") 24 hours
option(value="week") week
option(value="month") month
option(value="year", selected="selected") year
option(value="all") all time
if past === 'all' || !past
option(value="hour") hour
option(value="day") 24 hours
option(value="week") week
option(value="month") month
option(value="year") year
option(value="all", selected="selected") all time
input(type="submit", value="search") input(type="submit", value="search")
small sort by: #{sortby}
br
small past: #{past}
#links.search #links.search
each post in json.posts if json.posts.length <= 0
.link h1 no results
.upvotes else
div.arrow each post in json.posts
span #{kFormatter(post.ups)} .link
div.arrow.down .upvotes
.image div.arrow
a(href="" + post.permalink + "") span #{kFormatter(post.ups)}
div.no-image div.arrow.down
.title .image
a(href="" + post.permalink + "") #{post.title} a(href="" + post.permalink + "")
.meta div.no-image
p.submitted(title="" + toUTCString(post.created) + "") submitted #{timeDifference(post.created)} by .title
a(href="/u/" + post.author + "") #{post.author} a(href="" + post.permalink + "") #{post.title}
| to .meta
a(href="/r/" + post.subreddit + "", class="subreddit") r/#{post.subreddit} p.submitted(title="" + toUTCString(post.created) + "") submitted #{timeDifference(post.created)} by
a.comments(href="" + post.permalink + "") #{post.num_comments} comments a(href="/u/" + post.author + "") #{post.author}
| to
a(href="/r/" + post.subreddit + "", class="subreddit") r/#{post.subreddit}
a.comments(href="" + post.permalink + "") #{post.num_comments} comments
if json.before || json.after if json.before || json.after
p view more: p view more:
if json.before && !json.search_firstpage if json.before && !json.search_firstpage