From 153a50e60afa663576f6995bd1c7c0c92c011747 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 11:47:25 +0100 Subject: [PATCH] convert processSearchResults to async function --- inc/processSearchResults.js | 126 +++++++++++++++++++----------------- routes/subreddit.js | 2 +- 2 files changed, 68 insertions(+), 60 deletions(-) diff --git a/inc/processSearchResults.js b/inc/processSearchResults.js index aa97b9d..c968d59 100644 --- a/inc/processSearchResults.js +++ b/inc/processSearchResults.js @@ -1,62 +1,70 @@ -module.exports = function() { - const config = require('../config'); - const link = require('./components/link') - this.processSearchResults = (json, parsed, after, before, user_preferences) => { - 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 - } - } - } - - if(json.data.children) { - let view_more_posts = false - let posts_limit = 25 +const config = require('../config'); +const link = require('./components/link'); - 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 - - if(post.over_18) - if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') - continue - - let obj = await link.fromJson(post, user_preferences) - posts.push(obj) - } - } - - let obj = { - search_firstpage: search_firstpage, - before: before, - after: after, - posts: posts, - suggested_subreddits: suggested_subreddits, - } - - resolve(obj) - })() - }) +async function processSearchResults( + json, + parsed, + after, + before, + user_preferences +) { + if (!parsed) { + json = JSON.parse(json); } + let posts = []; + let search_firstpage = false; + + before = json.data.before; + 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; + } + } + } + + 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; + + if (post.over_18) + if ( + (config.nsfw_enabled === false && + user_preferences.nsfw_enabled != 'true') || + user_preferences.nsfw_enabled === 'false' + ) + continue; + + let obj = await link.fromJson(post, user_preferences); + posts.push(obj); + } + } + + let obj = { + search_firstpage: search_firstpage, + before: before, + after: after, + posts: posts, + suggested_subreddits: suggested_subreddits, + }; + + return obj; } + +module.exports = processSearchResults; diff --git a/routes/subreddit.js b/routes/subreddit.js index fa4d19f..cc6f06a 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -7,7 +7,7 @@ const { finalizeJsonPost, } = require('../inc/processJsonPost.js'); const processSubredditAbout = require('../inc/processSubredditAbout.js'); -const processSearches = require('../inc/processSearchResults.js')(); +const processSearchResults = require('../inc/processSearchResults.js'); const processSubreddit = require('../inc/processJsonSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')();