From e1df2ea8ae4b96a4056036059e263967e80b9975 Mon Sep 17 00:00:00 2001 From: json Date: Wed, 8 Sep 2021 12:28:45 +0100 Subject: [PATCH] convert processJsonSubredditsExplore to async function --- inc/processSubredditsExplore.js | 100 +++++++++++++++++--------------- routes/subreddit.js | 3 +- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/inc/processSubredditsExplore.js b/inc/processSubredditsExplore.js index ab3f931..66ed2f9 100644 --- a/inc/processSubredditsExplore.js +++ b/inc/processSubredditsExplore.js @@ -1,52 +1,58 @@ -module.exports = function() { - const config = require('../config'); - this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => { - return new Promise(resolve => { - (async () => { - if(from === 'redis') { - json = JSON.parse(json) - } - if(json.error) { - resolve({ error: true, error_data: json }) - } else { - let before = json.data.before - let after = json.data.after +const config = require('../config'); - let ret = { - info: { - before: before, - after: after - }, - links: [] - } +async function processJsonSubredditsExplore( + json, + from, + subreddit_front, + user_preferences +) { + if (from === 'redis') { + json = JSON.parse(json); + } + if (json.error) { + return { error: true, error_data: json }; + } else { + let before = json.data.before; + let after = json.data.after; - let children_len = json.data.children.length + let ret = { + info: { + before: before, + after: after, + }, + links: [], + }; - for(var i = 0; i < children_len; i++) { - let data = json.data.children[i].data - - if(data.over_18) - if((config.nsfw_enabled === false && user_preferences.nsfw_enabled != 'true') || user_preferences.nsfw_enabled === 'false') - continue - - let obj = { - created: data.created_utc, - id: data.id, - over_18: data.over_18, - display_name: data.display_name, - display_name_prefixed: data.display_name_prefixed, - public_description: data.public_description, - url: replaceDomains(data.url, user_preferences), - subscribers: data.subscribers, - over_18: data.over18, - title: data.title, - subreddit_front: subreddit_front, - } - ret.links.push(obj) - } - resolve(ret) - } - })() - }) + let children_len = json.data.children.length; + + for (var i = 0; i < children_len; i++) { + let data = json.data.children[i].data; + + if (data.over_18) + if ( + (config.nsfw_enabled === false && + user_preferences.nsfw_enabled != 'true') || + user_preferences.nsfw_enabled === 'false' + ) + continue; + + let obj = { + created: data.created_utc, + id: data.id, + over_18: data.over_18, + display_name: data.display_name, + display_name_prefixed: data.display_name_prefixed, + public_description: data.public_description, + url: replaceDomains(data.url, user_preferences), + subscribers: data.subscribers, + over_18: data.over18, + title: data.title, + subreddit_front: subreddit_front, + }; + ret.links.push(obj); + } + return ret; } } + +module.exports = processJsonSubredditsExplore; diff --git a/routes/subreddit.js b/routes/subreddit.js index ed0c97b..e6efad2 100644 --- a/routes/subreddit.js +++ b/routes/subreddit.js @@ -11,8 +11,7 @@ const processSearchResults = require('../inc/processSearchResults.js'); const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const tedditApiSubreddit = require('../inc/teddit_api/handleSubreddit.js')(); const processMoreComments = require('../inc/processMoreComments.js')(); -const processSubredditsExplore = - require('../inc/processSubredditsExplore.js')(); +const processJsonSubredditsExplore = require('../inc/processSubredditsExplore.js'); subredditRoutes.get('/r/:subreddit/search', (req, res, next) => { let subreddit = req.params.subreddit;