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,106 +1,119 @@
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 () => { function returnRelevantKeys(json) {
if(subreddit && !subreddit.includes('+') && subreddit !== 'all') { return {
function returnRelevantKeys(json) { title: json.data.title,
return { public_description_html: json.data.public_description_html,
title: json.data.title, active_user_count: json.data.active_user_count,
public_description_html: json.data.public_description_html, subscribers: json.data.subscribers,
active_user_count: json.data.active_user_count, created_utc: json.data.created_utc,
subscribers: json.data.subscribers, over18: json.data.over18,
created_utc: json.data.created_utc, description_html: json.data.description_html,
over18: json.data.over18, moderators: json.moderators,
description_html: json.data.description_html, };
moderators: json.moderators }
}
} let key = `${subreddit}:sidebar`;
redis.get(key, (error, json) => {
let key = `${subreddit}:sidebar` if (error) {
redis.get(key, (error, json) => { console.error(
if(error) { `Error getting the ${subreddit}:sidebar key from redis.`,
console.error(`Error getting the ${subreddit}:sidebar key from redis.`, error) error
resolve(null) );
} return null;
if(json) { }
json = JSON.parse(json) if (json) {
resolve(returnRelevantKeys(json)) json = JSON.parse(json);
} else { return returnRelevantKeys(json);
let url = `https://reddit.com/r/${subreddit}/about.json` } else {
if(config.use_reddit_oauth) { let url = `https://reddit.com/r/${subreddit}/about.json`;
url = `https://oauth.reddit.com/r/${subreddit}/about` if (config.use_reddit_oauth) {
} url = `https://oauth.reddit.com/r/${subreddit}/about`;
fetch(encodeURI(url), redditApiGETHeaders()) }
.then(result => { fetch(encodeURI(url), redditApiGETHeaders())
if(result.status === 200) { .then((result) => {
result.json() if (result.status === 200) {
.then(json => { result.json().then((json) => {
json.moderators = [] json.moderators = [];
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => { redis.setex(
if(error) { key,
console.error('Error setting the sidebar key to redis.', error) config.setexs.sidebar,
return res.render('index', { json: null, user_preferences: req.cookies }) JSON.stringify(json),
} else { (error) => {
console.log('Fetched the sidebar from reddit API.') if (error) {
let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json` console.error(
if(config.use_reddit_oauth) { 'Error setting the sidebar key to redis.',
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators` error
} );
resolve(returnRelevantKeys(json)) return res.render('index', {
/* json: null,
* The following code is commented out because Reddit doesn't user_preferences: req.cookies,
* anymore support fetching moderators for subreddits });
* when not logged in. } else {
* This might change in the future though. console.log('Fetched the sidebar from reddit API.');
* https://codeberg.org/teddit/teddit/issues/207 let moderators_url = `https://reddit.com/r/${subreddit}/about/moderators.json`;
*/ if (config.use_reddit_oauth) {
moderators_url = `https://oauth.reddit.com/r/${subreddit}/about/moderators`;
/*
fetch(encodeURI(moderators_url), redditApiGETHeaders())
.then(mod_result => {
if(mod_result.status === 200) {
mod_result.json()
.then(mod_json => {
json.moderators = mod_json
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the sidebar with moderators key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the moderators from reddit API.')
resolve(returnRelevantKeys(json))
}
})
})
} else {
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)
resolve(returnRelevantKeys(json))
}
}).catch(error => {
console.error('Error fetching moderators.', error)
resolve(returnRelevantKeys(json))
})
*/
} }
}) return returnRelevantKeys(json);
/*
* The following code is commented out because Reddit doesn't
* anymore support fetching moderators for subreddits
* when not logged in.
* This might change in the future though.
* https://codeberg.org/teddit/teddit/issues/207
*/
/*
fetch(encodeURI(moderators_url), redditApiGETHeaders())
.then(mod_result => {
if(mod_result.status === 200) {
mod_result.json()
.then(mod_json => {
json.moderators = mod_json
redis.setex(key, config.setexs.sidebar, JSON.stringify(json), (error) => {
if(error) {
console.error('Error setting the sidebar with moderators key to redis.', error)
return res.render('index', { json: null, user_preferences: req.cookies })
} else {
console.log('Fetched the moderators from reddit API.')
return(returnRelevantKeys(json))
}
})
})
} else {
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)
return(returnRelevantKeys(json))
}
}).catch(error => {
console.error('Error fetching moderators.', error)
return(returnRelevantKeys(json))
}) })
} else { */
console.error(`Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`) }
console.error(config.reddit_api_error_text) }
resolve(null) );
} });
}).catch(error => { } else {
console.error('Error fetching the sidebar.', error) console.error(
resolve(null) `Something went wrong while fetching data from reddit API. ${result.status} ${result.statusText}`
}) );
console.error(config.reddit_api_error_text);
return null;
} }
}) })
} else { .catch((error) => {
resolve(null) console.error('Error fetching the sidebar.', error);
} return null;
})() });
}) }
});
} else {
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')();