teddit/inc/processSearchResults.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-11-17 21:44:32 +01:00
module.exports = function() {
2021-01-01 18:18:54 +01:00
const config = require('../config');
this.processSearchResults = (json, parsed, after, before, user_preferences) => {
2020-11-17 21:44:32 +01:00
return new Promise(resolve => {
(async () => {
if(!parsed) {
json = JSON.parse(json)
}
let posts = []
let search_firstpage = false
let before = json.data.before
let after = json.data.after
if(!after && !before) {
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
}
}
}
2020-11-17 21:44:32 +01:00
if(json.data.children) {
let view_more_posts = false
let posts_limit = 25
if(json.data.children.length > posts_limit) {
view_more_posts = true
} else {
posts_limit = json.data.children.length
}
for(var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data
2021-01-01 18:18:54 +01:00
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_')
}
}
}
}
2020-11-17 21:44:32 +01:00
let obj = {
subreddit: post.subreddit,
title: post.title,
created: post.created_utc,
2021-01-01 18:18:54 +01:00
domain: post.domain,
2020-11-17 21:44:32 +01:00
subreddit_name_prefixed: post.subreddit_name_prefixed,
2021-01-01 18:18:54 +01:00
link_flair_text: post.link_flair_text,
2020-11-17 21:44:32 +01:00
ups: post.ups,
2021-01-01 18:18:54 +01:00
images: images,
url: replaceDomains(post.url, user_preferences),
2020-11-17 21:44:32 +01:00
edited: post.edited,
selftext_html: unescape(post.body_html),
num_comments: post.num_comments,
2021-01-01 18:18:54 +01:00
over_18: post.over_18,
2020-11-17 21:44:32 +01:00
permalink: post.permalink,
2021-01-01 18:18:54 +01:00
is_self_link: is_self_link,
2020-11-17 21:44:32 +01:00
author: post.author,
2021-01-01 18:18:54 +01:00
link_title: post.link_title,
link_flair: (user_preferences.flairs != 'false' ? await formatLinkFlair(post) : ''),
user_flair: (user_preferences.flairs != 'false' ? await formatUserFlair(post) : '')
2020-11-17 21:44:32 +01:00
}
posts.push(obj)
}
}
let obj = {
search_firstpage: search_firstpage,
before: before,
after: after,
posts: posts,
suggested_subreddits: suggested_subreddits,
2020-11-17 21:44:32 +01:00
}
resolve(obj)
})()
})
}
}