convert processSearchResults to async function

This commit is contained in:
json 2021-09-08 11:47:25 +01:00
parent 0bf3e36c56
commit 153a50e60a
2 changed files with 68 additions and 60 deletions

View File

@ -1,49 +1,58 @@
module.exports = function() {
const config = require('../config'); const config = require('../config');
const link = require('./components/link') const link = require('./components/link');
this.processSearchResults = (json, parsed, after, before, user_preferences) => {
return new Promise(resolve => { async function processSearchResults(
(async () => { json,
parsed,
after,
before,
user_preferences
) {
if (!parsed) { if (!parsed) {
json = JSON.parse(json) json = JSON.parse(json);
} }
let posts = [] let posts = [];
let search_firstpage = false let search_firstpage = false;
let before = json.data.before
let after = json.data.after before = json.data.before;
after = json.data.after;
if (!after && !before) { if (!after && !before) {
search_firstpage = true search_firstpage = true;
} }
let suggested_subreddits = false let suggested_subreddits = false;
if (json.suggested_subreddits) { if (json.suggested_subreddits) {
if (json.suggested_subreddits.data) { if (json.suggested_subreddits.data) {
if (json.suggested_subreddits.data.children.length > 0) { if (json.suggested_subreddits.data.children.length > 0) {
suggested_subreddits = json.suggested_subreddits.data.children suggested_subreddits = json.suggested_subreddits.data.children;
} }
} }
} }
if (json.data.children) { if (json.data.children) {
let view_more_posts = false let view_more_posts = false;
let posts_limit = 25 let posts_limit = 25;
if (json.data.children.length > posts_limit) { if (json.data.children.length > posts_limit) {
view_more_posts = true view_more_posts = true;
} else { } else {
posts_limit = json.data.children.length posts_limit = json.data.children.length;
} }
for (var i = 0; i < posts_limit; i++) { for (var i = 0; i < posts_limit; i++) {
let post = json.data.children[i].data let post = json.data.children[i].data;
if (post.over_18) if (post.over_18)
if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') if (
continue (config.nsfw_enabled === false &&
user_preferences.nsfw_enabled != 'true') ||
user_preferences.nsfw_enabled === 'false'
)
continue;
let obj = await link.fromJson(post, user_preferences) let obj = await link.fromJson(post, user_preferences);
posts.push(obj) posts.push(obj);
} }
} }
@ -53,10 +62,9 @@ module.exports = function() {
after: after, after: after,
posts: posts, posts: posts,
suggested_subreddits: suggested_subreddits, suggested_subreddits: suggested_subreddits,
};
return obj;
} }
resolve(obj) module.exports = processSearchResults;
})()
})
}
}

View File

@ -7,7 +7,7 @@ const {
finalizeJsonPost, finalizeJsonPost,
} = require('../inc/processJsonPost.js'); } = require('../inc/processJsonPost.js');
const processSubredditAbout = require('../inc/processSubredditAbout.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 processSubreddit = require('../inc/processJsonSubreddit.js')();
const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')();
const processMoreComments = require('../inc/processMoreComments.js')(); const processMoreComments = require('../inc/processMoreComments.js')();