teddit/inc/processSearchResults.js

63 lines
1.8 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');
2021-05-24 21:18:50 +02:00
const link = require('./components/link')
2021-01-01 18:18:54 +01:00
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
if(post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false')
continue
2021-05-24 21:18:50 +02:00
let obj = await link.fromJson(post, user_preferences)
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)
})()
})
}
}