convert processJsonSubredditsExplore to async function

This commit is contained in:
json 2021-09-08 12:28:45 +01:00
parent 6ced924bc8
commit e1df2ea8ae
2 changed files with 54 additions and 49 deletions

View File

@ -1,33 +1,40 @@
module.exports = function() {
const config = require('../config'); const config = require('../config');
this.processJsonSubredditsExplore = (json, from, subreddit_front, user_preferences) => {
return new Promise(resolve => { async function processJsonSubredditsExplore(
(async () => { json,
from,
subreddit_front,
user_preferences
) {
if (from === 'redis') { if (from === 'redis') {
json = JSON.parse(json) json = JSON.parse(json);
} }
if (json.error) { if (json.error) {
resolve({ error: true, error_data: json }) return { error: true, error_data: json };
} else { } else {
let before = json.data.before let before = json.data.before;
let after = json.data.after let after = json.data.after;
let ret = { let ret = {
info: { info: {
before: before, before: before,
after: after after: after,
}, },
links: [] links: [],
} };
let children_len = json.data.children.length let children_len = json.data.children.length;
for (var i = 0; i < children_len; i++) { for (var i = 0; i < children_len; i++) {
let data = json.data.children[i].data let data = json.data.children[i].data;
if (data.over_18) if (data.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 = { let obj = {
created: data.created_utc, created: data.created_utc,
@ -41,12 +48,11 @@ module.exports = function() {
over_18: data.over18, over_18: data.over18,
title: data.title, title: data.title,
subreddit_front: subreddit_front, subreddit_front: subreddit_front,
};
ret.links.push(obj);
} }
ret.links.push(obj) return ret;
}
resolve(ret)
}
})()
})
} }
} }
module.exports = processJsonSubredditsExplore;

View File

@ -11,8 +11,7 @@ const processSearchResults = require('../inc/processSearchResults.js');
const processJsonSubreddit = require('../inc/processJsonSubreddit.js'); const processJsonSubreddit = 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')();
const processSubredditsExplore = const processJsonSubredditsExplore = require('../inc/processSubredditsExplore.js');
require('../inc/processSubredditsExplore.js')();
subredditRoutes.get('/r/:subreddit/search', (req, res, next) => { subredditRoutes.get('/r/:subreddit/search', (req, res, next) => {
let subreddit = req.params.subreddit; let subreddit = req.params.subreddit;