convert processSubredditAbout to async function

This commit is contained in:
json 2021-09-07 14:24:10 +01:00
parent 1c7a3a8f5a
commit aae3e00ab1
2 changed files with 114 additions and 101 deletions

View File

@ -1,9 +1,7 @@
module.exports = function() { const config = require('../config');
const config = require('../config')
this.processSubredditAbout = (subreddit, redis, fetch, RedditAPI) => { async function processSubredditAbout(subreddit, redis, fetch, RedditAPI) {
return new Promise(resolve => { if (subreddit && !subreddit.includes('+') && subreddit !== 'all') {
(async () => {
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') {
function returnRelevantKeys(json) { function returnRelevantKeys(json) {
return { return {
title: json.data.title, title: json.data.title,
@ -13,41 +11,53 @@ module.exports = function() {
created_utc: json.data.created_utc, created_utc: json.data.created_utc,
over18: json.data.over18, over18: json.data.over18,
description_html: json.data.description_html, description_html: json.data.description_html,
moderators: json.moderators moderators: json.moderators,
} };
} }
let key = `${subreddit}:sidebar` let key = `${subreddit}:sidebar`;
redis.get(key, (error, json) => { redis.get(key, (error, json) => {
if(error) { if (error) {
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error) console.error(
resolve(null) `Error getting the ${subreddit}:sidebar key from redis.`,
error
);
return null;
} }
if(json) { if (json) {
json = JSON.parse(json) json = JSON.parse(json);
resolve(returnRelevantKeys(json)) return returnRelevantKeys(json);
} else { } else {
let url = `https://reddit.com/r/${subreddit}/about.json` let url = `https://reddit.com/r/${subreddit}/about.json`;
if(config.use_reddit_oauth) { if (config.use_reddit_oauth) {
url = `https://oauth.reddit.com/r/${subreddit}/about` url = `https://oauth.reddit.com/r/${subreddit}/about`;
} }
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then((result) => {
if(result.status === 200) { if (result.status === 200) {
result.json() result.json().then((json) => {
.then(json => { json.moderators = [];
json.moderators = [] redis.setex(
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { key,
if(error) { config.setexs.sidebar,
console.error('Error setting the sidebar key to redis.', error) JSON.stringify(json),
return res.render('index', { json: null, user_preferences: req.cookies }) (error) => {
if (error) {
console.error(
'Error setting the sidebar key to redis.',
error
);
return res.render('index', {
json: null,
user_preferences: req.cookies,
});
} else { } else {
console.log('Fetched the sidebar from reddit API.') console.log('Fetched the sidebar from reddit API.');
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json` let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`;
if(config.use_reddit_oauth) { if (config.use_reddit_oauth) {
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators` moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`;
} }
resolve(returnRelevantKeys(json)) return returnRelevantKeys(json);
/* /*
* The following code is commented out because Reddit doesn't * The following code is commented out because Reddit doesn't
* anymore support fetching moderators for subreddits * anymore support fetching moderators for subreddits
@ -69,38 +79,41 @@ module.exports = function() {
return res.render('index', { json: null, user_preferences: req.cookies }) return res.render('index', { json: null, user_preferences: req.cookies })
} else { } else {
console.log('Fetched the moderators from reddit API.') console.log('Fetched the moderators from reddit API.')
resolve(returnRelevantKeys(json)) return(returnRelevantKeys(json))
} }
}) })
}) })
} else { } else {
console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} ${mod_result.statusText}`) console.error(`Something went wrong while fetching moderators data from reddit API. ${mod_result.status} ${mod_result.statusText}`)
console.error(config.reddit_api_error_text) console.error(config.reddit_api_error_text)
resolve(returnRelevantKeys(json)) return(returnRelevantKeys(json))
} }
}).catch(error => { }).catch(error => {
console.error('Error fetching moderators.', error) console.error('Error fetching moderators.', error)
resolve(returnRelevantKeys(json)) return(returnRelevantKeys(json))
}) })
*/ */
} }
}) }
}) );
});
} else { } else {
console.error(`Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`) console.error(
console.error(config.reddit_api_error_text) `Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`
resolve(null) );
} console.error(config.reddit_api_error_text);
}).catch(error => { return null;
console.error('Error fetching the sidebar.', error)
resolve(null)
})
} }
}) })
.catch((error) => {
console.error('Error fetching the sidebar.', error);
return null;
});
}
});
} else { } else {
resolve(null) return null;
}
})()
})
} }
} }
module.exports = processSubredditAbout;

View File

@ -6,7 +6,7 @@ const {
processJsonPost, processJsonPost,
finalizeJsonPost, finalizeJsonPost,
} = require('../inc/processJsonPost.js'); } = require('../inc/processJsonPost.js');
const processAbout = require('../inc/processSubredditAbout.js')(); const processSubredditAbout = require('../inc/processSubredditAbout.js');
const tedditApiUser = require('../inc/teddit_api/handleUser.js')(); const tedditApiUser = require('../inc/teddit_api/handleUser.js')();
const processSearches = require('../inc/processSearchResults.js')(); const processSearches = require('../inc/processSearchResults.js')();
const processSubreddit = require('../inc/processJsonSubreddit.js')(); const processSubreddit = require('../inc/processJsonSubreddit.js')();