fix search results, fix #65

This commit is contained in:
teddit 2021-01-01 18:18:54 +01:00
parent 0c071f475f
commit d9e616a469
4 changed files with 99 additions and 28 deletions

View File

@ -1,5 +1,6 @@
module.exports = function() {
this.processSearchResults = function(json, parsed, after, before) {
const config = require('../config');
this.processSearchResults = (json, parsed, after, before, user_preferences) => {
return new Promise(resolve => {
(async () => {
if(!parsed) {
@ -24,24 +25,68 @@ module.exports = function() {
posts_limit = json.data.children.length
}
for(var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data
let post_id = post.permalink.split('/').slice(-2)[0] + '/'
let url = post.permalink.replace(post_id, '')
let images = null
let is_self_link = false
let valid_reddit_self_domains = ['reddit.com']
if(post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
if(post.domain) {
let tld = post.domain.split('self.')
if(tld.length > 1) {
if(!tld[1].includes('.')) {
is_self_link = true
}
}
if(config.valid_media_domains.includes(post.domain) || valid_reddit_self_domains.includes(post.domain)) {
is_self_link = true
}
}
if(post.preview && post.thumbnail !== 'self') {
if(!post.url.startsWith('/r/') && isGif(post.url)) {
images = {
thumb: await downloadAndSave(post.thumbnail, 'thumb_')
}
} else {
if(post.preview.images[0].resolutions[0]) {
images = {
thumb: await downloadAndSave(post.preview.images[0].resolutions[0].url, 'thumb_')
}
}
}
}
let obj = {
subreddit: post.subreddit,
title: post.title,
created: post.created_utc,
domain: post.domain,
subreddit_name_prefixed: post.subreddit_name_prefixed,
link_flair_text: post.link_flair_text,
ups: post.ups,
images: images,
url: url,
edited: post.edited,
selftext_html: unescape(post.body_html),
num_comments: post.num_comments,
over_18: post.over_18,
permalink: post.permalink,
is_self_link: is_self_link,
author: post.author,
link_title: post.link_title
link_title: post.link_title,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
}
posts.push(obj)
}

View File

@ -284,7 +284,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
if(json) {
console.log('Got search key from redis.');
(async () => {
let processed_json = await processSearchResults(json, false, after, before)
let processed_json = await processSearchResults(json, false, after, before, req.cookies)
return res.render('search', {
json: processed_json,
q: q,
@ -314,7 +314,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} else {
console.log('Fetched search results from Reddit.');
(async () => {
let processed_json = await processSearchResults(json, true, after, before)
let processed_json = await processSearchResults(json, true, after, before, req.cookies)
return res.render('search', {
json: processed_json,
q: q,

View File

@ -1144,7 +1144,7 @@ body.dark .flair {
margin-top: 0px;
}
#search.sr.search-page {
width: 100%;
width: auto;
margin-left: 20px;
margin-top: 40px;
}

View File

@ -94,35 +94,61 @@ html
option(value="all", selected="selected") all time
input(type="submit", value="search")
#links.search
if json.posts.length <= 0
h1 no results
if json.posts.length === 0
p no results
else
each post in json.posts
each link in json.posts
.link
.upvotes
.arrow
span #{kFormatter(post.ups)}
span #{kFormatter(link.ups)}
.arrow.down
.image
a(href="" + post.permalink + "", rel="noopener noreferrer")
.no-image no image
.title
a(href="" + post.permalink + "", rel="noopener noreferrer") #{cleanTitle(post.title)}
.meta
p.submitted(title="" + toUTCString(post.created) + "") submitted #{timeDifference(post.created)} by
//- I believe finding posts by deleted authors is impossible
if post.author === '[deleted]'
span [deleted]
if link.images
if link.is_self_link
a(href="" + link.permalink + "")
img(src="" + link.images.thumb + "", alt="")
else
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
a(href=""+ link.url +"", rel="noopener noreferrer")
img(src="" + link.images.thumb + "", alt="")
else
a(href="" + link.permalink + "")
.no-image no image
.entry
.title
if link.is_self_link
a(href="" + link.permalink + "")
h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)}
!= link.link_flair
span (#{link.domain})
else
a(href="" + link.url + "", rel="noopener noreferrer")
h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)}
!= link.link_flair
span (#{link.domain})
.meta
p.submitted submitted
span(title="" + toUTCString(link.created) + "") #{timeDifference(link.created)} by
if link.author === '[deleted]'
span(class="deleted") [deleted]
else
a(href="/u/" + link.author + "")
| #{link.author}
!= link.user_flair
p.to to
a(href="/r/" + link.subreddit + "")
| #{link.subreddit}
if link.stickied
span(class="green") stickied
.links
if link.over_18
span.tag.nsfw NSFW
a(href="" + link.permalink + "", class="comments")
| #{link.num_comments} comments
if json.before || json.after
p view more:
if json.before && !json.search_firstpage
a(href="?q=" + q + "&restrict_sr=" + restrict_sr + "&before=" + json.before + "") prev
if json.after
a(href="?q=" + q + "&restrict_sr=" + restrict_sr + "&after=" + json.after + "") next
.view-more-inks
if json.before && !subreddit_front
a(href="?q=" + q + "&restrict_sr=" + restrict_sr + "&before=" + json.before + "") prev
if json.after
a(href="?q=" + q + "&restrict_sr=" + restrict_sr + "&after=" + json.after + "") next
include includes/footer.pug